#include<bits/stdc++.h>
using namespace std;
struct Node{
int age;
int wealth;
char name[10];
};
bool cmp(Node a,Node b){
if(a.wealth!=b.wealth) return a.wealth>b.wealth;
else if(a.age!=b.age) return a.age<b.age;
else return strcmp(a.name,b.name)<0;
}
int main()
{
freopen("in.txt","r",stdin);
int n,k;cin>>n>>k;
vector<Node> ppp;ppp.resize(n);
for(int i=0;i<n;i++){
//cin>>ppp[i].name>>ppp[i].age>>ppp[i].wealth;
scanf("%s%d%d",&ppp[i].name,&ppp[i].age,&ppp[i].wealth);
}
sort(ppp.begin(),ppp.end(),cmp);
for(int i=1;i<=k;i++){
printf("Case #%d:\n",i);
int num;cin>>num;
int amin,amax;cin>>amin>>amax;
vector<Node> temp;int cnt=0;
for(int j=0;j<ppp.size();j++){
if(ppp[j].age<=amax&&ppp[j].age>=amin&&cnt<num&&j<ppp.size()){
temp.push_back(ppp[j]);cnt++;
}else if(cnt==num||j>=ppp.size()){
break;
}
}
if(cnt==0) printf("None\n");
else{
for(int j=0;j<temp.size();j++){
//cout<<temp[j].name<<' '<<temp[j].age<<' '<<temp[j].wealth<<endl;
printf("%s %d %d\n",temp[j].name,temp[j].age,temp[j].wealth);
}
}
}
return 0;
}