/*
Takes an input file named league_missed.  The format is

# of teams in league
# of idle days
  one line for each team with idle days
DAV 19 50 52 60 67 74 75 77 79 81 91 92 95 96
  one line for each player with team prepended, and # as a delimiter between
  team, player name and incidentals, and days missed
DAV # Biedrins, Andris   # 6-28

All days off must be in the form #-#,#-#.  0 means full 82 game eligibility.
*/


import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;

public class GamesMissed {
    public HashMap<String, HashSet<Integer>> teams = new HashMap<String, HashSet<Integer>>();
    private int season;

    public static void main(String args[]) {
	new GamesMissed().process();
    }
    
    BufferedReader getReader(String inputFile) {
	try {
	    BufferedReader contents=new BufferedReader(new FileReader(inputFile));
	    return contents;
	} catch (IOException ioe) { 
	    System.out.println(ioe);
	    return null;
	}
    }
    
    int getInt(String chars) { return Integer.valueOf(chars.trim()).intValue(); }

    int reportingDay(HashSet<Integer> idleDays, int proposed) {
	int actual = proposed;
	do {
	    actual++;
	} while (idleDays.contains(actual));
	return actual;
    }

    int advance(String team, int value) {
	HashSet<Integer> idleDays = teams.get(team);
	int count = 0;
	for (int i = 0; i < season; i++) {
	    if (idleDays.contains(i))
		continue;
	    count++;
	    if (count == value)
		// if the next day is idle report the following
		return reportingDay(idleDays, i);
	}
	return 0;
    }

    String produceList(String team, String origList) {
	if (origList.equals("0"))
	    return origList;

	StringBuilder newList = new StringBuilder();
	String groupings[] = origList.split(",");
	for (int i = 0; i < groupings.length; i++) {
	    String tokens[] = groupings[i].split("-");
	    if (tokens.length == 1) {
		newList.append(advance(team, getInt(groupings[i])) + ",");
		continue;
	    }
	    int first = getInt(tokens[0]);
	    int last = getInt(tokens[1]);
	    newList.append(advance(team, first) + "-" + advance(team, last) + ",");
	}
	newList.delete(newList.length() - 1, newList.length());
	return newList.toString();
    }
    
    void process() {
	String line;
	String tokens[];
	String team;
	String name;
	
	BufferedReader contents = getReader("league_missed");
	try {
	    line = contents.readLine();
	    int numTeams = getInt(line);
	    line = contents.readLine();
	    int idle = getInt(line);
	    season = 82 + idle;
	    for (int i = 0; i < numTeams; i++) {
		line = contents.readLine();
		tokens = line.split(" ");
		HashSet<Integer> gameArray = new HashSet<Integer>();
		for (int j = 0; j < idle; j++)
		    gameArray.add(getInt(tokens[j+1]));
		teams.put(tokens[0], gameArray);
	    }
	    line = contents.readLine();
	    while (line != null) {
		tokens = line.split("#");
		team = tokens[0].trim();
		name = tokens[1];
		System.out.println(name+ " " + produceList(team, tokens[2].trim()));
		line = contents.readLine();
	    }
	} catch (IOException ioe) { 
	}
    }
}
