#include using namespace std; #define int long long signed main(){ int n; cin >> n; vector a(n); for(int i = 0; i < n; i++)cin >> a[i]; int m; cin >> m; vector b(m); for(int i = 0; i < m; i++)cin >> b[i]; vector str, str1; vector tmp(n), tmp1(m); for(int i = 0; i < n; i++)tmp[i] = i; for(int i = 0; i < m; i++)tmp1[i] = i; string s; do{ s = ""; bool ok = false; for(int i = 0; i < n; i++)s += a[tmp[i]]; for(int i = 0; i < str.size(); i++){ if(str[i] == s)ok = true; } //cout << s << endl; if(!ok)str.push_back(s); }while(next_permutation(tmp.begin(), tmp.end())); do{ s = ""; bool ok = false; for(int i = 0; i < m; i++)s += b[tmp1[i]]; for(int i = 0; i < str1.size(); i++){ if(str1[i] == s)ok = true; } //cout << s << endl; if(!ok)str1.push_back(s); }while(next_permutation(tmp1.begin(), tmp1.end())); int count = 0; string ans; for(int i = 0; i < str.size(); i++){ for(int j = 0; j < str1.size(); j++){ if(str[i] == str1[j]){ count++; ans = str[i]; } } } if(count == 1)cout << ans << endl; else cout << -1 << endl; }