#include <bits/stdc++.h>
#define maxN 15

using namespace std;

string a[maxN],s;
vector<pair<int,int>> adj[maxN];
int dp[maxN][1000000],ne[maxN][1000000],g[maxN][maxN];

bool proveri(string a,string b,int x){
for(int i=x;i<a.size() && i-x<b.size();i++){
    if(a[i]!=b[i-x]) return false;
}
return true;
}

bool proveri1(int k){
for(int i=0;i<s.size();i++){
    if(s[i]-'a'>=k) return false;
}
return true;
}

int uporedi(string a,string b){
int i=0,j,res=a.size(),cnt=0;
for(i=a.size()-1;i>=0;i--){
    if(proveri(a,b,i)){
        if(i+b.size()<=a.size()) cnt++;
        res=i;
    }
}
//cout<<cnt<<" "<<a<<" "<<b<<endl;
if(cnt>1) return -1;
return res;
}

int Resi(int N, int K, int* L, char** A, char* S){
int i,j;
for(i=1;i<=N;i++){
    for(j=1;j<=L[i];j++) a[i]+=A[i][j];
}
for(i=1;i<=N;i++){
    for(j=1;j<=N;j++){
        if(j==i) continue;
        int tmp=uporedi(a[i],a[j]);
        if(tmp==-1) return -1;
        adj[i].push_back({j,min(a[j].size(),a[i].size()-tmp)});
        g[i][j]=min(a[j].size(),a[i].size()-tmp);
    }
}
for(i=1;i<=N;i++){
    dp[i][(1<<i)]=0;
    ne[i][(1<<i)]=-1;
}
for(j=2;j<(1<<(N+1));j++){
    for(i=1;i<=N;i++){
        if((j|(1<<i))!=j) continue;
        for(int t=1;t<=N;t++){
            //if(j==48 && i==5) cout<<t<<" "<<g[i][t]<<" "<<j<<" "<<(j&(1<<t))<<endl;
            if(t==i || (j|(1<<t))!=j) continue;
            //if(j==48 && i==5) cout<<t<<" "<<g[i][t]<<" "<<j<<" "<<(j&(1<<t))<<endl;
            if(g[i][t]+dp[t][j^(1<<i)]>=dp[i][j]){
                //if(j==48 && i==5) cout<<t<<" "<<g[i][t]<<endl;
                dp[i][j]=g[i][t]+dp[t][j^(1<<i)];
                ne[i][j]=t;
            }
        }
    }
}
int st=0,res=-1;
for(i=1;i<=N;i++){
    if(dp[i][(1<<(N+1))-1]>res){
        res=dp[i][(1<<(N+1))-1];
        st=i;
    }
}
//cout<<res<<endl;
s+='0';
s+=a[st];
res=(1<<(N+1))-2;
//cout<<st<<" "<<ne[st][res]<<endl;
while(ne[st][res]!=-1 && res>0){
    int tmp=ne[st][res];
    //cout<<st<<" "<<tmp<<" "<<g[st][tmp]<<endl;
    for(i=g[st][tmp];i<a[tmp].size();i++) s+=a[tmp][i];
    res-=(1<<st);
    st=tmp;
}
if(!proveri1(K)) return -1;
for(i=0;i<s.size();i++) S[i]=s[i];
return s.size()-1;
}