#include <bits/stdc++.h>
using namespace std;
struct node {
    int id;
    int cnt;
	struct node *child[26];
};
struct node *initialise()
{
	struct node *newnode = (struct node *)malloc(sizeof(struct node));
	newnode->id = 0;
	newnode->cnt = 0;
	for(int i = 0; i < 26; i++) {
		newnode->child[i] = NULL;
	}
	return newnode;
}
struct node *root = initialise();
struct node *addword(string s,int id,struct node *root)
{
	int i;
	int ch;
	struct node *temp = root;
	struct node *newnode = NULL;

	for(i = 0; i < s.size(); i++) {
		ch = s[i]-'a';

		if(temp->child[ch] == NULL) {
			newnode = initialise();
			temp->child[ch] = newnode;
			temp = temp->child[ch];
		}
		else {
			temp = temp->child[ch];
		}

	}
	temp->id = id+1;
	temp->cnt += 1;
	return root;
}
string s1[100005];
string s;
int len;
int h[100005];
int dp[100005];
vector<string> ans;
int f(int pos)
{
    struct node *temp = root;
    //cout<<pos<<endl;
    if(pos == len) {
        return 1;
    }

    if(dp[pos] != -1) {
        return dp[pos];
    }

    for(int i = pos; i < len; i++) {
        int ch = s[i]-'a';
        if(temp->child[ch] && temp->child[ch]->id && temp->child[ch]->cnt > 0) {
            temp->child[ch]->cnt--;
            if(f(i+1)) {
                ans.push_back(s1[temp->child[ch]->id-1]);
                dp[pos] = 1;
                return dp[pos];
            }
            else {
                temp->child[ch]->cnt++;
                temp = temp->child[ch];
            }
        }
        else if(temp->child[ch]) {
            temp = temp->child[ch];
        }
        else {
            dp[pos] = 0;
            return dp[pos];
        }
    }
    dp[pos] = 0;
    return 0;

}
int main()
{

	string s2,t;
	vector<string> v;
	int n;
	int m;
	int q;
	int i;

	cin>>n;
	cin>>s;
	cin>>m;
	len = s.size();
	for(i = 0; i <= len; i++) {
        dp[i] = -1;
	}
	for(i = 0; i < m; i++) {
		cin>>s1[i];
		s2 = s1[i];
		transform(s2.begin(), s2.end(), s2.begin(), ::tolower);
		reverse(s2.begin(),s2.end());
		root = addword(s2,i,root);
	}
	f(0);
	for(i = ans.size()-1; i >= 0; i--) {
        cout<<ans[i]<<' ';
	}

}


#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#include<stack>
#include<iostream>
#define sig 26
#define maxn 1100005
using namespace std;
string c;
string ss[100005];
int ch[maxn][26],tot;
char dd[maxn];
char d[maxn];
int en[maxn];
int dp[maxn];
int pre[maxn];
int pp[maxn];
void get_trie(int w){
    int i,p;
    int n=c.length();
    p=0;
    for(i=0;i<n;i++){
        if(c[i]>='A'&&c[i]<='Z') c[i]=c[i]-'A'+'a';
    }
    for(i=0;i<n;i++){
        if(ch[p][c[i]-'a']){
            p=ch[p][c[i]-'a'];
        }
        else{
            ch[p][c[i]-'a']=++tot;
            p=tot;
        }
    }
    en[p]=w;
}
int a[maxn];
int main()
{
    int i,n;
    cin>>n;
    scanf("%s",dd);
    for(i=n;i>=1;i--) dd[i]=dd[i-1];
    int m;
    cin>>m;
    for(i=1;i<=m;i++){
        cin>>c;
        ss[i]=c;
        get_trie(i);
    }
    dp[0]=1;
    for(int i=1;i<=n;i++){
        int p=i;
        int q=0;
        while(p&&ch[q][dd[p]-'a']){
            q=ch[q][dd[p]-'a'];
            if(en[q]&&dp[p-1]){
                dp[i]=1;
                pp[i]=p-1;
                pre[i]=en[q];
                break;
            }
            p--;
        }
    }
    int p;
    p=n;
    int tot=0;
    while(p){
        a[tot++]=pre[p];
        p=pp[p];
    }
    for(i=tot-1;i>=0;i--){
        cout<<ss[a[i]]<<" ";
    }
}


#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#include<stack>
#include<iostream>
#define sig 26
#define maxn 1100005
using namespace std;
string c;
string ss[100005];
int ch[maxn][26],tot;
char dd[maxn];
char d[maxn];
int en[maxn];
int dp[maxn];
int pre[maxn];
void get_trie(int w){
    int i,p;
    int n=c.length();
    for(int i=0;i<n;i++){
        d[i]=c[n-i-1];
        if(d[i]>='A'&&d[i]<='Z'){
            d[i]=d[i]-'A'+'a';
        }
    }
    for(i=0;i<n;i++) c[i]=d[i];
    p=0;
    for(i=0;i<n;i++){
        if(ch[p][c[i]-'a']){
            p=ch[p][c[i]-'a'];
        }
        else{
            ch[p][c[i]-'a']=++tot;
            p=tot;
        }
        en[p]=w;
    }
}
int a[maxn];
int main()
{
    int i,n;
    cin>>n;
    scanf("%s",dd);
    for(i=n;i>=1;i--) dd[i]=dd[i-1];
    int m;
    cin>>m;
    for(i=1;i<=m;i++){
        cin>>c;
        ss[i]=c;
        get_trie(i);
    }
    dp[0]=1;
    for(i=0;i<n;i++){
        if(dp[i]==0) continue;
        int p=i+1;
        int q=0;
        while(p<=n&&ch[q][dd[p]-'a']){
            q=ch[q][dd[p]-'a'];
            if(en[q]&&dp[p]==0){
                dp[p]=1;
                pre[p]=en[q];
            }
            p++;
        }
    }
    int p;
    p=n;
    int tot=0;
    while(p){
        int w=ss[pre[p]].length();
        a[tot++]=pre[p];
        p=p-w;
    }
    for(i=tot-1;i>=0;i--){
        cout<<ss[a[i]]<<" ";
    }
}


#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#include<stack>
#include<iostream>
#define sig 26
#define maxn 1100005
using namespace std;
string c;
string ss[100005];
int ch[maxn][26],tot;
char dd[maxn];
char d[maxn];
int en[maxn];
int dp[maxn];
int pre[maxn];
int pp[maxn];
void get_trie(int w){
    int i,p;
    int n=c.length();
    for(int i=0;i<n;i++){
        d[i]=c[n-i-1];
        if(d[i]>='A'&&d[i]<='Z'){
            d[i]=d[i]-'A'+'a';
        }
    }
    for(i=0;i<n;i++) c[i]=d[i];
    p=0;
    for(i=0;i<n;i++){
        if(ch[p][c[i]-'a']){
            p=ch[p][c[i]-'a'];
        }
        else{
            ch[p][c[i]-'a']=++tot;
            p=tot;
        }
        en[p]=w;
    }
}
int a[maxn];
int main()
{
    int i,n;
    cin>>n;
    scanf("%s",dd);
    for(i=n;i>=1;i--) dd[i]=dd[i-1];
    int m;
    cin>>m;
    for(i=1;i<=m;i++){
        cin>>c;
        ss[i]=c;
        get_trie(i);
    }
    dp[0]=1;
    for(i=0;i<n;i++){
        if(dp[i]==0) continue;
        int p=i+1;
        int q=0;
        while(p<=n&&ch[q][dd[p]-'a']){
            q=ch[q][dd[p]-'a'];
            if(en[q]&&dp[p]==0){
                dp[p]=1;
                pre[p]=en[q];
                pp[p]=i;
            }
            p++;
        }
    }
    int p;
    p=n;
    int tot=0;
    while(p){
        a[tot++]=pre[p];
        p=pp[p];
    }
    for(i=tot-1;i>=0;i--){
        cout<<ss[a[i]]<<" ";
    }
}


#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#include<stack>
#include<iostream>
#define sig 26
#define maxn 1010005
using namespace std;
int ch[maxn][26],tot;
int fail[maxn];
char c[200005];
char d[100005];
int en[maxn];
int val[maxn];
int dp[10005];
int pre[10005];
vector<char>g[100005];
void init(){
    memset(ch[0],-1,sizeof(ch[0]));
}
void get_trie(int w){
    int i,p;
    int n=(int)strlen(c);
    for(int i=0;i<n;i++){
        d[i]=c[n-i-1];
        if(d[i]>='A'&&d[i]<='Z'){
            d[i]=d[i]-'A'+'a';
        }
    }
    for(i=0;i<n;i++) c[i]=d[i];
    p=0;
    for(i=0;i<n;i++){
        if(ch[p][c[i]-'a']!=-1){
            p=ch[p][c[i]-'a'];
        }
        else{
            val[tot+1]=val[p]+1;
            ch[p][c[i]-'a']=++tot;
            memset(ch[tot],-1,sizeof(ch[tot]));
            p=tot;
        }
        en[p]=w;
    }
}
void get_fail()
{
    queue<int>Q;
    int root=0;
    fail[root] = root;
    for(int i = 0;i < 26;i++)
        if(ch[root][i] == -1)
            ch[root][i] = root;
        else
        {
            fail[ch[root][i]] = root;
            Q.push(ch[root][i]);
        }
    while( !Q.empty() )
    {
        int now = Q.front();
        Q.pop();
        for(int i = 0;i < 26;i++)
            if(ch[now][i] == -1)
                ch[now][i] = ch[fail[now]][i];
            else
            {
                fail[ch[now][i]]=ch[fail[now]][i];
                Q.push(ch[now][i]);
            }
    }
}
char dd[100005];
int main()
{
    int i,n;
    cin>>n;
    scanf("%s",dd);
    init();
    int m;
    cin>>m;
    for(i=1;i<=m;i++){
        scanf("%s",c);
        int n=strlen(c);
        for(int j=0;j<n;j++){
            g[i].push_back(c[j]);
        }
        get_trie(i);
    }
    get_fail();
    int p=0;
    dp[0]=1;
    for(i=0;i<n;i++){
        while(ch[p][dd[i]-'a']==-1){
            p=fail[p];
        }
        p=ch[p][dd[i]-'a'];
        if(p==-1) p=0;
        int w=p;
        while(w){
            if(en[w]){
                if(dp[i+1-val[w]]){
                    dp[i+1]=1;
                    pre[i+1]=en[w];
                    break;
                }
            }
            w=fail[w];
        }
    }
    p=n;
    int tot=0;
    while(p){
        int n=g[pre[p]].size();
        for(int i=n-1;i>=0;i--){
            c[tot++]=g[pre[p]][i];
        }
        c[tot++]=' ';
        p=p-n;
    }
    for(i=tot-1;i>=0;i--) printf("%c",c[i]);
}


#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#include<stack>
#include<iostream>
#define sig 26
#define maxn 1000005
using namespace std;
int ch[maxn][26],tot;
int fail[maxn];
char c[10005];
char d[maxn/10];
int en[maxn];
int val[maxn];
int dp[10005];
int pre[10005];
vector<char>g[100005];
stack<int>qq;
queue<int>q;
void get_trie(int w){
    int i,p;
    int n=(int)strlen(c);
    for(int i=0;i<n;i++){
        d[i]=c[n-i-1];
        if(d[i]>='A'&&d[i]<='Z'){
            d[i]=d[i]-'A'+'a';
        }
    }
    for(i=0;i<n;i++) c[i]=d[i];
    p=0;
    for(i=0;i<n;i++){
        if(ch[p][c[i]-'a']){
            p=ch[p][c[i]-'a'];
        }
        else{
            val[tot+1]=val[p]+1;
            ch[p][c[i]-'a']=++tot;
            p=tot;
        }
        en[p]=w;
    }
}
void get_fail(void){
    int i,j,f,p;
    while(!q.empty()) q.pop();
    for(i=0;i<sig;i++){
        if(!ch[0][i]) continue;
        q.push(ch[0][i]);
        while(!q.empty()){
            p=q.front();
            q.pop();
            for(j=0;j<sig;j++){
                if(!ch[p][j]){
                    continue;
                }
                q.push(ch[p][j]);
                f=fail[p];
                while(f&&!ch[f][j]) f=fail[f];
                f=ch[f][j];
                fail[ch[p][j]]=f;
            }
        }
    }
}
char dd[10005];
int main()
{
    int i,n;
    cin>>n;
    scanf("%s",dd);
    int m;
    cin>>m;
    for(i=1;i<=m;i++){
        scanf("%s",c);
        int n=strlen(c);
        for(int j=0;j<n;j++){
            g[i].push_back(c[j]);
        }
        get_trie(i);
    }
    get_fail();
    int p=0;
    dp[0]=1;
    for(i=0;i<n;i++){
        while(ch[p][dd[i]-'a']==0){
            p=fail[p];
        }
        p=ch[p][dd[i]-'a'];
        int w=p;
        while(w){
            if(en[w]){
                if(dp[i+1-val[w]]){
                    dp[i+1]=1;
                    pre[i+1]=en[w];
                    break;
                }
            }
            w=fail[w];
        }
    }
    p=n;
    while(p){
        int n=g[pre[p]].size();
        for(int i=n-1;i>=0;i--){
            qq.push(g[pre[p]][i]);
        }
        qq.push(' ');
        p=p-n;
    }
    qq.pop();
    while(!qq.empty()){
        int p=qq.top();
        qq.pop();
        printf("%c",p);
    }
}


#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#include<stack>
#include<iostream>
#define sig 26
#define maxn 1100005
using namespace std;
char c[maxn];
int ch[maxn][26],tot;
char dd[maxn];
char d[maxn];
int en[maxn];
vector<char>g[maxn];
int dp[maxn];
int pre[maxn];
void get_trie(int w){
    int i,p;
    int n=(int)strlen(c);
    for(int i=0;i<n;i++){
        d[i]=c[n-i-1];
        if(d[i]>='A'&&d[i]<='Z'){
            d[i]=d[i]-'A'+'a';
        }
    }
    for(i=0;i<n;i++) c[i]=d[i];
    p=0;
    for(i=0;i<n;i++){
        if(ch[p][c[i]-'a']){
            p=ch[p][c[i]-'a'];
        }
        else{
            ch[p][c[i]-'a']=++tot;
            p=tot;
        }
        en[p]=w;
    }
}
int main()
{
    int i,n;
    cin>>n;
    scanf("%s",dd);
    for(i=n;i>=1;i--) dd[i]=dd[i-1];
    int m;
    cin>>m;
    for(i=1;i<=m;i++){
        scanf("%s",c);
        int n=strlen(c);
        for(int j=0;j<n;j++){
            g[i].push_back(c[j]);
        }
        get_trie(i);

    }
    dp[0]=1;
    for(i=0;i<n;i++){
        if(dp[i]==0) continue;
        int p=i+1;
        int q=0;
        while(p<=n&&ch[q][dd[p]-'a']){
            q=ch[q][dd[p]-'a'];
            if(en[q]&&dp[p]==0){
                dp[p]=1;
                pre[p]=en[q];
            }
            p++;
        }
    }
    int p;
    p=n;
    int tot=0;
    while(p){
        int n=g[pre[p]].size();
        for(int i=n-1;i>=0;i--){
            c[tot++]=g[pre[p]][i];
        }
        c[tot++]=' ';
        p=p-n;
    }
    for(i=tot-1;i>=0;i--) printf("%c",c[i]);
}


#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#include<stack>
#include<iostream>
#define sig 26
#define maxn 1060050
using namespace std;
int ch[maxn][26],tot;
int fail[maxn];
char c[10005];
char d[maxn];
int en[maxn];
int val[maxn];
int dp[10005];
int pre[10005];
vector<int>g[100005];
stack<int>qq;
queue<int>q;
void get_trie(int w){
    int i,p;
    int n=(int)strlen(c);
    for(int i=0;i<n;i++){
        d[i]=c[n-i-1];
        if(d[i]>='A'&&d[i]<='Z'){
            d[i]=d[i]-'A'+'a';
        }
    }
    for(i=0;i<n;i++) c[i]=d[i];
    p=0;
    for(i=0;i<n;i++){
        if(ch[p][c[i]-'a']){
            p=ch[p][c[i]-'a'];
        }
        else{
            val[tot+1]=val[p]+1;
            ch[p][c[i]-'a']=++tot;
            p=tot;
        }
        en[p]=w;
    }
}
void get_fail(void){
    int i,j,f,p;
    while(!q.empty()) q.pop();
    for(i=0;i<sig;i++){
        if(!ch[0][i]) continue;
        q.push(ch[0][i]);
        while(!q.empty()){
            p=q.front();
            q.pop();
            for(j=0;j<sig;j++){
                if(!ch[p][j]){
                    continue;
                }
                q.push(ch[p][j]);
                f=fail[p];
                while(f&&!ch[f][j]) f=fail[f];
                f=ch[f][j];
                fail[ch[p][j]]=f;
            }
        }
    }
}
char dd[10005];
int main()
{
    int i,n;
    cin>>n;
    scanf("%s",dd);
    int m;
    cin>>m;
    for(i=1;i<=m;i++){
        scanf("%s",c);
        int n=strlen(c);
        for(int j=0;j<n;j++){
            g[i].push_back(c[j]);
        }
        get_trie(i);
    }
    get_fail();
    int p=0;
    dp[0]=1;
    for(i=0;i<n;i++){
        while(ch[p][dd[i]-'a']==0){
            p=fail[p];
        }
        p=ch[p][dd[i]-'a'];
        int w=p;
        while(w){
            if(en[w]){
                if(dp[i+1-val[w]]){
                    dp[i+1]=1;
                    pre[i+1]=en[w];
                    break;
                }
            }
            w=fail[w];
        }
    }
    p=n;
    while(p){
        int n=g[pre[p]].size();
        for(int i=n-1;i>=0;i--){
            qq.push(g[pre[p]][i]);
        }
        qq.push(' ');
        p=p-n;
    }
    qq.pop();
    while(!qq.empty()){
        int p=qq.top();
        qq.pop();
        printf("%c",p);
    }
}


#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#include<stack>
#include<iostream>
#define sig 26
#define maxn 1000005
using namespace std;
int ch[maxn][26],tot;
int fail[maxn];
char c[20005];
char d[10005];
int en[maxn];
int val[maxn];
int dp[10005];
int pre[10005];
vector<char>g[100005];
void get_trie(int w){
    int i,p;
    int n=(int)strlen(c);
    for(int i=0;i<n;i++){
        d[i]=c[n-i-1];
        if(d[i]>='A'&&d[i]<='Z'){
            d[i]=d[i]-'A'+'a';
        }
    }
    for(i=0;i<n;i++) c[i]=d[i];
    p=0;
    for(i=0;i<n;i++){
        if(ch[p][c[i]-'a']){
            p=ch[p][c[i]-'a'];
        }
        else{
            val[tot+1]=val[p]+1;
            ch[p][c[i]-'a']=++tot;
            p=tot;
        }
        en[p]=w;
    }
}
void get_fail()
{
    queue<int>Q;
    int root=0;
    fail[root] = root;
    for(int i = 0;i < 26;i++)
        if(ch[root][i] == 0)
            ch[root][i] = root;
        else
        {
            fail[ch[root][i]] = root;
            Q.push(ch[root][i]);
        }
    while( !Q.empty() )
    {
        int now = Q.front();
        Q.pop();
        for(int i = 0;i < 26;i++)
            if(ch[now][i] == 0)
                ch[now][i] = ch[fail[now]][i];
            else
            {
                fail[ch[now][i]]=ch[fail[now]][i];
                Q.push(ch[now][i]);
            }
    }
}
char dd[10005];
int main()
{
    int i,n;
    cin>>n;
    scanf("%s",dd);
    int m;
    cin>>m;
    for(i=1;i<=m;i++){
        scanf("%s",c);
        int n=strlen(c);
        for(int j=0;j<n;j++){
            g[i].push_back(c[j]);
        }
        get_trie(i);
    }
    get_fail();
    int p=0;
    dp[0]=1;
    for(i=0;i<n;i++){
        while(ch[p][dd[i]-'a']==0){
            p=fail[p];
        }
        p=ch[p][dd[i]-'a'];
        int w=p;
        while(w){
            if(en[w]){
                if(dp[i+1-val[w]]){
                    dp[i+1]=1;
                    pre[i+1]=en[w];
                    break;
                }
            }
            w=fail[w];
        }
    }
    p=n;
    int tot=0;
    while(p){
        int n=g[pre[p]].size();
        for(int i=n-1;i>=0;i--){
            c[tot++]=g[pre[p]][i];
        }
        c[tot++]=' ';
        p=p-n;
    }
    for(i=tot-1;i>=0;i--) printf("%c",c[i]);
}


#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#include<stack>
#include<iostream>
#define sig 26
#define maxn 1100005
using namespace std;
int ch[maxn][26],tot;
int fail[maxn];
char c[maxn];
char d[maxn];
int en[maxn];
int val[maxn];
int dp[maxn];
int pre[maxn];
vector<char>g[maxn];
void get_trie(int w){
    int i,p;
    int n=(int)strlen(c);
    for(int i=0;i<n;i++){
        d[i]=c[n-i-1];
        if(d[i]>='A'&&d[i]<='Z'){
            d[i]=d[i]-'A'+'a';
        }
    }
    for(i=0;i<n;i++) c[i]=d[i];
    p=0;
    for(i=0;i<n;i++){
        if(ch[p][c[i]-'a']){
            p=ch[p][c[i]-'a'];
        }
        else{
            val[tot+1]=val[p]+1;
            ch[p][c[i]-'a']=++tot;
            p=tot;
        }
        en[p]=w;
    }
}
void get_fail()
{
    queue<int>Q;
    int root=0;
    fail[root] = root;
    for(int i = 0;i < 26;i++)
        if(ch[root][i] == 0)
            ch[root][i] = root;
        else
        {
            fail[ch[root][i]] = root;
            Q.push(ch[root][i]);
        }
    while( !Q.empty() )
    {
        int now = Q.front();
        Q.pop();
        for(int i = 0;i < 26;i++)
            if(ch[now][i] == 0)
                ch[now][i] = ch[fail[now]][i];
            else
            {
                fail[ch[now][i]]=ch[fail[now]][i];
                Q.push(ch[now][i]);
            }
    }
}
char dd[maxn];
int main()
{
    int i,n;
    cin>>n;
    scanf("%s",dd);
    int m;
    cin>>m;
    for(i=1;i<=m;i++){
        scanf("%s",c);
        int n=strlen(c);
        for(int j=0;j<n;j++){
            g[i].push_back(c[j]);
        }
        get_trie(i);
    }
    get_fail();
    int p=0;
    dp[0]=1;
    for(i=0;i<n;i++){
        while(ch[p][dd[i]-'a']==0){
            p=fail[p];
        }
        p=ch[p][dd[i]-'a'];
        int w=p;
        while(w){
            if(en[w]){
                if(dp[i+1-val[w]]){
                    dp[i+1]=1;
                    pre[i+1]=en[w];
                    break;
                }
            }
            w=fail[w];
        }
    }
    p=n;
    int tot=0;
    while(p){
        int n=g[pre[p]].size();
        for(int i=n-1;i>=0;i--){
            c[tot++]=g[pre[p]][i];
        }
        c[tot++]=' ';
        p=p-n;
    }
    for(i=tot-1;i>=0;i--) printf("%c",c[i]);
}


#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#include<stack>
#include<iostream>
#define sig 26
#define maxn 1000005
using namespace std;
int ch[maxn][26],tot;
int fail[maxn];
char c[200005];
char d[100005];
int en[maxn];
int val[maxn];
int dp[10005];
int pre[10005];
vector<char>g[100005];
void get_trie(int w){
    int i,p;
    int n=(int)strlen(c);
    for(int i=0;i<n;i++){
        d[i]=c[n-i-1];
        if(d[i]>='A'&&d[i]<='Z'){
            d[i]=d[i]-'A'+'a';
        }
    }
    for(i=0;i<n;i++) c[i]=d[i];
    p=0;
    for(i=0;i<n;i++){
        if(ch[p][c[i]-'a']){
            p=ch[p][c[i]-'a'];
        }
        else{
            val[tot+1]=val[p]+1;
            ch[p][c[i]-'a']=++tot;
            p=tot;
        }
        en[p]=w;
    }
}
void get_fail()
{
    queue<int>Q;
    int root=0;
    fail[root] = root;
    for(int i = 0;i < 26;i++)
        if(ch[root][i] == 0)
            ch[root][i] = root;
        else
        {
            fail[ch[root][i]] = root;
            Q.push(ch[root][i]);
        }
    while( !Q.empty() )
    {
        int now = Q.front();
        Q.pop();
        for(int i = 0;i < 26;i++)
            if(ch[now][i] == 0)
                ch[now][i] = ch[fail[now]][i];
            else
            {
                fail[ch[now][i]]=ch[fail[now]][i];
                Q.push(ch[now][i]);
            }
    }
}
char dd[10005];
int main()
{
    int i,n;
    cin>>n;
    scanf("%s",dd);
    int m;
    cin>>m;
    for(i=1;i<=m;i++){
        scanf("%s",c);
        int n=strlen(c);
        for(int j=0;j<n;j++){
            g[i].push_back(c[j]);
        }
        get_trie(i);
    }
    get_fail();
    int p=0;
    dp[0]=1;
    for(i=0;i<n;i++){
        while(ch[p][dd[i]-'a']==0){
            p=fail[p];
        }
        p=ch[p][dd[i]-'a'];
        int w=p;
        while(w){
            if(en[w]){
                if(dp[i+1-val[w]]){
                    dp[i+1]=1;
                    pre[i+1]=en[w];
                    break;
                }
            }
            w=fail[w];
        }
    }
    p=n;
    int tot=0;
    while(p){
        int n=g[pre[p]].size();
        for(int i=n-1;i>=0;i--){
            c[tot++]=g[pre[p]][i];
        }
        c[tot++]=' ';
        p=p-n;
    }
    for(i=tot-1;i>=0;i--) printf("%c",c[i]);
}


#include <bits/stdc++.h>

using namespace::std;

#define ll long long int
#define mp make_pair
#define ff first
#define ss second
#define end END
const int MOD=1000000007,INF = 1e9,MAX = 2e6;

vector < ll >  v;
string name[MAX];

ll a[MAX][26];

ll nn ;
ll b[MAX],d[MAX];
ll end[MAX];
char words[MAX];

int main()
{
	std::ios_base::sync_with_stdio(false);

	ll i,j,k,n,m,t;
	cin>>n;

	string s;

	memset(a, 0, sizeof(a));

	cin>>s;
	cin>>m;

	end[1]=-1;

	nn=1;

	for(i=0;i<m;i++)
	{
		cin>>words;

		ll len = strlen(words);

		name[i]="";

		for(j=0;j<len;j++)
		{
			name[i]+=words[j];
		}

		for(j=0;j<len;j++)
		{
			if(words[j]>='A' && words[j]<='Z')
			words[j]+=32;
		}

		ll t = 1;

		for(j=0;j<len;j++)
		{
			ll c = words[j]-'a';

			if(a[t][c]==0)
			{
				nn++;
				end[nn]=-1;
				a[t][c]=nn;
			}

			t=a[t][c];
		}
		end[t]=i;
	}

	for(i=0;i<n;i++)
	{
		b[i]=-1;
		d[i]=-1;
	}

	b[n]=0;
	d[n]=0;

	for(i=n;i>0;i--)
	{
		if(b[i]==-1)
		continue;
		else
		{
			j = i-1;
			t=1;

			for(j=i-1;j>=0;j--)
			{
				ll c = s[j]-'a';

				if(a[t][c]==0)
				break;

				t=a[t][c];

				if(end[t]!=-1)
				{
					b[j]=i;
					d[j]=end[t];
				}
			}
		}
	}

	for(i=0;i<n;)
	{
		cout<<name[d[i]]<<" ";
		i=b[i];
	}

}


#include <bits/stdc++.h>

using namespace::std;

#define ll long long int
#define mp make_pair
#define ff first
#define ss second
#define end END
const int MOD=1000000007,INF = 1e9,MAX = 2e6;

vector < ll >  v;
string name[100001];
string name1[100001];
ll a[100004][26];
ll nn = 1;
ll b[10001],d[10001];
ll end[10001];

int main()
{
	std::ios_base::sync_with_stdio(false);

	ll i,j,k,n,m,t;

	cin>>n;

	string s;

	cin>>s;
	cin>>m;

	end[1]=-1;

	for(i=0;i<m;i++)
	{
		cin>>name[i];
		name1[i]=name[i];

		for(j=0;j<name[i].length();j++)
		{
			if(name[i][j]>='A' && name[i][j]<='Z')
			name[i][j]+=32;
		}

		ll t = 1;

		for(j=0;j<name[i].length();j++)
		{
			ll c = name[i][j]-'a';

			if(a[t][c]==0)
			{
				nn++;
				end[nn]=-1;
				a[t][c]=nn;
			}

			t=a[t][c];
		}
		end[t]=i;
	}

	for(i=0;i<n;i++)
	{
		b[i]=-1;
		d[i]=-1;
	}

	b[n]=0;
	d[n]=0;

	for(i=n;i>0;i--)
	{
		if(b[i]==-1)
		continue;
		else
		{
			j = i-1;
			t=1;

			for(j=i-1;j>=0;j--)
			{
				ll c = s[j]-'a';

				if(a[t][c]==0)
				break;

				t=a[t][c];

				if(end[t]!=-1)
				{
					b[j]=i;
					d[j]=end[t];
				}
			}
		}
	}

	for(i=0;i<n;)
	{
		cout<<name1[d[i]]<<" ";
		i=b[i];
	}

}


#include <bits/stdc++.h>

using namespace::std;

#define ll long long int
#define mp make_pair
#define ff first
#define ss second
const int MOD=1000000007,INF = 1e9,MAX = 2e6;

vector < ll >  v;
string name[100001];
string name1[100001];
ll a[100004][26];
ll nn = 1;
ll b[10001],d[10001];
ll end[10001];

int main()
{
	std::ios_base::sync_with_stdio(false);

	ll i,j,k,n,m,t;

	cin>>n;

	string s;

	cin>>s;
	cin>>m;

	end[1]=-1;

	for(i=0;i<m;i++)
	{
		cin>>name[i];
		name1[i]=name[i];

		for(j=0;j<name[i].length();j++)
		{
			if(name[i][j]>='A' && name[i][j]<='Z')
			name[i][j]+=32;
		}

		ll t = 1;

		for(j=0;j<name[i].length();j++)
		{
			ll c = name[i][j]-'a';

			if(a[t][c]==0)
			{
				nn++;
				end[nn]=-1;
				a[t][c]=nn;
			}

			t=a[t][c];
		}
		end[t]=i;
	}

	for(i=0;i<n;i++)
	{
		b[i]=-1;
		d[i]=-1;
	}

	b[n]=0;
	d[n]=0;
	;

	for(i=n;i>=0;i--)
	{
		if(b[i]==-1)
		continue;
		else
		{
			j = i-1;
			t=1;

			for(j=i-1;j>=0;j--)
			{
				ll c = s[j]-'a';

				if(a[t][c]==0)
				break;

				t=a[t][c];

				if(end[t]!=-1)
				{
					b[j]=i;
					d[j]=end[t];
				}
			}
		}
	}

	for(i=0;i<n;)
	{
		cout<<name1[d[i]]<<" ";
		i=b[i];
	}

}


#include <bits/stdc++.h>

using namespace::std;

#define ll long long int
#define mp make_pair
#define ff first
#define ss second
#define end END
const int MOD=1000000007,INF = 1e9,MAX = 2e6;

vector < ll >  v;
string name[100001];
string name1[100001];
ll a[100004][26];
ll nn = 1;
ll b[10001],d[10001];
ll end[10001];

int main()
{
	std::ios_base::sync_with_stdio(false);

	ll i,j,k,n,m,t;

	cin>>n;

	string s;

	cin>>s;
	cin>>m;

	end[1]=-1;

	for(i=0;i<m;i++)
	{
		cin>>name[i];
		name1[i]=name[i];

		for(j=0;j<name[i].length();j++)
		{
			if(name[i][j]>='A' && name[i][j]<='Z')
			name[i][j]+=32;
		}

		ll t = 1;

		for(j=0;j<name[i].length();j++)
		{
			ll c = name[i][j]-'a';

			if(a[t][c]==0)
			{
				nn++;
				end[nn]=-1;
				a[t][c]=nn;
			}

			t=a[t][c];
		}
		end[t]=i;
	}

	for(i=0;i<n;i++)
	{
		b[i]=-1;
		d[i]=-1;
	}

	b[n]=0;
	d[n]=0;

	for(i=n;i>=0;i--)
	{
		if(b[i]==-1)
		continue;
		else
		{
			j = i-1;
			t=1;

			for(j=i-1;j>=0;j--)
			{
				ll c = s[j]-'a';

				if(a[t][c]==0)
				break;

				t=a[t][c];

				if(end[t]!=-1)
				{
					b[j]=i;
					d[j]=end[t];
				}
			}
		}
	}

	for(i=0;i<n;)
	{
		cout<<name1[d[i]]<<" ";
		i=b[i];
	}

}


#include <bits/stdc++.h>

using namespace::std;

#define ll long long int
#define mp make_pair
#define ff first
#define ss second
const int MOD=1000000007,INF = 1e9,MAX = 2e6;

vector < ll >  v;
string name[100001];
string name1[100001];
ll a[100004][26];
ll nn = 1;
ll b[10001],d[10001];
ll end[10001];

int main()
{
	std::ios_base::sync_with_stdio(false);

	ll i,j,k,n,m,t;

	cin>>n;

	string s;

	cin>>s;
	cin>>m;

	end[1]=-1;

	for(i=0;i<m;i++)
	{
		cin>>name[i];
		name1[i]=name[i];

		for(j=0;j<name[i].length();j++)
		{
			if(name[i][j]>='A' && name[i][j]<='Z')
			name[i][j]+=32;
		}

		ll t = 1;

		for(j=0;j<name[i].length();j++)
		{
			ll c = name[i][j]-'a';

			if(a[t][c]==0)
			{
				nn++;
				end[nn]=-1;
				a[t][c]=nn;
			}

			t=a[t][c];
		}
		end[t]=i;
	}

	for(i=0;i<n;i++)
	{
		b[i]=-1;
		d[i]=-1;
	}

	b[n]=0;
	d[n]=0;

	for(i=n;i>=0;i--)
	{
		if(b[i]==-1)
		continue;
		else
		{
			j = i-1;
			t=1;

			for(j=i-1;j>=0;j--)
			{
				ll c = s[j]-'a';

				if(a[t][c]==0)
				break;

				t=a[t][c];

				if(end[t]!=-1)
				{
					b[j]=i;
					d[j]=end[t];
				}
			}
		}
	}

	for(i=0;i<n;)
	{
		cout<<name1[d[i]]<<" ";
		i=b[i];
	}

}


#include <bits/stdc++.h>

using namespace::std;

#define ll long long int
#define mp make_pair
#define ff first
#define ss second
#define end END
const int MOD=1000000007,INF = 1e9,MAX = 1e6+10;

vector < ll >  v;
string name[MAX];

ll a[MAX][26];

ll nn ;
ll b[MAX],d[MAX];
ll end[MAX];
char words[MAX];

int main()
{
	std::ios_base::sync_with_stdio(false);

	ll i,j,k,n,m,t;
	cin>>n;

	string s;

	memset(a, 0, sizeof(a));

	cin>>s;
	cin>>m;

	end[1]=-1;

	nn=1;

	for(i=0;i<m;i++)
	{
		cin>>words;

		ll len = strlen(words);

		name[i]="";

		for(j=0;j<len;j++)
		{
			name[i]+=words[j];
		}

		for(j=0;j<len;j++)
		{
			if(words[j]>='A' && words[j]<='Z')
			words[j]+=32;
		}

		ll t = 1;

		for(j=0;j<len;j++)
		{
			ll c = words[j]-'a';

			if(a[t][c]==0)
			{
				nn++;
				end[nn]=-1;
				a[t][c]=nn;
			}

			t=a[t][c];
		}
		end[t]=i;
	}

	for(i=0;i<n;i++)
	{
		b[i]=-1;
		d[i]=-1;
	}

	b[n]=0;
	d[n]=0;

	for(i=n;i>0;i--)
	{
		if(b[i]==-1)
		continue;
		else
		{
			j = i-1;
			t=1;

			for(j=i-1;j>=0;j--)
			{
				ll c = s[j]-'a';

				if(a[t][c]==0)
				break;

				t=a[t][c];

				if(end[t]!=-1)
				{
					b[j]=i;
					d[j]=end[t];
				}
			}
		}
	}

	for(i=0;i<n;)
	{
		cout<<name[d[i]]<<" ";
		i=b[i];
	}

}


#include <bits/stdc++.h>

using namespace::std;

#define ll long long int
#define mp make_pair
#define ff first
#define ss second
#define end END
const int MOD=1000000007,INF = 1e9,MAX = 2e6;

vector < ll >  v;
string name[100001];
string name1[100001];
ll a[100004][26];

ll nn = 1;
ll b[10001],d[10001];
ll end[10001];

int main()
{
	std::ios_base::sync_with_stdio(false);

	ll i,j,k,n,m,t;
	cin>>n;

	string s;

	memset(a, 0, sizeof(a));

	cin>>s;
	cin>>m;

	end[1]=-1;

	for(i=0;i<m;i++)
	{
		cin>>name[i];
		name1[i]=name[i];

		for(j=0;j<name[i].length();j++)
		{
			if(name[i][j]>='A' && name[i][j]<='Z')
			name[i][j]+=32;
		}

		ll t = 1;

		for(j=0;j<name[i].length();j++)
		{
			ll c = name[i][j]-'a';

			if(a[t][c]==0)
			{
				nn++;
				end[nn]=-1;
				a[t][c]=nn;
			}

			t=a[t][c];
		}
		end[t]=i;
	}

	for(i=0;i<n;i++)
	{
		b[i]=-1;
		d[i]=-1;
	}

	b[n]=0;
	d[n]=0;

	for(i=n;i>0;i--)
	{
		if(b[i]==-1)
		continue;
		else
		{
			j = i-1;
			t=1;

			for(j=i-1;j>=0;j--)
			{
				ll c = s[j]-'a';

				if(a[t][c]==0)
				break;

				t=a[t][c];

				if(end[t]!=-1)
				{
					b[j]=i;
					d[j]=end[t];
				}
			}
		}
	}

	for(i=0;i<n;)
	{
		cout<<name1[d[i]]<<" ";
		i=b[i];
	}

}


#include <bits/stdc++.h>

using namespace::std;

#define ll long long int
#define mp make_pair
#define ff first
#define ss second
#define end END
const int MOD=1000000007,INF = 1e9,MAX = 2e6;

vector < ll >  v;
string name[100001];
string name1[100001];
ll a[100004][26];

ll nn ;
ll b[100001],d[100001];
ll end[100001];

int main()
{
	std::ios_base::sync_with_stdio(false);

	ll i,j,k,n,m,t;
	cin>>n;

	string s;

	memset(a, 0, sizeof(a));

	cin>>s;
	cin>>m;

	end[1]=-1;

	nn=1;

	for(i=0;i<m;i++)
	{
		cin>>name[i];
		name1[i]=name[i];

		for(j=0;j<name[i].length();j++)
		{
			if(name[i][j]>='A' && name[i][j]<='Z')
			name[i][j]+=32;
		}

		ll t = 1;

		for(j=0;j<name[i].length();j++)
		{
			ll c = name[i][j]-'a';

			if(a[t][c]==0)
			{
				nn++;
				end[nn]=-1;
				a[t][c]=nn;
			}

			t=a[t][c];
		}
		end[t]=i;
	}

	for(i=0;i<n;i++)
	{
		b[i]=-1;
		d[i]=-1;
	}

	b[n]=0;
	d[n]=0;

	for(i=n;i>0;i--)
	{
		if(b[i]==-1)
		continue;
		else
		{
			j = i-1;
			t=1;

			for(j=i-1;j>=0;j--)
			{
				ll c = s[j]-'a';

				if(a[t][c]==0)
				break;

				t=a[t][c];

				if(end[t]!=-1)
				{
					b[j]=i;
					d[j]=end[t];
				}
			}
		}
	}

	for(i=0;i<n;)
	{
		cout<<name1[d[i]]<<" ";
		i=b[i];
	}

}


#include <bits/stdc++.h>

using namespace::std;

#define ll long long int
#define mp make_pair
#define ff first
#define ss second
#define end END
const int MOD=1000000007,INF = 1e9,MAX = 2e6;

vector < ll >  v;
string name[100001];

ll a[100004][26];

ll nn ;
ll b[100001],d[100001];
ll end[100001];
char words[100001];

int main()
{
	std::ios_base::sync_with_stdio(false);

	ll i,j,k,n,m,t;
	cin>>n;

	string s;

	memset(a, 0, sizeof(a));

	cin>>s;
	cin>>m;

	end[1]=-1;

	nn=1;

	for(i=0;i<m;i++)
	{
		cin>>words;

		ll len = strlen(words);

		name[i]="";

		for(j=0;j<len;j++)
		{
			name[i]+=words[j];
		}

		for(j=0;j<len;j++)
		{
			if(words[j]>='A' && words[j]<='Z')
			words[j]+=32;
		}

		ll t = 1;

		for(j=0;j<len;j++)
		{
			ll c = words[j]-'a';

			if(a[t][c]==0)
			{
				nn++;
				end[nn]=-1;
				a[t][c]=nn;
			}

			t=a[t][c];
		}
		end[t]=i;
	}

	for(i=0;i<n;i++)
	{
		b[i]=-1;
		d[i]=-1;
	}

	b[n]=0;
	d[n]=0;

	for(i=n;i>0;i--)
	{
		if(b[i]==-1)
		continue;
		else
		{
			j = i-1;
			t=1;

			for(j=i-1;j>=0;j--)
			{
				ll c = s[j]-'a';

				if(a[t][c]==0)
				break;

				t=a[t][c];

				if(end[t]!=-1)
				{
					b[j]=i;
					d[j]=end[t];
				}
			}
		}
	}

	for(i=0;i<n;)
	{
		cout<<name[d[i]]<<" ";
		i=b[i];
	}

}


#include <bits/stdc++.h>

using namespace std;

#define Long long long
#define ld long double
#define pii pair<int, int>
#define pli pair<Long, int>

const int me = 100025;
const int mod = 0x3b9aca07;
const int mod2 = 961748941LL;;
const int mul = 203;
const int mul2 = 257;
int max_s = 20;

int n, m;
string s;
string words[me], low[me];
int par[me];
map<pair<int, int>, int> pos;
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    cin >> n;
    cin >> s;
    reverse(s.begin(), s.end());

    cin >> m;
    for(int i = 0 ; i < m ; i ++)
        cin >> words[i];
    int max_iterations = 0;
    for(int i = 0 ; i < m ; i ++)
        max_iterations = max(max_iterations, (int)words[i].size());
    for(int i = 0 ; i < m ; i ++){
        low[i] = words[i];
        transform(low[i].begin(), low[i].end(), low[i].begin(), ::tolower);
        reverse(low[i].begin(), low[i].end());
        Long h = 0, h2 = 0;
        for(char j : low[i])
            h = (1LL * h * mul + j) % mod,
            h2 = (1LL * h2 * mul2 + j) % mod2;
        pos[make_pair(h, h2)] = i;
    }
    for(int i = 0 ; i < n ; i ++){
        Long h = 0, h2 = 0;
        for(int j = 0 ; j <= max_iterations && i - j >= 0; j ++){
            h = (1LL * h * mul + s[i - j]) % mod;
            h2 = (1LL * h2 * mul2 + s[i - j]) % mod2;
            if(pos.count(make_pair(h, h2))){
                par[i] = pos[make_pair(h, h2)];
                break;
            }
        }
    }
    vector<int> result;
    for(int i = n - 1 ; i >= 0 ; ){
        result.push_back(par[i]);
        i -= words[ par[i] ].size();
    }
    for(int i : result)
        cout << words[i] << " ";
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define Long long long
#define ld long double
#define pii pair<int, int>
#define pli pair<Long, int>

const int me = 100025;
const int mod = 0x3b9aca07;
const int mul = 203;
int max_s = 20;

int n, m;
string s;
string words[me], low[me];
int par[me];
unordered_map<int, int> pos;

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    cin >> n;
    cin >> s;
    reverse(s.begin(), s.end());

    cin >> m;
    for(int i = 0 ; i < m ; i ++)
        cin >> words[i];
    int max_iterations = 0;
    for(int i = 0 ; i < m ; i ++)
        max_iterations = max(max_iterations, (int)words[i].size());
    for(int i = 0 ; i < m ; i ++){
        low[i] = words[i];
        transform(low[i].begin(), low[i].end(), low[i].begin(), ::tolower);
        reverse(low[i].begin(), low[i].end());
        Long h = 0;
        for(char j : low[i])
            h = (1LL * h * mul + j) % mod;
        pos[h] = i;
    }
    for(int i = 0 ; i < n ; i ++){
        Long h = 0;
        for(int j = 0 ; j < max_iterations && i - j >= 0; j ++){
            h = (1LL * h * mul + s[i - j]) % mod;
            if(pos.count(h)){
                par[i] = pos[h];
                break;
            }
        }
    }
    vector<int> result;
    for(int i = n - 1 ; i >= 0 ; i ){
        result.push_back(par[i]);
        i -= words[ par[i] ].size();
    }
    for(int i : result)
        cout << words[i] << " ";
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define Long long long
#define ld long double
#define pii pair<int, int>
#define pli pair<Long, int>

const int me = 100025;
const int mod = 0x3b9aca09;
int max_s = 20;

int n, m;
string s;
string words[me], low[me];
int par[me];
unordered_map<string, int> pos;
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    cin >> n;
    cin >> s;
    reverse(s.begin(), s.end());

    cin >> m;
    for(int i = 0 ; i < m ; i ++)
        cin >> words[i];
    int max_iterations = 0;
    for(int i = 0 ; i < m ; i ++)
        max_iterations = max(max_iterations, (int)words[i].size());
    for(int i = 0 ; i < m ; i ++){
        low[i] = words[i];
        transform(low[i].begin(), low[i].end(), low[i].begin(), ::tolower);
        reverse(low[i].begin(), low[i].end());
        pos[ low[i] ] = i;
    }
    for(int i = 0 ; i < n ; i ++){
        string go = "";
        for(int j = 0 ; j < max_iterations && i - j >= 0; j ++){
            go.push_back(s[i - j]);
            if(pos.count(go)){
                par[i] = pos[go];
                break;
            }
        }
    }
    vector<int> result;
    for(int i = n - 1 ; i >= 0 ; i ){
        result.push_back(par[i]);
        i -= words[ par[i] ].size();
    }
    for(int i : result)
        cout << words[i] << " ";
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define Long long long
#define ld long double
#define pii pair<int, int>
#define pli pair<Long, int>

const int me = 100025;
const int mod = 0x3b9aca07;
const int mul = 211;
int max_s = 20;

int n, m;
string s;
string words[me], low[me];
int par[me];
unordered_map<int, int> pos;

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    cin >> n;
    cin >> s;
    reverse(s.begin(), s.end());

    cin >> m;
    for(int i = 0 ; i < m ; i ++)
        cin >> words[i];
    int max_iterations = 0;
    for(int i = 0 ; i < m ; i ++)
        max_iterations = max(max_iterations, (int)words[i].size());
    for(int i = 0 ; i < m ; i ++){
        low[i] = words[i];
        transform(low[i].begin(), low[i].end(), low[i].begin(), ::tolower);
        reverse(low[i].begin(), low[i].end());
        Long h = 0;
        for(char j : low[i])
            h = (1LL * h * mul + j) % mod;
        pos[h] = i;
    }
    for(int i = 0 ; i < n ; i ++){
        Long h = 0;
        for(int j = 0 ; j < max_iterations && i - j >= 0; j ++){
            h = (1LL * h * mul + s[i - j]) % mod;
            if(pos.count(h)){
                par[i] = pos[h];
                break;
            }
        }
    }
    vector<int> result;
    for(int i = n - 1 ; i >= 0 ; i ){
        result.push_back(par[i]);
        i -= words[ par[i] ].size();
    }
    for(int i : result)
        cout << words[i] << " ";
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define mp make_pair
#define all(x) x.begin(), x.end()
#define Long long long
#define pii pair<int, int>
#define pli pair<Long, Long>
#define ld long double
#define si(x) scanf("%d", &x)
#define out(x) printf("%d\n", x);
/// Me 3nc4nta Soli
const Long inf = 140101008;
const int me = 100005;
const Long fixx = 26;
const Long mod = 1.e9 + 7;

int n, m;
string s;
string a[me];

inline int g(char x){
    if(x < 97) return x + 32;
    return x;
}
int main()
{
    ios_base:: sync_with_stdio(false);
    cin.tie(false);

    cin >> n;
    cin >> s;
    cin >> m;

    for(int i = 0 ; i < m ; i ++)
        cin >> a[i];

    int ptr = 0;
    for( ; ptr < n ; ){
        int best = 0, idx = 0;
        for(int j = 0 ; j < m && ptr + best < n ; j ++){
            int k = 0;
            int fr = ptr, fs = a[j].size() - 1;

            while(fr < n && fs >= 0 && g(a[j][fs]) == s[fr])
                fr ++, fs --, k ++;
            if(fs == 0 && k > best){
                best = k;
                idx = j;
            }
        }
        cout << a[idx] << " ";
        ptr += best + 1;
    }
    cout << endl;

    ///cout << "Elapsed time is: " << clock() / CLOCKS_PER_SEC << endl;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define Long long long
#define ld long double
#define pii pair<int, int>
#define pli pair<Long, int>

const int me = 10025;
const int mod = 1.e9 + 7;

class Trie{
private:
    const static int ALPHABET = 26;
    const static int N = 123456;
    struct node{
        int link[ALPHABET];
        bool is_word;
        int who;
        node(){
            for(int i = 0; i < ALPHABET; i ++)
                link[i] = -1;
            is_word = false;
            who = -1;
        }
    };
    node make_node(){
        node c = node();
        return c;
    }
    int get(char x){
        return x - 'a';
    }
    void traverse(int pos, string s){
        if(trie[pos].is_word){
            cout << "word: " << s << endl;
        }
        for(int i = 0; i < ALPHABET; i ++)
            if(trie[pos].link[i] != -1)
                traverse(trie[pos].link[i], s + (char)(i + 'a'));
    }

public:
    vector<node> trie;
    int Size;
    Trie(){
        trie.push_back(make_node());
        Size = 1;
    }
    ~Trie() {
    };
    int size(){
        return Size;
    }
    void add(string s, int id){
        int pos = 0;
        for(char i : s){
            int x = get(i);
            if(trie[pos].link[x] == -1){
                node c = make_node();
                trie.push_back(c);
                trie[pos].link[x] = Size;
                pos = Size ++;
            }
            else{
                pos = trie[pos].link[x];
            }
        }
        trie[pos].is_word = true;
        trie[pos].who = id;
    }
    void output(){
        traverse(0, "");
    }
};

int n, m;
string s;
string w[me], c[me];
int dad[me], id[me];
vector<int> result;
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    cin >> n >> s;
    cin >> m;
    for(int i = 0; i < m; i ++)
        cin >> w[i];
    for(int i = 0; i < m; i ++){
        c[i] = w[i];
        transform(c[i].begin(), c[i].end(), c[i].begin(), ::tolower);
    }
    for(int i = 0; i < n; i ++)
        dad[i] = -1;
    Trie t = Trie();
    for(int i = 0; i < m; i ++)
        t.add(c[i], i);
    for(int i = 0; i < n; i ++){
        int pos = 0;
        for(int j = 1000, k = i ; j >= 0 && k >= 0; j --, k --){
            if(t.trie[pos].link[ s[k] - 'a' ] != -1){
                pos = t.trie[pos].link[ s[k] - 'a' ];
            }
            else break;
            if(t.trie[pos].is_word == true){
                if(k == 0 || dad[k - 1] != -1){
                    dad[i] = k;
                    id[i] = t.trie[pos].who;
                }
            }
        }
    }
    for(int i = n - 1; i >= 0; i --){
        result.push_back(id[i]);
        i = dad[i];
    }
    reverse(result.begin(), result.end());
    for(int i : result)
        cout << w[i] << " ";
    cout << endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define Long long long
#define ld long double
#define pii pair<int, int>
#define pli pair<Long, int>

const int me = 100025;
const int mod = 0x3b9aca07;
const int mod2 = 1.e9 + 3491123LL;
const int mul = 211;
const int mul2 = 31;
int max_s = 20;

int n, m;
string s;
string words[me], low[me];
int par[me];
map<pair<int, int>, int> pos;
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    cin >> n;
    cin >> s;
    reverse(s.begin(), s.end());

    cin >> m;
    for(int i = 0 ; i < m ; i ++)
        cin >> words[i];
    int max_iterations = 0;
    for(int i = 0 ; i < m ; i ++)
        max_iterations = max(max_iterations, (int)words[i].size());
    for(int i = 0 ; i < m ; i ++){
        low[i] = words[i];
        transform(low[i].begin(), low[i].end(), low[i].begin(), ::tolower);
        reverse(low[i].begin(), low[i].end());
        Long h = 0, h2 = 0;
        for(char j : low[i])
            h = (1LL * h * mul + j) % mod,
            h2 = (1LL * h2 * mul2 + j) % mod2;
        pos[make_pair(h, h2)] = i;
    }
    for(int i = 0 ; i < n ; i ++){
        Long h = 0, h2 = 0;
        for(int j = 0 ; j < max_iterations && i - j >= 0; j ++){
            h = (1LL * h * mul + s[i - j]) % mod;
            h2 = (1LL * h2 * mul2 + s[i - j]) % mod2;
            if(pos.count(make_pair(h, h2))){
                par[i] = pos[make_pair(h, h2)];
                break;
            }
        }
    }
    vector<int> result;
    for(int i = n - 1 ; i >= 0 ; ){
        result.push_back(par[i]);
        i -= words[ par[i] ].size();
    }
    for(int i : result)
        cout << words[i] << " ";
    return 0;
}


30
ariksihsidlihcdnaehsetahgnisol
10
Kira
hates
is
he
losing
death
childish
L
and
Note#include <bits/stdc++.h>

using namespace std;

#define Long long long
#define ld long double
#define pii pair<int, int>
#define pli pair<Long, int>

const int me = 100025;
const int mod = 0x3b9aca07;
const int mod2 = 1.e9 + 3491123LL;
const int mul = 211;
const int mul2 = 31;
int max_s = 20;

int n, m;
string s;
string words[me], low[me];
int par[me];
map<pair<int, int>, int> pos;
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    cin >> n;
    cin >> s;
    reverse(s.begin(), s.end());

    cin >> m;
    for(int i = 0 ; i < m ; i ++)
        cin >> words[i];
    int max_iterations = 0;
    for(int i = 0 ; i < m ; i ++)
        max_iterations = max(max_iterations, (int)words[i].size());
    for(int i = 0 ; i < m ; i ++){
        low[i] = words[i];
        transform(low[i].begin(), low[i].end(), low[i].begin(), ::tolower);
        reverse(low[i].begin(), low[i].end());
        Long h = 0, h2 = 0;
        for(char j : low[i])
            h = (1LL * h * mul + j) % mod,
            h2 = (1LL * h2 * mul2 + j) % mod2;
        pos[make_pair(h, h2)] = i;
    }
    for(int i = 0 ; i < n ; i ++){
        Long h = 0, h2 = 0;
        for(int j = 0 ; j < max_iterations && i - j >= 0; j ++){
            h = (1LL * h * mul + s[i - j]) % mod;
            h2 = (1LL * h2 * mul2 + s[i - j]) % mod2;
            if(pos.count(make_pair(h, h2))){
                par[i] = pos[make_pair(h, h2)];
                break;
            }
        }
    }
    vector<int> result;
    for(int i = n - 1 ; i >= 0 ; ){
        result.push_back(par[i]);
        i -= words[ par[i] ].size();
    }
    for(int i : result)
        cout << words[i] << " ";
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define Long long long
#define ld long double
#define pii pair<int, int>
#define pli pair<Long, int>

const int me = 100025;
const int mod = 0x3b9aca07;
const int mod2 = 961748941LL;;
const int mul = 203;
const int mul2 = 257;
int max_s = 20;

int n, m;
string s;
string words[me], low[me];
int par[me];
map<pair<int, int>, int> pos;
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    cin >> n;
    cin >> s;
    reverse(s.begin(), s.end());

    cin >> m;
    for(int i = 0 ; i < m ; i ++)
        cin >> words[i];
    int max_iterations = 0;
    for(int i = 0 ; i < m ; i ++)
        max_iterations = max(max_iterations, (int)words[i].size());
    for(int i = 0 ; i < m ; i ++){
        low[i] = words[i];
        transform(low[i].begin(), low[i].end(), low[i].begin(), ::tolower);
        reverse(low[i].begin(), low[i].end());
        Long h = 0, h2 = 0;
        for(char j : low[i])
            h = (1LL * h * mul + j) % mod,
            h2 = (1LL * h2 * mul2 + j) % mod2;
        pos[make_pair(h, h2)] = i;
    }
    for(int i = 0 ; i < n ; i ++){
        Long h = 0, h2 = 0;
        for(int j = 0 ; j <= max_iterations && i - j >= 0; j ++){
            h = (1LL * h * mul + s[i - j]) % mod;
            h2 = (1LL * h2 * mul2 + s[i - j]) % mod2;
            if(pos.count(make_pair(h, h2))){
                par[i] = pos[make_pair(h, h2)];
                break;
            }
        }
    }
    vector<int> result;
    for(int i = n - 1 ; i >= 0 ; ){
        result.push_back(par[i]);
        i -= words[ par[i] ].size();
    }
    for(int i : result)
        cout << words[i] << " ";
    if(words[ result.back() ].back() != s[0])
        cout << s[0] << " ";
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define Long long long
#define ld long double
#define pii pair<int, int>
#define pli pair<Long, int>

const int me = 100025;
const int mod = 0x3b9aca09;
int max_s = 20;

int n, m;
string s;
string words[me], low[me];
int par[me];
unordered_map<string, int> pos;
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    cin >> n;
    cin >> s;
    reverse(s.begin(), s.end());

    cin >> m;
    for(int i = 0 ; i < m ; i ++)
        cin >> words[i];
    int max_iterations = 0;
    for(int i = 0 ; i < m ; i ++)
        max_iterations = max(max_iterations, (int)words[i].size());
    for(int i = 0 ; i < m ; i ++){
        low[i] = words[i];
        transform(low[i].begin(), low[i].end(), low[i].begin(), ::tolower);
        pos[ low[i] ] = i;
    }
    for(int i = 0 ; i < n ; i ++){
        bool match = false;
        string go = "";
        for(int j = 0 ; j < max_iterations && i + j < n; j ++){
            go.push_back(s[j + i]);
            if(pos.count(go)){
                par[j + i] = pos[go];
                match = true;
            }
        }
    }
    vector<int> result;
    for(int i = n - 1 ; i >= 0 ; i ){
        result.push_back(par[i]);
        i -= words[ par[i] ].size();
    }
    for(int i : result)
        cout << words[i] << " ";
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define Long long long
#define ld long double
#define pii pair<int, int>
#define pli pair<Long, int>

const int me = 100025;
const int mod = 1.e9 + 7;

class Trie{
private:
    const static int ALPHABET = 26;
    const static int N = 123456;
    struct node{
        int link[ALPHABET];
        bool is_word;
        int who;
        node(){
            for(int i = 0; i < ALPHABET; i ++)
                link[i] = -1;
            is_word = false;
            who = -1;
        }
    };
    node make_node(){
        node c = node();
        return c;
    }
    int get(char x){
        return x - 'a';
    }
    void traverse(int pos, string s){
        if(trie[pos].is_word){
            cout << "word: " << s << endl;
        }
        for(int i = 0; i < ALPHABET; i ++)
            if(trie[pos].link[i] != -1)
                traverse(trie[pos].link[i], s + (char)(i + 'a'));
    }

public:
    vector<node> trie;
    int Size;
    Trie(){
        trie.push_back(make_node());
        Size = 1;
    }
    ~Trie() {
    };
    int size(){
        return Size;
    }
    void add(string s, int id){
        int pos = 0;
        for(char i : s){
            int x = get(i);
            if(trie[pos].link[x] == -1){
                node c = make_node();
                trie.push_back(c);
                trie[pos].link[x] = Size;
                pos = Size ++;
            }
            else{
                pos = trie[pos].link[x];
            }
        }
        trie[pos].is_word = true;
        trie[pos].who = id;
    }
    void output(){
        traverse(0, "");
    }
};

int n, m;
string s;
string w[me], c[me];
int dad[me], id[me];
vector<int> result;
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    cin >> n >> s;
    cin >> m;
    for(int i = 0; i < m; i ++)
        cin >> w[i];
    for(int i = 0; i < m; i ++){
        c[i] = w[i];
        transform(c[i].begin(), c[i].end(), c[i].begin(), ::tolower);
    }
    for(int i = 0; i < n; i ++)
        dad[i] = -1;
    Trie t = Trie();
    for(int i = 0; i < m; i ++)
        t.add(c[i], i);
    for(int i = 0; i < n; i ++){
        int pos = 0;
        for(int j = 1000, k = i ; j >= 0 && k >= 0; j --, k --){
            if(t.trie[pos].link[ s[k] - 'a' ] != -1){
                pos = t.trie[pos].link[ s[k] - 'a' ];
            }
            else break;
            if(t.trie[pos].is_word == true){
                if(k == 0 || dad[k - 1] != -1){
                    dad[i] = k;
                    id[i] = t.trie[pos].who;
                }
            }
        }
    }
    for(int i = n - 1; i >= 0; i --){
        result.push_back(id[i]);
        i = dad[i];
    }
    reverse(result.begin(), result.end());
    for(int i : result)
        cout << w[i] << " ";
    cout << endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define Long long long
#define ld long double
#define pii pair<int, int>
#define pli pair<Long, int>

const int me = 100025;
const int mod = 0x3b9aca07;
const int mod2 = 961748941LL;;
const int mul = 203;
const int mul2 = 257;
int max_s = 20;

int n, m;
string s;
string words[me], low[me];
int par[me];
map<pair<int, int>, int> pos;
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    cin >> n;
    cin >> s;
    reverse(s.begin(), s.end());

    cin >> m;
    for(int i = 0 ; i < m ; i ++)
        cin >> words[i];
    int max_iterations = 0;
    for(int i = 0 ; i < m ; i ++)
        max_iterations = max(max_iterations, (int)words[i].size());
    for(int i = 0 ; i < m ; i ++){
        low[i] = words[i];
        transform(low[i].begin(), low[i].end(), low[i].begin(), ::tolower);
        reverse(low[i].begin(), low[i].end());
        Long h = 0, h2 = 0;
        for(char j : low[i])
            h = (1LL * h * mul + j) % mod,
            h2 = (1LL * h2 * mul2 + j) % mod2;
        pos[make_pair(h, h2)] = i;
    }
    for(int i = 0 ; i < n ; i ++){
        Long h = 0, h2 = 0;
        for(int j = 0 ; j <= max_iterations && i - j >= 0; j ++){
            h = (1LL * h * mul + s[i - j]) % mod;
            h2 = (1LL * h2 * mul2 + s[i - j]) % mod2;
            if(pos.count(make_pair(h, h2))){
                par[i] = pos[make_pair(h, h2)];
                break;
            }
        }
    }
    vector<int> result;
    for(int i = n - 1 ; i >= 0 ; ){
        result.push_back(par[i]);
        i -= words[ par[i] ].size();
    }
    if(words[ result.back() ].back() != s[0])
        result.push_back(s[0]);
    for(int i : result)
        cout << words[i] << " ";
    return 0;
}


#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <iomanip>

using namespace std;

int n, m;
string s;
vector <string> vec;
map < string, string > mp;
int dp[10010];
int numb = 1;

struct mystruct
{
	bool flag;
	int edge[26];
};

mystruct node[2500000];

void update(int v, int i, string str)
{
	while (i != str.size())
	{
		char c = str[i];
		if (node[v].edge[c] != 0)
		{
			v = node[v].edge[c];
		}
		else
		{
			node[v].edge[c] = numb;
			numb++;
			v = numb - 1;
		}
		i++;
	}
	node[v].flag = 1;
	return;
}

int main()
{
	cin >> n;
	cin >> s;
	cin >> m;
	for (int i = 0; i < m; i++)
	{
		string str, str2;
		cin >> str;
		str2 = str;
		for (int j = 0; j < str2.size(); j++)
		{
			if (str2[j] >= 'A'&&str2[j] <= 'Z')
				str2[j] -= ('A' - 'a');
		}
		mp[str2] = str;
		update(0, 0, str2);
	}
	for (int i = 0; i < n; i++)
	{
		int v = 0;
		for (int j = i; j >= 0 && dp[i] == 0 && node[v].edge[s[j]] != 0; j--)
		{
			v = node[v].edge[s[j]];
			if (node[v].flag == 1 && (j == 0 || dp[j - 1] != 0))
			{
				dp[i] = j - 1;
			}
		}
	}
	int g = n - 1;
	while (g != -1)
	{
		string x;
		for (int i = g; i > dp[g]; i--)
		{
			x += s[i];
		}
		vec.push_back(mp[x]);
		g = dp[g];
	}
	for (int i = vec.size() - 1; i >= 0; i--)
	{
		cout << vec[i] << ' ';
	}
}


#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <iomanip>

using namespace std;

int n, m;
string s;
vector <string> vec;
map < string, string > mp;
int dp[10010];
int numb = 1;

struct mystruct
{
	bool flag;
	int edge[26];
};

mystruct node[5000000];

void update(int v, int i, string str)
{
	while (i != str.size())
	{
		char c = str[i];
		if (node[v].edge[c] != 0)
		{
			v = node[v].edge[c];
		}
		else
		{
			node[v].edge[c] = numb;
			numb++;
			v = numb - 1;
		}
		i++;
	}
	node[v].flag = 1;
	return;
}

int main()
{
	cin >> n;
	cin >> s;
	cin >> m;
	for (int i = 0; i < m; i++)
	{
		string str, str2;
		cin >> str;
		str2 = str;
		for (int j = 0; j < str2.size(); j++)
		{
			if (str2[j] >= 'A'&&str2[j] <= 'Z')
				str2[j] -= ('A' - 'a');
		}
		mp[str2] = str;
		update(0, 0, str2);
	}
	for (int i = 0; i < n; i++)
	{
		int v = 0;
		for (int j = i; j >= 0 && dp[i] == 0 && node[v].edge[s[j]] != 0; j--)
		{
			v = node[v].edge[s[j]];
			if (node[v].flag == 1 && (j == 0 || dp[j - 1] != 0))
			{
				dp[i] = j - 1;
			}
		}
	}
	int g = n - 1;
	while (g != -1)
	{
		string x;
		for (int i = g; i > dp[g]; i--)
		{
			x += s[i];
		}
		vec.push_back(mp[x]);
		g = dp[g];
	}
	for (int i = vec.size() - 1; i >= 0; i--)
	{
		cout << vec[i] << ' ';
	}
}


#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <iomanip>

using namespace std;

int n, m;
string s;
vector <string> vec;
map < string, string > mp;
int dp[10010];
int numb = 1;

struct mystruct
{
	bool flag;
	int edge[26];
};

mystruct node[2000000];

void update(int v, int i, string str)
{
	while (i != str.size())
	{
		char c = str[i];
		if (node[v].edge[c] != 0)
		{
			v = node[v].edge[c];
		}
		else
		{
			node[v].edge[c] = numb;
			numb++;
			v = numb - 1;
		}
		i++;
	}
	node[v].flag = 1;
	return;
}

int main()
{
	cin >> n;
	cin >> s;
	cin >> m;
	for (int i = 0; i < m; i++)
	{
		string str, str2;
		cin >> str;
		str2 = str;
		for (int j = 0; j < str2.size(); j++)
		{
			if (str2[j] >= 'A'&&str2[j] <= 'Z')
				str2[j] -= ('A' - 'a');
		}
		mp[str2] = str;
		update(0, 0, str2);
	}
	for (int i = 0; i < n; i++)
	{
		int v = 0;
		for (int j = i; j >= 0 && dp[i] == 0 && node[v].edge[s[j]] != 0; j--)
		{
			v = node[v].edge[s[j]];
			if (node[v].flag == 1 && (j == 0 || dp[j - 1] != 0))
			{
				dp[i] = j - 1;
			}
		}
	}
	int g = n - 1;
	while (g != -1)
	{
		string x;
		for (int i = g; i > dp[g]; i--)
		{
			x += s[i];
		}
		vec.push_back(mp[x]);
		g = dp[g];
	}
	for (int i = vec.size() - 1; i >= 0; i--)
	{
		cout << vec[i] << ' ';
	}
}


#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <iomanip>

using namespace std;

int n,m;
string s;
map <string, string> mp;
vector <string> vec;
int dp[20000];

int main()
{
	cin >> n;
	cin >> s;
	cin >> m;
	for (int i = 0; i < m; i++)
	{
		string str, str2;
		cin >> str;
		str2 = str;
		for (int j = 0; j < str2.size(); j++)
		{
			if (str2[j] >= 'A'&&str2[j] <= 'Z')
				str2[j] -= ('A' - 'a');
		}
		mp[str2] = str;
	}
	for (int i = 0; i < n; i++)
	{
		string x;
		for (int j = i; j >= 0 && dp[i] == 0; j--)
		{
			x += s[j];
			if (mp[x] != "" && (j == 0 || dp[j - 1] !=0))
				dp[i] = j - 1;
			if (mp[x] == "")
				mp[x].erase();
		}
	}
	int g = n-1;
	while (g!=-1)
	{
		string x;
		for (int i = g; i > dp[g]; i--)
		{
			x += s[i];
		}
		vec.push_back(mp[x]);
		g = dp[g];
	}
	for (int i = vec.size() - 1; i >= 0; i--)
	{
		cout << vec[i] << ' ';
	}
}


#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <iomanip>

using namespace std;

int n,m;
string s;
vector <string> vec;
map < string, string > mp;
int dp[20000];
int numb=1;

struct mystruct
{
	bool flag;
	int edge[26];
}node[20000];

void update(int v, int i, string str)
{
	if (i == str.size())
	{
		node[v].flag = 1;
		return;
	}
	char c = str[i];
	if (node[v].edge[c] != 0)
	{
		update(node[v].edge[c], i + 1, str);
	}
	else
	{
		node[v].edge[c] = numb;
		numb++;
		update(numb - 1, i + 1, str);
	}
}

int main()
{
	cin >> n;
	cin >> s;
	cin >> m;
	for (int i = 0; i < m; i++)
	{
		string str, str2;
		cin >> str;
		str2 = str;
		for (int j = 0; j < str2.size(); j++)
		{
			if (str2[j] >= 'A'&&str2[j] <= 'Z')
				str2[j] -= ('A' - 'a');
		}
		mp[str2] = str;
		update(0, 0, str2);
	}
	for (int i = 0; i < n; i++)
	{
		int v = 0;
		for (int j = i; j >= 0 && dp[i] == 0&&node[v].edge[s[j]]!=0; j--)
		{
			v = node[v].edge[s[j]];
			if (node[v].flag == 1&&dp[j-1]!=0)
			{
				dp[i] = j-1;
			}
		}
	}
	int g = n-1;
	while (g!=-1)
	{
		string x;
		for (int i = g; i > dp[g]; i--)
		{
			x += s[i];
		}
		vec.push_back(mp[x]);
		g = dp[g];
	}
	for (int i = vec.size() - 1; i >= 0; i--)
	{
		cout << vec[i] << ' ';
	}
}


#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <iomanip>

using namespace std;

int n, m;
string s;
vector <string> vec;
map < string, string > mp;
int dp[10010];
int numb = 1;

struct mystruct
{
	bool flag;
	int edge[26];
};

mystruct node[100000];

void update(int v, int i, string str)
{
	while (i != str.size())
	{
		char c = str[i];
		if (node[v].edge[c] != 0)
		{
			v = node[v].edge[c];
		}
		else
		{
			node[v].edge[c] = numb;
			numb++;
			v = numb - 1;
		}
		i++;
	}
	node[v].flag = 1;
	return;
}

int main()
{
	cin >> n;
	cin >> s;
	cin >> m;
	for (int i = 0; i < 10010; i++)
		dp[i] = -123;
	for (int i = 0; i < m; i++)
	{
		string str, str2;
		cin >> str;
		str2 = str;
		for (int j = 0; j < str2.size(); j++)
		{
			if (str2[j] >= 'A'&&str2[j] <= 'Z')
				str2[j] -= ('A' - 'a');
		}
		mp[str2] = str;
		update(0, 0, str2);
	}
	for (int i = 0; i < n; i++)
	{
		int v = 0;
		for (int j = i; j >= 0 && dp[i] == -123 && node[v].edge[s[j]] != 0; j--)
		{
			v = node[v].edge[s[j]];
			if (node[v].flag == 1 && (j == 0 || dp[j - 1] != -123))
			{
				dp[i] = j - 1;
			}
		}
	}
	int g = n - 1;
	while (g != -1)
	{
		string x;
		for (int i = g; i > dp[g]; i--)
		{
			x += s[i];
		}
		vec.push_back(mp[x]);
		g = dp[g];
	}
	for (int i = vec.size() - 1; i >= 0; i--)
	{
		cout << vec[i] << ' ';
	}
}


#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <iomanip>

using namespace std;

int n, m;
string s;
vector <string> vec;
map < string, string > mp;
int dp[10010];
int numb = 1;

struct mystruct
{
	bool flag;
	int edge[26];
};

mystruct node[20000];

void update(int v, int i, string str)
{
	while (i != str.size())
	{
		char c = str[i];
		if (node[v].edge[c] != 0)
		{
			v = node[v].edge[c];
		}
		else
		{
			node[v].edge[c] = numb;
			numb++;
			v = numb - 1;
		}
		i++;
	}
	node[v].flag = 1;
	return;
}

int main()
{
	cin >> n;
	cin >> s;
	cin >> m;
	for (int i = 0; i < m; i++)
	{
		string str, str2;
		cin >> str;
		str2 = str;
		for (int j = 0; j < str2.size(); j++)
		{
			if (str2[j] >= 'A'&&str2[j] <= 'Z')
				str2[j] -= ('A' - 'a');
		}
		mp[str2] = str;
		update(0, 0, str2);
	}
	for (int i = 0; i < n; i++)
	{
		int v = 0;
		for (int j = i; j >= 0 && dp[i] == 0 && node[v].edge[s[j]] != 0; j--)
		{
			v = node[v].edge[s[j]];
			if (node[v].flag == 1 && (j == 0 || dp[j - 1] != 0))
			{
				dp[i] = j - 1;
			}
		}
	}
	int g = n - 1;
	while (g != -1)
	{
		string x;
		for (int i = g; i > dp[g]; i--)
		{
			x += s[i];
		}
		vec.push_back(mp[x]);
		g = dp[g];
	}
	for (int i = vec.size() - 1; i >= 0; i--)
	{
		cout << vec[i] << ' ';
	}
}


#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <iomanip>

using namespace std;

int n, m;
string s;
vector <string> vec;
map < string, string > mp;
int dp[10010];
int numb = 1;

struct mystruct
{
	bool flag;
	int edge[30];
};

mystruct node[2000000];

void update(int v, int i, string str)
{
	while (i != str.size())
	{
		char c = str[i]-'a';
		if (node[v].edge[c] != 0)
		{
			v = node[v].edge[c];
		}
		else
		{
			node[v].edge[c] = numb;
			numb++;
			v = numb - 1;
		}
		i++;
	}
	node[v].flag = 1;
	return;
}

int main()
{
	cin >> n;
	cin >> s;
	cin >> m;
	for (int i = 0; i < 10010; i++)
		dp[i] = -123;
	for (int i = 0; i < m; i++)
	{
		string str, str2;
		cin >> str;
		str2 = str;
		for (int j = 0; j < str2.size(); j++)
		{
			if (str2[j] >= 'A'&&str2[j] <= 'Z')
				str2[j] -= ('A' - 'a');
		}
		mp[str2] = str;
		update(0, 0, str2);
	}
	for (int i = 0; i < n; i++)
	{
		int v = 0;
		for (int j = i; j >= 0 && dp[i] == -123 && node[v].edge[s[j]-'a'] != 0; j--)
		{
			v = node[v].edge[s[j]-'a'];
			if (node[v].flag == 1 && (j == 0 || dp[j - 1] != -123))
			{
				dp[i] = j - 1;
			}
		}
	}
	int g = n - 1;
	while (g != -1)
	{
		string x;
		for (int i = g; i > dp[g]; i--)
		{
			x += s[i];
		}
		vec.push_back(mp[x]);
		g = dp[g];
	}
	for (int i = vec.size() - 1; i >= 0; i--)
	{
		cout << vec[i] << ' ';
	}
}


#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <iomanip>

using namespace std;

int n, m;
string s;
vector <string> vec;
map < string, string > mp;
int dp[10010];
int numb = 1;

struct mystruct
{
	bool flag;
	int edge[26];
};

mystruct node[100000];

void update(int v, int i, string str)
{
	while (i != str.size())
	{
		char c = str[i];
		if (node[v].edge[c] != 0)
		{
			v = node[v].edge[c];
		}
		else
		{
			node[v].edge[c] = numb;
			numb++;
			v = numb - 1;
		}
		i++;
	}
	node[v].flag = 1;
	return;
}

int main()
{
	cin >> n;
	cin >> s;
	cin >> m;
	for (int i = 0; i < m; i++)
	{
		string str, str2;
		cin >> str;
		str2 = str;
		for (int j = 0; j < str2.size(); j++)
		{
			if (str2[j] >= 'A'&&str2[j] <= 'Z')
				str2[j] -= ('A' - 'a');
		}
		mp[str2] = str;
		update(0, 0, str2);
	}
	for (int i = 0; i < n; i++)
	{
		int v = 0;
		for (int j = i; j >= 0 && dp[i] == 0 && node[v].edge[s[j]] != 0; j--)
		{
			v = node[v].edge[s[j]];
			if (node[v].flag == 1 && (j == 0 || dp[j - 1] != 0))
			{
				dp[i] = j - 1;
			}
		}
	}
	int g = n - 1;
	while (g != -1)
	{
		string x;
		for (int i = g; i > dp[g]; i--)
		{
			x += s[i];
		}
		vec.push_back(mp[x]);
		g = dp[g];
	}
	for (int i = vec.size() - 1; i >= 0; i--)
	{
		cout << vec[i] << ' ';
	}
}


#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <iomanip>

using namespace std;

int n,m;
string s;
map <string, string> mp;
vector <string> vec;
int dp[20000];

int main()
{
	cin >> n;
	cin >> s;
	cin >> m;
	for (int i = 0; i < m; i++)
	{
		string str, str2;
		cin >> str;
		str2 = str;
		for (int j = 0; j < str2.size(); j++)
		{
			if (str2[j] >= 'A'&&str2[j] <= 'Z')
				str2[j] -= ('A' - 'a');
		}
		mp[str2] = str;
	}
	for (int i = 0; i < n; i++)
	{
		string x;
		for (int j = i; j >= 0 && dp[i] == 0; j--)
		{
			x += s[j];
			if (mp[x] != "" && (j == 0 || dp[j - 1] !=0))
				dp[i] = j - 1;
			if (mp[x] == "")
				mp[x].erase;
		}
	}
	int g = n-1;
	while (g!=-1)
	{
		string x;
		for (int i = g; i > dp[g]; i--)
		{
			x += s[i];
		}
		vec.push_back(mp[x]);
		g = dp[g];
	}
	for (int i = vec.size() - 1; i >= 0; i--)
	{
		cout << vec[i] << ' ';
	}
}


#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <iomanip>

using namespace std;

int n, m;
string s;
vector <string> vec;
map < string, string > mp;
int dp[10010];
int numb = 1;

struct mystruct
{
	bool flag;
	int edge[26];
};

mystruct node[1000000];

void update(int v, int i, string str)
{
	while (i != str.size())
	{
		char c = str[i];
		if (node[v].edge[c] != 0)
		{
			v = node[v].edge[c];
		}
		else
		{
			node[v].edge[c] = numb;
			numb++;
			v = numb - 1;
		}
		i++;
	}
	node[v].flag = 1;
	return;
}

int main()
{
	cin >> n;
	cin >> s;
	cin >> m;
	for (int i = 0; i < m; i++)
	{
		string str, str2;
		cin >> str;
		str2 = str;
		for (int j = 0; j < str2.size(); j++)
		{
			if (str2[j] >= 'A'&&str2[j] <= 'Z')
				str2[j] -= ('A' - 'a');
		}
		mp[str2] = str;
		update(0, 0, str2);
	}
	for (int i = 0; i < n; i++)
	{
		int v = 0;
		for (int j = i; j >= 0 && dp[i] == 0 && node[v].edge[s[j]] != 0; j--)
		{
			v = node[v].edge[s[j]];
			if (node[v].flag == 1 && (j==0||dp[j - 1] != 0))
			{
				dp[i] = j - 1;
			}
		}
	}
	int g = n - 1;
	while (g != -1)
	{
		string x;
		for (int i = g; i > dp[g]; i--)
		{
			x += s[i];
		}
		vec.push_back(mp[x]);
		g = dp[g];
	}
	for (int i = vec.size() - 1; i >= 0; i--)
	{
		cout << vec[i] << ' ';
	}
}


#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <iomanip>

using namespace std;

int n, m;
string s;
vector <string> vec;
map < string, string > mp;
int dp[10010];
int numb = 1;

struct mystruct
{
	bool flag;
	int edge[26];
};

mystruct node[1000000];

void update(int v, int i, string str)
{
	while (i != str.size())
	{
		char c = str[i];
		if (node[v].edge[c] != 0)
		{
			v = node[v].edge[c];
		}
		else
		{
			node[v].edge[c] = numb;
			numb++;
			v = numb - 1;
		}
		i++;
	}
	node[v].flag = 1;
	return;
}

int main()
{
	cin >> n;
	cin >> s;
	cin >> m;
	for (int i = 0; i < 10010; i++)
		dp[i] = -123;
	for (int i = 0; i < m; i++)
	{
		string str, str2;
		cin >> str;
		str2 = str;
		for (int j = 0; j < str2.size(); j++)
		{
			if (str2[j] >= 'A'&&str2[j] <= 'Z')
				str2[j] -= ('A' - 'a');
		}
		mp[str2] = str;
		update(0, 0, str2);
	}
	for (int i = 0; i < n; i++)
	{
		int v = 0;
		for (int j = i; j >= 0 && dp[i] == -123 && node[v].edge[s[j]] != 0; j--)
		{
			v = node[v].edge[s[j]];
			if (node[v].flag == 1 && (j == 0 || dp[j - 1] != 0))
			{
				dp[i] = j - 1;
			}
		}
	}
	int g = n - 1;
	while (g != -1)
	{
		string x;
		for (int i = g; i > dp[g]; i--)
		{
			x += s[i];
		}
		vec.push_back(mp[x]);
		g = dp[g];
	}
	for (int i = vec.size() - 1; i >= 0; i--)
	{
		cout << vec[i] << ' ';
	}
}


#include <iostream>
#include <unordered_map>
#include <stack>
using namespace std;

stack<string> st;
unordered_map<string, string> h;

struct Trie
{
    int words;
    Trie *edges[26];
    Trie()
    {
        words = 0;
        for (int i = 0; i < 26; i++)
            edges[i] = NULL;
    }
} root;

void addWord(int index, string &w, Trie *t)
{
    if (t->edges[w[index] - 'a'] == NULL)
        t->edges[w[index] - 'a'] = new Trie();

    t = t->edges[w[index] - 'a'];
    if (index == w.size() - 1)
        t->words++;
    else
        addWord(index + 1, w, t);
}

void dfs(int index, string &current, string &s, Trie *t)
{
    if (index == -1)
    {
        if (current == "")
        {
            while (!st.empty())
            {
                cout << h[st.top()] << ' ';
                st.pop();
            }
            exit(0);
        }
        return;
    }

    if (t->edges[s[index] - 'a'] == NULL)
        return;
    t = t->edges[s[index] - 'a'];
    current += s[index];

    if (t->words)
    {
        st.push(current);
        current = "";
        dfs(index - 1, current, s, &root);
        current = st.top();
        st.pop();
        dfs(index - 1, current, s, t);
    }
    else
    {
        dfs(index - 1, current, s, t);
        current.pop_back();
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    int n, m, i, j;
    cin >> n;
    string s, text, l, current;
    cin >> text >> m;
    for (i = 0; i < m; i++)
    {
        cin >> s;
        l = "";
        for (j = 0; j < s.size(); j++)
            l += tolower(s[j]);
        h[l] = s;
        addWord(0, l, &root);
    }

    dfs(text.size() - 1, current, text, &root);
    return 0;
}


#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;
/////////
vector<string> sol;

struct Trie
{
    string word;
    Trie *edges[26];
    Trie()
    {
        word = "";
        for (int i = 0; i < 26; i++)
            edges[i] = NULL;
    }
} *root;

void addWord(string &w, Trie *t)
{
    char letter;
    for (int i = 0; i < w.size(); i++)
    {
        letter = tolower(w[i]);
        if (t->edges[letter - 'a'] == NULL)
            t->edges[letter - 'a'] = new Trie();
        t = t->edges[letter - 'a'];
    }

    if (t->word.empty())
        t->word = w;
}

void dfs(int index, string &s, Trie *t)
{
    if (index == -1)
    {
        if (t == root)
        {
            for (int i = sol.size() - 1; i >= 0; i--)
                cout << sol[i] << ' ';
            exit(0);
        }
        return;
    }

    if (t->edges[s[index] - 'a'] == NULL)
        return;
    t = t->edges[s[index] - 'a'];

    if (!t->word.empty())
    {
        sol.push_back(t->word);
        dfs(index - 1, s, root);
        sol.pop_back();
    }

    dfs(index - 1, s, t);
}

int main()
{
    ios_base::sync_with_stdio(false);
    int n, m, i;
    cin >> n;
    string s, text, l, current;
    cin >> text >> m;

    root = new Trie();
    for (i = 0; i < m; i++)
    {
        cin >> s;
        addWord(s, root);
    }

    dfs(text.size() - 1, text, root);
    return 0;
}


#include <iostream>
#include <unordered_map>
#include <stack>

using namespace std;
/////////
stack<string> st;

struct Trie
{
    string word;
    Trie *edges[26];
    Trie()
    {
        word = "";
        for (int i = 0; i < 26; i++)
            edges[i] = NULL;
    }
} *root;

void addWord(string &w, Trie *t)
{
    char letter;
    for (int i = 0; i < w.size(); i++)
    {
        letter = tolower(w[i]);
        if (t->edges[letter - 'a'] == NULL)
            t->edges[letter - 'a'] = new Trie();
        t = t->edges[letter - 'a'];
    }

    if (t->word.empty())
        t->word = w;
}

void dfs(int index, string &s, Trie *t)
{
    if (index == -1 && !t->word.empty())
    {
        cout << t->word << ' ';
        while (!st.empty())
            {
                cout << st.top() << ' ';
                st.pop();
            }
            exit(0);
    }
    else if (!t->word.empty())
    {
        st.push(t->word);
        dfs(index, s, root);
        st.pop();
    }

    if (t->edges[s[index] - 'a'] == NULL)
        return;
    t = t->edges[s[index] - 'a'];
    dfs(index - 1, s, t);
}

int main()
{
    int n, m, i, j;
    cin >> n;
    string s, text, l, current;
    cin >> text >> m;
    root = new Trie();
    for (i = 0; i < m; i++)
    {
        cin >> s;
        addWord(s, root);
    }

    dfs(text.size() - 1, text, root);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

const int N = 100;

int n;
char arr[N][30];
int chk = 0, cnt = 0;

map<char, bool> mp;

void fill() {
	for(int i = 0; i < n; ++i) {
		for(int j = 0; arr[i][j]; ++j) {
			if(!mp[arr[i][j]]) {
				mp[arr[i][j]] = true;
				chk++;
			}
		}
	}
}

void solve() {
	while(chk > 0) {
		for(int i = 0; i < n; ++i) {
			bool b = false;
			char ch;
			for(int j = 0; arr[i][j]; ++j) {
				if(mp[arr[i][j]]) {
					b = true;
					ch = arr[i][j];
					break;
				}
			}
			if(!b) {
				continue;
			}
			for(int j = 0; j < n; ++j) {
				if(i != j) {
					for(int k = 0; arr[j][k]; ++k) {
						if((k > cnt) && arr[j][k] == ch) {
							b = false;
						}
					}
				}
			}
			if(b) {
				printf("%c", ch);
				mp[ch] = false;
				chk--;
				cnt++;
				for(int j = 0; j < n; ++j) {
					for(int k = 0; arr[j][k]; ++k) {
						if(arr[j][k] == ch) {
							for(int p = k; arr[j][p]; ++p) {
								if(mp[arr[j][p]]) {
									ch = arr[j][p];
									printf("%c", ch);
									mp[arr[j][p]] = false;
									chk--;
									cnt++;
									j = 0;
									k = 0;
									break;
								}
							}
						}
					}
				}
				i = 0;
			}
		}
	}
}

int main (int argc, char *argv[])
{
	scanf("%d", &n);
	for(int i = 0; i < n; ++i) {
		scanf("%s", arr[i]);
	}
	fill();
	solve();

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

const int N = 100;

int n, cnt = 0;
string str[N];
string ans;

map<char, bool> mp;
queue<char> q;

void solve() {
	while(ans.length() < cnt) {
		for(int i = 0; i < n; ++i) {
			char ch;
			if(!mp[str[i][0]]) {
				ch = str[i][0];
			} else {
				continue;
			}
			for(int j = 0; j < n; ++j) {
				for(int k = 1; k < str[j].length(); ++k) {
					if(str[j][k] == ch) {
						ch = '1';
					}
				}
			}
			if(ch != '1') {
				q.push(ch);
				break;
			}
		}
		while(!q.empty()) {
			char v = q.front();
			q.pop();
			while(mp[v] && !q.empty()) {
				v = q.front();
				q.pop();
			}
			if(mp[v]) {
				break;
			}
			mp[v] = true;
			ans += v;
			for(int i = 0; i < n; ++i) {
				for(int k = 0; k < str[i].length(); ++k) {
					if(str[i][k] == v) {
						for(k++; k < str[i].length(); ++k) {
							if(!mp[str[i][k]]) {
								q.push(str[i][k]);
							}
						}
					}
				}
			}
		}
	}
}

int main (int argc, char *argv[])
{
	scanf("%d", &n);
	map<char, bool> mp;
	for(int i = 0; i < n; ++i) {
		char ch[N];
		scanf("%s", &ch);
		str[i] = string(ch);
		for(int j = 0; j < str[i].length(); ++j) {
			if(!mp[str[i][j]]) {
				cnt++;
				mp[str[i][j]] = true;
			}
		}
	}
	solve();
	cout << ans;
	return 0;
}


                                /******SHOW US THE BEST YOU CAN DO******/

#include<bits/stdc++.h>

using namespace std;

#define ll long long int

ll gcd(ll a, ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a, ll b){return a * b / gcd(a, b);}

vector<int>ad[26];
bool vis[26], has[26];

void dfs(int v){
    vis[v] = 1;
    putchar((char)(v + 'a'));
    for(auto u : ad[v])
        if(!vis[u])
            dfs(u);
}

int main(){
    //freopen("output.in","r",stdin);
    //freopen("output.trest","w",stdout);
    int n;
    scanf("%d", &n);
    for(int i = 0; i < n; i++){
        string s;
        cin >> s;
        has[s[0] - 'a'] = 1;
        for(int i = 0; i < (int)s.size() - 1; i++)
            ad[s[i] - 'a'].push_back(s[i + 1] - 'a');
    }
    for(int i = 0; i < 26; i++)
        if(!vis[i] && has[i])
            dfs(i);
    puts("");
    return 0;
}


                                /******SHOW US THE BEST YOU CAN DO******/

#include<bits/stdc++.h>

using namespace std;

#define ll long long int

ll gcd(ll a, ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a, ll b){return a * b / gcd(a, b);}

bool vis[26], has[26], g[26][26];

void dfs(int v){
    vis[v] = 1;
    has[v] = 0;
    putchar((char)(v + 'a'));
    for(int i = 0; i < 26 ; i++)
        if(g[v][i] && !vis[i])
            dfs(i);
}

int main(){
    //freopen("output.in","r",stdin);
    //freopen("output.trest","w",stdout);
    int n;
    scanf("%d", &n);
    for(int i = 0; i < n; i++){
        string s;
        cin >> s;
        has[s[0] - 'a'] = 1;
        for(int i = 0; i < (int)s.size() - 1; i++){
            g[s[i] - 'a'][s[i + 1] - 'a'] = 1;
            if(has[s[i + 1] - 'a'])has[s[i + 1] - 'a'] = 0;
        }
    }
    for(int i = 0; i < 26; i++)
        if(!vis[i] && has[i])
            dfs(i);
    puts("");
    return 0;
}


                                /******SHOW US THE BEST YOU CAN DO******/

#include<bits/stdc++.h>

using namespace std;

#define ll long long int

ll gcd(ll a, ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a, ll b){return a * b / gcd(a, b);}

vector<int>ad[26];
bool vis[26], has[26];

void dfs(int v){
    vis[v] = 1;
    putchar((char)(v + 'a'));
    for(auto u : ad[v])
        if(!vis[u])
            dfs(u);
}

int main(){
    //freopen("output.in","r",stdin);
    //freopen("output.trest","w",stdout);
    int n;
    scanf("%d", &n);
    for(int i = 0; i < n; i++){
        string s;
        cin >> s;
        if(s.size() == 1)has[s[0] - 'a'] = 1;
        for(int i = 0; i < (int)s.size() - 1; i++)
            ad[s[i] - 'a'].push_back(s[i + 1] - 'a'), has[s[i] - 'a'] = has[s[i + 1] - 'a'] = 1;
    }
    for(int i = 0; i < 26; i++)
        if(!vis[i] && has[i])
            dfs(i);
    puts("");
    return 0;
}


                                /******SHOW US THE BEST YOU CAN DO******/

#include<bits/stdc++.h>

using namespace std;

#define ll long long int

ll gcd(ll a, ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a, ll b){return a * b / gcd(a, b);}

vector<int>ad[26];
bool vis[26], has[26], g[26][26];

void dfs(int v){
    vis[v] = 1;
    has[v] = 0;
    putchar((char)(v + 'a'));
    for(auto u : ad[v])
        if(!vis[u])
            dfs(u);
}

int main(){
    //freopen("output.in","r",stdin);
    //freopen("output.trest","w",stdout);
    int n;
    scanf("%d", &n);
    for(int i = 0; i < n; i++){
        string s;
        cin >> s;
        has[s[0] - 'a'] = 1;
        for(int i = 0; i < (int)s.size() - 1; i++)
            if(s[i] != s[i + 1] && !g[s[i] - 'a'][s[i + 1] - 'a'])
                ad[s[i] - 'a'].push_back(s[i + 1] - 'a'), g[s[i] - 'a'][s[i + 1] - 'a'] = 1;
    }
    for(int i = 0; i < 26; i++)
        if(!vis[i] && has[i])
            dfs(i);
    puts("");
    return 0;
}


                                /******SHOW US THE BEST YOU CAN DO******/

#include<bits/stdc++.h>

using namespace std;

#define ll long long int

ll gcd(ll a, ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a, ll b){return a * b / gcd(a, b);}

bool vis[26], g[26][26], has[26];
int in[26];

void dfs(int v){
    vis[v] = 1;
    putchar((char)(v + 'a'));
    for(int i = 0; i < 26 ; i++)
        if(g[v][i] && !vis[i])
            dfs(i);
}

int main(){
    //freopen("output.in","r",stdin);
    //freopen("output.trest","w",stdout);
    int n;
    scanf("%d", &n);
    for(int i = 0; i < n; i++){
        string s;
        cin >> s;
        has[s[0] - 'a'] = 1;
        for(int i = 0; i < (int)s.size() - 1; i++){
            g[s[i] - 'a'][s[i + 1] - 'a'] = 1;
            in[s[i + 1] - 'a']++;
        }
    }
    for(int i = 0; i < 26; i++)
        if(!vis[i] && !in[i] && has[i])
            dfs(i);
    puts("");
    return 0;
}


                                /******SHOW US THE BEST YOU CAN DO******/

#include<bits/stdc++.h>

using namespace std;

#define ll long long int

ll gcd(ll a, ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a, ll b){return a * b / gcd(a, b);}

vector<int>ad[26];
bool vis[26], has[26], g[26][26];

void dfs(int v){
    vis[v] = 1;
    has[v] = 0;
    putchar((char)(v + 'a'));
    for(auto u : ad[v])
        if(!vis[u])
            dfs(u);
}

int main(){
    //freopen("output.in","r",stdin);
    //freopen("output.trest","w",stdout);
    int n;
    scanf("%d", &n);
    for(int i = 0; i < n; i++){
        string s;
        cin >> s;
        has[s[0] - 'a'] = 1;
        for(int i = 0; i < (int)s.size() - 1; i++)
            if(s[i] != s[i + 1] && !g[s[i] - '0'][s[i + 1] - '0'])
                ad[s[i] - 'a'].push_back(s[i + 1] - 'a'), g[s[i] - '0'][s[i + 1] - '0'] = 1;
    }
    for(int i = 0; i < 26; i++)
        if(!vis[i] && has[i])
            dfs(i);
    puts("");
    return 0;
}


                                /******SHOW US THE BEST YOU CAN DO******/

#include<bits/stdc++.h>

using namespace std;

#define ll long long int

ll gcd(ll a, ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a, ll b){return a * b / gcd(a, b);}

vector<int>ad[26];
bool vis[26], has[26];

void dfs(int v){
    vis[v] = 1;
    has[v] = 0;
    putchar((char)(v + 'a'));
    for(auto u : ad[v])
        if(!vis[u])
            dfs(u);
}

int main(){
    //freopen("output.in","r",stdin);
    //freopen("output.trest","w",stdout);
    int n;
    scanf("%d", &n);
    for(int i = 0; i < n; i++){
        string s;
        cin >> s;
        has[s[0] - 'a'] = 1;
        for(int i = 0; i < (int)s.size() - 1; i++)
            ad[s[i] - 'a'].push_back(s[i + 1] - 'a');
    }
    for(int i = 0; i < 26; i++)
        if(!vis[i] && has[i])
            dfs(i);
    puts("");
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define  vi vector<int>
#define pb push_back
//#define int long long
#define mod (ll)(1e9+7)
#define boost ios_base::sync_with_stdio(false);	cin.tie(0);	cout.tie(0)
using namespace std;

void solve()
{ vector<int>h(26);
  vector<int>isparent(26,1);
  map<char,char>pc;
  int n;
  cin>>n;
  for(int i=0;i<n;i++)
  {
    string s;
    cin>>s;
    h[s[0]-'a']++;

      for(int j=1;j<s.length();j++)
      {
        isparent[s[j]-'a']=0;
        h[s[j]-'a']++;
        pc[s[j-1]]=s[j];
      }
  }
  for(int i=0;i<26;i++)
  {
     if(isparent[i]==1&&h[i]!=0)
     {
       char ans=char(i+'a');
       cout<<ans;
       while(pc[ans]!=0)
       {
         cout<<pc[ans];
         ans=pc[ans];
       }

     }

  }

  cout<<endl;

}

signed main()
{ //boost;
 //cout<<fixed<<setprecision(14 );
 // int t;cin>>t;
  //while(t--)
  solve();

return 0;
}


#include<bits/stdc++.h>
#define F(i,a,b) for(int i =a;i<=(int)b; ++i)
using namespace std;
typedef long long LL;
const int MaxN = 3e5 + 5;

int vis[50];
vector<int> G[50];

void dfs(int x) {
	vis[x] = 2;
	cout << (char)('a' + x);
	for(int i = 0; i < G[x].size(); i++) {
		int u = G[x][i];
		if(vis[u] != 2) {
			dfs(u);
		}
	}
}

int main()
{
	int n; cin >> n;
	for(int i = 1; i <= n; i++) {
		string s; cin >> s;
		for(int j = 1; j < s.size(); j++) {
			vis[s[j]-'a'] = 1;
			G[s[j-1]-'a'].push_back(s[j] - 'a');
		}
		if(!vis[s[0]-'a']) vis[s[0]-'a'] = 6;
	}
	for(int i = 0; i < 26; i++) {
		if(vis[i] == 6) dfs(i);
	}
	cout << endl;
    return 0;
}


#include<bits/stdc++.h>
#define F(i,a,b) for(int i =a;i<=(int)b; ++i)
using namespace std;
typedef long long LL;
const int MaxN = 3e5 + 5;

int vis[50];
vector<int> G[50];

void dfs(int x) {
	vis[x] = 2;
	cout << (char)('a' + x);
    for(auto u:G[x]){
		if(vis[u] != 2) {
			dfs(u);
		}
	}
}

int main()
{
	int n; cin >> n;
	for(int i = 1; i <= n; i++) {
		string s; cin >> s;
		for(int j = 1; j < s.size(); j++) {
			vis[s[j]-'a'] = 1;
			G[s[j-1]-'a'].push_back(s[j] - 'a');
		}
		if(!vis[s[0]-'a']) vis[s[0]-'a'] = 6;
	}
	for(int i = 0; i < 26; i++) {
		if(vis[i] == 6) dfs(i);
	}
	cout << endl;
    return 0;
}


#include<bits/stdc++.h>
#define F(i,a,b) for(int i =a;i<=(int)b; ++i)
using namespace std;
vector<char> adj[110];
map<char,int> cnt;
void dfs(char node)
{
	for(char x:adj[node])
	{
		dfs(x);
		cnt[x]++;
		if(cnt[x]==1)
		cout<<x;
	}
}
int main()
{
	int n;
	string str;
	cin>>n;
	while(n--)
	{
		cin>>str;
		adj['A'].push_back(str[0]);
		if(str.length()!=1)
		{
			F(i,0,str.length()-2)
			{
				adj[str[i]].push_back(str[i+1]);
			}
		}

	}
	dfs('A');
}


#include <bits/stdc++.h>
#define F first
#define S second
#define ll long long
#define ld long double
#define PB push_back
#define B back()
#define P push
#define sz size()
#define L length()
#define INF 0x7f7f7f7f
#define inf 1e15
#define loop(i,s,e) for(ll i=s;i<=e;i++)
#define rloop(i,s,e) for(ll i=s;i>=e;i--)
#define MEM(a,x) memset(a,x,sizeof a)
#define bitcount __builtin_popcount
using namespace std;
const ll N=100*5;
ll n,vis[30];
pair<ll,ll>p[30];
main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
    cin>>n;
    loop(i,0,30) p[i].F=p[i].S=-1;
    loop(i,1,n)
    {
        string s; cin>>s;
        vis[s[0]-'a']=1;
        loop(i,1,s.L-1)
        {
            p[s[i]-'a'].F = s[i-1]-'a';
            p[s[i-1]-'a'].S = s[i]-'a';
            vis[s[i]-'a']=1;
        }
    }
    int f=1;
    loop(i,0,25)
    {
        //cout<<p[i].F<<" "<<p[i].S<<" "<<vis[i]<<endl;
        if(vis[i] && p[i].F == -1)
        {
            int ii = i;
            while(vis[ii])
            {
                vis[ii]=0;
                char c= ii+'a';
                cout<<c;
                ii = p[ii].S;
                if(ii==-1) break;
            }
        }
    }
}


#include <bits/stdc++.h>
#define F first
#define S second
#define ll long long
#define ld long double
#define PB push_back
#define B back()
#define P push
#define sz size()
#define L length()
#define INF 0x7f7f7f7f
#define inf 1e15
#define loop(i,s,e) for(ll i=s;i<=e;i++)
#define rloop(i,s,e) for(ll i=s;i>=e;i--)
#define MEM(a,x) memset(a,x,sizeof a)
#define bitcount __builtin_popcount
using namespace std;
const ll N=100*5;
vector<ll>adj[N];
ll n,vis[30];
string ans;
void dfs(ll u)
{
    vis[u]=2;
    printf("%c",u+'a');
    for(auto o:adj[u])
        if(vis[o]!=2) dfs(o);
}
main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
    cin>>n;
    loop(i,1,n)
    {
        string s; cin>>s;
        //vis[s[0]-'a']=1;
        loop(i,1,s.L-1)
        {
            vis[s[i]-'a']=1;
            adj[s[i-1]-'a'].PB(s[i]-'a');
        }
        if(!vis[s[0]-'a']) vis[s[0]-'a']=3;//root
    }
    loop(i,0,25)
    {
        //cout<<vis[i]<<" ";
        if(vis[i]==3) dfs(i);
    }
}


#include <bits/stdc++.h>
#define F first
#define S second
#define ll long long
#define ld long double
#define PB push_back
#define B back()
#define P push
#define sz size()
#define L length()
#define INF 0x7f7f7f7f
#define inf 1e15
#define loop(i,s,e) for(ll i=s;i<=e;i++)
#define rloop(i,s,e) for(ll i=s;i>=e;i--)
#define MEM(a,x) memset(a,x,sizeof a)
#define bitcount __builtin_popcount
using namespace std;
const ll N=100*5;
ll n,vis[30];
pair<ll,ll>p[30];
main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
    cin>>n;
    loop(i,0,30) p[i].F=p[i].S=-1;
    loop(i,1,n)
    {
        string s; cin>>s;
        if(s.L==1) vis[s[0]-'a']=1;
        else
        {
        p[s[0]-'a'].S = s[1]-'a';
        vis[s[0]-'a']=1;
        loop(i,1,s.L-2)
        {
            p[s[i]-'a'].F = s[i-1]-'a';
            p[s[i]-'a'].S = s[i+1]-'a';
            vis[s[i]-'a']=1;
        }
        p[s[s.L-1]-'a'].F = s[s.L-2]-'a';
        vis[s[s.L-1]-'a'] = 1;
        }
    }
    //loop(i,0,26)
    int f=1;
    loop(i,0,26)
    {
        //cout<<p[i].F<<" "<<p[i].S<<" "<<vis[i]<<endl;
        if(vis[i] && p[i].F == -1)
        {
            int ii = i;
            while(vis[ii])
            {
                char c= ii+'a';
                cout<<c;
                ii = p[ii].S;
                //if(i==-1) break;
            }
        }
    }
}


#include <bits/stdc++.h>
#define F first
#define S second
#define ll long long
#define ld long double
#define PB push_back
#define B back()
#define P push
#define sz size()
#define L length()
#define INF 0x7f7f7f7f
#define inf 1e15
#define loop(i,s,e) for(ll i=s;i<=e;i++)
#define rloop(i,s,e) for(ll i=s;i>=e;i--)
#define MEM(a,x) memset(a,x,sizeof a)
#define bitcount __builtin_popcount
using namespace std;
const ll N=100*5;
ll n,vis[30];
pair<ll,ll>p[30];
main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
    cin>>n;
    loop(i,0,30) p[i].F=p[i].S=-1;
    loop(i,1,n)
    {
        string s; cin>>s;
        if(s.L==1) vis[s[0]-'a']=1;
        else
        {
        p[s[0]-'a'].S = s[1]-'a';
        vis[s[0]-'a']=1;
        loop(i,1,s.L-2)
        {
            p[s[i]-'a'].F = s[i-1]-'a';
            p[s[i]-'a'].S = s[i+1]-'a';
            vis[s[i]-'a']=1;
        }
        p[s[s.L-1]-'a'].F = s[s.L-2]-'a';
        vis[s[s.L-1]-'a'] = 1;
        }
    }
    //loop(i,0,26)
    int f=1;
    loop(i,0,26)
    {
        //cout<<p[i].F<<" "<<p[i].S<<" "<<vis[i]<<endl;
        if(vis[i] && p[i].F == -1)
        {
            int ii = i;
            while(vis[ii])
            {
                vis[ii]=0;
                char c= ii+'a';
                cout<<c;
                ii = p[ii].S;
                //if(i==-1) break;
            }
        }
    }
}


#include <bits/stdc++.h>
#define F first
#define S second
#define ll long long
#define ld long double
#define PB push_back
#define B back()
#define P push
#define sz size()
#define L length()
#define INF 0x7f7f7f7f
#define inf 1e15
#define loop(i,s,e) for(ll i=s;i<=e;i++)
#define rloop(i,s,e) for(ll i=s;i>=e;i--)
#define MEM(a,x) memset(a,x,sizeof a)
#define bitcount __builtin_popcount
using namespace std;
const ll N=100*5;
ll n,vis[30];
string ans;
pair<ll,ll>p[30];
main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
    cin>>n;
    loop(i,0,30)
    {
        p[i].F=p[i].S=-1;
        vis[i]=0;
    }
    loop(i,1,n)
    {
        string s; cin>>s;
        vis[s[0]-'a']=1;
        loop(i,1,s.L-1)
        {
            p[s[i]-'a'].F = s[i-1]-'a';
            p[s[i-1]-'a'].S = s[i]-'a';
            vis[s[i]-'a']=1;
        }
    }
    int f=1,j=0;
    loop(i,0,25)
    {
        //cout<<p[i].F<<" "<<p[i].S<<" "<<vis[i]<<endl;
        if(vis[i] && p[i].F == -1)
        {
            int ii = i;
            while(vis[ii])
            {
                vis[ii]=0;
                char c = ii+'a';
                ans += c;
                //cout<<c;
                ii = p[ii].S;
                //cout<<ii<<endl;
                if(ii==-1) break;
            }
        }
    }
    cout<<ans<<endl;
}


#include <bits/stdc++.h>
#define F first
#define S second
#define ll long long
#define ld long double
#define PB push_back
#define B back()
#define P push
#define sz size()
#define L length()
#define INF 0x7f7f7f7f
#define inf 1e15
#define loop(i,s,e) for(ll i=s;i<=e;i++)
#define rloop(i,s,e) for(ll i=s;i>=e;i--)
#define MEM(a,x) memset(a,x,sizeof a)
#define bitcount __builtin_popcount
using namespace std;
const ll N=100*5;
ll n,vis[30];
string ans;
pair<ll,ll>p[30];
main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
    cin>>n;
    loop(i,0,30)
    {
        p[i].F=p[i].S=-1;
        vis[i]=0;
    }
    loop(i,1,n)
    {
        string s; cin>>s;
        vis[s[0]-'a']=1;
        loop(i,1,s.L-1)
        {
            p[s[i]-'a'].F = s[i-1]-'a';
            p[s[i-1]-'a'].S = s[i]-'a';
            vis[s[i]-'a']=1;
        }
    }
    int f=1,j=0;
    loop(i,0,25)
    {
        //cout<<p[i].F<<" "<<p[i].S<<" "<<vis[i]<<endl;
        if(vis[i] && p[i].F == -1)
        {
            int ii = i;
            while(vis[ii])
            {
                vis[ii]=0;
                printf("%c",ii+'a');
                ii = p[ii].S;
                //cout<<ii<<endl;
                if(ii==-1) break;
            }
        }
    }
}


#include <bits/stdc++.h>
#define F first
#define S second
#define ll long long
#define ld long double
#define PB push_back
#define B back()
#define P push
#define sz size()
#define L length()
#define INF 0x7f7f7f7f
#define inf 1e15
#define loop(i,s,e) for(ll i=s;i<=e;i++)
#define rloop(i,s,e) for(ll i=s;i>=e;i--)
#define MEM(a,x) memset(a,x,sizeof a)
#define bitcount __builtin_popcount
using namespace std;
const ll N=100*5;
ll n,vis[30];
pair<ll,ll>p[30];
main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
    cin>>n;
    loop(i,0,30)
    {
        p[i].F=p[i].S=-1;
        vis[i]=0;
    }
    loop(i,1,n)
    {
        string s; cin>>s;
        vis[s[0]-'a']=1;
        loop(i,1,s.L-1)
        {
            p[s[i]-'a'].F = s[i-1]-'a';
            p[s[i-1]-'a'].S = s[i]-'a';
            vis[s[i]-'a']=1;
        }
    }
    int f=1;
    loop(i,0,25)
    {
        //cout<<p[i].F<<" "<<p[i].S<<" "<<vis[i]<<endl;
        if(vis[i] && p[i].F == -1)
        {
            int ii = i;
            while(vis[ii])
            {
                vis[ii]=0;
                char c= ii+'a';
                cout<<c;
                ii = p[ii].S;
                if(ii==-1) break;
            }
        }
    }
}


#include <bits/stdc++.h>
#define F first
#define S second
#define ll long long
#define ld long double
#define PB push_back
#define B back()
#define P push
#define sz size()
#define L length()
#define INF 0x7f7f7f7f
#define inf 1e15
#define loop(i,s,e) for(ll i=s;i<=e;i++)
#define rloop(i,s,e) for(ll i=s;i>=e;i--)
#define MEM(a,x) memset(a,x,sizeof a)
#define bitcount __builtin_popcount
using namespace std;
const ll N=100*5;
ll n,vis[30];
pair<ll,ll>p[30];
main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
    cin>>n;
    loop(i,0,30) p[i].F=p[i].S=-1;
    loop(i,1,n)
    {
        string s; cin>>s;
        vis[s[0]-'a']=1;
        loop(i,1,s.L-1)
        {
            p[s[i]-'a'].F = s[i-1]-'a';
            p[s[i-1]-'a'].S = s[i]-'a';
            vis[s[i]-'a']=1;
        }
    }
    int f=1;
    loop(i,0,26)
    {
        //cout<<p[i].F<<" "<<p[i].S<<" "<<vis[i]<<endl;
        if(vis[i] && p[i].F == -1)
        {
            int ii = i;
            while(vis[ii])
            {
                vis[ii]=0;
                char c= ii+'a';
                cout<<c;
                ii = p[ii].S;
                //if(i==-1) break;
            }
        }
    }
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <set>
#include <string>
#include <algorithm>
#include <queue>
#include <cstdio>
#include <fstream>
#include <unordered_map>
#include <map>
#include <iterator>
#include <iomanip>
#include <stack>
#include <math.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef unsigned long long ull;

#define TASK "numrecovery"
#define X first
#define Y second
#define mp make_pair
#define inb push_back
#define eps 1e-10

int main()
{
#ifdef _DEBUG
	freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
#else
	//freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
	//freopen(TASK".in", "r", stdin), freopen(TASK".out", "w", stdout);
#endif
	int n;
	cin >> n;
	vector<string> s(n);
	vector<bool> us(n);
	for (int i = 0; i < n; ++i)
		cin >> s[i];
	char c = '0';
	for (int i = 0; i < n; ++i)
	{
		int u = -1;
		for (int j = 0; j < n; ++j)
			if (!us[j])
				for (int k = 0; k < s[j].size(); ++k)
					if (s[j][k] == c)
						u = j;
		if (u == -1)
		{
			for (int j = 0; j < n; ++j)
				if (!us[j])
				{
					bool f = 1;
						for (int u = 0; u < n; ++u)
							if (u != j && !us[u])
								for (int k = 1; k < s[u].size(); ++k)
									if (s[u][k] == s[j][0])
										f = 0;
					if (f)
					{
						for (int k = 0; k < s[j].size(); ++k)
							cout << s[j][k];
						us[j] = 1;
						c = s[j][s[j].size() - 1];
						break;
					}
				}
		}
		else
		{
			us[u] = 1;
			bool f = 0;
			for (int k = 0; k < s[u].size(); ++k)
				if (s[u][k] == c)
					f = 1;
				else if (f)
					cout << s[u][k];
			c = s[u][s[u].size() - 1];
		}
	}
	//cout << endl << clock() * 1.0 / CLOCKS_PER_SEC;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <set>
#include <string>
#include <algorithm>
#include <queue>
#include <cstdio>
#include <fstream>
#include <unordered_map>
#include <map>
#include <iterator>
#include <iomanip>
#include <stack>
#include <math.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef unsigned long long ull;

#define TASK "numrecovery"
#define X first
#define Y second
#define mp make_pair
#define inb push_back
#define eps 1e-10

int main()
{
#ifdef _DEBUG
	freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
#else
	//freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
	//freopen(TASK".in", "r", stdin), freopen(TASK".out", "w", stdout);
#endif
	int n;
	cin >> n;
	vector<string> s(n);
	vector<bool> us(n);
	for (int i = 0; i < n; ++i)
		cin >> s[i];
	char c = '0';
	string y = "";
	for (int i = 0; i < n; ++i)
	{
		int u = -1;
		for (int j = 0; j < n; ++j)
			if (!us[j])
				for (int k = 0; k < s[j].size(); ++k)
					if (s[j][k] == c)
						u = j;
		if (u == -1)
		{
			for (int j = 0; j < n; ++j)
				if (!us[j])
				{
					bool f = 1;
					for (int k = 0; k < y.size(); ++k)
						if (y[k] == s[j][0])
							f = 0;
					for (int u = 0; u < n; ++u)
						if (u != j && !us[u])
							for (int k = 1; k < s[u].size(); ++k)
								if (s[u][k] == s[j][0])
									f = 0;
					if (f)
					{
						for (int k = 0; k < s[j].size(); ++k)
							y += s[j][k];
						us[j] = 1;
						c = s[j][s[j].size() - 1];
						break;
					}
				}
		}
		else
		{
			us[u] = 1;
			bool f = 0;
			for (int k = 0; k < s[u].size(); ++k)
				if (s[u][k] == c)
					f = 1;
				else if (f)
					y += s[u][k];
			c = s[u][s[u].size() - 1];
		}
	}
	cout << y;
	//cout << endl << clock() * 1.0 / CLOCKS_PER_SEC;
}


#include <string>
#include <vector>
#include <iostream>
using namespace std;
int n; string s; bool used[33], indeg[33]; int edge[33];
int main() {
	cin >> n;
	fill(edge, edge + 26, -1);
	for (int i = 0; i < n; i++) {
		cin >> s; used[s[0] - 97] = true;
		for (int j = 1; j < s.size(); j++) {
			edge[s[j - 1] - 97] = s[j] - 97;
			indeg[s[j] - 97] = true;
		}
	}
	string ret;
	for (int i = 0; i < 26; i++) {
		if (used[i] && !indeg[i]) {
			int pos = i;
			while (pos != -1) {
				ret += pos + 97;
				pos = edge[pos];
			}
		}
	}
	cout << ret << endl;
	return 0;
}