/*
Ellie Kozlowski
Foodtruck Solution - Final Team Contest COP 4516 Spring 2021
*/

import java.util.*;

public class foodtruck {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        int c = in.nextInt();
        for (int ca = 1; ca <= c; ca++) {
            System.out.printf("Test Case #%d:\n", ca);

            int g = in.nextInt();
            int f = in.nextInt();
            int p = in.nextInt();

            HashMap<String, Integer> foodItems = new HashMap<>();
            for (int i = 0; i < f; i++) {
                foodItems.put(in.next(), in.nextInt());
            }

            PriorityQueue<Integer> pq = new PriorityQueue<>();
            for (int i = 0; i < g; ++i) {
                pq.add(0);
            }

            Person[] people = new Person[p];

            for (int i = 0; i < p; i++) {
                String m = in.next();
                String o = in.next();

                int time = pq.poll() + foodItems.get(o);

                pq.add(time);

                people[i] = new Person(m, time);
            }

            Arrays.sort(people);

            for (Person pe: people) {
                System.out.println(pe);
            }
        }
    }

    static class Person implements Comparable<Person> {
        String name;
        int time;

        Person(String n, int t) {
            name = n;
            time = t;
        }

        @Override
		public int compareTo(Person o) {
			if (time == o.time) return name.compareTo(o.name);
            return Integer.compare(time, o.time);
		}

        public String toString() {
            return name + " " + time;
        }
    }
}
