#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include template inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } const long long INF = 1LL << 60; const long long MOD = 1000000007LL; const long long MAX = 500000LL; using namespace std; typedef unsigned long long ull; typedef long long ll; int main() { ll N, M; cin >> N; vector vs(N); for (ll i = 0; i < N; i++) { cin >> vs[i]; } cin >> M; vector vt(M); for (ll i = 0; i < M; i++) { cin >> vt[i]; } set st; for (ll i = 0; i < N; i++) { for (ll j = 0; j < vs[i].size(); j++) { st.insert(vs[i][j]); } } vector idg(26, 0); vector> g(26); for (ll i = 0; i < N; i++) { for (ll j = 0; j < (ll)vs[i].size() - 1; j++) { g[vs[i][j] - 'A'].push_back(vs[i][j + 1] - 'A'); idg[vs[i][j + 1] - 'A']++; } } for (ll i = 0; i < M; i++) { for (ll j = 0; j < (ll)vt[i].size() - 1; j++) { bool flag = true; for (ll k = 0; k < g[vt[i][j]-'A'].size(); k++) { if (g[vt[i][j] - 'A'][k] == vt[i][j + 1] - 'A') { flag = false; break; } } if (flag) { g[vt[i][j] - 'A'].push_back(vt[i][j + 1] - 'A'); idg[vt[i][j + 1] - 'A']++; } } } queue q; vector topo; for (auto itr = st.begin(); itr != st.end(); itr++) { if (idg[*itr - 'A'] == 0) { q.push(*itr - 'A'); topo.push_back(*itr - 'A'); } } while (!q.empty()) { ll n = q.front(); q.pop(); for (ll nn : g[n]) { idg[nn]--; if (idg[nn] == 0) { q.push(nn); topo.push_back(nn); } } } if (topo.size() != st.size()) { puts("-1"); return 0; } else { for (ll i = 0; i < (ll)topo.size() - 1; i++) { bool flag = false; for (ll x : g[topo[i]]) { if (x == topo[i + 1]) { flag = true; break; } } if (!flag) { puts("-1"); return 0; } } for (ll i = 0; i < topo.size(); i++) { cout << (char)(topo[i] + 'A'); } cout << endl; } return 0; }