// Arup Guha
// 4/26/2021
// Solution to 2021 COP 4516 Team Final Contest Problem F: Food Truck

import java.util.*;

public class foodtruck_arup {

	public static void main(String[] args) {
	
		Scanner stdin = new Scanner(System.in);
		int nC = stdin.nextInt();
		
		// Process cases.
		for (int loop=1; loop<=nC; loop++) {
		
			int numGrills = stdin.nextInt();
			int food = stdin.nextInt();
			int people = stdin.nextInt();
			
			// Store cook times in a map.
			HashMap<String,Integer> cookTime = new HashMap<String,Integer>();
			for (int i=0; i<food; i++) {
				String item = stdin.next();
				int time = stdin.nextInt();
				cookTime.put(item, time);
			}
			
			// Just store names and items in order.
			String[] names = new String[people];
			int[] items = new int[people];
			
			// Just store person and cook time.
			for (int i=0; i<people; i++) {
				names[i] = stdin.next();
				items[i] = cookTime.get(stdin.next());
			}
			
			// Will store when each grill is free.
			PriorityQueue<Integer> pq = new PriorityQueue<Integer>();
			for (int i=0; i<numGrills; i++) pq.offer(0);
			
			// Store end answers here.
			person[] list = new person[people];
			
			// Simulate everyone getting their food.
			for (int i=0; i<people; i++) {
			
				// Get the next grill.
				int cur = pq.poll();
				
				// We know when this food will finish.
				list[i] = new person(names[i], cur+items[i]);
				
				// This is when this grill will be free again.
				pq.offer(cur+items[i]);
			}
			
			// Sort it!
			Arrays.sort(list);
			
			// Print it.
			System.out.println("Test Case #"+loop+":");
			for (int i=0; i<people; i++)
				System.out.println(list[i]);
		}
	}
}

class person implements Comparable<person> {
	
	public String name;
	public int time;
	
	public person(String n, int t) {
		name = n;
		time = t;
	}
	
	// How our list is to be printed.
	public int compareTo(person other) {
		if (time != other.time) return time-other.time;
		return name.compareToIgnoreCase(other.name);
	}
	
	// What is supposed to be printed for each person.
	public String toString() {
		return name + " " + time;
	}
}