/* Задача "Финал ACM ICPC"
 * Интернет олимпиады для школьников. Сезон 2006 - 2007
 * Автор задачи:  Александр Торопов, toropov@rain.ifmo.ru
 * Автор решения: Александр Торопов, toropov@rain.ifmo.ru
 */

import java.io.*;
import java.util.*;

public class final_at implements Runnable {
	BufferedReader in;
	PrintWriter out;
	
	public void solve() throws IOException{
		StringTokenizer st = new StringTokenizer(in.readLine());
		int p = Integer.parseInt(st.nextToken());
		int n = Integer.parseInt(st.nextToken());
		int k = Integer.parseInt(st.nextToken());
		HashMap<String, Integer> numberOfTeams = new HashMap<String, Integer>();
		ArrayList<Integer> countOfTeams = new ArrayList<Integer>();
		ArrayList<String> array = new ArrayList<String>();
		String allString[] = new String[p];
		int teamsNum[] = new int [p];
		for(int i = 0; i < p; ++i) {
			allString[i] = in.readLine();
		}
		st = new StringTokenizer(in.readLine());
		for(int i = 0; i < p; ++i) {
			teamsNum[i] = Integer.parseInt(st.nextToken());
		}
		for(int i = 0; i < p; ++i) {
			String nameU = allString[i];
			int number = -1;
			if (numberOfTeams.get(nameU) == null) {
				numberOfTeams.put(nameU, countOfTeams.size());
				countOfTeams.add(1);
				array.add(nameU + " #" + Integer.toString(teamsNum[i]));
			}
			else {
				number = numberOfTeams.get(nameU);
				if (countOfTeams.get(number) < k) {
					countOfTeams.set(number, countOfTeams.get(number) + 1);
					array.add(nameU + " #" + Integer.toString(teamsNum[i]));
				}
			}
		}
		for(int i = 0; i < Math.min(array.size(), n); ++i) {
			out.println(array.get(i));
		}
	}
	
	public void run() {
		try {
			solve();
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(89);
		} finally {
			out.close();
		}
	}
	
	private final_at(String name) {
		try {
			in = new BufferedReader(new FileReader(name + ".in"));
			out = new PrintWriter(new File(name + ".out"));
			
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(89);
		}
	}

	public static void main(String[] args) {
		(new Thread(new final_at("final"))).start();
	}
}
