#include using namespace std; #define int long long using ll=long long; using vi=vector; using vl=vector; using pii=pair; using pll=pair; #define ITR(i,c) for(auto i=begin(c);i!=end(c);++i) #define FORE(x,c) for(auto &x:c) #define REPF(i,a,n) for(int i=a,i##len=(int)(n);i=0;--i) #define REPW(i,n) for(i=0;i<(int)(n);++i) #define ALL(c) begin(c),end(c) #define RALL(c) rbegin(c),rend(c) // c++14 #define SZ(c) ((int)c.size()) #define EXIST(c,x) (c.find(x)!=end(c)) #define OUTOFRANGE(y,x,h,w) (y<0||x<0||y>=h||x>=w) #define dump(...) const int DX[9]={0,1,0,-1,1,1,-1,-1,0},DY[9]={-1,0,1,0,-1,1,1,-1,0}; #define INF (1001001001) #define INFLL (1001001001001001001ll) template ostream& operator << (ostream &os,const vector &v) { ITR(i,v) os << *i << (i==end(v)-1 ? "" : "\n"); return os; } template istream& operator >> (istream &is,vector &v) { ITR(i,v) is >> * i; return is; } template istream& operator >> (istream &is, pair &p) { is >> p.first >> p.second; return is; } templatebool chmax(T &a,const T &b){if(abool chmin(T &a,const T &b){if(b adj; REP(i,26) adj[i+'A']='#'; int n,m; cin>>n; vectora(n); cin>>a; REP(i,n) { REPF(j,1,SZ(a[i])) { adj[a[i][j-1]]=a[i][j]; } } cin>>m; vectorb(m); cin>>b; string ans=""; vectordeleted(m,false); while(1) { bool ch=false; REP(i,m) { if(deleted[i]) continue; REP(j,m) { if(deleted[j]) continue; if(i==j) continue; if(adj[b[i][SZ(b[i])-1]]==b[j][0]) { b[i]+=b[j]; deleted[j]=true; ch=true; } } } if(!ch) { int cnt=0; REP(i,m) { if(!deleted[i]) { ans=b[i]; cnt++; } } if(cnt!=1) { cout<<-1<