#include #include #include #include void fail() { std::cout << "-1\n"; std::exit(0); } void solve() { std::vector to(26, -1); std::vector in(26, 0); std::vector used(26, false); for (int q = 0; q < 2; ++q) { int n; std::cin >> n; while (n--) { std::string s; std::cin >> s; for (char c : s) used[c - 'A'] = true; int m = s.length(); for (int i = 0; i + 1 < m; ++i) { int u = s[i] - 'A', v = s[i + 1] - 'A'; to[u] = v; ++in[v]; } } } int r = -1; for (int v = 0; v < 26; ++v) { if (!used[v]) continue; if (in[v] == 0) { if (r != -1) fail(); r = v; } } if (r == -1) fail(); std::string ans; while (r != -1) { ans.push_back('A' + r); used[r] = false; r = to[r]; } if (std::any_of(used.begin(), used.end(), [](auto b) { return b; })) fail(); std::cout << ans << "\n"; } int main() { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); solve(); return 0; }