#include #define rep(i,a,b) for(int i=a;i vi; typedef vector vvi; bool visit(const vector& g, int v, vector& order, vector& color) { color[v] = 1; for (int u : g[v]) { if (color[u] == 2) continue; if (color[u] == 1) return false; if (!visit(g, u, order, color)) return false; } order.push_back(v); color[v] = 2; return true; } bool TopologicalSort(const vector& g, vector& order) { int n = g.size(); vector color(n); for (int u = 0; u < n; u++) if (!color[u] && !visit(g, u, order, color)) return false; reverse(order.begin(), order.end()); return true; } /*---------------------------------------------------------------------------------------------------             ∧_∧       ∧_∧  (´<_` )  Welcome to My Coding Space!      ( ´_ゝ`) /  ⌒i     /   \    | |     /   / ̄ ̄ ̄ ̄/  |   __(__ニつ/  _/ .| .|____      \/____/ (u ⊃ ---------------------------------------------------------------------------------------------------*/ int N; string A[30]; int M; string B[30]; //--------------------------------------------------------------------------------------------------- int E[26][26]; int cnt[26]; //--------------------------------------------------------------------------------------------------- void _main() { cin >> N; rep(i, 0, N) cin >> A[i]; cin >> M; rep(i, 0, M) cin >> B[i]; rep(i, 0, N) for (char c : A[i]) cnt[c - 'A']++; rep(i, 0, 26) if (1 < cnt[i]) { printf("-1\n"); return; } rep(i, 0, 26) cnt[i] = 0; rep(i, 0, M) for (char c : B[i]) cnt[c - 'A']++; rep(i, 0, 26) if (1 < cnt[i]) { printf("-1\n"); return; } rep(i, 0, N) { if (A[i].length() == 1) continue; rep(j, 0, A[i].length() - 1) { int a = A[i][j] - 'A'; int b = A[i][j + 1] - 'A'; E[a][b] = 1; } } rep(i, 0, M) { if (B[i].length() == 1) continue; rep(j, 0, B[i].length() - 1) { int a = B[i][j] - 'A'; int b = B[i][j + 1] - 'A'; E[a][b] = 1; } } vvi EE(26); rep(i, 0, 26) { rep(j, 0, 26) if (E[i][j]) EE[i].push_back(j); } vector ord; bool ok = TopologicalSort(EE, ord); if (!ok) { printf("-1\n"); return; } set s; rep(i, 0, N) for (char c : A[i]) s.insert(c - 'A'); rep(i, 0, M) for (char c : B[i]) s.insert(c - 'A'); string ans = ""; for (int i : ord) if (s.count(i)) ans += char('A' + i); ok = true; rep(i, 1, ans.size()) { int a = ans[i - 1] - 'A'; int b = ans[i] - 'A'; if (E[a][b]) ok = false; } if (!ok) { printf("-1\n"); return; } cout << ans << endl; }