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

#define N 10045
vector < string > dp(N,"");
string str;

struct node{
	vector<node*> child;
	bool isword;
	string word;
	node (): child(26,NULL),isword(false),word("") {}
};

struct node *root = new node();

void add(string s){

	int n=s.length();

	node* cur = root;

	for(int i=0; i<n; i++){
		if(cur->child[tolower(s[i])-'a']!=NULL) 	cur = cur->child[tolower(s[i])-'a'];
		else{	struct node *nd = new node();
			    cur->child[tolower(s[i])-'a'] = nd;
			    cur = nd;
		}
	}
	cur->isword=true;
	cur->word = s;
}

string solve(int p){
	int n = str.length();

	string tmp="";
	//int tmp1=0,tmp2=0;
	struct node *cur = root;
	for(int i=p; i<n; i++){
		if(cur->child[tolower(str[i])-'a']==NULL) return "";
		//tmp+= cur->child[tolower(str[i])-'a']->data;
		cur = cur->child[tolower(str[i])-'a'];

		if(cur->isword){
			if(i==n-1){
				dp[p] =cur->word;
				return dp[p];
			}
			else if(dp[i+1]!=""){
				dp[p] = dp[i+1]+" "+cur->word;
				return dp[p];
			}
			else{

				string t = "";
				t = solve(i+1);
				if(t=="") continue;
				else{
					dp[p] = t+" "+cur->word;
					return dp[p];
				}

			}
		}
	}

	return "";
}

string lower(string s){

	int n= s.length();

	for(int i=0; i<n; i++){
		s[i] = tolower(s[i]);
	}

	return s;
}

int main(){

	int n;
	cin>>n;

	cin>>str;

	int m;
	cin>>m;

	for(int i=0; i<m; i++){
		string str1;
		cin>>str1;

		add(str1);

	}

	string rs="";

	for(int i=0; i<n; i++){
		rs+=str[n-i-1];
	}

	str = rs;

	string tmp = solve(0);

	cout<<tmp<<endl;

	return 0;
}


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

#define N 10045
vector < string > dp(N,"");
string str;

struct node{
	vector<node*> child;
	bool isword;
	string word;
	node (): child(26,NULL),isword(false),word("") {}
};

struct node *root = new node();

void add(string s){

	int n=s.length();

	node* cur = root;

	for(int i=0; i<n; i++){
		if(cur->child[tolower(s[i])-'a']!=NULL) 	cur = cur->child[tolower(s[i])-'a'];
		else{	struct node *nd = new node();
			    cur->child[tolower(s[i])-'a'] = nd;
			    cur = nd;
		}
	}
	cur->isword=true;
	cur->word = s;
}

string solve(int p){
	int n = str.length();

	string tmp="";
	//int tmp1=0,tmp2=0;
	struct node *cur = root;
	for(int i=p; i<n; i++){
		if(cur->child[tolower(str[i])-'a']==NULL) return "";
		//tmp+= cur->child[tolower(str[i])-'a']->data;
		cur = cur->child[tolower(str[i])-'a'];

		if(cur->isword){
			if(i==n-1){
				dp[p] =cur->word;
				return dp[p];
			}
			else if(dp[i+1]!=""&&dp[i+1]!="1"){
				dp[p] = dp[i+1]+" "+cur->word;
				return dp[p];
			}
			else if(dp[i+1]=="1"){
				continue;
			}
			else{

				string t = "";
				t = solve(i+1);

				if(t=="") {
					dp[i+1] = "1";
					continue;
				}
				else{
					dp[p] = t+" "+cur->word;
					return dp[p];
				}

			}
		}
	}

	return "";
}

string lower(string s){

	int n= s.length();

	for(int i=0; i<n; i++){
		s[i] = tolower(s[i]);
	}

	return s;
}

int main(){

	int n;
	cin>>n;

	cin>>str;

	int m;
	cin>>m;

	for(int i=0; i<m; i++){
		string str1;
		cin>>str1;

		add(str1);

	}

	string rs="";

	for(int i=0; i<n; i++){
		rs+=str[n-i-1];
	}

	str = rs;

	string tmp = solve(0);

	cout<<tmp<<endl;

	return 0;
}


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

#define N 10045
vector < string > dp(N,"");
string str;

struct node{
	char data='a';
	node* child[26];
	bool leaf = true;

	node (): data('\0') {}
	node (char c): data(c){}
};

struct node *root = new node();

void add(string s){

	int n=s.length();

	root->leaf = false;

	struct node* cur = root;

	for(int i=0; i<n; i++){

		if(cur->child[tolower(s[i])-'a']!=NULL) 	cur = cur->child[tolower(s[i])-'a'];
		else{	struct node *nd = new node(s[i]);
			    cur->child[tolower(s[i])-'a'] = nd;
			    cur->leaf =false;
			    cur = nd;
		}
	}
}

string solve(int p){
	int n = str.length();

	string tmp="";
	//int tmp1=0,tmp2=0;
	struct node *cur = root;
	for(int i=p; i<n; i++){
		if(cur->child[tolower(str[i])-'a']==NULL) return "";
		tmp+= cur->child[tolower(str[i])-'a']->data;
		cur = cur->child[tolower(str[i])-'a'];

		if(cur->leaf){
			if(i==n-1){
				dp[p] =tmp;
				return dp[p];
			}
			else if(dp[i+1]!=""){
				dp[p] = dp[i+1]+" "+tmp;
				return dp[p];
			}
			else{

				string t = "";
				t = solve(i+1);
				if(t=="") continue;
				else{
					dp[p] = t+" "+tmp;
					return dp[p];
				}

			}
		}
	}

	return "";
}

string lower(string s){

	int n= s.length();

	for(int i=0; i<n; i++){
		s[i] = tolower(s[i]);
	}

	return s;
}

int main(){

	int n;
	cin>>n;

	cin>>str;

	int m;
	cin>>m;

	for(int i=0; i<m; i++){
		string str1;
		cin>>str1;

		add(str1);

	}

	string rs="";

	for(int i=0; i<n; i++){
		rs+=str[n-i-1];
	}

	str = rs;

	string tmp = solve(0);

	cout<<tmp<<endl;

	return 0;
}


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

#define mod1 1000009
#define b1 31
#define mod2 10000007
#define b2 23
typedef  long long int ll;
#define N 10045

map<  int  , string > words;
vector < string > dp(N,"");
string str;

int hsh1(string s){

	int n= s.length();

	int h =0;

	for(int i=0; i<n; i++){

		h = (h*b1 + s[i] -'a')%mod1;
	}

	return h;
}

int hsh2(string s){

	int n= s.length();

	int h =0;

	for(int i=0; i<n; i++){

		h = (h*b2 + s[i] -'a')%mod2;
	}

	return h;
}

string solve(int p){
	int n = str.length();

	//string tmp="";
	int tmp1=0,tmp2=0;

	for(int i=p; i<n; i++){
		tmp1 = (tmp1*b1 + str[i]-'a')%mod1;
		//tmp2 = (tmp2*b2 + str[i]-'a')%mod2;

		if(words.find(tmp1)!=words.end()){
			if(i==n-1){
				dp[p] = words[tmp1];
				return dp[p];
			}
			else if(dp[i+1]!=""){
				//dp[p] = tmp+" "+dp[i+1];
				dp[p] = dp[i+1]+" "+words[tmp1];
				return dp[p];
			}
			else{

				string t = "";
				t = solve(i+1);
				if(t=="") continue;
				else{
					dp[p] = t+" "+words[tmp1];
					return dp[p];
				}

			}
		}
	}

	return "";
}

string lower(string s){

	int n= s.length();

	for(int i=0; i<n; i++){
		s[i] = tolower(s[i]);
	}

	return s;
}

int main(){

	int n;
	cin>>n;

	cin>>str;

	int m;
	cin>>m;

	for(int i=0; i<m; i++){
		string str1;
		cin>>str1;

		words[hsh1(lower(str1))] = str1;
		//cout<<"*"<<str<<endl;

	}

	string rs="";

	for(int i=0; i<n; i++){
		rs+=str[n-i-1];
	}

	str = rs;

	string tmp = solve(0);

	cout<<tmp<<endl;

	return 0;

}


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

#define N 10045
vector < string > dp(N,"");
string str;

struct node{
	char data='a';
	node* child[26];
	bool leaf = true;

	node (): data('\0') {}
	node (char c): data(c){}
};

struct node *root = new node();

void add(string s){

	int n=s.length();

	root->leaf = false;

	struct node* cur = root;

	for(int i=0; i<n; i++){

		if(cur->child[tolower(s[i])-'a']!=NULL) cur = cur->child[tolower(s[i])-'a'];
		else{	struct node *nd = new node(s[i]);
			    cur->child[tolower(s[i])-'a'] = nd;
			    cur->leaf =false;
			    cur = nd;
		}
	}
}

string solve(int p){
	int n = str.length();

	string tmp="";
	//int tmp1=0,tmp2=0;
	struct node *cur = root;
	for(int i=p; i<n; i++){
		if(cur->child[tolower(str[i])-'a']==NULL) return "";
		tmp+= cur->child[tolower(str[i])-'a']->data;
		cur = cur->child[tolower(str[i])-'a'];

		if(cur->leaf){
			if(i==n-1){
				dp[p] =tmp;
				return dp[p];
			}
			else if(dp[i+1]!=""){
				dp[p] = dp[i+1]+" "+tmp;
				return dp[p];
			}
			else{

				string t = "";
				t = solve(i+1);
				if(t=="") continue;
				else{
					dp[p] = t+" "+tmp;
					return dp[p];
				}

			}
		}
	}

	return "";
}

string lower(string s){

	int n= s.length();

	for(int i=0; i<n; i++){
		s[i] = tolower(s[i]);
	}

	return s;
}

int main(){

	int n;
	cin>>n;

	cin>>str;

	int m;
	cin>>m;

	for(int i=0; i<m; i++){
		string str1;
		cin>>str1;

		add(str1);

	}

	string rs="";

	for(int i=0; i<n; i++){
		rs+=str[n-i-1];
	}

	str = rs;

	string tmp = solve(0);

	cout<<tmp<<endl;

	return 0;
}


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

#define mod1 10000009
#define b1 26
#define mod2 10000007
#define b2 23
typedef  long long int ll;
#define N 100045

map< pair < ll , ll > ,string> words;
vector < string > dp(N,"");
string str;

ll hsh1(string s){

	int n= s.length();

	ll h =0;

	for(int i=0; i<n; i++){

		h = (h*b1 + s[i] -'a')%mod1;
	}

	return h;
}

ll hsh2(string s){

	int n= s.length();

	ll h =0;

	for(int i=0; i<n; i++){

		h = (h*b2 + s[i] -'a')%mod2;
	}

	return h;
}

string solve(int p){
	int n = str.length();

	//string tmp="";
	ll tmp1=0,tmp2=0;

	for(int i=p; i<n; i++){
		tmp1 = (tmp1*b1 + str[i]-'a')%mod1;
		tmp2 = (tmp2*b2 + str[i]-'a')%mod2;

		if(words.find(make_pair(tmp1,tmp2))!=words.end()){
			if(i==n-1){
				dp[p] = words[make_pair(tmp1,tmp2)];
				return dp[p];
			}
			else if(dp[i+1]!=""){
				//dp[p] = tmp+" "+dp[i+1];
				dp[p] = dp[i+1]+" "+words[make_pair(tmp1,tmp2)];
				return dp[p];
			}
			else{

				string t = "";
				t = solve(i+1);
				if(t=="") continue;
				else{
					dp[p] = t+" "+words[make_pair(tmp1,tmp2)];
					return dp[p];
				}

			}
		}
	}

	return "";
}

string lower(string s){

	int n= s.length();

	for(int i=0; i<n; i++){
		s[i] = tolower(s[i]);
	}

	return s;
}

int main(){

	int n;
	cin>>n;

	cin>>str;

	int m;
	cin>>m;

	for(int i=0; i<m; i++){
		string str1;
		cin>>str1;

		words[make_pair(hsh1(lower(str1)),hsh2(lower(str1)))] = str1;
		//cout<<"*"<<str<<endl;

	}

	string rs="";

	for(int i=0; i<n; i++){
		rs+=str[n-i-1];
	}

	str = rs;

	string tmp = solve(0);

	cout<<tmp<<endl;

	return 0;

}


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

#define mod 1000000009
#define b 26
typedef long long int  ll;

ll hsh(string s){

	int n= s.length();

	ll h =0;

	for(int i=0; i<n; i++){

		h = (h*b + s[i] -'a')%mod;
	}

	return h;
}

string solve(vector<string> &dp,string &s,int p,unordered_map<ll,string> &words){
	int n = s.length();

	//string tmp="";
	ll tmp=0;

	for(int i=p; i<n; i++){
		tmp = (tmp*b + s[i]-'a')%mod;

		if(words.find(tmp)!=words.end()){
			if(i==n-1){
				dp[p] = words[tmp];
				return dp[p];
			}
			else if(dp[i+1]!=""){
				//dp[p] = tmp+" "+dp[i+1];
				dp[p] = dp[i+1]+" "+words[tmp];
				return dp[p];
			}
			else{

				string t = "";
				t = solve(dp,s,i+1,words);
				if(t=="") continue;
				else{
					dp[p] = t+" "+words[tmp];
					return dp[p];
				}

			}
		}
	}

	return "";
}

string lower(string s){

	int n= s.length();

	for(int i=0; i<n; i++){
		s[i] = tolower(s[i]);
	}

	return s;
}

int main(){

	int n;
	cin>>n;

	string s;
	cin>>s;

	int m;
	cin>>m;

	unordered_map<ll,string> words;

	for(int i=0; i<m; i++){
		string str;
		cin>>str;

		words[hsh(lower(str))] = str;

		//cout<<"*"<<str<<endl;

	}

	vector<string> dp(n,"");

	string rs="";

	for(int i=0; i<n; i++){
		rs+=s[n-i-1];
	}

	string tmp = solve(dp,rs,0,words);

	cout<<tmp<<endl;

	return 0;

}


#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <list>
#include <queue>
#include <array>
#include <deque>
#include <string>
#include <forward_list>
#include <cmath>
#include <algorithm>
#include <stdlib.h>
#include <stdio.h>
#include <iomanip>
#include <fstream>
#include <bitset>
#define Endl endl;
using namespace std;
typedef long long ll;
typedef long double ld;
int k=1;
int edge [2000009][27];
int words[2000009];
//int pres [2000009];
int id[2000009];

ll n,m;
string s[10009];
string v,l;
int dp[10009];
string a;

void add(int v,string word){
    int node=0;
    for(int i=0; i<word.length(); i++){
        int tc=word[i]-'a';
        if(edge[node][tc]==0){
            edge[node][tc]=k;
            k++;
        }
       // pres[node]++;
        node=edge[node][tc];
    }
    //pres[node]++;
    words[node]=1;
    id[node]=v;
}

int main(){
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    //  ifstream fin("land.in");
    //  ofstream fout("land.out");
    cin>>n>>a>>m;
    for(int i=0; i<m; i++){
        cin>>s[i];
        v=s[i];
        for(int o=0; o<v.length(); o++){
            if(v[o]<'a'){
                v[o]+=32;
            }
        }
        add(i+1,v);
    }
    reverse(a.begin(),a.end());
    dp[0]=1;
    int node;
    int h;
    for(int i=0; i<n; i++){
        if(dp[i]==0){
            continue;
        }
        node=0;
        for(int o=i; o<n; o++){
            h=a[o]-'a';
            node=edge[node][h];
            if(node==0){
                break;
            }
            if(words[node]>0){
                dp[o+1]=o-i+1;
            }

        }

    }
    int x=n;
    string y;
    int z;
    while(x>0){
        y=a.substr(x-dp[x],dp[x]);
        // cout<<y<<' ';
        x-=dp[x];
        z=0;
        for(int i=0; i<y.length(); i++){
            z=edge[z][y[i]-'a'];
        }
        cout<<s[id[z]-1]<<' ';

    }

}


#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <list>
#include <queue>
#include <array>
#include <deque>
#include <string>
#include <forward_list>
#include <cmath>
#include <algorithm>
#include <stdlib.h>
#include <stdio.h>
#include <iomanip>
#include <fstream>
#include <bitset>
#define Endl endl;
using namespace std;
typedef long long ll;
typedef long double ld;
int k=1;
int edge [10000009][27];
int words[10000009];
int pres [10000009];
int id[10000009];

ll n,m;
string s[10009];
string v,l;
int dp[10009];
string a;

void add(int v,string word){
    int node=0;
    for(int i=0; i<word.length(); i++){
        int tc=word[i]-'a';
        if(edge[node][tc]==0){
            edge[node][tc]=k;
            k++;
        }
        pres[node]++;
        node=edge[node][tc];
    }
    pres[node]++;
    words[node]++;
    id[node]=v;
}

int main(){
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    //  ifstream fin("land.in");
    //  ofstream fout("land.out");
    cin>>n>>a>>m;
    for(int i=0; i<m; i++){
        cin>>s[i];
        v=s[i];
        for(int o=0; o<v.length(); o++){
            if(v[o]<'a'){
                v[o]+=32;
            }
        }
        add(i+1,v);
    }
    reverse(a.begin(),a.end());
    dp[0]=1;
    int node;
    int h;
    for(int i=0; i<n; i++){
        if(dp[i]==0){
            continue;
        }
        node=0;
        for(int o=i; o<n; o++){
            h=a[o]-'a';
            node=edge[node][h];
            if(words[node]>0){
                dp[o+1]=o-i+1;
            }
            if(pres[node]==0){
                break;
            }
        }
    }

    int x=n;
    string y;
    int z;
    while(x>0){
        y=a.substr(x-dp[x],dp[x]);
       // cout<<y<<' ';
        x-=dp[x];
        z=0;
        for(int i=0; i<y.length(); i++){
            z=edge[z][y[i]-'a'];
        }
       cout<<s[id[z]-1]<<' ';

    }

}


#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <list>
#include <queue>
#include <array>
#include <deque>
#include <string>
#include <forward_list>
#include <cmath>
#include <algorithm>
#include <stdlib.h>
#include <stdio.h>
#include <iomanip>
#include <fstream>
#include <bitset>
#define Endl endl;
using namespace std;
typedef long long ll;
typedef long double ld;
int k=1;
int edge [1200009][27];
int words[1200009];
int pres [1200009];
int id[1200009];

ll n,m;
string s[10009];
string v,l;
int dp[10009];
string a;

void add(int v,string word){
    int node=0;
    for(int i=0; i<word.length(); i++){
        int tc=word[i]-'a';
        if(edge[node][tc]==0){
            edge[node][tc]=k;
            k++;
        }
        pres[node]++;
        node=edge[node][tc];
    }
    pres[node]++;
    words[node]++;
    id[node]=v;
}

int main(){
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    //  ifstream fin("land.in");
    //  ofstream fout("land.out");
    cin>>n>>a>>m;
    for(int i=0; i<m; i++){
        cin>>s[i];
        v=s[i];
        for(int o=0; o<v.length(); o++){
            if(v[o]<'a'){
                v[o]+=32;
            }
        }
        add(i+1,v);
    }
    reverse(a.begin(),a.end());
    dp[0]=1;
    int node;
    int h;
    for(int i=0; i<n; i++){
        if(dp[i]==0){
            continue;
        }
        node=0;
        for(int o=i; o<n; o++){
            h=a[o]-'a';
            node=edge[node][h];
            if(node==0){
                break;
            }
            if(words[node]>0){
                dp[o+1]=o-i+1;
            }
            if(pres[node]==0){
                break;
            }
        }

    }
    int x=n;
    string y;
    int z;
    while(x>0){
        y=a.substr(x-dp[x],dp[x]);
       // cout<<y<<' ';
        x-=dp[x];
        z=0;
        for(int i=0; i<y.length(); i++){
            z=edge[z][y[i]-'a'];
        }
       cout<<s[id[z]-1]<<' ';

    }

}


#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <list>
#include <queue>
#include <array>
#include <deque>
#include <string>
#include <forward_list>
#include <cmath>
#include <algorithm>
#include <stdlib.h>
#include <stdio.h>
#include <iomanip>
#include <fstream>
#include <bitset>
#define Endl endl;
using namespace std;
typedef long long ll;
typedef long double ld;
int k=1;
int edge [1000009][27];
int words[1000009];
int pres [1000009];
int id[1000009];

ll n,m;
string s[10009];
string v,l;
int dp[10009];
string a;

void add(int v,string word){
    int node=0;
    for(int i=0; i<word.length(); i++){
        int tc=word[i]-'a';
        if(edge[node][tc]==0){
            edge[node][tc]=k;
            k++;
        }
        pres[node]++;
        node=edge[node][tc];
    }
    pres[node]++;
    words[node]++;
    id[node]=v;
}

int main(){
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    //  ifstream fin("land.in");
    //  ofstream fout("land.out");
    cin>>n>>a>>m;
    for(int i=0; i<m; i++){
        cin>>s[i];
        v=s[i];
        for(int o=0; o<v.length(); o++){
            if(v[o]<'a'){
                v[o]+=32;
            }
        }
        add(i+1,v);
    }
    reverse(a.begin(),a.end());
    dp[0]=1;
    int node;
    int h;
    for(int i=0; i<n; i++){
        if(dp[i]==0){
            continue;
        }
        node=0;
        for(int o=i; o<n; o++){
            h=a[o]-'a';
            node=edge[node][h];
            if(words[node]>0){
                dp[o+1]=o-i+1;
            }
            if(pres[node]==0){
                break;
            }
        }
    }

    int x=n;
    string y;
    int z;
    while(x>0){
        y=a.substr(x-dp[x],dp[x]);
       // cout<<y<<' ';
        x-=dp[x];
        z=0;
        for(int i=0; i<y.length(); i++){
            z=edge[z][y[i]-'a'];
        }
       cout<<s[id[z]-1]<<' ';

    }

}


#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <list>
#include <queue>
#include <array>
#include <deque>
#include <string>
#include <forward_list>
#include <cmath>
#include <algorithm>
#include <stdlib.h>
#include <stdio.h>
#include <iomanip>
#include <fstream>
#include <bitset>
#define Endl endl;
using namespace std;
typedef long long ll;
typedef long double ld;
int k=1;
int edge [1200009][27];
int words[1200009];
int pres [1200009];
int id[1200009];

ll n,m;
string s[10009];
string v,l;
int dp[10009];
string a;

void add(int v,string word){
    int node=0;
    for(int i=0; i<word.length(); i++){
        int tc=word[i]-'a';
        if(edge[node][tc]==0){
            edge[node][tc]=k;
            k++;
        }
        pres[node]++;
        node=edge[node][tc];
    }
    pres[node]++;
    words[node]++;
    id[node]=v;
}

int main(){
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    //  ifstream fin("land.in");
    //  ofstream fout("land.out");
    cin>>n>>a>>m;
    for(int i=0; i<m; i++){
        cin>>s[i];
        v=s[i];
        for(int o=0; o<v.length(); o++){
            if(v[o]<'a'){
                v[o]+=32;
            }
        }
        add(i+1,v);
    }
    reverse(a.begin(),a.end());
    dp[0]=1;
    int node;
    int h;
    for(int i=0; i<n; i++){
        if(dp[i]==0){
            continue;
        }
        node=0;
        for(int o=i; o<n; o++){
            h=a[o]-'a';
            node=edge[node][h];
            if(words[node]>0){
                dp[o+1]=o-i+1;
            }
        }
    }

    int x=n;
    string y;
    int z;
    while(x>0){
        y=a.substr(x-dp[x],dp[x]);
       // cout<<y<<' ';
        x-=dp[x];
        z=0;
        for(int i=0; i<y.length(); i++){
            z=edge[z][y[i]-'a'];
        }
       cout<<s[id[z]-1]<<' ';

    }

}


#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <list>
#include <queue>
#include <array>
#include <deque>
#include <string>
#include <forward_list>
#include <cmath>
#include <algorithm>
#include <stdlib.h>
#include <stdio.h>
#include <iomanip>
#include <fstream>
#include <bitset>
#define Endl endl;
using namespace std;
typedef long long ll;
typedef long double ld;
int k=1;
int edge [2000009][27];
int words[2000009];
int pres [2000009];
int id[2000009];

ll n,m;
string s[10009];
string v,l;
int dp[10009];
string a;

void add(int v,string word){
    int node=0;
    for(int i=0; i<word.length(); i++){
        int tc=word[i]-'a';
        if(edge[node][tc]==0){
            edge[node][tc]=k;
            k++;
        }
        pres[node]++;
        node=edge[node][tc];
    }
    pres[node]++;
    words[node]++;
    id[node]=v;
}

int main(){
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    //  ifstream fin("land.in");
    //  ofstream fout("land.out");
    cin>>n>>a>>m;
    for(int i=0; i<m; i++){
        cin>>s[i];
        v=s[i];
        for(int o=0; o<v.length(); o++){
            if(v[o]<'a'){
                v[o]+=32;
            }
        }
        add(i+1,v);
    }
    reverse(a.begin(),a.end());
    dp[0]=1;
    int node;
    int h;
    for(int i=0; i<n; i++){
        if(dp[i]==0){
            continue;
        }
        node=0;
        for(int o=i; o<n; o++){
            h=a[o]-'a';
            node=edge[node][h];
            if(node==0){
                break;
            }
            if(words[node]>0){
                dp[o+1]=o-i+1;
            }
            if(pres[node]==0){
                break;
            }
        }

    }
    int x=n;
    string y;
    int z;
    while(x>0){
        y=a.substr(x-dp[x],dp[x]);
       // cout<<y<<' ';
        x-=dp[x];
        z=0;
        for(int i=0; i<y.length(); i++){
            z=edge[z][y[i]-'a'];
        }
       cout<<s[id[z]-1]<<' ';

    }

}


#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <list>
#include <queue>
#include <array>
#include <deque>
#include <string>
#include <forward_list>
#include <cmath>
#include <algorithm>
#include <stdlib.h>
#include <stdio.h>
#include <iomanip>
#include <fstream>
#include <bitset>
#define Endl endl;
using namespace std;
typedef long long ll;
typedef long double ld;
int k=1;
int edge [2000009][27];
int words[2000009];
//int pres [2000009];
int id[2000009];

ll n,m;
string s[100009];
string v,l;
int dp[10009];
string a;

void add(int v,string word){
    int node=0;
    for(int i=0; i<word.length(); i++){
        int tc=word[i]-'a';
        if(edge[node][tc]==0){
            edge[node][tc]=k;
            k++;
        }
       // pres[node]++;
        node=edge[node][tc];
    }
    //pres[node]++;
    words[node]=1;
    id[node]=v;
}

int main(){
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    //  ifstream fin("land.in");
    //  ofstream fout("land.out");
    cin>>n>>a>>m;
    for(int i=0; i<m; i++){
        cin>>s[i];
        v=s[i];
        for(int o=0; o<v.length(); o++){
            if(v[o]<'a'){
                v[o]+=32;
            }
        }
        add(i+1,v);
    }
    reverse(a.begin(),a.end());
    dp[0]=1;
    int node;
    int h;
    for(int i=0; i<n; i++){
        if(dp[i]==0){
            continue;
        }
        node=0;
        for(int o=i; o<n; o++){
            h=a[o]-'a';
            node=edge[node][h];
            if(node==0){
                break;
            }
            if(words[node]>0){
                dp[o+1]=o-i+1;
            }

        }

    }
    int x=n;
    string y;
    int z;
    while(x>0){
        y=a.substr(x-dp[x],dp[x]);
        // cout<<y<<' ';
        x-=dp[x];
        z=0;
        for(int i=0; i<y.length(); i++){
            z=edge[z][y[i]-'a'];
        }
        cout<<s[id[z]-1]<<' ';

    }

}


#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <list>
#include <queue>
#include <array>
#include <deque>
#include <string>
#include <forward_list>
#include <cmath>
#include <algorithm>
#include <stdlib.h>
#include <stdio.h>
#include <iomanip>
#include <fstream>
#include <bitset>
#define Endl endl;
using namespace std;
typedef long long ll;
typedef long double ld;
int k=1;
int edge [1200009][27];
int words[1200009];
int pres [1200009];
int id[1200009];

ll n,m;
string s[10009];
string v,l;
int dp[10009];
string a;

void add(int v,string word){
    int node=0;
    for(int i=0; i<word.length(); i++){
        int tc=word[i]-'a';
        if(edge[node][tc]==0){
            edge[node][tc]=k;
            k++;
        }
        pres[node]++;
        node=edge[node][tc];
    }
    pres[node]++;
    words[node]++;
    id[node]=v;
}

int main(){
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    //  ifstream fin("land.in");
    //  ofstream fout("land.out");
    cin>>n>>a>>m;
    for(int i=0; i<m; i++){
        cin>>s[i];
        v=s[i];
        for(int o=0; o<v.length(); o++){
            if(v[o]<'a'){
                v[o]+=32;
            }
        }
        add(i+1,v);
    }
    reverse(a.begin(),a.end());
    dp[0]=1;
    int node;
    int h;
    for(int i=0; i<n; i++){
        if(dp[i]==0){
            continue;
        }
        node=0;
        for(int o=i; o<n; o++){
            h=a[o]-'a';
            node=edge[node][h];
            if(words[node]>0){
                dp[o+1]=o-i+1;
            }
            if(pres[node]==0){
                break;
            }
        }
    }

    int x=n;
    string y;
    int z;
    while(x>0){
        y=a.substr(x-dp[x],dp[x]);
       // cout<<y<<' ';
        x-=dp[x];
        z=0;
        for(int i=0; i<y.length(); i++){
            z=edge[z][y[i]-'a'];
        }
       cout<<s[id[z]-1]<<' ';

    }

}


#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <list>
#include <queue>
#include <array>
#include <deque>
#include <string>
#include <forward_list>
#include <cmath>
#include <algorithm>
#include <stdlib.h>
#include <stdio.h>
#include <iomanip>
#include <fstream>
#include <bitset>
#define Endl endl;
using namespace std;
typedef long long ll;
typedef long double ld;
ll n,m;
string s[10009];
string v,l;
map<string,int> k;
int dp[10009];
string a;
vector<string> ans;
int main(){
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    //  ifstream fin("land.in");
    //  ofstream fout("land.out");
    cin>>n>>a>>m;
    for(int i=0; i<m; i++){
        cin>>s[i];
        v=s[i];
        for(int i=0; i<v.length(); i++){
            if(v[i]<'a'){
                v[i]+=32;
            }
        }
        k[v]=i+1;
    }
    reverse(a.begin(),a.end());
    for(int i=0; i<n; i++){
        l.clear();
        for(int o=i-1; o>=max(-1,i-1001); o--){
            l=a[o+1]+l;
             //cout<<l<<' ';
            if(o==-1 || dp[o]>0){
                if(k[l]>0){
                    dp[i]=l.length();
                    break;
                }
            }
        }
      //  cout<<i<<' '<<dp[i]<<endl;
    }
    int pp=n-1;
    string j;
    while(pp>=0){
        j=a.substr(pp-dp[pp]+1,dp[pp]);
        cout<<s[k[j]-1]<<' ';
        pp-=dp[pp];
    }

}


#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <list>
#include <queue>
#include <array>
#include <deque>
#include <string>
#include <forward_list>
#include <cmath>
#include <algorithm>
#include <stdlib.h>
#include <stdio.h>
#include <iomanip>
#include <fstream>
#include <bitset>
#define Endl endl;
using namespace std;
typedef long long ll;
typedef long double ld;
int k=1;
int edge [2000009][27];
int words[2000009];
//int pres [2000009];
int id[2000009];

ll n,m;
string s[100009];
string v,l;
int dp[10009];
string a;

void add(int v,string word){
    int node=0;
    for(int i=0; i<word.length(); i++){
        int tc=word[i]-'a';
        if(edge[node][tc]==0){
            edge[node][tc]=k;
            k++;
        }
        // pres[node]++;
        node=edge[node][tc];
    }
    //pres[node]++;
    words[node]=1;
    id[node]=v;
}

int main(){
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    //  ifstream fin("land.in");
    //  ofstream fout("land.out");
    cin>>n>>a>>m;
    for(int i=0; i<m; i++){
        cin>>s[i];
        v=s[i];
        for(int o=0; o<v.length(); o++){
            if(v[o]<'a'){
                v[o]+=32;
            }
        }
        add(i+1,v);
    }
    reverse(a.begin(),a.end());
    dp[0]=1;
    int node;
    int h;
    for(int i=0; i<n; i++){
        if(dp[i]==0){
            continue;
        }
        node=0;
        for(int o=i; o<n; o++){
            h=a[o]-'a';
            node=edge[node][h];
            if(node==0){
                break;
            }
            if(words[node]>0){
                dp[o+1]=o-i+1;
            }

        }

    }
    int x=n;
    string y;
    int z;
    while(x>0){
        y=a.substr(x-dp[x],dp[x]);
        // cout<<y<<' ';
        x-=dp[x];
        z=0;
        for(int i=0; i<y.length(); i++){
            z=edge[z][y[i]-'a'];
        }
        cout<<s[id[z]-1]<<' ';

    }

}


#include <bits/stdc++.h>

using namespace std;

const int N = 100005;

string BIG, s;
vector< pair<int, string> > pp;
bool f[N];

long long H[N], P[N], H2[N], P2[N];
int B = 277;
int B2 = 10007;
int MOD = 1000000007;
int MOD2 = INT_MAX;

int main() {
   //freopen("input.txt", "r", stdin);
   ios :: sync_with_stdio(false);
   int L;
   cin >> L >> BIG;
   int n; cin >> n;
   P[0] = P2[0] = 1;
   for(int i = 1; i <= L; i++) {
      H[i] = (H[i - 1] * B + BIG[i - 1]) % MOD;
      P[i] = (P[i - 1] * B) % MOD;
      H2[i] = (H2[i - 1] * B2 + BIG[i - 1]) % MOD2;
      P2[i] = (P2[i - 1] * B2) % MOD2;
   }

   while(n--) {
      long long now = 0, now2 = 0;
      cin >> s;
      int l = s.size();
      for(int i = l - 1; i >= 0; i--) {
         char ch = s[i] < 'a'? s[i] + 32 : s[i];
         now = (now * B + ch) % MOD;
         now2 = (now2 * B2 + ch) % MOD2;
      }

      for(int i = l; i <= L; i++) {
         long long cp = (H[i] - (H[i - l] * P[l]) % MOD + MOD) % MOD;
         long long cp2 = (H2[i] - (H2[i - l] * P2[l]) % MOD2 + MOD2) % MOD2;
         if(now == cp && now2 == cp2 && !f[i]) {

            for(int j = i - l + 1; j <= i; j++) {
               f[j] = 1;
            }
            pp.push_back(make_pair(i, s));
            break;
         }
      }
   }
   sort(pp.begin(), pp.end());
   int cnt = 0;
   for(auto it : pp) {
      int l = it.second.size();
      cnt += l;
      cnt = min(L, cnt);
      for(int i = cnt - 1, k = 0; i >= cnt - l; i--, k++) {
         char ch = it.second[k];
         putchar(ch);
      }
      putchar(' ');
   }
   return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 123456;
const long long B = 255;

string str, word;
map<long long, string> Hash;
long long f[N];

void print(int n) {
   if(n == 0) {
      return;
   }
   string now = Hash[f[n]];
   print(n - now.size());
   cout << now << " ";
}

int main() {
   ios_base :: sync_with_stdio(false);
   //freopen("input.txt", "r", stdin);
   int L, n;
   long long strhash;
   cin >> L >> str >> n;
   for(int i = 0; i < n; i++) {
      cin >> word;
      strhash = 0;
      for(int j = 0; word[j]; j++) {
         strhash = strhash * B + tolower(word[j]);
      }
      Hash[strhash] = word;
   }
   fill(f, f + L, -1);
   f[0] = 0;
   for(int i = 0; i < L; i++) {
      strhash = 0;
      for(int j = i; j >= 0; j--) {
         strhash = strhash * B + str[j];
         if(f[j] != -1 && Hash.count(strhash)) {
            f[i + 1] = strhash;
            break;
         }
      }
   }
   print(L);
   return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 123456;
const long long B = 125;

string str, word;
map<long long, string> Hash;
long long f[N];

void print(int n) {
   if(n == 0) {
      return;
   }
   string now = Hash[f[n]];
   print(n - now.size());
   cout << now << " ";
}

int main() {
   int L, n;
   long long strhash;
   cin >> L >> str >> n;
   for(int i = 0; i < n; i++) {
      cin >> word;
      strhash = 0;
      for(int j = 0; word[j]; j++) {
         strhash = strhash * B + tolower(word[j]);
      }
      Hash[strhash] = word;
   }
   fill(f, f + L, -1);
   f[0] = 0;
   for(int i = 0; i < L; i++) {
      strhash = 0;
      for(int j = i; j >= 0; j--) {
         strhash = strhash * B + str[j];
         if(f[j] != -1 && Hash.count(strhash)) {
            f[i + 1] = strhash;
            break;
         }
      }
   }
   print(L);
   return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 100005;

string BIG, s;
vector< pair<int, string> > pp;
bool f[N];

long long H[N], P[N];
int B = 277;
int MOD = 1000000007;

int main() {
  // freopen("input.txt", "r", stdin);
   ios :: sync_with_stdio(false);
   int L;
   cin >> L >> BIG;
   int n; cin >> n;
   P[0] = 1;
   for(int i = 1; i <= L; i++) {
      H[i] = (H[i - 1] * B + BIG[i - 1]) % MOD;
      P[i] = (P[i - 1] * B) % MOD;
   }

   while(n--) {
      long long now = 0;
      cin >> s;
      int l = s.size();
      for(int i = l - 1; i >= 0; i--) {
         char ch = s[i] < 'a'? s[i] + 32 : s[i];
         now = (now * B + ch) % MOD;
      }

      for(int i = l; i <= L; i++) {
         long long cp = (H[i] - (H[i - l] * P[l]) % MOD + MOD) % MOD;
         if(now == cp && !f[i]) {

            for(int j = i - l + 1; j <= i; j++) {
               f[j] = 1;
            }
            pp.push_back(make_pair(i, s));
            break;
         }
      }
   }
   sort(pp.begin(), pp.end());
   for(auto it : pp) {
      cout << it.second << ' ';
   }
   return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 100005;

string BIG, s, ar[N];
vector< pair<int, string> > pp;
bool f[N];

long long H[N], P[N], H2[N], P2[N];
int B = 277;
int B2 = 10007;
int MOD = 1000000007;
int MOD2 = INT_MAX;

bool cmp(string a, string b) {
   return a.size() > b.size();
}

int main() {
  // freopen("input.txt", "r", stdin);
   ios :: sync_with_stdio(false); cin.tie(0);
   int L;
   cin >> L >> BIG;
   int n; cin >> n;
   P[0] = P2[0] = 1;
   reverse(BIG.begin(), BIG.end());
   for(int i = 1; i <= L; i++) {
      H[i] = (H[i - 1] * B + BIG[i - 1]) % MOD;
      P[i] = (P[i - 1] * B) % MOD;
      H2[i] = (H2[i - 1] * B2 + BIG[i - 1]) % MOD2;
      P2[i] = (P2[i - 1] * B2) % MOD2;
   }

   for(int i = 0; i < n; i++) {
      cin >> ar[i];
   }

   sort(ar, ar + n, cmp);

   for(int m = 0; m < n; m++) {
      long long now = 0, now2 = 0;
      s = ar[m];
      int l = s.size();
      for(int i = 0; i < l; i++) {
         char ch = s[i] < 'a'? s[i] + 32 : s[i];
         now = (now * B + ch) % MOD;
         now2 = (now2 * B2 + ch) % MOD2;
      }

      for(int i = l; i <= L; i++) {
         long long cp = (H[i] - (H[i - l] * P[l]) % MOD + MOD) % MOD;
         long long cp2 = (H2[i] - (H2[i - l] * P2[l]) % MOD2 + MOD2) % MOD2;
         if(now == cp && now2 == cp2 && !f[i] && !f[i - l + 1]) {
            for(int j = i - l + 1; j <= i; j++) {
               f[j] = 1;
            }
            pp.push_back(make_pair(-i, s));
         }
      }
   }
   sort(pp.begin(), pp.end());
   int cnt = 0;
   for(int i = 0, k = pp.size(); i < k; i++) {
      cout << pp[i].second << ' ';
   }
   return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 100005;

string BIG, s;
set<long long > st;
vector< pair<int, string> > pp;
bool f[N];

long long H[N], P[N];
int B = 277;
int MOD = 1000000007;

int main() {
   ios :: sync_with_stdio(false);
   int L;
   cin >> L >> BIG;
   int n; cin >> n;
   P[0] = 1;
   for(int i = 1; i <= L; i++) {
      H[i] = (H[i - 1] * B + BIG[i - 1]) % MOD;
      P[i] = (P[i - 1] * B) % MOD;
   }

   while(n--) {
      long long now = 0;
      cin >> s;
      int l = s.size();
      if(l < 2) continue;
      for(int i = l - 1; i >= 0; i--) {
         char ch = s[i] < 'a'? s[i] + 32 : s[i];
         now = (now * B + ch) % MOD;
      }
      if(st.count(now)) continue;
      st.insert(now);
      for(int i = l; i <= L; i++) {
         long long cp = (H[i] - (H[i - l] * P[l]) % MOD + MOD) % MOD;
         if(now == cp && !f[i]) {
            f[i] = 1;
            pp.push_back(make_pair(i, s));
            break;
         }
      }
   }
   sort(pp.begin(), pp.end());
   for(auto it : pp) {
      cout << it.second << ' ';
   }
   return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 100005;

string BIG, s;
vector< pair<int, string> > pp;
bool f[N];

long long H[N], P[N], H2[N], P2[N];
int B = 277;
int B2 = 10007;
int MOD = 1000000007;
int MOD2 = INT_MAX;

int main() {
   //freopen("input.txt", "r", stdin);
   ios :: sync_with_stdio(false);
   int L;
   cin >> L >> BIG;
   int n; cin >> n;
   P[0] = P2[0] = 1;
   for(int i = 1; i <= L; i++) {
      H[i] = (H[i - 1] * B + BIG[i - 1]) % MOD;
      P[i] = (P[i - 1] * B) % MOD;
      H2[i] = (H2[i - 1] * B2 + BIG[i - 1]) % MOD2;
      P2[i] = (P2[i - 1] * B2) % MOD2;
   }

   while(n--) {
      long long now = 0, now2 = 0;
      cin >> s;
      int l = s.size();
      for(int i = l - 1; i >= 0; i--) {
         char ch = s[i] < 'a'? s[i] + 32 : s[i];
         now = (now * B + ch) % MOD;
         now2 = (now2 * B2 + ch) % MOD2;
      }

      for(int i = l; i <= L; i++) {
         long long cp = (H[i] - (H[i - l] * P[l]) % MOD + MOD) % MOD;
         long long cp2 = (H2[i] - (H2[i - l] * P2[l]) % MOD2 + MOD2) % MOD2;
         if(now == cp && now2 == cp2 && !f[i]) {

            for(int j = i - l + 1; j <= i; j++) {
               f[j] = 1;
            }
            pp.push_back(make_pair(i, s));
           // break;
         }
      }
   }
   sort(pp.begin(), pp.end());
   int cnt = 0;
   for(auto it : pp) {
      int l = it.second.size();
      cnt += l;
      cnt = min(L, cnt);
      for(int i = cnt - 1, k = 0; i >= cnt - l; i--, k++) {
         char ch = it.second[k];
         putchar(ch);
      }
      putchar(' ');
   }
   return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 100005;

string BIG, s;
vector< pair<int, string> > pp;
bool f[N];

long long H[N], P[N], H2[N], P2[N];
int B = 277;
int B2 = 10007;
int MOD = 1000000007;
int MOD2 = INT_MAX;

int main() {
   freopen("input.txt", "r", stdin);
   ios :: sync_with_stdio(false);
   int L;
   cin >> L >> BIG;
   int n; cin >> n;
   P[0] = P2[0] = 1;
   for(int i = 1; i <= L; i++) {
      H[i] = (H[i - 1] * B + BIG[i - 1]) % MOD;
      P[i] = (P[i - 1] * B) % MOD;
      H2[i] = (H2[i - 1] * B2 + BIG[i - 1]) % MOD2;
      P2[i] = (P2[i - 1] * B2) % MOD2;
   }

   while(n--) {
      long long now = 0, now2 = 0;
      cin >> s;
      int l = s.size();
      for(int i = l - 1; i >= 0; i--) {
         char ch = s[i] < 'a'? s[i] + 32 : s[i];
         now = (now * B + ch) % MOD;
         now2 = (now2 * B2 + ch) % MOD2;
      }

      for(int i = l; i <= L; i++) {
         long long cp = (H[i] - (H[i - l] * P[l]) % MOD + MOD) % MOD;
         long long cp2 = (H2[i] - (H2[i - l] * P2[l]) % MOD2 + MOD2) % MOD2;
         if(now == cp && now2 == cp2 && !f[i]) {

            for(int j = i - l + 1; j <= i; j++) {
               f[j] = 1;
            }
            pp.push_back(make_pair(i, s));
           // break;
         }
      }
   }
   sort(pp.begin(), pp.end());
   int cnt = 0;
   for(auto it : pp) {
      int l = it.second.size();
      cnt += l;
      cnt = min(L, cnt);
      for(int i = cnt - 1, k = 0; i >= cnt - l; i--, k++) {
         char ch = it.second[k];
         putchar(ch);
      }
      putchar(' ');
   }
   return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 100005;

string BIG, s;
vector< pair<int, string> > pp;
bool f[N];

long long H[N], P[N], H2[N], P2[N];
int B = 277;
int B2 = 10007;
int MOD = 1000000007;
int MOD2 = INT_MAX;

int main() {
   //freopen("input.txt", "r", stdin);
   ios :: sync_with_stdio(false);
   int L;
   cin >> L >> BIG;
   int n; cin >> n;
   P[0] = P2[0] = 1;
   for(int i = 1; i <= L; i++) {
      H[i] = (H[i - 1] * B + BIG[i - 1]) % MOD;
      P[i] = (P[i - 1] * B) % MOD;
      H2[i] = (H2[i - 1] * B2 + BIG[i - 1]) % MOD2;
      P2[i] = (P2[i - 1] * B2) % MOD2;
   }

   while(n--) {
      long long now = 0, now2 = 0;
      cin >> s;
      int l = s.size();
      for(int i = l - 1; i >= 0; i--) {
         char ch = s[i] < 'a'? s[i] + 32 : s[i];
         now = (now * B + ch) % MOD;
         now2 = (now2 * B2 + ch) % MOD2;
      }

      for(int i = l; i <= L; i++) {
         long long cp = (H[i] - (H[i - l] * P[l]) % MOD + MOD) % MOD;
         long long cp2 = (H2[i] - (H2[i - l] * P2[l]) % MOD2 + MOD2) % MOD2;
         if(now == cp && now2 == cp2 && !f[i]) {

            for(int j = i - l + 1; j <= i; j++) {
               f[j] = 1;
            }
            pp.push_back(make_pair(i, s));
         }
      }
   }
   sort(pp.begin(), pp.end());
   for(auto it : pp) {
      cout << it.second << ' ';
   }
   return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 100005;

string BIG, s, ar[N];
vector< pair<int, string> > pp;
bool f[N];

long long H[N], P[N], H2[N], P2[N];
int B = 277;
int B2 = 10007;
int MOD = 1000000007;
int MOD2 = INT_MAX;

bool cmp(string a, string b) {
   return a.size() > b.size();
}

int main() {
   //freopen("input.txt", "r", stdin);
   ios :: sync_with_stdio(false);
   int L;
   cin >> L >> BIG;
   int n; cin >> n;
   P[0] = P2[0] = 1;
   reverse(BIG.begin(), BIG.end());
   for(int i = 1; i <= L; i++) {
      H[i] = (H[i - 1] * B + BIG[i - 1]) % MOD;
      P[i] = (P[i - 1] * B) % MOD;
      H2[i] = (H2[i - 1] * B2 + BIG[i - 1]) % MOD2;
      P2[i] = (P2[i - 1] * B2) % MOD2;
   }

   for(int i = 0; i < n; i++) {
      cin >> ar[i];
   }

   sort(ar, ar + n, cmp);

   for(int m = 0; m < n; m++) {
      long long now = 0, now2 = 0;
      s = ar[m];
      int l = s.size();
      for(int i = 0; i < l; i++) {
         char ch = s[i] < 'a'? s[i] + 32 : s[i];
         now = (now * B + ch) % MOD;
         now2 = (now2 * B2 + ch) % MOD2;
      }

      for(int i = l; i <= L; i++) {
         long long cp = (H[i] - (H[i - l] * P[l]) % MOD + MOD) % MOD;
         long long cp2 = (H2[i] - (H2[i - l] * P2[l]) % MOD2 + MOD2) % MOD2;
         if(now == cp && now2 == cp2) {
            bool ok = 1;
            for(int j = i - l + 1; j <= i; j++) {
               if(f[j]) {
                  ok = 0;
                  break;
               }
            }
            if(!ok) continue;
            for(int j = i - l + 1; j <= i; j++) {
               f[j] = 1;
            }
            pp.push_back(make_pair(-i, s));

         }
      }
   }
   sort(pp.begin(), pp.end());
   int cnt = 0;
   for(auto it : pp) {
      int l = it.second.size();
      cnt += l;
      cnt = min(L, cnt);
      for(int i = cnt - 1, k = 0; i >= cnt - l; i--, k++) {
         char ch = it.second[k];
         putchar(ch);
      }
      putchar(' ');
   }
   return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 100005;

string BIG, s, ar[N];
vector< pair<int, string> > pp;
bool f[N];

long long H[N], P[N], H2[N], P2[N];
int B = 277;
int B2 = 10007;
int MOD = 1000000007;
int MOD2 = INT_MAX;

bool cmp(string a, string b) {
   return a.size() > b.size();
}

int main() {
  // freopen("input.txt", "r", stdin);
   ios :: sync_with_stdio(false); cin.tie(0);
   int L;
   cin >> L >> BIG;
   int n; cin >> n;
   P[0] = P2[0] = 1;
   reverse(BIG.begin(), BIG.end());
   for(int i = 1; i <= L; i++) {
      H[i] = (H[i - 1] * B + BIG[i - 1]) % MOD;
      P[i] = (P[i - 1] * B) % MOD;
      H2[i] = (H2[i - 1] * B2 + BIG[i - 1]) % MOD2;
      P2[i] = (P2[i - 1] * B2) % MOD2;
   }

   for(int i = 0; i < n; i++) {
      cin >> ar[i];
   }

   sort(ar, ar + n, cmp);

   for(int m = 0; m < n; m++) {
      long long now = 0, now2 = 0;
      s = ar[m];
      int l = s.size();
      for(int i = 0; i < l; i++) {
         char ch = s[i] < 'a'? s[i] + 32 : s[i];
         now = (now * B + ch) % MOD;
         now2 = (now2 * B2 + ch) % MOD2;
      }

      for(int i = l; i <= L; i++) {
         long long cp = (H[i] - (H[i - l] * P[l]) % MOD + MOD) % MOD;
         long long cp2 = (H2[i] - (H2[i - l] * P2[l]) % MOD2 + MOD2) % MOD2;
         if(now == cp && now2 == cp2 && !f[i]) {
            for(int j = i - l + 1; j <= i; j++) {
               f[j] = 1;
            }
            pp.push_back(make_pair(-i, s));
         }
      }
   }
   sort(pp.begin(), pp.end());
   int cnt = 0;
   for(int i = 0, k = pp.size(); i < k; i++) {
      cout << pp[i].second << ' ';
   }
   return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 100005;

string BIG, s;
set<long long > st;
vector< pair<int, string> > pp;
bool f[N];

long long H[N], P[N];
int B = 277;
int MOD = 1000000007;

int main() {
  // freopen("input.txt", "r", stdin);
   ios :: sync_with_stdio(false);
   int L;
   cin >> L >> BIG;
   int n; cin >> n;
   P[0] = 1;
   for(int i = 1; i <= L; i++) {
      H[i] = (H[i - 1] * B + BIG[i - 1]) % MOD;
      P[i] = (P[i - 1] * B) % MOD;
   }

   while(n--) {
      long long now = 0;
      cin >> s;
      int l = s.size();
      for(int i = l - 1; i >= 0; i--) {
         char ch = s[i] < 'a'? s[i] + 32 : s[i];
         now = (now * B + ch) % MOD;
      }
      if(st.count(now)) continue;
      st.insert(now);
      for(int i = l; i <= L; i++) {
         long long cp = (H[i] - (H[i - l] * P[l]) % MOD + MOD) % MOD;
         if(now == cp && !f[i]) {

            for(int j = i - l + 1; j <= i; j++) {
               f[j] = 1;
            }
            pp.push_back(make_pair(i, s));
            break;
         }
      }
   }
   sort(pp.begin(), pp.end());
   for(auto it : pp) {
      cout << it.second << ' ';
   }
   return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 100005;

string BIG, s;
set<long long > st;
vector< pair<int, string> > pp;
bool f[N];

long long H[N], P[N];
int B = 277;
int MOD = 1000000007;

int main() {
   freopen("input.txt", "r", stdin);
   ios :: sync_with_stdio(false);
   int L;
   cin >> L >> BIG;
   int n; cin >> n;
   P[0] = 1;
   for(int i = 1; i <= L; i++) {
      H[i] = (H[i - 1] * B + BIG[i - 1]) % MOD;
      P[i] = (P[i - 1] * B) % MOD;
   }

   while(n--) {
      long long now = 0;
      cin >> s;
      int l = s.size();
      if(l < 2) continue;
      for(int i = l - 1; i >= 0; i--) {
         char ch = s[i] < 'a'? s[i] + 32 : s[i];
         now = (now * B + ch) % MOD;
      }
      if(st.count(now)) continue;
      st.insert(now);
      for(int i = l; i <= L; i++) {
         long long cp = (H[i] - (H[i - l] * P[l]) % MOD + MOD) % MOD;
         if(now == cp && !f[i]) {
            f[i] = 1;
            pp.push_back(make_pair(i, s));
            break;
         }
      }
   }
   sort(pp.begin(), pp.end());
   for(auto it : pp) {
      cout << it.second << ' ';
   }
   return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 100005;

string BIG, s;
vector< pair<int, string> > pp;
bool f[N];

long long H[N], P[N], H2[N], P2[N];
int B = 277;
int B2 = 10007;
int MOD = 1000000007;
int MOD2 = INT_MAX;

int main() {
   //freopen("input.txt", "r", stdin);
   ios :: sync_with_stdio(false);
   int L;
   cin >> L >> BIG;
   int n; cin >> n;
   P[0] = P2[0] = 1;
   for(int i = 1; i <= L; i++) {
      H[i] = (H[i - 1] * B + BIG[i - 1]) % MOD;
      P[i] = (P[i - 1] * B) % MOD;
      H2[i] = (H2[i - 1] * B2 + BIG[i - 1]) % MOD2;
      P2[i] = (P2[i - 1] * B2) % MOD2;
   }

   while(n--) {
      long long now = 0, now2 = 0;
      cin >> s;
      int l = s.size();
      for(int i = l - 1; i >= 0; i--) {
         char ch = s[i] < 'a'? s[i] + 32 : s[i];
         now = (now * B + ch) % MOD;
         now2 = (now2 * B2 + ch) % MOD2;
      }

      for(int i = l; i <= L; i++) {
         long long cp = (H[i] - (H[i - l] * P[l]) % MOD + MOD) % MOD;
         long long cp2 = (H2[i] - (H2[i - l] * P2[l]) % MOD2 + MOD2) % MOD2;
         if(now == cp && now2 == cp2 && !f[i]) {

            for(int j = i - l + 1; j <= i; j++) {
               f[j] = 1;
            }
            pp.push_back(make_pair(i, s));
            break;
         }
      }
   }
   sort(pp.begin(), pp.end());
   for(auto it : pp) {
      cout << it.second << ' ';
   }
   return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 100005;

vector< pair < int, int> > pp;
bool f[N];
char BIG[N];

long long H[N], P[N], H2[N], P2[N];
int B = 277;
int B2 = 10007;
int MOD = 1000000007;
int MOD2 = INT_MAX;

struct LOL {
   char str[1010];
   int len;

   bool operator <(const LOL& st) const {
      return len > st.len;
   }
};

LOL ar[N];

int main() {
   //freopen("input.txt", "r", stdin);
   int L, n;
   scanf("%d %s", &L, BIG);
   scanf("%d", &n);
   P[0] = P2[0] = 1;
   reverse(BIG, BIG + L);
   for(int i = 1; i <= L; i++) {
      H[i] = (H[i - 1] * B + BIG[i - 1]) % MOD;
      P[i] = (P[i - 1] * B) % MOD;
     // H2[i] = (H2[i - 1] * B2 + BIG[i - 1]) % MOD2;
     // P2[i] = (P2[i - 1] * B2) % MOD2;
   }

   for(int i = 0; i < n; i++) {
      scanf("%s", ar[i].str);
      ar[i].len = strlen(ar[i].str);
   }

   sort(ar, ar + n);

   for(int m = 0; m < n; m++) {
      long long now = 0, now2 = 0;
      int l = ar[m].len;
      for(int i = 0; i < l; i++) {
         char ch = ar[m].str[i] < 'a'? ar[m].str[i] + 32 : ar[m].str[i];
         now = (now * B + ch) % MOD;
         //now2 = (now2 * B2 + ch) % MOD2;
      }

      for(int i = ar[m].len; i <= L; i++) {
         long long cp = (H[i] - (H[i - l] * P[l]) % MOD + MOD) % MOD;
         //long long cp2 = (H2[i] - (H2[i - l] * P2[l]) % MOD2 + MOD2) % MOD2;
         if(now == cp /* && now2 == cp2 */&& !f[i] && !f[i - l + 1]) {
            for(int j = i - l + 1; j <= i; j++) {
               f[j] = 1;
            }
            pp.push_back(make_pair(-i, m));
         }
      }
   }
   sort(pp.begin(), pp.end());
   int cnt = 0;
   for(int i = 0, k = pp.size(); i < k; i++) {
      printf("%s ", ar[pp[i].second].str);
   }
   return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 100005;

vector< pair < int, int> > pp;
bool f[N];
char BIG[N];

long long H[N], P[N], H2[N], P2[N];
int B = 277;
int B2 = 10007;
int MOD = 1000000007;
int MOD2 = INT_MAX;

struct LOL {
   char str[1010];
   int len;

   bool operator <(const LOL& st) const {
      return len > st.len;
   }
};

LOL ar[N];

int main() {
   //freopen("input.txt", "r", stdin);
   int L, n;
   scanf("%d %s", &L, BIG);
   scanf("%d", &n);
   P[0] = P2[0] = 1;
   reverse(BIG, BIG + L);
   for(int i = 1; i <= L; i++) {
      H[i] = (H[i - 1] * B + BIG[i - 1]) % MOD;
      P[i] = (P[i - 1] * B) % MOD;
      H2[i] = (H2[i - 1] * B2 + BIG[i - 1]) % MOD2;
      P2[i] = (P2[i - 1] * B2) % MOD2;
   }

   for(int i = 0; i < n; i++) {
      scanf("%s", ar[i].str);
      ar[i].len = strlen(ar[i].str);
   }

   sort(ar, ar + n);

   for(int m = 0; m < n; m++) {
      long long now = 0, now2 = 0;
      int l = ar[m].len;
      for(int i = 0; i < l; i++) {
         char ch = ar[m].str[i] < 'a'? ar[m].str[i] + 32 : ar[m].str[i];
         now = (now * B + ch) % MOD;
         now2 = (now2 * B2 + ch) % MOD2;
      }

      for(int i = ar[m].len; i <= L; i++) {
         long long cp = (H[i] - (H[i - l] * P[l]) % MOD + MOD) % MOD;
         long long cp2 = (H2[i] - (H2[i - l] * P2[l]) % MOD2 + MOD2) % MOD2;
         if(now == cp && now2 == cp2 && !f[i] && !f[i - l + 1]) {
            for(int j = i - l + 1; j <= i; j++) {
               f[j] = 1;
            }
            pp.push_back(make_pair(-i, m));
         }
      }
   }
   sort(pp.begin(), pp.end());
   int cnt = 0;
   for(int i = 0, k = pp.size(); i < k; i++) {
      printf("%s ", ar[pp[i].second].str);
   }
   return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int MAX=1e4+9,B=137761,MOD=1e9+7,K=1e3+9;
int n,m,pw[MAX],dp[MAX];
map<int,int> c;
string s,t[MAX],ss;
vector<int> ans;
int main()
{
	ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	cin>>n>>s>>m,pw[0]=1;
	for (int i=1;i<MAX;i++) pw[i]=1LL*pw[i-1]*B%MOD;
	reverse(s.begin(),s.end());
	for (int j=0;j<m;j++)
	{
		cin>>ss;
		t[j]=ss;
		int hsh=0;
		for (int i=0;i<ss.size();i++)
		{
			char ch=ss[i];
			if (ch>='A' && ch<='Z') ch=ch-'A'+'a';
			hsh=(hsh+1LL*pw[i]*ch)%MOD;
		}
		c[hsh]=j;
	}
	dp[n]=true;
	for (int i=n-1;i>=0;i--)
	{
		int hsh=0;
		dp[i]=-1;
		for (int j=i;j<min(n,i+K);j++)
		{
			hsh=(hsh+1LL*s[j]*pw[j-i])%MOD;
			if (c.count(hsh) && (j+1==n || dp[j+1]!=-1)) {dp[i]=c[hsh];break;}
		}
	}
	int p=0;
	while (p!=n) ans.push_back(dp[p]),p+=t[dp[p]].size();
	for (int i=ans.size()-1;i>=0;i--) cout<<t[ans[i]]<<' ';
}


#include<bits/stdc++.h>
using namespace std;
const int MAX=1e5+9,B=137761,MOD=1e9+7,K=1e3+9;
int n,m,pw[MAX],dp[MAX];
map<int,int> c;
string s,t[MAX],ss;
vector<int> ans;
int main()
{
	ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	cin>>n>>s>>m,pw[0]=1;
	for (int i=1;i<MAX;i++) pw[i]=1LL*pw[i-1]*B%MOD;
	reverse(s.begin(),s.end());
	for (int j=0;j<m;j++)
	{
		cin>>ss;
		t[j]=ss;
		int hsh=0;
		for (int i=0;i<ss.size();i++)
		{
			char ch=ss[i];
			if (ch>='A' && ch<='Z') ch=ch-'A'+'a';
			hsh=(hsh+1LL*pw[i]*ch)%MOD;
		}
		c[hsh]=j;
	}
	dp[n]=true;
	for (int i=n-1;i>=0;i--)
	{
		int hsh=0;
		dp[i]=-1;
		for (int j=i;j<min(n,i+K);j++)
		{
			hsh=(hsh+1LL*s[j]*pw[j-i])%MOD;
			if (c.count(hsh) && (j+1==n || dp[j+1]!=-1)) {dp[i]=c[hsh];break;}
		}
	}
	int p=0;
	while (p!=n) ans.push_back(dp[p]),p+=t[dp[p]].size();
	for (int i=ans.size()-1;i>=0;i--) cout<<t[ans[i]]<<' ';
}


#include<bits/stdc++.h>
using namespace std;
const int MAX=1e4+9,B=1999,MOD=2e7+9,K=1e3+9;
int n,m,pw[MAX];
string dp[MAX];
string s,c[MOD],ss;
vector<string> ans;
int main()
{
	ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	cin>>n>>s>>m,pw[0]=1;
	for (int i=1;i<MAX;i++) pw[i]=1LL*pw[i-1]*B%MOD;
	reverse(s.begin(),s.end());
	while (m--)
	{
		cin>>ss;
		int hsh=0;
		for (int i=0;i<ss.size();i++)
		{
			char ch=ss[i];
			if (ch>='A' && ch<='Z') ch=ch-'A'+'a';
			hsh=(hsh+1LL*pw[i]*ch)%MOD;
		}
		c[hsh]=ss;
	}
	dp[n]=true;
	for (int i=n-1;i>=0;i--)
	{
		int hsh=0;
		for (int j=i;j<min(n,i+K);j++)
		{
			hsh=(hsh+1LL*s[j]*pw[j-i])%MOD;
			if (c[hsh].size() && (j+1==n || dp[j+1].size())) {dp[i]=c[hsh];break;}
		}
	}
	int p=0;
	while (p!=n) ans.push_back(dp[p]),p+=dp[p].size();
	for (int i=ans.size()-1;i>=0;i--) cout<<ans[i]<<' ';
}


#include<bits/stdc++.h>
using namespace std;
const int MAX=1e4+9,B=1999,MOD=1e9+7,K=1e3+9;
int n,m,pw[MAX],dp[MAX];
map<int,int> c;
string s,t[MAX],ss;
vector<int> ans;
int main()
{
	ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	cin>>n>>s>>m,pw[0]=1;
	for (int i=1;i<MAX;i++) pw[i]=1LL*pw[i-1]*B%MOD;
	reverse(s.begin(),s.end());
	for (int j=0;j<m;j++)
	{
		cin>>ss;
		t[j]=ss;
		int hsh=0;
		for (int i=0;i<ss.size();i++)
		{
			char ch=ss[i];
			if (ch>='A' && ch<='Z') ch=ch-'A'+'a';
			hsh=(hsh+1LL*pw[i]*ch)%MOD;
		}
		c[hsh]=j;
	}
	dp[n]=true;
	for (int i=n-1;i>=0;i--)
	{
		int hsh=0;
		dp[i]=-1;
		for (int j=i;j<min(n,i+K);j++)
		{
			hsh=(hsh+1LL*s[j]*pw[j-i])%MOD;
			if (c.count(hsh) && (j+1==n || dp[j+1]!=-1)) {dp[i]=c[hsh];break;}
		}
	}
	int p=0;
	while (p!=n) ans.push_back(dp[p]),p+=t[dp[p]].size();
	for (int i=ans.size()-1;i>=0;i--) cout<<t[ans[i]]<<' ';
}


#include<bits/stdc++.h>
using namespace std;
const int MAX=1e4+9,B=1999,MOD=1e7+9,K=1e3+9;
int n,m,pw[MAX];
string dp[MAX];
string s,c[MOD],ss;
vector<string> ans;
int main()
{
	ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	cin>>n>>s>>m,pw[0]=1;
	for (int i=1;i<MAX;i++) pw[i]=1LL*pw[i-1]*B%MOD;
	reverse(s.begin(),s.end());
	while (m--)
	{
		cin>>ss;
		int hsh=0;
		for (int i=0;i<ss.size();i++)
		{
			char ch=ss[i];
			if (ch>='A' && ch<='Z') ch=ch-'A'+'a';
			hsh=(hsh+1LL*pw[i]*ch)%MOD;
		}
		c[hsh]=ss;
	}
	dp[n]=true;
	for (int i=n-1;i>=0;i--)
	{
		int hsh=0;
		for (int j=i;j<min(n,i+K);j++)
		{
			hsh=(hsh+1LL*s[j]*pw[j-i])%MOD;
			if (c[hsh].size() && (j+1==n || dp[j+1].size())) {dp[i]=c[hsh];break;}
		}
	}
	int p=0;
	while (p!=n) ans.push_back(dp[p]),p+=dp[p].size();
	for (int i=ans.size()-1;i>=0;i--) cout<<ans[i]<<' ';
}


#include<bits/stdc++.h>
using namespace std;
const int MAX=1e4+9,B=1999,MOD=5e6+9,K=1e3+9;
int n,m,pw[MAX],dp[MAX],c[MOD];
string s,t[MAX],ss;
vector<int> ans;
int main()
{
	ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	memset(c,-1,sizeof c);
	cin>>n>>s>>m,pw[0]=1;
	for (int i=1;i<MAX;i++) pw[i]=1LL*pw[i-1]*B%MOD;
	reverse(s.begin(),s.end());
	for (int j=0;j<m;j++)
	{
		cin>>ss;
		t[j]=ss;
		int hsh=0;
		for (int i=0;i<ss.size();i++)
		{
			char ch=ss[i];
			if (ch>='A' && ch<='Z') ch=ch-'A'+'a';
			hsh=(hsh+1LL*pw[i]*ch)%MOD;
		}
		c[hsh]=j;
	}
	dp[n]=true;
	for (int i=n-1;i>=0;i--)
	{
		int hsh=0;
		dp[i]=-1;
		for (int j=i;j<min(n,i+K);j++)
		{
			hsh=(hsh+1LL*s[j]*pw[j-i])%MOD;
			if (c[hsh]!=-1 && (j+1==n || dp[j+1]!=-1)) {dp[i]=c[hsh];break;}
		}
	}
	int p=0;
	while (p!=n) ans.push_back(dp[p]),p+=t[dp[p]].size();
	for (int i=ans.size()-1;i>=0;i--) cout<<t[ans[i]]<<' ';
}


#include<bits/stdc++.h>
using namespace std;
const int MAX=1e6+9,B=1999,MOD=1e9+7;
int n,m,pw[MAX],dp[MAX];
map<int,int> c;
string s,t[MAX],ss;
vector<int> ans;
int main()
{
	ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	cin>>n>>s>>m,pw[0]=1;
	for (int i=1;i<MAX;i++) pw[i]=1LL*pw[i-1]*B%MOD;
	reverse(s.begin(),s.end());
	for (int j=0;j<m;j++)
	{
		cin>>ss;
		t[j]=ss;
		int hsh=0;
		for (int i=0;i<ss.size();i++)
		{
			char ch=ss[i];
			if (ch>='A' && ch<='Z') ch=ch-'A'+'a';
			hsh=(hsh+1LL*pw[i]*ch)%MOD;
		}
		c[hsh]=j;
	}
	dp[n]=0;
	for (int i=n-1;i>=0;i--)
	{
		int hsh=0;
		dp[i]=-1;
		for (int j=i;j<min(n,i+1009);j++)
		{
			hsh=(hsh+1LL*s[j]*pw[j-i])%MOD;
			if (c.count(hsh) && dp[j+1]!=-1) {dp[i]=c[hsh];break;}
		}
	}
	int p=0;
	while (p!=n) ans.push_back(dp[p]),p+=t[dp[p]].size();
	for (int i=ans.size()-1;i>=0;i--) if (i) cout<<t[ans[i]]<<' '; else cout<<t[ans[i]];
}


#include<bits/stdc++.h>
using namespace std;
const int MAX=1e4+9,B=1999,MOD=1e7+9,K=1e3+9;
int n,m,pw[MAX],dp[MAX],c[MOD];
string s,t[MAX],ss;
vector<int> ans;
int main()
{
	ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	memset(c,-1,sizeof c);
	cin>>n>>s>>m,pw[0]=1;
	for (int i=1;i<MAX;i++) pw[i]=1LL*pw[i-1]*B%MOD;
	reverse(s.begin(),s.end());
	for (int j=0;j<m;j++)
	{
		cin>>ss;
		t[j]=ss;
		int hsh=0;
		for (int i=0;i<ss.size();i++)
		{
			char ch=ss[i];
			if (ch>='A' && ch<='Z') ch=ch-'A'+'a';
			hsh=(hsh+1LL*pw[i]*ch)%MOD;
		}
		c[hsh]=j;
	}
	dp[n]=true;
	for (int i=n-1;i>=0;i--)
	{
		int hsh=0;
		dp[i]=-1;
		for (int j=i;j<min(n,i+K);j++)
		{
			hsh=(hsh+1LL*s[j]*pw[j-i])%MOD;
			if (c[hsh]!=-1 && (j+1==n || dp[j+1]!=-1)) {dp[i]=c[hsh];break;}
		}
	}
	int p=0;
	while (p!=n) ans.push_back(dp[p]),p+=t[dp[p]].size();
	for (int i=ans.size()-1;i>=0;i--) cout<<t[ans[i]]<<' ';
}


#include<bits/stdc++.h>
using namespace std;
const int MAX=1e6+9,B=1999,MOD=1e9+7;
int n,m,pw[MAX],dp[MAX];
map<int,int> c;
string s,t[MAX],ss;
vector<int> ans;
int main()
{
	ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	cin>>n>>s>>m,pw[0]=1;
	for (int i=1;i<MAX;i++) pw[i]=1LL*pw[i-1]*B%MOD;
	reverse(s.begin(),s.end());
	for (int j=0;j<m;j++)
	{
		cin>>ss;
		t[j]=ss;
		int hsh=0;
		for (int i=0;i<ss.size();i++)
		{
			char ch=ss[i];
			if (ch>='A' && ch<='Z') ch=ch-'A'+'a';
			hsh=(hsh+1LL*pw[i]*ch)%MOD;
		}
		c[hsh]=j;
	}
	dp[n]=0;
	for (int i=n-1;i>=0;i--)
	{
		int hsh=0;
		dp[i]=-1;
		for (int j=i;j<min(n,i+K);j++)
		{
			hsh=(hsh+1LL*s[j]*pw[j-i])%MOD;
			if (c.count(hsh) && dp[j+1]!=-1) {dp[i]=c[hsh];break;}
		}
	}
	int p=0;
	while (p!=n) ans.push_back(dp[p]),p+=t[dp[p]].size();
	for (int i=ans.size()-1;i>=0;i--) if (i) cout<<t[ans[i]]<<' '; else cout<<t[ans[i]];
}


#include<bits/stdc++.h>
using namespace std;
const int MAX=1e4+9,B=1999,MOD=1e7+7,K=1e3+9;
int n,m,pw[MAX],dp[MAX],c[MOD];
string s,t[MAX],ss;
vector<int> ans;
int main()
{
	ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	memset(c,-1,sizeof c);
	cin>>n>>s>>m,pw[0]=1;
	for (int i=1;i<MAX;i++) pw[i]=1LL*pw[i-1]*B%MOD;
	reverse(s.begin(),s.end());
	for (int j=0;j<m;j++)
	{
		cin>>ss;
		t[j]=ss;
		int hsh=0;
		for (int i=0;i<ss.size();i++)
		{
			char ch=ss[i];
			if (ch>='A' && ch<='Z') ch=ch-'A'+'a';
			hsh=(hsh+1LL*pw[i]*ch)%MOD;
		}
		c[hsh]=j;
	}
	dp[n]=true;
	for (int i=n-1;i>=0;i--)
	{
		int hsh=0;
		dp[i]=-1;
		for (int j=i;j<min(n,i+K);j++)
		{
			hsh=(hsh+1LL*s[j]*pw[j-i])%MOD;
			if (c[hsh]!=-1 && (j+1==n || dp[j+1]!=-1)) {dp[i]=c[hsh];break;}
		}
	}
	int p=0;
	while (p!=n) ans.push_back(dp[p]),p+=t[dp[p]].size();
	for (int i=ans.size()-1;i>=0;i--) cout<<t[ans[i]]<<' ';
}


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

#define forn(i,n) for(int i=0;i<int(n);i++)
#define forsn(i,s,n) for(int i=(int)(s);i<(int)(n);i++)
#define dforsn(i,s,n) for(int i=(int)(n-1);i>=int(s);i--)
#define si(a) ((int)(a).size())
#define pb push_back
#define mp make_pair
#define endl '\n'
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef long long int tint;

const int MAXN = 10000 +10;
int n,m;
string s;

map <string,string> decod;
set <string> words[1001];
string suffix[MAXN];
//bool possible[MAXN];
vector <int> done;

void test(int a){

    int subsz;
    string cmp;
    int i = si(done)-1;
    while(done[i] > a && i >= 0){
        subsz = done[i]-a;
        //cerr << a << endl;
        cmp = s.substr(a,subsz);
        if(words[subsz].find(cmp)==words[subsz].end()){
            i--;
            continue;
        }
        if(done[i] == n)suffix[a] = decod[cmp] ;
        else suffix[a]= suffix[done[i]] + " " + decod[cmp] ;
        done.insert((lower_bound(done.begin(),done.end(),a)),a);
        return;
    }

    return;
}

int main() {
	ios_base::sync_with_stdio(false); cin.tie(0);
    //freopen("C.in","r",stdin);

    cin >> n >> s >> m;
    reverse(s.begin(),s.end());
    forn(i,m){
        string a;
        cin >> a;
        string ora =a; //original word
        forn(j,si(a)){
            if(a[j] <= 'Z')a[j]= ((a[j] - 'A')+'a');
        }
        //cerr << a << " " << ora << endl;
        words[si(a)].insert(a);
        decod[a]=ora;
        //cerr << i << endl;
    }
    done.pb(n);
    suffix[n]="";

    //test(26);
    dforsn(i,0,n){
        test(i);
    }

    cout << suffix[0] << endl;

	return 0;
}


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

#define forn(i,n) for(int i=0;i<int(n);i++)
#define forsn(i,s,n) for(int i=(int)(s);i<(int)(n);i++)
#define dforsn(i,s,n) for(int i=(int)(n-1);i>=int(s);i--)
#define si(a) ((int)(a).size())
#define pb push_back
#define mp make_pair
#define endl '\n'
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef long long int tint;

const int MAXN = 10000 +10;
int n,m;
string s;

vector <pair<string,string> > words[26];
string suffix[MAXN];
bool possible[MAXN];

void test(int a){
    int limit = n-a;
    int c = s[a] - 'a';
    forn(i,si(words[c])){
        string w = words[c][i].first;
        if(si(w)>limit)continue;
        string comp= s.substr(a,si(w));
        cerr <<w << " " << a << " " << comp << endl;
        if(w != comp)continue;
        if(si(w)== limit){
            possible[a]=true;
            suffix[a]=words[c][i].second;
            cerr << a << " "<<suffix[a] << endl;
            return;
        }
        if(possible[a+si(w)]){
            possible[a]=true;
            suffix[a]= suffix[a+si(w)] + " " + words[c][i].second;
            cerr << a << " " << suffix[a] << endl;
            return;
        }
    }
    return;
}

int main() {
	ios_base::sync_with_stdio(false); cin.tie(0);
    freopen("C.in","r",stdin);

    cin >> n >> s >> m;
    reverse(s.begin(),s.end());
    forn(i,m){
        string a;
        cin >> a;
        string ora =a;
        forn(j,si(a)){
            if(a[j] <= 'Z')a[j]= ((a[j] - 'A')+'a');
        }
        //cerr << a << " " << ora << endl;
        words[a[0]-'a'].pb(mp(a,ora));
        //cerr << i << endl;
    }
    forn(i,n)possible[i]=false;

    //test(26);
    dforsn(i,0,n){
        test(i);
    }

    cout << suffix[0] << endl;

	return 0;
}


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

#define forn(i,n) for(int i=0;i<int(n);i++)
#define forsn(i,s,n) for(int i=(int)(s);i<(int)(n);i++)
#define dforsn(i,s,n) for(int i=(int)(n-1);i>=int(s);i--)
#define si(a) ((int)(a).size())
#define pb push_back
#define mp make_pair
#define endl '\n'
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef long long int tint;

const int MAXN = 10000 +10, MAXS=1001;
int n,m;
string s;

map <string,string> decod;
set <string> words[MAXS];
string suffix[MAXN];
//bool possible[MAXN];
vector <int> done;

void test(int a){
    //cerr << a << endl;
    int subsz;
    string cmp;
    int i = si(done)-1;
    //cerr << "a" << endl;
    while(done[i] > a && i >= 0){
        //cerr << "b" << endl;
        subsz = done[i]-a;
        if(subsz >= MAXS){
            i--;
            continue;
        }
        //cerr << a << endl;
        cmp = s.substr(a,subsz);
        if(words[subsz].find(cmp)==words[subsz].end()){
            i--;
            continue;
        }

        if(done[i] == n)suffix[a] = decod[cmp] ;
        else suffix[a]= suffix[done[i]] + " " + decod[cmp] ;
        done.insert((lower_bound(done.begin(),done.end(),a)),a);
        return;
    }

    return;
}

int main() {
	ios_base::sync_with_stdio(false); cin.tie(0);
    freopen("C.in","r",stdin);

    cin >> n;
    cin >> s;

    cin >> m;
    reverse(s.begin(),s.end());
    //cerr << endl << si(s) << " " << s <<endl;
    forn(i,m){
        string a;
        cin >> a;
        string ora =a; //original word
        forn(j,si(a)){
            if(a[j] <= 'Z')a[j]= ((a[j] - 'A')+'a');
        }
        //cerr << a << " " << ora << endl;
        words[si(a)].insert(a);
        decod[a]=ora;
        //cerr << i << endl;
    }

    done.pb(n);
    suffix[n]="";

    dforsn(i,0,n){
        test(i);
        //cerr << i << endl;
    }

    cout <<  suffix[0] << endl;

	return 0;
}


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

#define forn(i,n) for(int i=0;i<int(n);i++)
#define forsn(i,s,n) for(int i=(int)(s);i<(int)(n);i++)
#define dforsn(i,s,n) for(int i=(int)(n-1);i>=int(s);i--)
#define si(a) ((int)(a).size())
#define pb push_back
#define mp make_pair
#define endl '\n'
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef long long int tint;

const int MAXN = 10000 +10;
int n,m;
string s;

map <string,string> decod;
set <string> words[1001];
string suffix[MAXN];
//bool possible[MAXN];
vector <int> done;

void test(int a){

    int subsz;
    string cmp;
    int i = si(done)-1;
    while(done[i] > a && i >= 0){
        subsz = done[i]-a;
        //cerr << a << endl;
        cmp = s.substr(a,subsz);
        if(words[subsz].find(cmp)==words[subsz].end()){
            i--;
            continue;
        }
        if(done[i] == n)suffix[a] = decod[cmp] ;
        else suffix[a]= suffix[done[i]] + " " + decod[cmp] ;
        done.insert((lower_bound(done.begin(),done.end(),a)),a);
        return;
    }

    return;
}

int main() {
	ios_base::sync_with_stdio(false); cin.tie(0);
    //freopen("C.in","r",stdin);

    cin >> n;
    cin >> s;
    cin >> m;
    reverse(s.begin(),s.end());
    forn(i,m){
        string a;
        cin >> a;
        string ora =a; //original word
        forn(j,si(a)){
            if(a[j] <= 'Z')a[j]= ((a[j] - 'A')+'a');
        }
        //cerr << a << " " << ora << endl;
        words[si(a)].insert(a);
        decod[a]=ora;
        //cerr << i << endl;
    }
    done.pb(n);
    suffix[n]="";

    //test(26);
    dforsn(i,0,n){
        test(i);
    }

    cout << suffix[0] << endl;

	return 0;
}


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

#define forn(i,n) for(int i=0;i<int(n);i++)
#define forsn(i,s,n) for(int i=(int)(s);i<(int)(n);i++)
#define dforsn(i,s,n) for(int i=(int)(n-1);i>=int(s);i--)
#define si(a) ((int)(a).size())
#define pb push_back
#define mp make_pair
#define endl '\n'
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef long long int tint;

vector <string> sl;

bool comp(string a , string b){
    return a+b < b+a;

}

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

	int n;
	cin >> n;

	forn(i,n){
        string s;
        cin >> s;
        sl.pb(s);
	}

    //cerr << comp(sl[0],sl[1]) << endl;
    //cerr << comp(sl[1],sl[0]) << endl;

    sort(sl.begin(),sl.end(),comp);

    forn(i,n)cout << sl[i];
    cout <<endl;

	return 0;
}


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

#define forn(i,n) for(int i=0;i<int(n);i++)
#define forsn(i,s,n) for(int i=(int)(s);i<(int)(n);i++)
#define dforsn(i,s,n) for(int i=(int)(n-1);i>=int(s);i--)
#define si(a) ((int)(a).size())
#define pb push_back
#define mp make_pair
#define endl '\n'
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef long long int tint;

const int MAXN = 10000 +10;
int n,m;
string s;

map <string,string> decod;
set <string> words[1001];
string suffix[MAXN];
//bool possible[MAXN];
vector <int> done;

void test(int a){

    int subsz;
    string cmp;
    int i = si(done)-1;
    while(done[i] > a && i >= 0){
        subsz = done[i]-a;
        //cerr << a << endl;
        cmp = s.substr(a,subsz);
        if(words[subsz].find(cmp)==words[subsz].end()){
            i--;
            continue;
        }
        if(done[i] == n)suffix[a] = decod[cmp] ;
        else suffix[a]= suffix[done[i]] + " " + decod[cmp] ;
        done.insert((lower_bound(done.begin(),done.end(),a)),a);
        return;
    }

    return;
}

int main() {
	ios_base::sync_with_stdio(false); cin.tie(0);
    //freopen("C.in","r",stdin);

    cin >> n >> s >> m;
    if(n> 1000)cout << m <<endl;
    reverse(s.begin(),s.end());
    forn(i,m){
        string a;
        cin >> a;
        string ora =a; //original word
        forn(j,si(a)){
            if(a[j] <= 'Z')a[j]= ((a[j] - 'A')+'a');
        }
        //cerr << a << " " << ora << endl;
        words[si(a)].insert(a);
        decod[a]=ora;
        //cerr << i << endl;
    }
    done.pb(n);
    suffix[n]="";

    //test(26);
    dforsn(i,0,n){
        test(i);
    }

    cout << suffix[0] << endl;

	return 0;
}


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

#define forn(i,n) for(int i=0;i<int(n);i++)
#define forsn(i,s,n) for(int i=(int)(s);i<(int)(n);i++)
#define dforsn(i,s,n) for(int i=(int)(n-1);i>=int(s);i--)
#define si(a) ((int)(a).size())
#define pb push_back
#define mp make_pair
#define endl '\n'
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef long long int tint;

const int MAXN = 10000 +10;
int n,m;
string s;

vector <pair<string,string> > words[26];
string suffix[MAXN];
bool possible[MAXN];

void test(int a){
    int limit = n-a;
    int c = s[a] - 'a';
    forn(i,si(words[c])){
        string w = words[c][i].first;
        if(si(w)>limit)continue;
        string comp= s.substr(a,si(w));
        cerr <<w << " " << a << " " << comp << endl;
        if(w != comp)continue;
        if(si(w)== limit){
            possible[a]=true;
            suffix[a]=words[c][i].second;
            cerr << a << " "<<suffix[a] << endl;
            return;
        }
        if(possible[a+si(w)]){
            possible[a]=true;
            suffix[a]= suffix[a+si(w)] + " " + words[c][i].second;
            cerr << a << " " << suffix[a] << endl;
            return;
        }
    }
    return;
}

int main() {
	ios_base::sync_with_stdio(false); cin.tie(0);
    //freopen("C.in","r",stdin);

    cin >> n >> s >> m;
    reverse(s.begin(),s.end());
    forn(i,m){
        string a;
        cin >> a;
        string ora =a;
        forn(j,si(a)){
            if(a[j] <= 'Z')a[j]= ((a[j] - 'A')+'a');
        }
        //cerr << a << " " << ora << endl;
        words[a[0]-'a'].pb(mp(a,ora));
        //cerr << i << endl;
    }
    forn(i,n)possible[i]=false;

    //test(26);
    dforsn(i,0,n){
        test(i);
    }

    cout << suffix[0] << endl;

	return 0;
}


#include<stdio.h>
#include<algorithm>
#include<map>
using namespace std;
const int n=131072;
int T,X[n*3],ind=0,jnd=0,M[n*2];
long long dp[n*4];
struct node{
	int a,b;
	long long k;
	bool operator <(const node& B){
		return b==B.b?a<B.a:b<B.b;
	}
	void read(){
		a=lower_bound(M,M+ind,X[jnd++])-M;
		b=lower_bound(M,M+ind,X[jnd++])-M;
		k=X[jnd++];
	}
}p[n];
long long query(int L,int R,int l,int r,int rt){
	if(L<=l&&R>=r)return dp[rt];
	int mid=(l+r)>>1;
	return max( mid>=L?query(L,R,l,mid,rt<<1):0 , mid<R?query(L,R,mid+1,r,rt<<1|1):0);
}
long long getmax(int t){
//	return query(t,n*2,1,n*2,1);
	t+=n*2-1;
	long long res=dp[t];
	do{
		if(!(t&1))if(res<dp[t|1])res=dp[t|1];
	}while(t>>=1);
	return res;
}
void verify(int x,long long y){
	int t=n*2+x-1;
	dp[t]=y;
	while(t>>=1){
		dp[t]=max(dp[t<<1],dp[t<<1|1]);
	}
}
int main(){
	scanf("%d",&T);
	for(int i=0;i<3*T;i++){
		scanf("%d",X+i);
		if(i%3!=2)M[ind++]=X[i];
	}
	sort(M,M+ind);
	for(int i=0;i<T;i++)
		p[i].read();
	sort(p,p+T);
	long long temp;
	for(int i=0;i<T;i++){
		temp=getmax(p[i].a+1)+p[i].k;
		if(dp[p[i].b+2*n-1]<temp)verify(p[i].b,temp);
	}
	printf("%I64d",dp[1]);
	return 0;
}


#include<stdio.h>
#include<algorithm>
#include<map>
using namespace std;
const int n=131072;
int T,X[n*3],ind=0,jnd=0,M[n*2];
long long dp[n*4];
struct node{
	int a,b;
	long long k;
	bool operator <(const node& B){
		return b==B.b?a<B.a:b<B.b;
	}
	void read(){
		a=lower_bound(M,M+ind,X[jnd++])-M;
		b=lower_bound(M,M+ind,X[jnd++])-M;
		k=X[jnd++];
	}
}p[n];
long long query(int L,int R,int l,int r,int rt){
	if(L<=l&&R>=r)return dp[rt];
	int mid=(l+r)>>1;
	return max( mid>=L?query(L,R,l,mid,rt<<1):0 , mid<R?query(L,R,mid+1,r,rt<<1|1):0);
}
long long getmax(int t){
//	return query(t,n*2,1,n*2,1);
	t+=n*2-1;
	int res=-1;
	do{
		if(t&1)if(res<dp[t])res=dp[t];
	}while(t>>=1);
	return res;
}
void verify(int x,long long y){
	int t=n*2+x-1;
	dp[t]=y;
	while(t>>=1){
		dp[t]=max(dp[t<<1],dp[t<<1|1]);
	}
}
int main(){
	scanf("%d",&T);
	for(int i=0;i<3*T;i++){
		scanf("%d",X+i);
		if(i%3!=2)M[ind++]=X[i];
	}
	sort(M,M+ind);
	for(int i=0;i<T;i++)
		p[i].read();
	sort(p,p+T);
	long long temp;
	for(int i=0;i<T;i++){
		temp=getmax(p[i].a+1)+p[i].k;
		if(dp[p[i].b+2*n-1]<temp)verify(p[i].b,temp);
	}
	printf("%I64d",getmax(0));
	return 0;
}


#include<stdio.h>
#include<algorithm>
#include<map>
using namespace std;
const int n=131072;
int T,X[n*3],ind=0,jnd=0,M[n*2];
long long dp[n*4];
struct node{
	int a,b;
	long long k;
	bool operator <(const node& B){
		return b==B.b?a<B.a:b<B.b;
	}
	void read(){
		a=lower_bound(M,M+ind,X[jnd++])-M;
		b=lower_bound(M,M+ind,X[jnd++])-M;
		k=X[jnd++];
	}
}p[n];
long long query(int L,int R,int l,int r,int rt){
	if(L<=l&&R>=r)return dp[rt];
	int mid=(l+r)>>1;
	return max( mid>=L?query(L,R,l,mid,rt<<1):0 , mid<R?query(L,R,mid+1,r,rt<<1|1):0);
}
long long getmax(int t){
//	return query(t,n*2,1,n*2,1);
	t+=n*2-1;
	int res=-1;
	do{
		if(!(t&1))if(res<dp[t|1])res=dp[t|1];
	}while(t>>=1);
	return res;
}
void verify(int x,long long y){
	int t=n*2+x-1;
	dp[t]=y;
	while(t>>=1){
		dp[t]=max(dp[t<<1],dp[t<<1|1]);
	}
}
int main(){
	scanf("%d",&T);
	for(int i=0;i<3*T;i++){
		scanf("%d",X+i);
		if(i%3!=2)M[ind++]=X[i];
	}
	sort(M,M+ind);
	for(int i=0;i<T;i++)
		p[i].read();
	sort(p,p+T);
	long long temp;
	for(int i=0;i<T;i++){
		temp=getmax(p[i].a+1)+p[i].k;
		if(dp[p[i].b+2*n-1]<temp)verify(p[i].b,temp);
	}
	printf("%I64d",dp[1]);
	return 0;
}


#include<stdio.h>
#include<algorithm>
#include<map>
using namespace std;
const int n=131072;
int T,X[n*3],ind=0,jnd=0,M[n*2];
long long dp[n*4];
struct node{
	int a,b;
	long long k;
	bool operator <(const node& B){
		return b==B.b?a<B.a:b<B.b;
	}
	void read(){
		a=lower_bound(M,M+ind,X[jnd++])-M;
		b=lower_bound(M,M+ind,X[jnd++])-M;
		k=X[jnd++];
	}
}p[n];
long long query(int L,int R,int l,int r,int rt){
	if(L<=l&&R>=r)return dp[rt];
	int mid=(l+r)>>1;
	return max( mid>=L?query(L,R,l,mid,rt<<1):0 , mid<R?query(L,R,mid+1,r,rt<<1|1):0);
}
long long getmax(int t){
//	return query(t,n*2,1,n*2,1);
	t+=n*2-1;
	int res=-1;
	do{
		if(!(t&1))if(res<dp[t|1])res=dp[t|1];
	}while(t>>=1);
	return res;
}
void verify(int x,long long y){
	int t=n*2+x-1;
	dp[t]=y;
	while(t>>=1){
		dp[t]=max(dp[t<<1],dp[t<<1|1]);
	}
}
int main(){
	scanf("%d",&T);
	for(int i=0;i<3*T;i++){
		scanf("%d",X+i);
		if(i%3!=2)M[ind++]=X[i];
	}
	sort(M,M+ind);
	for(int i=0;i<T;i++)
		p[i].read();
	sort(p,p+T);
	long long temp;
	for(int i=0;i<T;i++){
		temp=getmax(p[i].a)+p[i].k;
		if(dp[p[i].b+2*n-1]<temp)verify(p[i].b,temp);
	}
	printf("%I64d",dp[1]);
	return 0;
}


#include<stdio.h>
#include<algorithm>
#include<map>
using namespace std;
const int n=131072;
int T,X[n*3],ind=0,jnd=0,M[n*2];
long long dp[n*4];
struct node{
	int a,b;
	long long k;
	bool operator <(const node& B){
		return b==B.b?a<B.a:b<B.b;
	}
	void read(){
		a=lower_bound(M,M+ind,X[jnd++])-M;
		b=lower_bound(M,M+ind,X[jnd++])-M;
		k=X[jnd++];
	}
}p[n];
long long query(int L,int R,int l,int r,int rt){
	if(L<=l&&R>=r)return dp[rt];
	int mid=(l+r)>>1;
	return max( mid>=L?query(L,R,l,mid,rt<<1):0 , mid<R?query(L,R,mid+1,r,rt<<1|1):0);
}
long long getmax(int t){
//	return query(t,n*2,1,n*2,1);
	t+=n*2-1;
	int res=dp[t];
	do{
		if(!(t&1))if(res<dp[t|1])res=dp[t|1];
	}while(t>>=1);
	return res;
}
void verify(int x,long long y){
	int t=n*2+x-1;
	dp[t]=y;
	while(t>>=1){
		dp[t]=max(dp[t<<1],dp[t<<1|1]);
	}
}
int main(){
	scanf("%d",&T);
	for(int i=0;i<3*T;i++){
		scanf("%d",X+i);
		if(i%3!=2)M[ind++]=X[i];
	}
	sort(M,M+ind);
	for(int i=0;i<T;i++)
		p[i].read();
	sort(p,p+T);
	long long temp;
	for(int i=0;i<T;i++){
		temp=getmax(p[i].a+1)+p[i].k;
		if(dp[p[i].b+2*n-1]<temp)verify(p[i].b,temp);
	}
	printf("%I64d",dp[1]);
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <sstream>
#include <set>
#include <map>
#include <vector>
#include <list>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <string>
#include <queue>
#include <bitset>		//UWAGA - w czasie kompilacji musi byc znany rozmiar wektora - nie mozna go zmienic
#include <cassert>
#include <iomanip>		//do setprecision
#include <ctime>
#include <complex>
using namespace std;

#define FOR(i,b,e) for(int i=(b);i<(e);++i)
#define FORQ(i,b,e) for(int i=(b);i<=(e);++i)
#define FORD(i,b,e) for(int i=(b)-1;i>=(e);--i)
#define REP(x, n) for(int x = 0; x < (n); ++x)

#define ST first
#define ND second
#define PB push_back
#define MP make_pair
#define LL long long
#define ULL unsigned LL
#define LD long double

const double pi = 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342;

const int MR = 1e5+10;

pair < pair < int, int>, int > t[MR];

int main()
{
	int n;
	scanf("%d", &n);
	REP(i,n)
	{
		scanf("%d%d%d", &t[i].first.second, &t[i].first.first, &t[i].second);
		t[i].first.first = -t[i].first.first;
	}

	sort(t,t+n);
	REP(i,n)
		t[i].first.first = -t[i].first.first;

	map<int,LL> M;

	REP(i,n)
	{
		map<int,LL> :: iterator it = M.lower_bound(t[i].first.first);
		if(it != M.begin())
		{
			it--;
			LL v = t[i].second+it->second;
			M[t[i].first.second] = max(M[t[i].first.second], v);
			it = M.find(t[i].first.second);
			if(it != M.begin())
			{
				it--;
				if(it->second >= v)
					M.erase(t[i].first.second);
			}
			while(true)
			{
				it = M.find(t[i].first.second);
				it++;
				if(it == M.end())
					break;
				if(it->second <= v)
					M.erase(it);
				else
					break;
			}
		}
		else
		{
			M[t[i].first.second] = t[i].second;
			while(true)
			{
				it = M.find(t[i].first.second);
				it++;
				if(it == M.end())
					break;
				if(it->second <= t[i].second)
					M.erase(it);
				else
					break;
			}
		}
	}

	printf("%lld\n", (--M.end())->second);

	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <sstream>
#include <set>
#include <map>
#include <vector>
#include <list>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <string>
#include <queue>
#include <bitset>		//UWAGA - w czasie kompilacji musi byc znany rozmiar wektora - nie mozna go zmienic
#include <cassert>
#include <iomanip>		//do setprecision
#include <ctime>
#include <complex>
using namespace std;

#define FOR(i,b,e) for(int i=(b);i<(e);++i)
#define FORQ(i,b,e) for(int i=(b);i<=(e);++i)
#define FORD(i,b,e) for(int i=(b)-1;i>=(e);--i)
#define REP(x, n) for(int x = 0; x < (n); ++x)

#define ST first
#define ND second
#define PB push_back
#define MP make_pair
#define LL long long
#define ULL unsigned LL
#define LD long double

const double pi = 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342;

const int MR = 1e5+10;

pair < pair < int, int>, int > t[MR];

int main()
{
	int n;
	scanf("%d", &n);
	REP(i,n)
	{
		scanf("%d%d%d", &t[i].first.second, &t[i].first.first, &t[i].second);
		t[i].first.first = -t[i].first.first;
		t[i].first.second = -t[i].first.second;
	}

	sort(t,t+n);
	REP(i,n)
	{
		t[i].first.first = -t[i].first.first;
		t[i].first.second = -t[i].first.second;
	}

	map<int,LL> M;

	REP(i,n)
	{
		map<int,LL> :: iterator it = M.lower_bound(t[i].first.first);
		if(it != M.begin())
		{
			it--;
			LL v = t[i].second+it->second;
			v = max(M[t[i].first.second], v);
			M[t[i].first.second] = v;
			it = M.find(t[i].first.second);
			if(it != M.begin())
			{
				it--;
				if(it->second >= v)
					M.erase(t[i].first.second);
			}
			while(true)
			{
				it = M.find(t[i].first.second);
				it++;
				if(it == M.end())
					break;
				if(it->second <= v)
					M.erase(it);
				else
					break;
			}
		}
		else
		{
			M[t[i].first.second] = t[i].second;
			while(true)
			{
				it = M.find(t[i].first.second);
				it++;
				if(it == M.end())
					break;
				if(it->second <= t[i].second)
					M.erase(it);
				else
					break;
			}
		}
	}

	printf("%lld\n", (--M.end())->second);

	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <sstream>
#include <set>
#include <map>
#include <vector>
#include <list>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <string>
#include <queue>
#include <bitset>		//UWAGA - w czasie kompilacji musi byc znany rozmiar wektora - nie mozna go zmienic
#include <cassert>
#include <iomanip>		//do setprecision
#include <ctime>
#include <complex>
using namespace std;

#define FOR(i,b,e) for(int i=(b);i<(e);++i)
#define FORQ(i,b,e) for(int i=(b);i<=(e);++i)
#define FORD(i,b,e) for(int i=(b)-1;i>=(e);--i)
#define REP(x, n) for(int x = 0; x < (n); ++x)

#define ST first
#define ND second
#define PB push_back
#define MP make_pair
#define LL long long
#define ULL unsigned LL
#define LD long double

const double pi = 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342;

const int MR = 1e5+10;

pair < pair < int, int>, int > t[MR];

int main()
{
	int n;
	scanf("%d", &n);
	REP(i,n)
	{
		scanf("%d%d%d", &t[i].first.second, &t[i].first.first, &t[i].second);
		t[i].first.first = -t[i].first.first;
	}

	sort(t,t+n);
	REP(i,n)
		t[i].first.first = -t[i].first.first;

	map<int,LL> M;

	REP(i,n)
	{
		map<int,LL> :: iterator it = M.lower_bound(t[i].first.first);
		if(it != M.begin())
		{
			it--;
			LL v = t[i].second+it->second;
			v = max(M[t[i].first.second], v);
			M[t[i].first.second] = v;
			it = M.find(t[i].first.second);
			if(it != M.begin())
			{
				it--;
				if(it->second >= v)
					M.erase(t[i].first.second);
			}
			while(true)
			{
				it = M.find(t[i].first.second);
				it++;
				if(it == M.end())
					break;
				if(it->second <= v)
					M.erase(it);
				else
					break;
			}
		}
		else
		{
			M[t[i].first.second] = t[i].second;
			while(true)
			{
				it = M.find(t[i].first.second);
				it++;
				if(it == M.end())
					break;
				if(it->second <= t[i].second)
					M.erase(it);
				else
					break;
			}
		}
	}

	printf("%lld\n", (--M.end())->second);

	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <sstream>
#include <set>
#include <map>
#include <vector>
#include <list>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <string>
#include <queue>
#include <bitset>		//UWAGA - w czasie kompilacji musi byc znany rozmiar wektora - nie mozna go zmienic
#include <cassert>
#include <iomanip>		//do setprecision
#include <ctime>
#include <complex>
using namespace std;

#define FOR(i,b,e) for(int i=(b);i<(e);++i)
#define FORQ(i,b,e) for(int i=(b);i<=(e);++i)
#define FORD(i,b,e) for(int i=(b)-1;i>=(e);--i)
#define REP(x, n) for(int x = 0; x < (n); ++x)

#define ST first
#define ND second
#define PB push_back
#define MP make_pair
#define LL long long
#define ULL unsigned LL
#define LD long double

const double pi = 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342;

const int MR = 1e5+10;

pair < pair < int, int>, int > t[MR];

int main()
{
	int n;
	scanf("%d", &n);
	REP(i,n)
	{
		scanf("%d%d%d", &t[i].first.second, &t[i].first.first, &t[i].second);
		t[i].first.first = -t[i].first.first;
	}

	sort(t,t+n);
	REP(i,n)
		t[i].first.first = -t[i].first.first;

	map<int,LL> M;

	REP(i,n)
	{
		map<int,LL> :: iterator it = M.lower_bound(t[i].first.first);
		if(it != M.begin())
		{
			it--;
			LL v = t[i].second+it->second;
			M[t[i].first.second] = max(M[t[i].first.second], v);
			it = M.find(t[i].first.second);
			if(it != M.begin())
			{
				it--;
				if(it->second >= v)
					M.erase(t[i].first.second);
			}
			while(true)
			{
				it = M.find(t[i].first.second);
				it++;
				if(it == M.end())
					break;
				if(it->second <= v)
					M.erase(it);
				else
					break;
			}
		}
		else
		{
			M[t[i].first.second] = max((LL)t[i].second, M[t[i].first.second]);
			while(true)
			{
				it = M.find(t[i].first.second);
				it++;
				if(it == M.end())
					break;
				if(it->second <= t[i].second)
					M.erase(it);
				else
					break;
			}
		}
	}

	printf("%lld\n", (--M.end())->second);

	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <sstream>
#include <set>
#include <map>
#include <vector>
#include <list>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <string>
#include <queue>
#include <bitset>		//UWAGA - w czasie kompilacji musi byc znany rozmiar wektora - nie mozna go zmienic
#include <cassert>
#include <iomanip>		//do setprecision
#include <ctime>
#include <complex>
using namespace std;

#define FOR(i,b,e) for(int i=(b);i<(e);++i)
#define FORQ(i,b,e) for(int i=(b);i<=(e);++i)
#define FORD(i,b,e) for(int i=(b)-1;i>=(e);--i)
#define REP(x, n) for(int x = 0; x < (n); ++x)

#define ST first
#define ND second
#define PB push_back
#define MP make_pair
#define LL long long
#define ULL unsigned LL
#define LD long double

const double pi = 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342;

const int MR = 1e5+10;

pair < pair < int, int>, int > t[MR];

int main()
{
	int n;
	scanf("%d", &n);
	REP(i,n)
	{
		scanf("%d%d%d", &t[i].first.second, &t[i].first.first, &t[i].second);
		t[i].first.first = -t[i].first.first;
	}

	sort(t,t+n);
	REP(i,n)
		t[i].first.first = -t[i].first.first;

	map<int,LL> M;

	REP(i,n)
	{
		map<int,LL> :: iterator it = M.lower_bound(t[i].first.first);
		if(it != M.begin())
		{
			it--;
			LL v = t[i].second+it->second;
			M[t[i].first.second] = max(M[t[i].first.second], v);
			it = M.find(t[i].first.second);
			if(it != M.begin())
			{
				it--;
				if(it->second >= v)
					M.erase(t[i].first.second);
			}
			while(true)
			{
				it = M.find(t[i].first.second);
				it++;
				if(it == M.end())
					break;
				if(it->second <= v)
					M.erase(it);
				else
					break;
			}
		}
		else
		{
			M[t[i].first.second] = t[i].second;
		}
	}

	printf("%lld\n", (--M.end())->second);

	return 0;
}


#include<cstdio>
#include<iostream>
#include<stack>
#include<algorithm>
using namespace std;
struct Node{
	int inner;
	int outer;
	int h;
};
Node node[10010];
bool cmp(Node a,Node b)
{
	if(a.outer != b.outer)
		return a.outer > b.outer;
	else
		return a.inner > b.inner;
}
int main()
{
	int n;
	cin>>n;
	int i;
	long long maxh = 0,nowh = 0;
	stack <Node> s;
	for(i = 0;i < n;i++)
	{
		cin>>node[i].inner>>node[i].outer>>node[i].h;
	}
	sort(node,node + n,cmp);
	for(i = 0;i < n;i++)
	{
		while(!s.empty() && node[i].outer <= s.top().inner)
		{
			nowh -= s.top().h;
			s.pop();
		}
		s.push(node[i]);
		nowh += node[i].h;
		maxh = max(maxh,nowh);
	}
	cout<<maxh<<endl;
	return 0;
}


#include<cstdio>
#include<iostream>
#include<stack>
#include<algorithm>
using namespace std;
struct Node{
	int inner;
	int outer;
	int h;
};
Node node[100100];
bool cmp(Node a,Node b)
{
	if(a.outer != b.outer)
		return a.outer > b.outer;
	else
		return a.inner > b.inner;
}
int main()
{
	int n;
	cin>>n;
	int i;
	long long maxh = 0,nowh = 0;
	stack <Node> s;
	for(i = 0;i < n;i++)
	{
		cin>>node[i].inner>>node[i].outer>>node[i].h;
	}
	sort(node,node + n,cmp);
	for(i = 0;i < n;i++)
	{
		while(!s.empty() && node[i].outer <= s.top().inner)
		{
			nowh -= s.top().h;
			s.pop();
		}
		s.push(node[i]);
		nowh += node[i].h;
		maxh = max(maxh,nowh);
	}
	cout<<maxh<<endl;
	//system("pause");
	return 0;
}


#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
using namespace std;
#define LL long long
#define  maxx 100010

struct node
{
	int x, y, h;
}a[maxx];
bool cmp(node a, node b)
{
	if (a.y != b.y) return a.y < b.y;
	return a.x < b.x;
}
int main()
{
	int n;
	while (~scanf("%d", &n))
	{
		for (int i = 0; i < n; i++)
			scanf("%d%d%d", &a[i].x, &a[i].y, &a[i].h);
		sort(a, a + n, cmp);
		stack<node> p;
		LL ans = 0;
		LL mx = 0;
		for (int i = n - 1; i >= 0; i--)
		{
			while (1)
			{
				if (p.empty())
					break;
				node f = p.top();
				if (f.y >= a[i].y && f.x < a[i].y)
					break;
				ans -= f.h;
				p.pop();
			}
			p.push(a[i]);
			ans += a[i].h;
			mx = max(mx, ans);
		}
		printf("%lld\n", mx);
	}
	return 0;
}


#include <iostream>
#include <cstdio>
#include <queue>
#include <vector>
#include <algorithm>
#include <cmath>
#include <map>
#include <ctime>
#include <set>
#include <list>

#define INF 1000000000
#define MAXN 800000
#define MOD 1000000007

using namespace std;

long long n;
struct ring {
	long long i;
	long long o;
	long long h;
	long long index;
};
set<long long> st;
map<long long, long long> mp;
ring arr[100000];

long long t[800008];

void add(long long v, long long l, long long r, long long index, long long val) {
	if (l == r) {
		t[v] = max(t[v], val);
		return;
	}
	long long mid = (l + r) / 2;
	if (index <= mid) {
		add(2 * v, l, mid, index, val);
	}
	else
		add(2 * v + 1, mid + 1, r, index, val);
	t[v] = max(t[v * 2], t[2 * v + 1]);
}

long long get(long long v, long long l, long long r, long long wl, long long wr) {
	if (wl > wr)
		return -1;
	if (wl == l && wr == r) {
		return t[v];
	}
	long long mid = (l + r) / 2;
	long long ll = get(2 * v, l, mid, wl, min(mid, wr));
	long long rr = get(2 * v + 1, mid + 1, r, max(mid + 1, wl), wr);
	return max(ll, rr);
}

bool cmp(ring a, ring b) {
	if (a.o != b.o)
		return a.o > b.o;
	else
		return a.index < b.index;
}

int main() {
	scanf("%I64d", &n);
	for (long long i = 0; i < n; i++) {
		scanf("%I64d %I64d %I64d", &arr[i].i, &arr[i].o, &arr[i].h);
		arr[i].index = i;
		st.insert(arr[i].i);
		st.insert(arr[i].o);
	}
	long long cnt = 1;
	for (set<long long>::iterator it = st.begin(); it != st.end(); it++) {
		mp[*it] = cnt;
		cnt++;
	}
	for (long long i = 0; i < n; i++) {
		arr[i].i = mp[arr[i].i];
		arr[i].o = mp[arr[i].o];
	}
	sort(arr, arr + n, cmp);
	for (long long i = 0; i < n; i++) {
		long long val = get(1, 0, MAXN, 0, arr[i].o - 1);
		add(1, 0, MAXN, arr[i].i, val + arr[i].h);
	}
	cout << get(1, 0, MAXN, 0, MAXN) << endl;
	return 0;
}


#include <iostream>
#include <cstdio>
#include <queue>
#include <vector>
#include <algorithm>
#include <cmath>
#include <map>
#include <ctime>
#include <set>
#include <list>

#define INF 1000000000
#define MAXN 200006
#define MOD 1000000007

using namespace std;

long long n;
struct ring {
	long long i;
	long long o;
	long long h;
	long long index;
};
set<long long> st;
map<long long, long long> mp;
ring arr[100009];

long long t[900018];

void add(long long v, long long l, long long r, long long index, long long val) {
	if (l == r) {
		t[v] = max(t[v], val);
		return;
	}
	long long mid = (l + r) / 2;
	if (index <= mid) {
		add(2 * v, l, mid, index, val);
	}
	else
		add(2 * v + 1, mid + 1, r, index, val);
	t[v] = max(t[v * 2], t[2 * v + 1]);
}

long long get(long long v, long long l, long long r, long long wl, long long wr) {
	if (wl > wr)
		return -1;
	if (wl == l && wr == r) {
		return t[v];
	}
	long long mid = (l + r) / 2;
	long long ll = get(2 * v, l, mid, wl, min(mid, wr));
	long long rr = get(2 * v + 1, mid + 1, r, max(mid + 1, wl), wr);
	return max(ll, rr);
}

bool cmp(ring a, ring b) {
	if (a.o != b.o)
		return a.o > b.o;
	else if (a.i != b.i)
		return a.i > b.i;
	else
		return a.index < b.index;
}

int main() {
	scanf("%I64d", &n);
	for (long long i = 0; i < n; i++) {
		scanf("%I64d %I64d %I64d", &arr[i].i, &arr[i].o, &arr[i].h);
		arr[i].index = i;
		st.insert(arr[i].i);
		st.insert(arr[i].o);
	}
	long long cnt = 1;
	for (set<long long>::iterator it = st.begin(); it != st.end(); it++) {
		mp[*it] = cnt;
		cnt++;
	}
	for (long long i = 0; i < n; i++) {
		arr[i].i = mp[arr[i].i];
		arr[i].o = mp[arr[i].o];
	}
	sort(arr, arr + n, cmp);
	for (long long i = 0; i < n; i++) {
		long long val = get(1, 0, MAXN, 0, arr[i].o - 1);
		add(1, 0, MAXN, arr[i].i, val + arr[i].h);
	}
	cout << get(1, 0, MAXN, 0, MAXN) << endl;
	return 0;
}


#include <iostream>
#include <cstdio>
#include <queue>
#include <vector>
#include <algorithm>
#include <cmath>
#include <map>
#include <ctime>
#include <set>
#include <list>

#define INF 1000000000
#define MAXN 200006
#define MOD 1000000007

using namespace std;

long long n;
struct ring {
	long long i;
	long long o;
	long long h;
	long long index;
};
set<long long> st;
map<long long, long long> mp;
ring arr[100009];

long long t[900018];

void add(long long v, long long l, long long r, long long index, long long val) {
	if (l == r) {
		t[v] = max(t[v], val);
		return;
	}
	long long mid = (l + r) / 2;
	if (index <= mid) {
		add(2 * v, l, mid, index, val);
	}
	else
		add(2 * v + 1, mid + 1, r, index, val);
	t[v] = max(t[v * 2], t[2 * v + 1]);
}

long long get(long long v, long long l, long long r, long long wl, long long wr) {
	if (wl > wr)
		return -1;
	if (wl == l && wr == r) {
		return t[v];
	}
	long long mid = (l + r) / 2;
	long long ll = get(2 * v, l, mid, wl, min(mid, wr));
	long long rr = get(2 * v + 1, mid + 1, r, max(mid + 1, wl), wr);
	return max(ll, rr);
}

bool cmp(ring a, ring b) {
	if (a.o != b.o)
		return a.o > b.o;
	else
		return a.index < b.index;
}

int main() {
	scanf("%I64d", &n);
	for (long long i = 0; i < n; i++) {
		scanf("%I64d %I64d %I64d", &arr[i].i, &arr[i].o, &arr[i].h);
		arr[i].index = i;
		st.insert(arr[i].i);
		st.insert(arr[i].o);
	}
	long long cnt = 1;
	for (set<long long>::iterator it = st.begin(); it != st.end(); it++) {
		mp[*it] = cnt;
		cnt++;
	}
	for (long long i = 0; i < n; i++) {
		arr[i].i = mp[arr[i].i];
		arr[i].o = mp[arr[i].o];
	}
	sort(arr, arr + n, cmp);
	for (long long i = 0; i < n; i++) {
		long long val = get(1, 0, MAXN, 0, arr[i].o - 1);
		add(1, 0, MAXN, arr[i].i, val + arr[i].h);
	}
	cout << get(1, 0, MAXN, 0, MAXN) << endl;
	return 0;
}


# include <iostream>
# include <sstream>
# include <cstdio>
# include <cstdlib>
# include <algorithm>
# include <string>
# include <cstring>
# include <cmath>
# include <stack>
# include <queue>
# include <vector>
# include <list>
# include <map>
# include <set>
# include <deque>
# include <iterator>
# include <functional>
# include <bitset>
# include <climits>
# include <ctime>
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define odd(x) (x&1)
typedef long long ll;
typedef long double ld;
const ll oo = 2 * 1000 * 1000 * 1000;
const int _cnt = 1000 * 1000;
const int _p = 1000 * 1000 * 1000 + 7;
ll o(ll x) { return x%_p; }
int gcd(int a, int b) { return b ? gcd(b, a%b) : a; }
int lcm(int a, int b) { return a*b / gcd(a, b); }
using namespace std;

void file_put() {
	freopen("filename.in", "r", stdin);
	freopen("filename.out", "w", stdout);
}

struct plate{
	int a, b, h;
};

plate p[100005];
int n, m;
ll f[100005], ans = 0;
stack <int> S;

bool cmp(plate p, plate q) {
	return p.b > q.b || p.b == q.b && p.h > q.h;
}

int main() {
	//file_put();

	scanf("%d", &n);
	for (int i = 1; i <= n; i++) scanf("%d%d%d", &p[i].a, &p[i].b, &p[i].h);
	sort(p + 1, p + n + 1, cmp);
	for (int i = n; i >= 1; i--)
		if (p[i].b == p[i - 1].b) {
			p[i - 1].h += p[i].h;
			p[i - 1].a = max(p[i - 1].a, p[i].a);
		}
	for (int i = 1; i <= n; i++) {
		if (p[i].b == p[i-1].b) continue;
		while (!S.empty() && p[S.top()].a >= p[i].b) S.pop();
		if (!S.empty()) f[i] = f[S.top()];
		f[i] += p[i].h;
		ans = max(ans, f[i]);
		S.push(i);
	}
	printf("%I64d\n", ans);

	return 0;
}


# include <iostream>
# include <sstream>
# include <cstdio>
# include <cstdlib>
# include <algorithm>
# include <string>
# include <cstring>
# include <cmath>
# include <stack>
# include <queue>
# include <vector>
# include <list>
# include <map>
# include <set>
# include <deque>
# include <iterator>
# include <functional>
# include <bitset>
# include <climits>
# include <ctime>
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define odd(x) (x&1)
typedef long long ll;
typedef long double ld;
const ll oo = 2 * 1000 * 1000 * 1000;
const int _cnt = 1000 * 1000;
const int _p = 1000 * 1000 * 1000 + 7;
ll o(ll x) { return x%_p; }
int gcd(int a, int b) { return b ? gcd(b, a%b) : a; }
int lcm(int a, int b) { return a*b / gcd(a, b); }
using namespace std;

void file_put() {
	freopen("filename.in", "r", stdin);
	freopen("filename.out", "w", stdout);
}

struct plate{
	int a, b, h;
};

plate p[100005];
int n, m;
ll f[100005], ans = 0;
stack <int> S;

bool cmp(plate p, plate q) {
	return p.b > q.b;
}

int main() {
	//file_put();

	scanf("%d", &n);
	for (int i = 1; i <= n; i++) scanf("%d%d%d", &p[i].a, &p[i].b, &p[i].h);
	sort(p + 1, p + n + 1, cmp);
	for (int i = 1; i <= n; i++) {
		while (!S.empty() && p[S.top()].a > p[i].b) S.pop();
		if (!S.empty()) f[i] = f[S.top()];
		f[i] += p[i].h;
		ans = max(ans, f[i]);
		S.push(i);
	}
	printf("%I64d\n", ans);

	return 0;
}