#include <stdio.h>

struct Hotel {
	int bedSize, roomCapacity, nAvailable, roomPrice;
	char name[30];
};

Hotel hs[51];
int T, nH, nT;

int main(){
	scanf("%d",&T);
	for (int TC=1; TC<=T; TC++){
		printf("Case #%d:\n",TC);
		scanf("%d %d",&nH,&nT);
		
		for (int i=0; i<nH; i++){
			scanf("%d",&hs[i].bedSize);
			scanf("%d",&hs[i].roomCapacity);
			scanf("%d",&hs[i].nAvailable);
			scanf("%d",&hs[i].roomPrice);
			scanf("%s",hs[i].name);
		}

		for (int i=0; i<nT; i++){
			char type[10];
			int nPeople, maxPerson;
			scanf("%s %d %d",type,&nPeople,&maxPerson);

			int totalCost = 1000000000, bedSize, suggestedHotel = -1;
			for (int j=0; j<nH; j++){
				int nPersonPerRoom = hs[j].roomCapacity;
				if (nPersonPerRoom > maxPerson) nPersonPerRoom = maxPerson;
				int nRoom = (nPeople + nPersonPerRoom - 1) / nPersonPerRoom;
				if (nRoom > hs[j].nAvailable) continue;

				int range[] = { 0, 35, 48, 62 };
				int idx = type[0]-'A';
				int min = range[idx] + 1;
				int max = range[idx + 1];
				if (hs[j].bedSize < min || max < hs[j].bedSize) continue;

				int price = nRoom * hs[j].roomPrice;
				if (price > totalCost) continue;
				if (price == totalCost && hs[j].bedSize <= bedSize) continue;

				totalCost = price;
				bedSize = hs[j].bedSize;
				suggestedHotel = j;
			}
			
			if (suggestedHotel != -1)
				printf("%d %s\n",totalCost,hs[suggestedHotel].name);
			else
				puts("no-hotel");
		}
	}
}
