#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using ll = long long; using ull = unsigned long long; using PII = pair; using PLL = pair; template ostream& operator<<(ostream& s, const pair& p) { return s << "(" << p.first << ", " << p.second << ")"; } template ostream& operator<<(ostream& s, const vector& v) { s << "["; for (int i = 0; i < v.size(); i++) s << (i == 0 ? "" : ", ") << v[i]; s << "]"; return s; } #define ALL(a) (a).begin(), (a).end() int main() { map map; char revmap[100]; int id = 0; int N; cin >> N; vector A(N); for (int i = 0; i < N; i++) { cin >> A[i]; for (char c : A[i]) { map[c] = id; revmap[id] = c; id++; } } int M; cin >> M; vector B(M); for (int i = 0; i < M; i++) { cin >> B[i]; } vector next(map.size(), -1); for (int i = 0; i < N; i++) { for (int j = 0; j < A[i].size() - 1; j++) { int cid = map[A[i][j]], nid = map[A[i][j + 1]]; next[cid] = nid; } } for (int i = 0; i < M; i++) { for (int j = 0; j < B[i].size() - 1; j++) { int cid = map[B[i][j]], nid = map[B[i][j + 1]]; next[cid] = nid; } } for (int i = 0; i < next.size(); i++) { string s = ""; int id = i; while (id >= 0) { s.push_back(revmap[id]); id = next[id]; } if (s.size() == next.size()) { cout << s << endl; return 0; } } cout << -1 << endl; return 0; }