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

public class numbers_pk {

	void solve() throws IOException {
		int n = nextInt();
		HashMap<Integer, HashSet<Integer>> main = new HashMap<Integer, HashSet<Integer>>();
		for (int i = 0; i < n; i++) {
			int c = nextInt();
			int k = nextInt();
			main.put(c, new HashSet<Integer>());
			for (int j = 0; j < k; j++) {
				main.get(c).add(nextInt());
			}
		}
		int m = nextInt();
		for (int i = 0; i < m; i++) {
			String c = br.readLine();
			boolean found = false;
			for (int t1 = 1; t1 < 4; t1++) {
				for (int t2 = 3; t2 < 6; t2++) {
					if (found)
						continue;
					String c1 = c.substring(0, t1);
					String c2 = c.substring(t1, t1 + t2);
					String c3 = c.substring(t1 + t2);
					if (c1.charAt(0) == '0' || c2.charAt(0) == '0'
							|| c3.charAt(0) == '0') {
						continue;
					}
					int i1 = Integer.parseInt(c1);
					int i2 = Integer.parseInt(c2);
					if (main.get(i1) == null || !main.get(i1).contains(i2)) {
						continue;
					}
					found = true;
					out.print("+" + i1 + "(" + i2 + ")");
					if (c3.length() == 7 || c3.length() == 5
							|| c3.length() == 3) {
						out.print(c3.substring(0, 3));
						c3 = c3.substring(3);
						if (c3.length() > 0) {
							out.print("-");
						}
					}
					while (c3.length() > 0) {
						out.print(c3.substring(0, 2));
						c3 = c3.substring(2);
						if (c3.length() > 0) {
							out.print("-");
						}
					}
					out.println();
				}
			}
			if (!found) {
				out.println("Incorrect");
			}
		}
	}

	BufferedReader br;
	StringTokenizer st;
	PrintWriter out;

	void run() {
		try {
			// br = new BufferedReader(new InputStreamReader(System.in));
			// out = new PrintWriter(System.out);

			br = new BufferedReader(new FileReader(new File("numbers.in")));
			out = new PrintWriter("numbers.out");
			solve();

			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		new numbers_pk().run();
	}

	String nextToken() throws IOException {
		while ((st == null) || !st.hasMoreTokens())
			st = new StringTokenizer(br.readLine());
		return st.nextToken();
	}

	int nextInt() throws NumberFormatException, IOException {
		return Integer.parseInt(nextToken());
	}

	double nextDouble() throws NumberFormatException, IOException {
		return Double.parseDouble(nextToken());
	}

	long nextLong() throws NumberFormatException, IOException {
		return Long.parseLong(nextToken());
	}
}
