import java.util.*;

public class Hotel {
	int bedSize, roomCapacity, nAvailable, roomPrice;
	String name;

	public static void main(String[] args){
		Scanner scan = new Scanner(System.in);
		int T = scan.nextInt();
		for (int TC=1; TC<=T; TC++){
			System.out.printf("Case #%d:\n",TC);

			int nH = scan.nextInt();
			int nT = scan.nextInt();
			
			Hotel[] hs = new Hotel[nH];
			for (int i=0; i<nH; i++){
				hs[i] = new Hotel();
				hs[i].bedSize = scan.nextInt();
				hs[i].roomCapacity = scan.nextInt();
				hs[i].nAvailable = scan.nextInt();
				hs[i].roomPrice = scan.nextInt();
				hs[i].name = scan.next();
			}

			for (int i=0; i<nT; i++){
				String type = scan.next();
				int nPeople = scan.nextInt();
				int maxPerson = scan.nextInt();

				String suggestedHotel = "no-hotel";
				int totalCost = Integer.MAX_VALUE;
				int bedSize = Integer.MIN_VALUE;
				for (int j=0; j<nH; j++){
					int nPersonPerRoom = Math.min(maxPerson, hs[j].roomCapacity);
					int nRoom = (nPeople + nPersonPerRoom - 1) / nPersonPerRoom;
					if (nRoom > hs[j].nAvailable) continue;

					int[] range = new int[]{ 0, 35, 48, 62 };
					int idx = type.charAt(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 = hs[j].name;
				}
				
				if (totalCost < Integer.MAX_VALUE)
					System.out.printf("%d ",totalCost);
				System.out.println(suggestedHotel);
			}
		}
	}
}
