#include <iostream>
#include <ctime>
#include <cassert>
#include <cmath>
#include <string>
#include <cstring>
#include <vector>
#include <fstream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <set>
#include <map>
#include <iomanip>
#define nextLine() { for (int c = getchar(); c != '\n' && c != EOF; c = getchar()); }
#define sqr(a) ((a)*(a))
#define has(mask,i) (((mask) & (1<<(i))) == 0 ? false : true)

#ifdef LOCAL
	#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
	#define eprintf(...)
#endif

#define TASK "numbers"
using namespace std;

#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define fi first
#define se second

#if ( _WIN32 || __WIN32__ )
    #define LLD "%I64d"
#else
    #define LLD "%lld"
#endif

typedef long long LL;
typedef long double ldb;
typedef vector <int> vi;
typedef vector <vi> vvi;
typedef vector <bool> vb;
typedef vector <vb> vvb;

const int INF = (1 << 30) - 1;
const ldb EPS = 1e-9;
const ldb PI = fabsl(atan2l(0.0, -1.0));

int n;
vi codes[1000];
void load() {
	scanf("%d", &n);
	for (int i = 0, country, c; i < n; i++) {
		scanf("%d", &country);
		scanf("%d", &c);
		for (int j = 0, code; j < c; j++) {
			scanf("%d", &code);
			codes[country].pb(code);
		}
	}
}

inline const char* prettyPrint(const string &number) {
	switch(number.size()) {
	case 3:
		return number.c_str();
	case 4:
		return (number.substr(0, 2) + "-" + number.substr(2)).c_str();
	case 5:
		return (number.substr(0, 3) + "-" + number.substr(3)).c_str();
	case 6:
		return (number.substr(0, 2) + "-" + number.substr(2, 2) + "-" + number.substr(4)).c_str();
	case 7:
		return (number.substr(0, 3) + "-" + number.substr(3, 2) + "-" + number.substr(5)).c_str();
	default:
		throw;
	};
}

void solve() {
	for (int i = 0; i < 1000; i++) {
		sort(codes[i].begin(), codes[i].end());
	}
	int m;
	scanf("%d", &m);
	for (int i = 0; i < m; i++) {
		char s[13] = {0};
		scanf("%s", s);
		int country = 0;
		bool ok = false;
		for (int j = 0; !ok && j < 3; j++) {
			country = country * 10 + s[j] - '0';
			int code = 0;
			if (s[j + 1] == '0') {
				continue;
			}
			for (int k = j + 1; k < j + 6; k++) {
				code = code * 10 + s[k] - '0';
				vi::const_iterator it = lower_bound(codes[country].begin(), codes[country].end(), code);
				if (it != codes[country].end() && *it == code && s[k + 1] != '0') {
					printf("+%d(%d)%s\n", country, code, prettyPrint(s + k + 1));
					ok = true;
					break;
				}
			}
		}
		if (!ok) {
			puts("Incorrect");
		}
	}
}

int main() {
	clock_t start = clock();
	freopen(TASK".in", "r", stdin);
	freopen(TASK".out", "w", stdout);
	load();
	solve();
	eprintf("time: %.3lf\n", (double)(clock() - start) / CLOCKS_PER_SEC);
	return 0;
}
