#include #include #include #include #include using namespace std; int main(){ int n; cin >> n; vector a(n); for(auto &p: a) cin >> p; int m; cin >> m; vector b(m); for(auto &p: b) cin >> p; vector> G(26); vector is_exist(26); int len = 0; for(auto &p: a){ len += p.length(); for(auto &pp: p) is_exist[pp-'A'] = 1; for(int i = 1; i < p.length(); i++){ G[p[i-1]-'A'].insert(p[i]-'A'); } } for(auto &p: b){ for(int i = 1; i < p.length(); i++){ G[p[i-1]-'A'].insert(p[i]-'A'); } } vector in(26); for(int i = 0; i < 26; i++){ for(auto &p: G[i]){ in[p]++; } } vector ans; queue Q; for(int i = 0; i < 26; i++){ if(is_exist[i] && in[i] == 0) Q.push(i); } if(Q.size() != 1){ cout << -1 << endl; return 0; } while(Q.size()){ int c = Q.front(); Q.pop(); ans.push_back(c); for(auto &p: G[c]){ in[p]--; if(in[p] == 0){ Q.push(p); } } } /* for(int i = 0; i < 26; i++){ cout << is_exist[i] << " : " << in[i] << " : " << G[i].size() << ": "; for(auto &p: G[i]) cout << p << " "; cout << endl; } for(auto &p: ans) cout << p << " "; */ if(ans.size() != len) cout << -1 << endl; else{ string st = ""; for(auto &p: ans) st += (char)(p+'A'); cout << st << endl; } return 0; }