#include "string.h"
#include "strings.h"
#include "iostream"
#include "stdio.h"
#include "math.h"

std::string getPair(int &n)
{
    std::string res = "pair<";
    std::string buf;
    //std::cout<<n;
    if (n>0)
    {
        std::cin>>buf;

        if (buf.compare("pair") == 0)
        {
            res += getPair(n);
        }
        else
        {
            n--;
            res += buf;
        }
        res += ",";
        if (n>0)
        {
            std::cin>>buf;
            if (buf.compare("pair") == 0)
            {
                res += getPair(n);
            }
            else
            {
                n--;
                res += buf;
            }
            res += ">";
            return res;
        }
        else
        {
            return "Error occurred";
        }
    }
    else
    {
        return "Error occurred";
    }
}

int main()
{
    int n;
    std::string buf;
    std::string result;

    std::cin>>n;
    if (n > 0)
    {
        std::cin>>buf;
        if (buf.compare("pair") == 0)
        {
           if(n>=2)
               result += (getPair(n));
           else
               result += "Error occurred";
        }
        else
        {
            n--;
        }
    }
    if (std::string::npos != result.find("Error occurred"))
        result = "Error occurred";

    std::cout<<result;

    return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAXN 1000000

char as[MAXN];
int git,at;
int ar[MAXN];

void read(){

	char a[20];
	int N;

	scanf(" %d",&N);

	while(scanf(" %s",a)!=EOF){

		if(a[0]=='p')	ar[++at]=1;
		else ar[++at]=0;

	}

}

void sol(){

	if(git>at){	printf("Error occurred\n"); exit(0);}
	if(!ar[git]){ strcat(as,"int"); return;}

	strcat(as,"pair<");
	git++;
	sol();
	strcat(as,",");
	git++;
	sol();
	strcat(as,">");

}

int main(){

/*	freopen("a.in","r",stdin);
	freopen("a.out","w",stdout);*/

	read();
	git=1;
	sol();

	printf("%s\n",as);

	return 0;

}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAXN 1000000

char as[MAXN];
int git,at;
int ar[MAXN];

void read(){

	char a[20];
	int N;

	scanf(" %d",&N);

	while(scanf(" %s",a)!=EOF){

		if(a[0]=='p')	ar[++at]=1;
		else ar[++at]=0;

	}

}

void sol(){

	if(git>at){	printf("Error occurred\n"); exit(0);}
	if(!ar[git]){ strcat(as,"int"); git++; return;}
	git++;

	strcat(as,"pair<");
	sol();
	strcat(as,",");
	sol();
	strcat(as,">");

}

int main(){

/*	freopen("a.in","r",stdin);
	freopen("a.out","w",stdout);*/

	read();
	git=1;
	sol();

	printf("%s\n",as);

	return 0;

}


#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <iostream>

#define MAXN 1000000

using namespace std;

string as;
int git,at;
int ar[MAXN];

void read(){

	char a[20];
	int N;

	scanf(" %d",&N);

	while(scanf(" %s",a)!=EOF){

		if(a[0]=='p')	ar[++at]=1;
		else ar[++at]=0;

	}

}

void sol(){

	if(git>at){	printf("Error occurred\n"); exit(0);}
	if(!ar[git]){ as+="int"; git++; return;}
	git++;

	as+="pair<";
	sol();
	as+=",";
	sol();
	as+=">";
}

int main(){

	/*freopen("a.in","r",stdin);
	freopen("a.out","w",stdout);*/

	read();
	if(git!=at){	printf("Error occurred\n"); exit(0);}
	git=1;
	sol();

	cout << as << endl;

	return 0;

}


#include <stdio.h>
#include <stdlib.h>

#define MAXN 100010

int ar[MAXN];
char used[MAXN];
char as[20];
int at,res=0;
int N;

void read(){

	scanf(" %d",&N);

	while(scanf(" %s",as)!=EOF){

		if(as[0]=='p')	ar[++at]=1;
		else ar[++at]=2;

	}

}
void sol(int a,int b){

	if(ar[a]==1 && ar[a+1]==2 && ar[a+2]==2){ 	used[a]='<'; used[a+1]=',' ; used[a+2]='>';

		if(ar[a+3]==1)	sol(a+3,b);

		return;
	}
	if(ar[a]==1 && ar[a+1]==2 && ar[a+2]!=2){  	printf("Error occurred\n"); exit(0);}
	if(ar[a]==1 && ar[a+1]==1 && ar[b]==2){	used[a]='<'; used[b]='>'; sol(a+1,b-1);}

}
int main(){

/*	freopen("a.in","r",stdin);
	freopen("a.out","w",stdout);*/

	read();

	sol(1,at);

	int i;

	for( i=1 ; i<=at ; i++ ){

		if(ar[i]==1)	printf("pair");
		if(ar[i]==2)	printf("int");
		printf("%c",used[i]);

	}

	return 0;

}


#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <iostream>

#define MAXN 1000000

using namespace std;

string as;
int git,at;
int ar[MAXN];

void read(){

	char a[20];
	int N;

	scanf(" %d",&N);

	while(scanf(" %s",a)!=EOF){

		if(a[0]=='p')	ar[++at]=1;
		else ar[++at]=0;

	}

}

void sol(){

	if(git>at){	printf("Error occurred\n"); exit(0);}
	if(!ar[git]){ as+="int"; git++; return;}
	git++;

	as+="pair<";
	sol();
	as+=",";
	sol();
	as+=">";
}

int main(){

	/*freopen("a.in","r",stdin);
	freopen("a.out","w",stdout);*/

	read();
	git=1;
	sol();
	if(git-1!=at){	printf("Error occurred\n"); exit(0);}

	cout << as << endl;

	return 0;

}


#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <iostream>

#define MAXN 1000000

using namespace std;

string as;
int git,at;
int ar[MAXN];

void read(){

	char a[20];
	int N;

	scanf(" %d",&N);

	while(scanf(" %s",a)!=EOF){

		if(a[0]=='p')	ar[++at]=1;
		else ar[++at]=0;

	}

}

void sol(){

	if(git>at){	printf("Error occurred\n"); exit(0);}
	if(!ar[git]){ as+="int"; git++; return;}
	git++;

	as+="pair<";
	sol();
	as+=",";
	sol();
	as+=">";
}

int main(){

	/*freopen("a.in","r",stdin);
	freopen("a.out","w",stdout);*/

	read();
	if(!ar[1] && at!=1){	printf("Error occurred\n"); exit(0);}
	git=1;
	sol();

	cout << as << endl;

	return 0;

}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAXN 1000000

char as[MAXN];
int git,at;
int ar[MAXN];

void read(){

	char a[20];
	int N;

	scanf(" %d",&N);

	while(scanf(" %s",a)!=EOF){

		if(a[0]=='p')	ar[++at]=1;
		else ar[++at]=0;

	}

}

void sol(){

	if(git>at){	printf("Error occurred\n"); exit(0);}
	if(!ar[git]){ strcat(as,"int"); git++; return;}

	strcat(as,"pair<");
	git++;
	sol();
	strcat(as,",");
	git++;
	sol();
	strcat(as,">");

}

int main(){

	/*freopen("a.in","r",stdin);
	freopen("a.out","w",stdout);*/

	read();
	git=1;
	sol();

	printf("%s\n",as);

	return 0;

}


#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <iostream>

#define MAXN 1000000

using namespace std;

string as;
int git,at;
int ar[MAXN];

void read(){

	char a[20];
	int N;

	scanf(" %d",&N);

	while(scanf(" %s",a)!=EOF){

		if(a[0]=='p')	ar[++at]=1;
		else ar[++at]=0;

	}

}

void sol(){

	if(git>at){	printf("Error occurred\n"); exit(0);}
	if(!ar[git]){ as+="int"; git++; return;}
	git++;

	as+="pair<";
	sol();
	as+=",";
	sol();
	as+=">";
}

int main(){

	/*freopen("a.in","r",stdin);
	freopen("a.out","w",stdout);*/

	read();
	if(!ar[1]){	printf("Error occurred\n"); exit(0);}
	git=1;
	sol();

	cout << as << endl;

	return 0;

}


#include <stdio.h>
#include <stdlib.h>

#define MAXN 100010

int ar[MAXN];
int used[MAXN];
char as[20];
int at,res=0;
int N;

void read(){

	scanf(" %d",&N);

	while(scanf(" %s",as)!=EOF){

		if(as[0]=='p')	ar[++at]=1;
		else ar[++at]=2;

	}

}
void sol(int a,int b){

	if(ar[a]==1 && ar[a+1]==2 && ar[a+2]==2){ 	used[a]=1; used[a+1]=2 ; used[a+2]=3;

		if(a+2!=at)	used[a+2]=4;

		if(ar[a+3]==1)	sol(a+3,b);

		return;
	}
	if(ar[a]==1 && ar[a+1]==2 && ar[a+2]!=2){  	printf("Error occurred\n"); exit(0);}
	if(ar[a]==1 && ar[a+1]==1 && ar[b]==2){	used[a]=1; used[b]=3 ;

		if(b!=at)	used[b]=4;

		sol(a+1,b-1);
	}

}
int main(){

/*	freopen("a.in","r",stdin);
	freopen("a.out","w",stdout);*/

	read();

	sol(1,at);

	int i;

	for( i=1 ; i<=at ; i++ ){

		if(ar[i]==1)	printf("pair");
		if(ar[i]==2)	printf("int");
		if(used[i]==1)	printf("<");
		if(used[i]==2) 	printf(",");
		if(used[i]==3) 	printf(">");
		if(used[i]==4) 	printf(">,");

	}

	return 0;

}


#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <iostream>

#define MAXN 1000000

using namespace std;

string as;
int git,at;
int ar[MAXN];

void read(){

	char a[20];
	int N;

	scanf(" %d",&N);

	while(scanf(" %s",a)!=EOF){

		if(a[0]=='p')	ar[++at]=1;
		else ar[++at]=0;

	}

}

void sol(){

	if(git>at){	printf("Error occurred\n"); exit(0);}
	if(!ar[git]){ as+="int"; git++; return;}
	git++;

	as+="pair<";
	sol();
	as+=",";
	sol();
	as+=">";
}

int main(){

	/*freopen("a.in","r",stdin);
	freopen("a.out","w",stdout);*/

	read();
	git=1;
	sol();

	cout << as << endl;

	return 0;

}


#include<iostream>
#include<cstdio>

using namespace std;

int main()
{
	int nump,numi,i,j,k,n;
	char str[10];
	scanf("%d",&n);
	i = n;
	nump = numi = 0;
	while(i)
	{
		scanf("%s",str);
		if(str[0] == 'p')
			++nump;
		else
		{
			++numi;
			i--;
		}
	}
	if(nump + 1 != numi)
		printf("Error occurred\n");
	else
	{
		i = n;
		while(nump--)
		{
			printf("pair<");
		}
		printf("int,int>");
		i -= 2;
		while(i--)
		{
			printf(",int>");
		}
		printf("\n");
	}

	return 0;
}

/*ecqrdcflrdidtedlaaiphbbmlhreqrimaebdsggpchsblneeoe*/


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<cmath>
#include<map>
#include<string>

using namespace std;
const int Maxv=100100;
const int Inf=1000000000;
int m;
char str[Maxv][10];
bool flag;
int l;
int ls[Maxv];int rs[Maxv];
int len;
bool vis[Maxv];

void dfs(int x)
{
    if(x>=len)
    {
        flag=false;
        return ;
    }
    vis[x]=true;
    l++;
    if(str[x][0]=='p')
    {
        ls[x]=l;
        dfs(l);
        rs[x]=l;
        dfs(l);
    }
}

void dfsp(int x)
{
    printf("%s",str[x]);
    if(str[x][0]=='p')
    {
        printf("<");
        dfsp(ls[x]);
        printf(",");
        dfsp(rs[x]);
        printf(">");
    }
}
void Put2()
{
    flag=true;
    l=0;
    dfs(0);
    for(int i=0;i<len;i++)
    if(!vis[i])
    flag=false;
    if(!flag)
    {
        puts("Error occurred");
        return ;
    }
    dfsp(0);
    puts("");
}
int main()
{
    int i=0,k;
    int n;
    scanf("%d",&n);
    while(~scanf("%s",str[i++]));
    len=i-1;
    Put2();
    return 0;
}

/*qqqsanmeqnjoopfmlonotpealbgjjlsrthilaapigqommaqdoj*/


#include<iostream>
#include<string>
#include<cstring>
using namespace std;
string s[111111]="";
int a[111111];
int ans;
int main()
{
	int n;
	string s1="";
	cin>>n;
	int num2;
	int i,j;
	num2=0;
	ans=0;
	memset(a,0,sizeof(a));
	int ant=0;
	int m=n;
	while(n)
	{
		cin>>s1;
		if(s1=="pair")
		{
		   s[ans++]=s1;
		   if(num2==0)num2+=2;
		   else num2+=1;
		}
		else
		{
			s[ans++]=s1;
			num2--;
			n--;
		}
		s1="";
	}
	if(m==1&&s[0]=="int")
	{
		cout<<s[0]<<endl;
	    return 0;
	}
	if(num2!=0)
	{
		cout<<"Error occurred"<<endl;
	}
	else
	{
		for(i=0;i<ans;i++)
		{
			if(s[i]=="pair")
			{
				if(i==0)
				cout<<s[i]<<"<";
				else
				{
					if(s[i-1]=="int")
					cout<<",";
					cout<<s[i]<<"<";
				}
				a[ant++]=0;
			}
			else
			{
			  if(s[i-1]=="int")
			  {
                   cout<<","<<s[i];
                   a[ant-1]++;
				   for(j=ant-1;j>=0;j--)
				   {
				       if(a[ant-1]==2)
				       {
	   					cout<<">";
	   					ant--;
	   					a[ant-1]++;
	   				   }
	   				   else break;
				   }
              }
             else
  			  {
  			  	 cout<<s[i];
  			  	 a[ant-1]++;
  			  	 for(j=ant-1;j>=0;j--)
				   {
				       if(a[ant-1]==2)
				       {
	   					cout<<">";
	   					ant--;
	   					a[ant-1]++;
	   				   }
	   				   else break;
				   }
  			  }
			}
		}
		cout<<endl;
	}
	return 0;
}
/*
5
pair pair pair int pair int int int int
*/
/*
5
pair pair pair int int int pair int int
pair<pair<pair<int,int>,int>,pair<int,int>>
1
int
3
pair int pair int int
*/

/*dpfimaniakllcdqscjjkrjdjaasetnppiadbbibbsnemiakkkt*/


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#define ll (v<<1)
#define rr (v<<1|1)

using namespace std;

char str[101111];
char s[201111];
int stack[100000];
int main(){
	int n,num,i,j,flag,cnt,pnum,sc;
	scanf("%d\n",&n);
	gets(str);
	num=0;
	flag=0;
	cnt=0;
	sc=0;
	s[sc++]=str[0];
	if(str[0]=='p')  pnum=1;
	else pnum=0;
	if(str[0]==' ') flag=1;
	for(i=1;str[i] && !flag;i++){
		if(str[i]=='i' && str[i+1]=='n')
			num++;
		if(str[i]=='p') pnum++;
		if(str[i]==' '){
			if(str[i-1]=='r'){
				s[sc++]='<';
				stack[++cnt]=1;
			}
			else if(str[i-1]=='t' || str[i-1]=='>'){
				if(cnt==0){
					flag=1;
					break;
				}
				stack[cnt]++;
				if(stack[cnt]==3){
					while(stack[cnt]==3){
						s[sc++]='>';
						pnum--;
						cnt--;
						if(cnt==0) break;
						stack[cnt]++;
					}
				}
				if(stack[cnt]==2){
					s[sc++]=',';
				}
			}
		}
		else s[sc++]=str[i];
	}
	stack[cnt]++;
	while(stack[cnt]==3){
		s[sc++]='>';
		pnum--;
		cnt--;
		if(cnt==0) break;
		stack[cnt]++;
	}
	s[sc]=0;
//	puts(s);
	if(cnt!=0 || flag || num!=n ||  pnum!=0) puts("Error occurred");
	else	puts(s);
	return 0;
}
/*

5
pair pair pair int int int pair int int
pair<pair<pair<int,int>,int>,pair<int,int>>

3
pair int pair int int

*/

/*bpmqomsspakrtncrehrjlijjgtfkhdqjkafenpdaqfshkaeotc*/


#include<iostream>
#include<string>
using namespace std;
int main()
{
	int n;
	string s[411111]="";
	string s1="";
	cin>>n;
	int ans=0;
	int num1;
	int num2;
	num1=0;
	num2=0;
	while(n)
	{
		cin>>s1;
		if(s1=="pair")
		{
		   s[ans++]=s1;
		   num1+=2;
		   if(num2==0)num2+=2;
		   else num2+=1;
		}
		else
		{
			s[ans++]=s1;
			num2--;
			n--;
		}
		s1="";

	}
	string s2="";
	int i,j;
	if(num2!=0)
	{
		cout<<"Error occurred"<<endl;
	}
	else
	{
		for(i=0;i<ans;i++)
	    {
    	   if(i==0&&s[i]=="pair")
    	   {
		      cout<<s[i];
		      cout<<"<";
		      s2="<";
		      num1--;
    	   }
 	       else
 	       {
 	       	 if(s[i]=="pair")
 	       	 {
 	       	 	if(s2==","||s2=="<")
 	       	 	{
 	       	 		cout<<s[i]<<"<";
 	 		        s2="<";
 	 		        num1--;
 	       	 	}
 	       	 	else if(s2==">")
			    {
			        cout<<","<<s[i]<<"<";
					s2="<";
					num1--;
			    }
 	       	 }
 	       	 else
 	       	 {
       	 	     if(s2=="<")
       	 	     {
       	 	     	cout<<s[i]<<",";
       	 	     	s2=",";
       	 	     }
       	 	     else if(s2==",")
       	 	     {
       	 	     	cout<<s[i]<<">";
       	 	     	s2=">";
       	 	     	num1--;
       	 	     }
       	 	     else if(s2==">")
       	 	     {
	       	 	       cout<<","<<s[i]<<">";
					   s2=">";
					   num1--;
       	 	     }
 	       	 }
 	       }
	    }
	    for(j=0;j<num1;j++)
	     cout<<">";
        cout<<endl;
	}
	return 0;
}

/*shfocleqjgjbfcgaihfqlbpqkkoplaqktcqcfasohtimbgmkoj*/


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<set>
#include<string>

using namespace std;

#define MAXN 100100
#define _clr(x,y) memset(x,y,sizeof(x))

int tot,up;
string res;
string list[MAXN];

bool dfs()
{
	if(tot>=up)
	{
		return false;
	}
	if(list[tot]=="int")
	{
		tot++;
		res+="int";
		return true;
	}
	else
	{
		tot++;
		res+="pair<";
		if(!dfs()) return false;
		res+=",";
		if(!dfs()) return false;
		res+=">";
		return true;
	}
}

int main()
{
	int n;
	cin>>n;
	up=0;
	while(cin>>list[up])
	{
		up++;
	}
	bool ok=dfs();
	if(ok && tot==up) cout<<res<<endl;
	else cout<<"Error occurred"<<endl;
	return 0;
}

/*nooadgjjsoqprlaasllmacddkmltgbdlirmddnmohabrenjcqn*/


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#define ll (v<<1)
#define rr (v<<1|1)

using namespace std;

char str[1001111];
char s[2001111];
int stack[100000];
int main(){
	int n,num,i,j,flag,cnt,pnum,sc,pp;
	scanf("%d\n",&n);
	gets(str);
	if(n==1){
		num=0;
		for(i=0;str[i];i++)
			if(str[i]=='i') num++;
		if(strstr(str,"int") && num==1) puts("int");
		else puts("Error occurred");
		return 0;
	}
	num=0;
	flag=0;
	cnt=0;
	sc=0;
	pp=0;
	s[sc++]=str[0];
	if(str[0]=='p')  pp=pnum=1;
	else pp=pnum=0;
	if(str[0]==' ') flag=1;
	for(i=1;str[i] && !flag;i++){
		if(str[i]=='i' && str[i+1]=='n')
			num++;
		if(str[i]=='p') pnum++,pp++;
		if(str[i]==' '){
			if(str[i-1]=='r'){
				s[sc++]='<';
				stack[++cnt]=1;
			}
			else if(str[i-1]=='t' || str[i-1]=='>'){
				if(cnt==0){
					flag=1;
					break;
				}
				stack[cnt]++;
				if(stack[cnt]==3){
					while(stack[cnt]==3){
						s[sc++]='>';
						pnum--;
						cnt--;
						if(cnt==0) break;
						stack[cnt]++;
					}
				}
				if(stack[cnt]==2){
					s[sc++]=',';
				}
			}
		}
		else s[sc++]=str[i];
	}
	stack[cnt]++;
	while(stack[cnt]==3){
		s[sc++]='>';
		pnum--;
		cnt--;
		if(cnt==0) break;
		stack[cnt]++;
	}
	s[sc]=0;
//	puts(s);
	if(cnt!=0 || flag || num!=n ||  pnum!=0 ||pp!=num-1) puts("Error occurred");
	else	puts(s);
	return 0;
}
/*

5
pair pair pair int int int pair int int
pair<pair<pair<int,int>,int>,pair<int,int>>

1
int

3
pair int pair int int

*/

/*hdlnijmqbnpnslrrnteiaklarhflotsoojtdldlmqgfgkdepoa*/


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>

using namespace std;

char str[100005];
int main()
{
    int n;
    cin>>n;
    getchar();
    gets(str);

    int len=strlen(str);
    int t=(len-4*n+1)/5;
    if(len==3)
        cout<<"int"<<endl;
    else if((t+1)!=n)
        cout<<"Error occurred"<<endl;
    else
    {
        for(int i=1;i<=t;i++)
            cout<<"pair<";
        cout<<"int";
        for(int i=1;i<=t;i++)
            cout<<",int>";
        cout<<endl;
    }
    return 0;
}

/*jngpjmsggbjtomtdldlaareneihbnpccabrknpitjrsdkktbfk*/


#include<iostream>
#include<string>
#include<cstring>
using namespace std;
string s[111111]="";
int a[111111];
int ans;
int main()
{
	int n;
	string s1="";
	cin>>n;
	int num2;
	int i,j;
	num2=0;
	ans=0;
	memset(a,0,sizeof(a));
	int ant=0;
	int m=n;
	int check;
	check=0;
	while(n)
	{
		cin>>s1;
		if(s1=="pair")
		{
		   s[ans++]=s1;
		   if(num2==0)
		   {
		    num2+=2;

		   }
		   else num2+=1;
		}
		else
		{
			s[ans++]=s1;
			num2--;
			n--;
			if(num2==0)check++;
		}
		s1="";
	}
	if(m==1&&s[0]=="int")
	{
		cout<<s[0]<<endl;
	    return 0;
	}
	if(num2!=0||check>1)
	{
		cout<<"Error occurred"<<endl;
	}
	else
	{
		for(i=0;i<ans;i++)
		{
			if(s[i]=="pair")
			{
				if(i==0)
				cout<<s[i]<<"<";
				else
				{
					if(s[i-1]=="int")
					cout<<",";
					cout<<s[i]<<"<";
				}
				a[ant++]=0;
			}
			else
			{
			  if(s[i-1]=="int")
			  {
                   cout<<","<<s[i];
                   a[ant-1]++;
				   for(j=ant-1;j>=0;j--)
				   {
				       if(a[ant-1]==2)
				       {
	   					cout<<">";
	   					ant--;
	   					a[ant-1]++;
	   				   }
	   				   else break;
				   }
              }
             else
  			  {
  			  	 cout<<s[i];
  			  	 a[ant-1]++;
  			  	 for(j=ant-1;j>=0;j--)
				   {
				       if(a[ant-1]==2)
				       {
	   					cout<<">";
	   					ant--;
	   					a[ant-1]++;
	   				   }
	   				   else break;
				   }
  			  }
			}
		}
		cout<<endl;
	}
	return 0;
}
/*
5
pair pair pair int pair int int int int
*/
/*
5
pair pair pair int int int pair int int
pair<pair<pair<int,int>,int>,pair<int,int>>
1
int
3
pair int pair int int
4
pair pair int int pair int int
pair<pair<int,int>,pair<int,int>>
4
pair int int pair int int
Error occurred
*/

/*oqrerrrncafbgalifkgflsqcoommbmlqaaarkklmkifrajrgtd*/


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>

using namespace std;

char str[100005];
int main()
{
    int n;
    cin>>n;
    getchar();
    gets(str);

    int len=strlen(str);
    int t=(len-4*n+1)/5;
    if(len==3)
        cout<<"int"<<endl;
    else if((t+1)!=n)
        cout<<"Error occurred"<<endl;
    else
    {
        for(int i=1;i<=t;i++)
            cout<<"pair<";
        cout<<"int,";
        for(int i=1;i<=t;i++)
            cout<<"int>";
        cout<<endl;
    }
    return 0;
}

/*mhhmsenckehoabjggabtqksdbntgtoedblhahbojroerpnpbfq*/


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<cmath>
#include<map>
#include<string>

using namespace std;
const int Maxv=100100;
const int Inf=1000000000;
int m;
char str[Maxv][10];
char stk[Maxv];
int tot;
bool flag;
int id[Maxv];
char ss[Maxv];

void Put(int n)
{
    int i,j,k=0;
    j=0;
    for(i=0;i<n;i++)
    {
        if(str[i][0]=='p')
        {
            id[k++]=2;
            ss[j++]='p';
            ss[j++]='<';
        }
        else if(str[i][0]=='i')
        {
            if(id[k-1]>0)
            {
                id[k-1]--;
                ss[j++]='i';
                if(id[k-1]==1)
                    ss[j++]=',';
                else if(id[k-1]==0)
                {
                    k--;
                    if(k-1>=0)
                    id[k-1]--;
                    ss[j++]='>';
                }
            }
        }
    }
    int t=0;
    k=0;
    for(i=0;i<j;i++)
    {
        if(ss[i]=='<') t++;
        if(ss[i]=='>') k++;
        if(ss[i-1]=='>' && ss[i]=='p') cout<<",";
        if(ss[i]=='p')
        printf("pair");
        else if(ss[i]=='i')
        printf("int");
        else
        printf("%c",ss[i]);
    }
    while(t>k)
    {
        cout<<">";
        k++;
    }
    cout<<endl;
}

void slove(int len)
{
    bool flag=true;
    for(int i=0;i<len;i++)
    {
        stk[tot++]=str[i][0];
        if(flag)
        {
            while(tot&&stk[tot-1]=='i')
            {
                if(tot==1)
                {
                    flag=false;
                    break;
                }
                if(stk[tot-2]=='i')
                {
                    if(tot==2||stk[tot-3]!='p')
                    {
                        flag=false;
                        break;
                    }
                    else
                    {
                        if(tot==3)
                        tot=0;
                        else
                        {
                            stk[tot-3]='i';tot-=2;
                        }
                    }
                }
                else
                break;
            }
        }
    }
    if(tot)
    flag=false;
    if(!flag)
    {
        puts("Error occurred");
        return ;
    }
    Put(len);
}

int main()
{
    int i,j=0,k;
    int n;
    scanf("%d",&n);
    for(i=0;j<n;i++)
    {
        scanf("%s",str[i]);
        if(str[i][0]=='i')
        j++;
    }
    if(i==1&&n==1&&str[0][0]=='i')
    puts("int");
    else
    {
       slove(i);
    }
    return 0;
}

/*tdrmiqetcrcknibonkhmdfpltfpkrgfjbcbjkraeoogtotohjn*/


#include<iostream>
#include<cstdio>

using namespace std;

int store[100005];
int val[100005];
int main()
{
	int nump,numi,i,j,k,n;
	char str[10];
	scanf("%d",&n);
	i = n;
 	numi = 0;
	while(i)
	{
		scanf("%s",str);
		if(str[0] == 'p')
		{
			++store[0];
			j = store[0];
			store[j] = 1;
			if(numi==0)
				numi += 2;
			else
				++numi;
		}
		else
		{
			++store[0];
			j = store[0];
			store[j] = 2;
			--numi;
			i--;
		}
	}
	if(numi != 0)
		printf("Error occurred\n");
	else
	{
		for(i = 1;i<=store[0];i++)
		{
			if(store[i]==1)
			{
					if(store[i-1]==2 && store[i-2]==1)
						printf(",pair<");
					else
						printf("pair<");
					if(numi==0)
						numi += 2;
					else
						numi++;
			}

			else
			{
				if(store[i-1]!=2)
					printf("int");
				else
					printf(",int>");
					--numi;

			}
		}

		printf("\n");
	}

	return 0;
}

/*sesjbhqgdqskthpqofpfgonjlpoelebbjakctsidhgfgfaolrj*/


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#define ll (v<<1)
#define rr (v<<1|1)

using namespace std;

char str[1001111];
char s[2001111];
int stack[100000];
int main(){
	int n,num,i,j,flag,cnt,pnum,sc;
	scanf("%d\n",&n);
	gets(str);
	num=0;
	flag=0;
	cnt=0;
	sc=0;
	s[sc++]=str[0];
	if(str[0]=='p')  pnum=1;
	else pnum=0;
	if(str[0]==' ') flag=1;
	for(i=1;str[i] && !flag;i++){
		if(str[i]=='i' && str[i+1]=='n')
			num++;
		if(str[i]=='p') pnum++;
		if(str[i]==' '){
			if(str[i-1]=='r'){
				s[sc++]='<';
				stack[++cnt]=1;
			}
			else if(str[i-1]=='t' || str[i-1]=='>'){
				if(cnt==0){
					flag=1;
					break;
				}
				stack[cnt]++;
				if(stack[cnt]==3){
					while(stack[cnt]==3){
						s[sc++]='>';
						pnum--;
						cnt--;
						if(cnt==0) break;
						stack[cnt]++;
					}
				}
				if(stack[cnt]==2){
					s[sc++]=',';
				}
			}
		}
		else s[sc++]=str[i];
	}
	stack[cnt]++;
	while(stack[cnt]==3){
		s[sc++]='>';
		pnum--;
		cnt--;
		if(cnt==0) break;
		stack[cnt]++;
	}
	s[sc]=0;
//	puts(s);
	if(cnt!=0 || flag || num!=n ||  pnum!=0) puts("Error occurred");
	else	puts(s);
	return 0;
}
/*

5
pair pair pair int int int pair int int
pair<pair<pair<int,int>,int>,pair<int,int>>

3
pair int pair int int

*/

/*dhbihfkplpkoertcpidjrfrtakrjegatgbhfgkmjrcdcldeglh*/


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<string>
#include<vector>
#include<queue>
using namespace std;
int a[1500000], b[1500000];
int main()
{
    int n;
    scanf("%d",&n);
    char str[10];
    int tot=0;
    string s;
    int r=0;
    while(scanf("%s",str)!=EOF)
    {
        if(str[0]=='p')
        a[tot++]=1;
        else
        {
            if(str[0]=='i')
            a[tot++]=2;
            n--;
        }
    }
   // cout<<n<<endl;
    if(n!=0)
    {
        printf("Error occurred");
        return 0;
    }
    int m=tot;
    for(int i=0;i<m;i++)
    {
        if(a[i]==1)
        {
            b[r]--;
            r++;
            b[r]=2;
            s+="pair<";
        }
        else
        {
            b[r]--;
            s+="int";
        }
        while(b[r]==0)
        {
            r--;
            s+=">";
        }
        if(b[r]==1)
        {
            s+=",";
        }
    }
    if (r!=0||b[r]!= -1)
    {
        printf("Error occurred");
        return 0;
    }
    cout<<s<<endl;
    return 0;
}

/*frjrihqijqmemkjocfofpjnnflftqgpnpqkpqhefpibhlcnnha*/


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>

using namespace std;

char str[100005];
int main()
{
    int n;
    cin>>n;
    getchar();
    gets(str);

    int len=strlen(str);
    int t=(len-4*n+1)/5;
    if((t+1)!=n)
        cout<<"Error occurred"<<endl;
    else
    {
        for(int i=1;i<=t;i++)
            cout<<"pair<";
        cout<<"int,";
        for(int i=1;i<=t;i++)
            cout<<"int>";
        cout<<endl;
    }
    return 0;
}

/*eqspqhlahehbtimpsjskcrtdepenrhccpmrlaietfdnqdrlbgb*/


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<cmath>
#include<map>
#include<string>

using namespace std;
const int Maxv=100100;
const int Inf=1000000000;
int m;
char str[Maxv][10];
char stk[Maxv];
int tot;
bool flag;
int id[Maxv];
char ss[Maxv];

void Put(int n)
{
    int i,j,k=0;
    j=0;
    for(i=0;i<n;i++)
    {
        if(str[i][0]=='p')
        {
            id[k++]=2;
            ss[j++]='p';
            ss[j++]='<';
        }
        else if(str[i][0]=='i')
        {
            if(id[k-1]>0)
            {
                id[k-1]--;
                ss[j++]='i';
                if(id[k-1]==1)
                    ss[j++]=',';
                else if(id[k-1]==0)
                {
                    k--;
                    if(k-1>=0)
                    id[k-1]--;
                    ss[j++]='>';
                }
            }
        }
    }
    int t=0;
    k=0;
    for(i=0;i<j;i++)
    {
        if(ss[i]=='<') t++;
        if(ss[i]=='>') k++;
        if(ss[i-1]=='>' && ss[i]=='p') cout<<",";
        if(ss[i-1]=='>'&&ss[i]=='i') cout<<",";
        if(ss[i]=='p')
        printf("pair");
        else if(ss[i]=='i')
        printf("int");
        else
        printf("%c",ss[i]);
    }
    while(t>k)
    {
        cout<<">";
        k++;
    }
    cout<<endl;
}
int l;
int ls[Maxv];int rs[Maxv];
void dfs(int x)
{
    l++;
    if(str[x][0]=='p')
    {
        ls[x]=l;
        dfs(l);
        rs[x]=l;
        dfs(l);
    }
    else
    {
        ls[x]=-1;rs[x]=-1;
    }
}

void dfsp(int x)
{
    printf("%s",str[x]);
    if(str[x][0]=='p')
    {
        printf("<");
        dfsp(ls[x]);
        printf(",");
        dfsp(rs[x]);
        printf(">");
    }
}
void Put2()
{
    l=0;dfs(0);
    dfsp(0);
}

void slove(int len)
{
    bool flag=true;
    for(int i=0;i<len;i++)
    {
        stk[tot++]=str[i][0];
        if(flag)
        {
            while(tot&&stk[tot-1]=='i')
            {
                if(tot==1)
                {
                    flag=false;
                    break;
                }
                if(stk[tot-2]=='i')
                {
                    if(tot==2||stk[tot-3]!='p')
                    {
                        flag=false;
                        break;
                    }
                    else
                    {
                        if(tot==3)
                        {
                            if(i!=len-1)
                            flag=false;
                            tot=0;
                        }
                        else
                        {
                            stk[tot-3]='i';tot-=2;
                        }
                    }
                }
                else
                break;
            }
        }
    }
    if(tot)
    flag=false;
    if(!flag)
    {
        puts("Error occurred");
        return ;
    }
    //Put(len);
    Put2();
}

int main()
{
    int i,j=0,k;
    int n;
    scanf("%d",&n);
    for(i=0;j<n;i++)
    {
        scanf("%s",str[i]);
        if(str[i][0]=='i')
        j++;
    }
    if(i==1&&n==1&&str[0][0]=='i')
    puts("int");
    else if(i!=n+n-1)
    {
        puts("Error occurred");
    }
    else
    {
       slove(i);
    }
    return 0;
}

/*keeanjtemkmmjlsrjhsoijtamadlsbhifebsfmoscgkesibtil*/


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<cmath>
#include<map>
#include<string>

using namespace std;
const int Maxv=100100;
const int Inf=1000000000;
int m;
char str[Maxv][10];
char stk[Maxv];
int tot;
bool flag;
int id[Maxv];
char ss[Maxv];

void Put(int n)
{
    int i,j,k=0;
    j=0;
    for(i=0;i<n;i++)
    {
        if(str[i][0]=='p')
        {
            id[k++]=2;
            ss[j++]='p';
            ss[j++]='<';
        }
        else if(str[i][0]=='i')
        {
            if(id[k-1]>0)
            {
                id[k-1]--;
                ss[j++]='i';
                if(id[k-1]==1)
                    ss[j++]=',';
                else if(id[k-1]==0)
                {
                    k--;
                    if(k-1>=0)
                    id[k-1]--;
                    ss[j++]='>';
                }
            }
        }
    }
    int t=0;
    k=0;
    for(i=0;i<j;i++)
    {
        if(ss[i]=='<') t++;
        if(ss[i]=='>') k++;
        if(ss[i-1]=='>' && ss[i]=='p') cout<<",";
        if(ss[i-1]=='>'&&ss[i]=='i') cout<<",";
        if(ss[i]=='p')
        printf("pair");
        else if(ss[i]=='i')
        printf("int");
        else
        printf("%c",ss[i]);
    }
    while(t>k)
    {
        cout<<">";
        k++;
    }
    cout<<endl;
}

void slove(int len)
{
    bool flag=true;
    for(int i=0;i<len;i++)
    {
        stk[tot++]=str[i][0];
        if(flag)
        {
            while(tot&&stk[tot-1]=='i')
            {
                if(tot==1)
                {
                    flag=false;
                    break;
                }
                if(stk[tot-2]=='i')
                {
                    if(tot==2||stk[tot-3]!='p')
                    {
                        flag=false;
                        break;
                    }
                    else
                    {
                        if(tot==3)
                        {
                            if(i!=len-1)
                            flag=false;
                            tot=0;
                        }
                        else
                        {
                            stk[tot-3]='i';tot-=2;
                        }
                    }
                }
                else
                break;
            }
        }
    }
    if(tot)
    flag=false;
    if(!flag)
    {
        puts("Error occurred");
        return ;
    }
    Put(len);
}

int main()
{
    int i,j=0,k;
    int n;
    scanf("%d",&n);
    for(i=0;j<n;i++)
    {
        scanf("%s",str[i]);
        if(str[i][0]=='i')
        j++;
    }
    if(i==1&&n==1&&str[0][0]=='i')
    puts("int");
    else
    {
       slove(i);
    }
    return 0;
}

/*ilehlcfjlbncqodqjsjdssnakqtqccocfsbrnhseadgijkqtaf*/


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#define ll (v<<1)
#define rr (v<<1|1)

using namespace std;

char str[1001111];
char s[2001111];
int stack[100000];
int main(){
	int n,num,i,j,flag,cnt,pnum,sc;
	scanf("%d\n",&n);
	gets(str);
	if(n==1){
		num=0;
		for(i=0;str[i];i++)
			if(str[i]=='i') num++;
		if(strstr(str,"int") && num==1) puts("int");
		else puts("Error occurred");
		return 0;
	}
	num=0;
	flag=0;
	cnt=0;
	sc=0;
	s[sc++]=str[0];
	if(str[0]=='p')  pnum=1;
	else pnum=0;
	if(str[0]==' ') flag=1;
	for(i=1;str[i] && !flag;i++){
		if(str[i]=='i' && str[i+1]=='n')
			num++;
		if(str[i]=='p') pnum++;
		if(str[i]==' '){
			if(str[i-1]=='r'){
				s[sc++]='<';
				stack[++cnt]=1;
			}
			else if(str[i-1]=='t' || str[i-1]=='>'){
				if(cnt==0){
					flag=1;
					break;
				}
				stack[cnt]++;
				if(stack[cnt]==3){
					while(stack[cnt]==3){
						s[sc++]='>';
						pnum--;
						cnt--;
						if(cnt==0) break;
						stack[cnt]++;
					}
				}
				if(stack[cnt]==2){
					s[sc++]=',';
				}
			}
		}
		else s[sc++]=str[i];
	}
	stack[cnt]++;
	while(stack[cnt]==3){
		s[sc++]='>';
		pnum--;
		cnt--;
		if(cnt==0) break;
		stack[cnt]++;
	}
	s[sc]=0;
//	puts(s);
	if(cnt!=0 || flag || num!=n ||  pnum!=0) puts("Error occurred");
	else	puts(s);
	return 0;
}
/*

5
pair pair pair int int int pair int int
pair<pair<pair<int,int>,int>,pair<int,int>>

1
int

3
pair int pair int int

*/

/*phsmimaphgarqejekoigotmtbjlksdgfkejkrjgqhsneooaqbi*/


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<cmath>
#include<map>
#include<string>

using namespace std;
const int Maxv=100100;
const int Inf=1000000000;
int m;
char str[Maxv][10];
bool flag;
int l;
int ls[Maxv];int rs[Maxv];
int len;
bool vis[Maxv];

void dfs(int x)
{
    if(x>=len)
    {
        flag=false;
        return ;
    }
    vis[x]=true;
    l++;
    if(str[x][0]=='p')
    {
        ls[x]=l;
        dfs(l);
        rs[x]=l;
        dfs(l);
    }
}

void dfsp(int x)
{
    printf("%s",str[x]);
    if(str[x][0]=='p')
    {
        printf("<");
        dfsp(ls[x]);
        printf(",");
        dfsp(rs[x]);
        printf(">");
    }
}
void Put2()
{
    flag=true;
    l=0;
    dfs(0);
    for(int i=0;i<len;i++)
    if(!vis[i])
    flag=false;
    if(!flag)
    {
        puts("Error occurred");
        return ;
    }
    dfsp(0);
    puts("");
}
int main()
{
    int i,j=0,k;
    int n;
    scanf("%d",&n);
    for(i=0;;i++)
    {
        int c=scanf("%s",str[i]);
        if(c<0)
        break;
    }
    len=i;
    Put2();
    return 0;
}

/*mrmsrrrksndjmmgtrbdtietcagrqpqeaniscsimqbprftderfh*/


#include<iostream>
#include<string>
using namespace std;
int main()
{
	int n;
	string s[411111]="";
	string s1="";
	cin>>n;
	int ans=0;
	int num1;
	int num2;
	num1=0;
	num2=0;
	while(n)
	{
		cin>>s1;
		if(s1=="pair")
		{
		   s[ans++]=s1;
		   num1+=2;
		   if(num2==0)num2+=2;
		   else num2+=1;
		}
		else
		{
			s[ans++]=s1;
			num2--;
			n--;
		}

	}
	string s2="";
	int i,j;
	if(num2!=0)
	{
		cout<<"Error occurred"<<endl;
	}
	else
	{
		for(i=0;i<ans;i++)
	    {
    	   if(i==0&&s[i]=="pair")
    	   {
		      cout<<s[i];
		      cout<<"<";
		      s2="<";
		      num1--;
    	   }
 	       else
 	       {
 	       	 if(s[i]=="pair")
 	       	 {
 	       	 	if(s2==","||s2=="<")
 	       	 	{
 	       	 		cout<<s[i];
 	       	 		cout<<"<";
 	 		        s2="<";
 	 		        num1--;
 	       	 	}
 	       	 	else if(s2==">")
			    {
			        cout<<","<<s[i]<<"<";
					s2="<";
					num1--;
			    }
 	       	 }
 	       	 else
 	       	 {
       	 	     if(s2=="<")
       	 	     {
       	 	     	cout<<s[i]<<",";
       	 	     	s2=",";
       	 	     }
       	 	     else if(s2==",")
       	 	     {
       	 	     	cout<<s[i]<<">";
       	 	     	s2=">";
       	 	     	num1--;
       	 	     }
       	 	     else if(s2==">")
       	 	     {
       	 	       cout<<","<<s[i]<<">";
				   s2=">";
				   num1--;
       	 	     }
 	       	 }
 	       }
	    }
	    for(j=0;j<num1;j++)
	     cout<<">";
        cout<<endl;
	}
	return 0;
}

/*asflqhmcjilbrfhefmgoopjdmjoortsjjpafofapflrciqgnan*/


#include<iostream>
#include<string>
#include<cstring>
using namespace std;
string s[111111]="";
int a[111111];
int ans;
int main()
{
	int n;
	string s1="";
	cin>>n;
	int num2;
	int i,j;
	num2=0;
	ans=0;
	memset(a,0,sizeof(a));
	int ant=0;
	while(n)
	{
		cin>>s1;
		if(s1=="pair")
		{
		   s[ans++]=s1;
		   if(num2==0)num2+=2;
		   else num2+=1;
		}
		else
		{
			s[ans++]=s1;
			num2--;
			n--;
		}
		s1="";
	}
	if(num2!=0)
	{
		cout<<"Error occurred"<<endl;
	}
	else
	{
		for(i=0;i<ans;i++)
		{
			if(s[i]=="pair")
			{
				if(i==0)
				cout<<s[i]<<"<";
				else
				{
					if(s[i-1]=="int")
					cout<<",";
					cout<<s[i]<<"<";
				}
				a[ant++]=0;
			}
			else
			{
			  if(s[i-1]=="int")
			  {
                   cout<<","<<s[i];
                   a[ant-1]++;
				   for(j=ant-1;j>=0;j--)
				   {
				       if(a[ant-1]==2)
				       {
	   					cout<<">";
	   					ant--;
	   					a[ant-1]++;
	   				   }
	   				   else break;
				   }
              }
             else
  			  {
  			  	 cout<<s[i];
  			  	 a[ant-1]++;
  			  }
			}
		}
		cout<<endl;
	}
	return 0;
}
/*
5
pair pair pair int pair int int int int
*/

/*mofgslbecqsbqthrbrhgefdmfosqbjnopkmobnsqjrjgiqpjfo*/


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<cmath>
#include<map>
#include<string>

using namespace std;
const int Maxv=100100;
const int Inf=1000000000;
int m;
char str[Maxv][10];
char stk[Maxv];
int tot;
bool flag;
int id[Maxv];
char ss[Maxv];

void Put(int n)
{
    int i,j,k=0;
    j=0;
    for(i=0;i<n;i++)
    {
        if(str[i][0]=='p')
        {
            id[k++]=2;
            ss[j++]='p';
            ss[j++]='<';
        }
        else if(str[i][0]=='i')
        {
            if(id[k-1]>0)
            {
                id[k-1]--;
                ss[j++]='i';
                if(id[k-1]==1)
                    ss[j++]=',';
                else if(id[k-1]==0)
                {
                    k--;
                    if(k-1>=0)
                    id[k-1]--;
                    ss[j++]='>';
                }
            }
        }
    }
    int t=0;
    k=0;
    for(i=0;i<j;i++)
    {
        if(ss[i]=='<') t++;
        if(ss[i]=='>') k++;
        if(ss[i-1]=='>' && ss[i]=='p') cout<<",";
        if(ss[i-1]=='>'&&ss[i]=='i') cout<<",";
        if(ss[i]=='p')
        printf("pair");
        else if(ss[i]=='i')
        printf("int");
        else
        printf("%c",ss[i]);
    }
    while(t>k)
    {
        cout<<">";
        k++;
    }
    cout<<endl;
}
int l;
int ls[Maxv];int rs[Maxv];
void dfs(int x)
{
    l++;
    if(str[x][0]=='p')
    {
        ls[x]=l;
        dfs(l);
        rs[x]=l;
        dfs(l);
    }
    else
    {
        ls[x]=-1;rs[x]=-1;
    }
}

void dfsp(int x)
{
    printf("%s",str[x]);
    if(str[x][0]=='p')
    {
        printf("<");
        dfsp(ls[x]);
        printf(",");
        dfsp(rs[x]);
        printf(">");
    }
}
void Put2()
{
    l=0;dfs(0);
    dfsp(0);
    puts("");
}

void slove(int len)
{
    bool flag=true;
    for(int i=0;i<len;i++)
    {
        stk[tot++]=str[i][0];
        if(flag)
        {
            while(tot&&stk[tot-1]=='i')
            {
                if(tot==1)
                {
                    flag=false;
                    break;
                }
                if(stk[tot-2]=='i')
                {
                    if(tot==2||stk[tot-3]!='p')
                    {
                        flag=false;
                        break;
                    }
                    else
                    {
                        if(tot==3)
                        {
                            if(i!=len-1)
                            flag=false;
                            tot=0;
                        }
                        else
                        {
                            stk[tot-3]='i';tot-=2;
                        }
                    }
                }
                else
                break;
            }
        }
    }
    if(tot)
    flag=false;
    if(!flag)
    {
        puts("Error occurred");
        return ;
    }
    //Put(len);
    Put2();
}

int main()
{
    int i,j=0,k;
    int n;
    scanf("%d",&n);
    for(i=0;j<n;i++)
    {
        scanf("%s",str[i]);
        if(str[i][0]=='i')
        j++;
    }
    if(i==1&&n==1&&str[0][0]=='i')
    puts("int");
    else if(i!=n+n-1)
    {
        puts("Error occurred");
    }
    else
    {
       slove(i);
    }
    return 0;
}

/*brelfqronkbigjpklkbpopjtqsdqdteqiiiorrakhblolgqcqr*/


#include <queue>
#include <algorithm>
#include <cstring>
#include <string>
#include <iostream>
#include <cstdio>

using namespace std;

string ans;
char str[1000000],a[10];
int n,ret,leap,flag,s1,s2,q1,q2;

queue<string>s;

void dfs()
{
     while(!s.empty())
     {
          if(ret==n)leap=1;
          if(s.front()=="pair")
          {
              ans+="pair";
              q2++;
              s.pop();
              ans+="<";
              dfs();
              ans+=",";
              dfs();
              if(!flag)leap=1;
              else flag=0;
              ans+=">";
              flag=1;
              return ;
          }
          else
          {
              ans+="int";
              s2++;
              flag=1;
              s.pop();
              ret++;
              return ;
          }
     }
}

int main()
{
          scanf("%d",&n);
          getchar();
          gets(str);
          if(n==1&&strlen(str)==3)
          printf("int\n");
          else
          if(n<2)printf("Error occurred\n");
          else
          {
              int len=strlen(str);
              str[len++]=' ';
              int l=0;
              s1=0;s2=0;q1=0;q2=0;
              for(int i=0; i<len; i++)
              {
                   if(str[i]==' ')
                   {
                       a[l]='\0';
                       if(l==3)s1++;
                       else if(l==4)q1++;
                       l=0;
                       s.push(a);
                   }
                   else a[l++]=str[i];
              }
              ret=0;
              leap=0;
              flag=0;
              dfs();
              if(leap||s1!=s2||q1!=q2)printf("Error occurred\n");
              else
              cout<<ans<<endl;

        }     //getchar();
              //getchar();
}


#include <queue>
#include <algorithm>
#include <cstring>
#include <string>
#include <iostream>
#include <cstdio>

using namespace std;

string ans;
char str[1000000],a[10],n,ret,leap,flag;

queue<string>s;

void dfs()
{
     while(!s.empty())
     {
          if(ret==n)leap=1;
          if(s.front()=="pair")
          {
              ans+="pair";
              s.pop();
              ans+="<";
              dfs();
              ans+=",";
              dfs();
              if(!flag)leap=1;
              else flag=0;
              ans+=">";
              return ;
          }
          else
          {
              ans+="int";
              flag=1;
              s.pop();
              ret++;
              return ;
          }
     }
}

int main()
{
          scanf("%d",&n);
          getchar();
          gets(str);
          //if(n<2)printf("Error occurred\n");
          //else
          {
              int len=strlen(str);
              str[len++]=' ';
              int l=0;
              for(int i=0; i<len; i++)
              {
                   if(str[i]==' ')
                   {
                       a[l]='\0';
                       l=0;
                       s.push(a);
                   }
                   else a[l++]=str[i];
              }
              ret=0;
              leap=0;
              flag=0;
              dfs();
              if(leap)printf("Error occurred\n");
              else
              cout<<ans<<endl;
              //getchar();
              //getchar();
        }
}


#include <queue>
#include <algorithm>
#include <cstring>
#include <string>
#include <iostream>
#include <cstdio>

using namespace std;

string ans;
char str[1000000],a[10];
int n,ret,leap,flag,s1,s2;

queue<string>s;

void dfs()
{
     while(!s.empty())
     {
          if(ret==n)leap=1;
          if(s.front()=="pair")
          {
              ans+="pair";
              s.pop();
              ans+="<";
              dfs();
              ans+=",";
              dfs();
              if(!flag)leap=1;
              else flag=0;
              ans+=">";
              flag=1;
              return ;
          }
          else
          {
              ans+="int";
              s2++;
              flag=1;
              s.pop();
              ret++;
              return ;
          }
     }
}

int main()
{
          scanf("%d",&n);
          getchar();
          gets(str);
          if(n==1&&strlen(str)==3)
          printf("int\n");
          else
          if(n<2)printf("Error occurred\n");
          else
          {
              int len=strlen(str);
              str[len++]=' ';
              int l=0;
              s1=0;s2=0;
              for(int i=0; i<len; i++)
              {
                   if(str[i]==' ')
                   {
                       a[l]='\0';
                       if(l==3)s1++;
                       l=0;
                       s.push(a);
                   }
                   else a[l++]=str[i];
              }
              ret=0;
              leap=0;
              flag=0;
              dfs();
              if(leap||s1!=s2)printf("Error occurred\n");
              else
              cout<<ans<<endl;

        }     //getchar();
              //getchar();
}


#include <iostream>
#include <math.h>
#include <vector>
#include <algorithm>
#include <map>
#include <string>
#include <cstring>
#include <bitset>
#include <sstream>
#include <set>
#include <stack>
#include <tuple>

#define FI(n) for(int i=0;i<n;++i)
#define FJ(n) for(int j=0;j<n;++j)
#define FK(n) for(int k=0;k<n;++k)
#define FL(n) for(int l=0;l<n;++l)
#define FQ(n) for(int q=0;q<n;++q)

#define MAX_N 100001

using namespace std;
using ll = long long;

bool desc_comp(char a, char b) {
    return a > b;
}
bool checked[30];
int main() {
    int n, k;
    string str;

    scanf("%d %d", &n, &k);
    cin >> str;
    vector<char> alpha = vector<char>();
    for (auto a : str) {
        if (!checked[a  - 'a']) {
            checked[a  - 'a'] = true;
            alpha.push_back(a);
        }
    }

    char ans[MAX_N];
    ans[k] = '\0';
    sort(alpha.begin(), alpha.end(), desc_comp);
    int idx = -1;

    if (k > n) {
        idx = k - 1;
        ans[idx] = alpha.at(alpha.size() - 1);
    } else {
        for (int i = k - 1; i > -1; i--) {
            for (auto a : alpha) {
                if (str[i] < a) {
                    idx = i;
                    ans[i] = a;
                    break;
                }
            }
            if (idx != -1)
                break;
        }
    }

    for (int i = idx - 1; i > -1; i--) {
        ans[i] = str[i];
    }

    for (int i = idx + 1; i < k; i++) {
        ans[i] = alpha.at(alpha.size() - 1);
    }

    cout << ans << endl;

    return 0;
}


#include <iostream>
#include <math.h>
#include <vector>
#include <algorithm>
#include <map>
#include <string>
#include <cstring>
#include <bitset>
#include <sstream>
#include <set>
#include <stack>
#include <tuple>

#define FI(n) for(int i=0;i<n;++i)
#define FJ(n) for(int j=0;j<n;++j)
#define FK(n) for(int k=0;k<n;++k)
#define FL(n) for(int l=0;l<n;++l)
#define FQ(n) for(int q=0;q<n;++q)

#define MAX_N 100003

using namespace std;
using ll = long long;

bool desc_comp(char a, char b) {
    return a > b;
}
bool checked[30];
int main() {
    int n, k;
    string str;

    scanf("%d %d", &n, &k);
    cin >> str;
    vector<char> alpha = vector<char>();
    for (auto a : str) {
        if (!checked[a  - 'a']) {
            checked[a  - 'a'] = true;
            alpha.push_back(a);
        }
    }

    char ans[MAX_N];
    ans[k] = '\0';
    sort(alpha.begin(), alpha.end(), desc_comp);
    int idx = -1;

    if (k > n) {
        idx = k - 1;
        ans[idx] = alpha.at(alpha.size() - 1);
    } else {
        for (int i = k - 1; i > -1; i--) {
            for (auto a : alpha) {
                if (str[i] < a) {
                    idx = i;
                    ans[i] = a;
                    break;
                }
            }
            if (idx != -1)
                break;
        }
    }

    for (int i = idx - 1; i > -1; i--) {
        if (i >= n)
            ans[i] = alpha.at(alpha.size() - 1);
        else
            ans[i] = str[i];
    }

    for (int i = idx + 1; i < k; i++) {
        ans[i] = alpha.at(alpha.size() - 1);
    }

    cout << ans << endl;

    return 0;
}


#include <iostream>
#include <math.h>
#include <vector>
#include <algorithm>
#include <map>
#include <string>
#include <cstring>
#include <bitset>
#include <sstream>
#include <set>
#include <stack>
#include <tuple>

#define FI(n) for(int i=0;i<n;++i)
#define FJ(n) for(int j=0;j<n;++j)
#define FK(n) for(int k=0;k<n;++k)
#define FL(n) for(int l=0;l<n;++l)
#define FQ(n) for(int q=0;q<n;++q)

#define MAX_N 100001

using namespace std;
using ll = long long;

bool desc_comp(char a, char b) {
    return a > b;
}
bool checked[30];
int main() {
    int n, k;
    string str;

    scanf("%d %d", &n, &k);
    cin >> str;
    vector<char> alpha = vector<char>();
    for (auto a : str) {
        if (!checked[a  - '0']) {
            checked[a  - '0'] = true;
            alpha.push_back(a);
        }
    }

    char ans[MAX_N];
    ans[k] = '\0';
    sort(alpha.begin(), alpha.end(), desc_comp);
    int idx = -1;

    if (k > n) {
        idx = k - 1;
        ans[idx] = alpha.at(alpha.size() - 1);
    } else {
        for (int i = k - 1; i > -1; i--) {
            for (auto a : alpha) {
                if (str[i] < a) {
                    idx = i;
                    ans[i] = a;
                    break;
                }
            }
            if (idx != -1)
                break;
        }
    }

    for (int i = idx - 1; i > -1; i--) {
        ans[i] = str[i];
    }

    for (int i = idx + 1; i < k; i++) {
        ans[i] = alpha.at(alpha.size() - 1);
    }

    cout << ans << endl;

    return 0;
}


#include <iostream>
#include <math.h>
#include <vector>
#include <algorithm>
#include <map>
#include <string>
#include <cstring>
#include <bitset>
#include <sstream>
#include <set>
#include <stack>
#include <tuple>

#define FI(n) for(int i=0;i<n;++i)
#define FJ(n) for(int j=0;j<n;++j)
#define FK(n) for(int k=0;k<n;++k)
#define FL(n) for(int l=0;l<n;++l)
#define FQ(n) for(int q=0;q<n;++q)

#define MAX_N 100003

using namespace std;
using ll = long long;

bool desc_comp(char a, char b) {
    return a > b;
}
bool checked[30];
int main() {
    int n, k;
    string str;

    scanf("%d %d", &n, &k);
    cin >> str;
    vector<char> alpha = vector<char>();
    for (auto a : str) {
        if (!checked[a  - 'a']) {
            checked[a  - 'a'] = true;
            alpha.push_back(a);
        }
    }

    char ans[MAX_N];
    ans[k] = '\0';
    sort(alpha.begin(), alpha.end(), desc_comp);
    int idx = -1;

    if (k > n) {
        idx = k - 1;
        ans[idx] = alpha.at(alpha.size() - 1);
    } else {
        for (int i = k - 1; i > -1; i--) {
            for (int j = alpha.size() - 1; j > -1; j--) {
                char a = alpha[j];
                if (str[i] < a) {
                    idx = i;
                    ans[i] = a;
                    break;
                }
            }
            if (idx != -1)
                break;
        }
    }

    for (int i = idx - 1; i > -1; i--) {
        if (i >= n)
            ans[i] = alpha.at(alpha.size() - 1);
        else
            ans[i] = str[i];
    }

    for (int i = idx + 1; i < k; i++) {
        ans[i] = alpha.at(alpha.size() - 1);
    }

    cout << ans << endl;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
#define pb push_back
#define all(c) c.begin(),c.end()
#define allr(c) c.rbegin(),c.rend()
#define MOD 998244353
typedef double ld;
#define PI 3.14159265

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

    int n , k;
    cin >> n >> k;
    string str;
    cin >> str;
    set<char> s;
    for(auto it : str) s.insert(it);
    char cm = *s.rbegin();
    if(n == k) {
        for(int i = n - 1 ; i >= 1 ; i--) {
            if(str[i] != str[i - 1]) {
                swap(str[i] , str[i - 1]);
                break;
            }
        }
        str.erase(n - 1 , 1);
        str.insert(n - 1 , 1 , *s.begin());
        cout << str << endl;
    }
    else if(k > n) {
        int ind = n - 2;
        for(int i = 0 ; i < k - n ; i++) {
            str.insert(++ind , 1 , *s.begin());
        }
        cout << str << endl;
    }
    else {
        char temp = 0;
        int ind = 0;
        for(int i = n - 1 ; i >= 0 ; i--) {
            if(i == n - 1) {
                temp = str[i];
            }
            else if(str[i] != temp && str[i] < cm) {

                auto it = s.upper_bound(str[i]);

                str[i] = *it;
                ind = i;
                break;
            }
        }
        for(int i = 0 ; i <= ind ; i++) {
            cout << str[i];
        }
        cout << endl;
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
#define pb push_back
#define all(c) c.begin(),c.end()
#define allr(c) c.rbegin(),c.rend()
#define MOD 998244353
typedef double ld;
#define PI 3.14159265

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

    int n , k;
    cin >> n >> k;
    string str;
    cin >> str;
    set<char> s;
    for(auto it : str) s.insert(it);
    char cm = *s.rbegin();
    if(n == k) {
        char temp = 0;
        int ind = 0;
        for(int i = n - 1 ; i >= 0 ; i--) {
            if(i == n - 1) {
                temp = str[i];
            }
            else if(str[i] != temp && str[i] < cm) {

                auto it = s.upper_bound(str[i]);

                str[i] = *it;
                ind = i;
                break;
            }
        }
        for(int i = ind + 1 ; i < n ; i++) {
            str[i] = *s.begin();
        }

        cout << str << endl;
    }
    else if(k > n) {
        int ind = n - 1;
        for(int i = 0 ; i < k - n ; i++) {
            str.insert(++ind , 1 , *s.begin());
        }
        cout << str << endl;
    }
    else {
        char temp = 0;
        int ind = 0;
        for(int i = n - 1 ; i >= 0 ; i--) {
            if(i == n - 1) {
                temp = str[i];
            }
            else if(str[i] != temp && str[i] < cm) {

                auto it = s.upper_bound(str[i]);

                str[i] = *it;
                ind = i;
                break;
            }
        }
        for(int i = 0 ; i <= ind ; i++) {
            cout << str[i];
        }
        cout << endl;
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
#define pb push_back
#define all(c) c.begin(),c.end()
#define allr(c) c.rbegin(),c.rend()
#define MOD 998244353
typedef double ld;
#define PI 3.14159265

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

    int n , k;
    cin >> n >> k;
    string str;
    cin >> str;
    set<char> s;
    for(auto it : str) s.insert(it);
    char cm = *s.rbegin();

    if(n == k) {
        string str1 = "{";

        if(str[n - 1] < cm && *s.upper_bound(str[n - 1]) != str[n - 1]) {
            str1 = str;
            sort(str1.begin() + 1 , str1.end());
            str1[n - 1] = *s.upper_bound(str1[n - 1]);
        }
        char temp = 0;
        int ind = 0;
        for(int i = n - 1 ; i >= 0 ; i--) {
            if(i == n - 1) {
                temp = str[i];
            }
            else if(str[i] != temp && str[i] < cm) {

                auto it = s.upper_bound(str[i]);

                str[i] = *it;
                ind = i;
                break;
            }
        }
        for(int i = ind + 1 ; i < n ; i++) {
            str[i] = *s.begin();
        }

        cout << min(str , str1) << endl;
    }
    else if(k > n) {
        int ind = n - 1;
        for(int i = 0 ; i < k - n ; i++) {
            str.insert(++ind , 1 , *s.begin());
        }
        cout << str << endl;
    }
    else {
        char temp = 0;
        int ind = 0;
        for(int i = n - 1 ; i >= 0 ; i--) {
            if(i == n - 1) {
                temp = str[i];
            }
            else if(str[i] != temp && str[i] < cm) {

                auto it = s.upper_bound(str[i]);

                str[i] = *it;
                ind = i;
                break;
            }
        }
        for(int i = 0 ; i <= ind ; i++) {
            cout << str[i];
        }
        cout << endl;
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
#define pb push_back
#define all(c) c.begin(),c.end()
#define allr(c) c.rbegin(),c.rend()
#define MOD 998244353
typedef double ld;
#define PI 3.14159265

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

    int n , k;
    cin >> n >> k;
    string str;
    cin >> str;
    set<char> s;
    for(auto it : str) s.insert(it);
    char cm = *s.rbegin();
    if(n == k) {
        for(int i = n - 1 ; i >= 1 ; i--) {
            if(str[i] != str[i - 1]) {
                swap(str[i] , str[i - 1]);
                break;
            }
        }
        str.erase(n - 1 , 1);
        str.insert(n - 1 , 1 , *s.begin());
        cout << str << endl;
    }
    else if(k > n) {
        int ind = n - 1;
        for(int i = 0 ; i < k - n ; i++) {
            str.insert(++ind , 1 , *s.begin());
        }
        cout << str << endl;
    }
    else {
        char temp = 0;
        int ind = 0;
        for(int i = n - 1 ; i >= 0 ; i--) {
            if(i == n - 1) {
                temp = str[i];
            }
            else if(str[i] != temp && str[i] < cm) {

                auto it = s.upper_bound(str[i]);

                str[i] = *it;
                ind = i;
                break;
            }
        }
        for(int i = 0 ; i <= ind ; i++) {
            cout << str[i];
        }
        cout << endl;
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
#define pb push_back
#define all(c) c.begin(),c.end()
#define allr(c) c.rbegin(),c.rend()
#define MOD 998244353
typedef double ld;
#define PI 3.14159265

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

    int n , k;
    cin >> n >> k;
    string str;
    cin >> str;
    set<char> s;
    for(auto it : str) s.insert(it);
    char cm = *s.rbegin();

    if(n == k) {
        string str1 = "{";
        sort(str1.begin() + 1 , str1.end());
        if(str1[n - 1] < cm && *s.upper_bound(str1[n - 1]) != str[n - 1]) {
            str1[n - 1] = *s.upper_bound(str1[n - 1]);
        }
        char temp = 0;
        int ind = 0;
        for(int i = n - 1 ; i >= 0 ; i--) {
            if(i == n - 1) {
                temp = str[i];
            }
            else if(str[i] != temp && str[i] < cm) {

                auto it = s.upper_bound(str[i]);

                str[i] = *it;
                ind = i;
                break;
            }
        }
        for(int i = ind + 1 ; i < n ; i++) {
            str[i] = *s.begin();
        }

        cout << min(str , str1) << endl;
    }
    else if(k > n) {
        int ind = n - 1;
        for(int i = 0 ; i < k - n ; i++) {
            str.insert(++ind , 1 , *s.begin());
        }
        cout << str << endl;
    }
    else {
        char temp = 0;
        int ind = 0;
        for(int i = n - 1 ; i >= 0 ; i--) {
            if(i == n - 1) {
                temp = str[i];
            }
            else if(str[i] != temp && str[i] < cm) {

                auto it = s.upper_bound(str[i]);

                str[i] = *it;
                ind = i;
                break;
            }
        }
        for(int i = 0 ; i <= ind ; i++) {
            cout << str[i];
        }
        cout << endl;
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
#define pb push_back
#define all(c) c.begin(),c.end()
#define allr(c) c.rbegin(),c.rend()
#define MOD 998244353
typedef double ld;
#define PI 3.14159265

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

    int n , k;
    cin >> n >> k;
    string str;
    cin >> str;
    set<char> s;
    for(auto it : str) s.insert(it);
    char cm = *s.rbegin();

    if (n >= k) {
        string temp = str.substr(0 , k);
        for(int i = (int)temp.size() - 1 ; i >= 0 ; i--) {
            if(temp[i] < cm) {
                temp[i] = *s.upper_bound(temp[i]);
                break;
            }
            else if(temp[i] == cm) {
                temp[i] = *s.begin();
            }
        }
        cout << temp << endl;
    }
    else {
        int ind = n - 1;
        for(int i = 0 ; i < k - n ; i++) {
            str.insert(++ind , 1 , *s.begin());
        }
        cout << str << endl;
    }

    return 0;
}


#include <bits\stdc++.h>

using namespace std;

int n, k;
string a;
set <char> have;

int main() {
  cin >> n >> k >> a;
  for (auto i : a)
    have.insert(i);
  if (k > n) {
    cout << a;
    for (int j = n; j < k; j++)
      cout << *have.begin();
  }
  else {
    string ans = "";
    int ok = 0;
    for (int i = 0; i < k; i++) {
      ans += *have.begin();
    }
    for (int i = k - 1 ; i > -1; i--) {
      if (have.upper_bound(a[i]) != have.end()) {
        ans[i] = *have.upper_bound(a[i]);
        for (int j = i - 1; j > -1; j--)
          ans[j] = a[j];
        break;
      }
    }
    cout << ans;
  }
  return 0;
}


#include <bits/stdc++.h>

#define all(x) x.begin(), x.end()
#define y0 rerewrwerwrere
#define y1 alsdlsdlsd

using namespace std;

typedef long long intl;

inline void ARRIAS() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
#endif
}

int n, k;
string a;

struct cmp {
  bool operator() (char a, char b) {
    return a > b;
  }
};

set <char> f;

set<char> qew[100003];

signed main(void) {
  ARRIAS();
  cin >> n >> k >> a;
  for (int i = n - 1; i > -1; i--) {
    qew[i] = qew[i + 1];
    qew[i].insert(a[i]);
  }

  for (auto i : a)
    f.insert(i);
  auto q = f.end();
  q--;
  char l = *q;
  if (k > n) {
    cout << a + *f.begin();
  }
  else  if (n==k) {
    for (int i = 0; i < n; i++) {
      if (*qew[i + 1].begin() < l)
        cout << a[i];
      else {
        cout << l;
        for (int j = i + 1; j < n; j++)
          cout << *f.begin();
        break;
      }
    }
  }
  else {
    string ans = "";
    for (int i = k-1; i > -1; i--) {
      if (a[i] < l) {
        ans += l;
        for (int j = i - 1; j > -1; j--)
          ans += a[j];
        break;
      }
      else {
        ans += a[i];
      }
   }
    reverse(all(ans));
    cout << ans;
  }
  return 0;
}


#include <bits\stdc++.h>

using namespace std;

int n, k;
string a;
set <char> have;

int main() {
  cin >> n >> k >> a;
  for (auto i : a)
    have.insert(i);
  if (k > n) {
    cout << a;
    for (int j = n; j < k; j++)
      cout << *have.begin();
  }
  else {
    string ans = "";
    int ok = 0;
    for (int i = 0; i < k; i++) {
      ans += *have.begin();
    }
    for (int i = k -1 ; i > -1; i--) {
      if (have.upper_bound(a[i]) != have.end()) {
        ans[i] = *have.upper_bound(a[i]);
        break;
      }
    }
    cout << ans;
  }
  return 0;
}


#include <bits/stdc++.h>

#define all(x) x.begin(), x.end()
#define y0 rerewrwerwrere
#define y1 alsdlsdlsd

using namespace std;

typedef long long intl;

inline void ARRIAS() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
#endif
}

int n, k;
string a;

struct cmp {
  bool operator() (char a, char b) {
    return a > b;
  }
};

set <char> f;

set<char> qew[100003];

signed main(void) {
  ARRIAS();
  cin >> n >> k >> a;
  for (int i = n - 1; i > -1; i--) {
    qew[i] = qew[i + 1];
    qew[i].insert(a[i]);
  }

  for (auto i : a)
    f.insert(i);
  auto q = f.end();
  q--;
  char l = *q;
  if (k > n) {
    cout << a;
    for (int i=n; i<k;i++)
   cout<< *f.begin();
  }
  else  if (n==k) {
    for (int i = 0; i < n; i++) {
      if (*qew[i + 1].begin() < l)
        cout << a[i];
      else {
        cout << l;
        for (int j = i + 1; j < n; j++)
          cout << *f.begin();
        break;
      }
    }
  }
  else {
    string ans = "";
    for (int i = k-1; i > -1; i--) {
      if (a[i] < l) {
        ans += l;
        for (int j = i - 1; j > -1; j--)
          ans += a[j];
        break;
      }
      else {
        ans += a[i];
      }
   }
    reverse(all(ans));
    cout << ans;
  }
  return 0;
}


#include <bits\stdc++.h>

using namespace std;

int n, k;
string a;
set <char> have;

int main() {
  cin >> n >> k >> a;
  vector <int> pref(n);
  for (auto i : a)
    have.insert(i);
  int x = 0;
  for (int i = n - 1; i > -1; i--) {
    if (a[i] == *(--have.end())) {
      x++;
   }
    pref[i] = x;
  }
  if (k > n) {
    cout << a;
    for (int j = n; j < k; j++)
      cout << *have.begin();
  }
  else {
    string ans = "";
    int ok = 0;
    for (int i = 0; i < k; i++) {
      if ( (i==k-1 || pref[i + 1] == n-i-1 ) && !ok) {
        ans += *have.upper_bound(a[i]);
        ok = 1;
      }
      else {
        ans += *have.begin();
      }
    }
    cout << ans;
  }
  return 0;
}


#include <iostream>
#include <cstdio>
#include <cassert>
#include <vector>
#include <string>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <map>

using namespace std;

#define FOR(i, a, b) for (int i=(a); i<(b); i++)
#define REP(i, n) FOR(i, 0, n)
#define TRACE(x) cerr << #x << " = " << x << endl
#define _ << " _ " <<
#define X first
#define Y second
#define pb push_back
#define ll long long int
#define INF 0x3F3F3F3F
#define M_PI 3.14159265358979323846
#define EPS 1e-9

int P[30];
vector <char> v;

char FindNext(char c) {
    char s = 'a';
    FOR(i, 0, (int)v.size())
        if(c==v[i])
            s=v[i+1];
    return s;
}

int main(){
    int n, t;
    char c;
    string s, ns;
    cin >> n >> t;
    FOR(i, 0, n) {
        cin >> c;
        s+=c;
        P[c-'a']=1;
    }

    FOR(i, 0, 30)
        if(P[i]==1)
            v.pb((char)('a'+i));

    if(t>n) {
        ns=s;
        FOR(i, 0, t-n)
            ns+=v[0];
    }

    else {
        int pos = -1, beg = -1;
        for(int i = n-1; i >= 0; i--) {
            pos ++;
            if(s[i] == v[v.size()-1]) {
                beg = i;
                while(s[i] == v[v.size()-1]) {
                    pos++;
                    i--;
                }
            break;
            }
        }

        pos = n - pos;
        FOR(i, 0, t) {
            if(i == t - 1) {
                ns += FindNext(s[i]);
                break;
            }

            if(i+1 == pos && beg == t-1) {
                ns += FindNext(s[i]);
                FOR(j, i+1, t)
                    ns += v[0];
                break;
            }

            ns += s[i];
        }
    }

    cout << ns;

return 0;
}


#include <iostream>
#include <cstdio>
#include <cassert>
#include <vector>
#include <string>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <map>

using namespace std;

#define FOR(i, a, b) for (int i=(a); i<(b); i++)
#define REP(i, n) FOR(i, 0, n)
#define TRACE(x) cerr << #x << " = " << x << endl
#define _ << " _ " <<
#define X first
#define Y second
#define pb push_back
#define ll long long int
#define INF 0x3F3F3F3F
#define M_PI 3.14159265358979323846
#define EPS 1e-9

int P[30];
vector <char> v;

char FindNext(char c) {
    char s = 'a';
    FOR(i, 0, (int)v.size())
        if(c==v[i])
            s=v[i+1];
    return s;
}

int main(){
    int n, t;
    char c;
    string s, ns;
    cin >> n >> t;
    FOR(i, 0, n) {
        cin >> c;
        s+=c;
        P[c-'a']=1;
    }

    FOR(i, 0, 30)
        if(P[i]==1)
            v.pb((char)('a'+i));

    if(t>n) {
        ns=s;
        FOR(i, 0, t-n)
            ns+=v[0];
    }

    else {
        int pos = -1;
        for(int i = n-1; i >= 0; i--) {
            pos ++;
            if(s[i] == v[v.size()-1]) {
                while(s[i] == v[v.size()-1]) {
                    pos++;
                    i--;
                }
            break;
            }
        }

        pos = n - pos;
        FOR(i, 0, t) {
            //cout << "slovo: " << s[i] << " pozicija: " << pos << endl;
            if(i == t - 1) {
                ns += FindNext(s[i]);
                break;
            }

            if(i + 1 != pos) {
                ns += s[i];
                continue;
            }

            if(i+1 == pos) {
                ns += FindNext(s[i]);
                FOR(j, i+1, t)
                    ns += v[0];
                break;
            }
        }
    }

    cout << ns;

return 0;
}


#include <iostream>
#include <cstdio>
#include <cassert>
#include <vector>
#include <string>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <map>

using namespace std;

#define FOR(i, a, b) for (int i=(a); i<(b); i++)
#define REP(i, n) FOR(i, 0, n)
#define TRACE(x) cerr << #x << " = " << x << endl
#define _ << " _ " <<
#define X first
#define Y second
#define pb push_back
#define ll long long int
#define INF 0x3F3F3F3F
#define M_PI 3.14159265358979323846
#define EPS 1e-9

int P[30];
vector <char> v;

char FindNext(char c) {
    FOR(i, 0, (int)v.size())
        if(c==v[i])
            return v[i+1];
}

int main(){
    int n, t;
    char c;
    string s, ns;
    cin >> n >> t;
    FOR(i, 0, n) {
        cin >> c;
        s+=c;
        P[c-'a']=1;
    }

    FOR(i, 0, 30)
        if(P[i]==1)
            v.pb((char)('a'+i));

    if(t>n) {
        ns=s;
        FOR(i, 0, t-n)
            ns+=v[0];
    }

    else
        FOR(i, 0, t) {
            //cout << s[i] << " " << s[i+1] << " " << v[v.size()-1] << endl;
            if(s[i+1]!=v[v.size()-1]) {
                ns+=s[i];
                continue;
            }

            if(s[i+1]==v[v.size()-1]) {
                ns+=FindNext(s[i]);
                FOR(j, i+1, t)
                    ns+=v[0];
                break;
            }

            if(i==t-1)
                ns+=FindNext(s[i]);
        }

    cout << ns;

return 0;
}


#include <iostream>
#include <cstdio>
#include <cassert>
#include <vector>
#include <string>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <map>

using namespace std;

#define FOR(i, a, b) for (int i=(a); i<(b); i++)
#define REP(i, n) FOR(i, 0, n)
#define TRACE(x) cerr << #x << " = " << x << endl
#define _ << " _ " <<
#define X first
#define Y second
#define pb push_back
#define ll long long int
#define INF 0x3F3F3F3F
#define M_PI 3.14159265358979323846
#define EPS 1e-9

int P[30];
vector <char> v;

char FindNext(char c) {
    char s = 'a';
    FOR(i, 0, (int)v.size())
        if(c==v[i])
            s=v[i+1];
    return s;
}

int main(){
    int n, t;
    char c;
    string s, ns;
    cin >> n >> t;
    FOR(i, 0, n) {
        cin >> c;
        s+=c;
        P[c-'a']=1;
    }

    FOR(i, 0, 30)
        if(P[i]==1)
            v.pb((char)('a'+i));

    if(t>n) {
        ns=s;
        FOR(i, 0, t-n)
            ns+=v[0];
    }

    else {
        int pos = -1;
        for(int i = n-1; i >= 0; i--) {
            pos ++;
            if(s[i] == v[v.size()-1]) {
                while(s[i] == v[v.size()-1]) {
                    pos++;
                    i--;
                }
            break;
            }
        }
        pos = n - pos - 1;

        FOR(i, 0, t) {
            //cout << "slovo: " << s[i] << " pozicija: " << pos << endl;
            if(i == t - 1) {
                ns += FindNext(s[i]);
                break;
            }

            if(i + 1 != pos) {
                ns += s[i];
                continue;
            }

            if(i+1 == pos) {
                ns += FindNext(s[i]);
                FOR(j, i+1, t)
                    ns += v[0];
                break;
            }
        }
    }

    cout << ns;

return 0;
}


#include <iostream>
#include <cstdio>
#include <cassert>
#include <vector>
#include <string>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <map>

using namespace std;

#define FOR(i, a, b) for (int i=(a); i<(b); i++)
#define REP(i, n) FOR(i, 0, n)
#define TRACE(x) cerr << #x << " = " << x << endl
#define _ << " _ " <<
#define X first
#define Y second
#define pb push_back
#define ll long long int
#define INF 0x3F3F3F3F
#define M_PI 3.14159265358979323846
#define EPS 1e-9

int P[30];
vector <char> v;

char FindNext(char c) {
    char s = 'a';
    FOR(i, 0, (int)v.size())
        if(c==v[i])
            s=v[i+1];
    return s;
}

int main(){
    int n, t;
    char c;
    string s, ns;
    cin >> n >> t;
    FOR(i, 0, n) {
        cin >> c;
        s+=c;
        P[c-'a']=1;
    }

    FOR(i, 0, 30)
        if(P[i]==1)
            v.pb((char)('a'+i));

    if(t>n) {
        ns=s;
        FOR(i, 0, t-n)
            ns+=v[0];
    }

    else {
        int pos = -1;
        for(int i = n-1; i >= 0; i--)
            if(s[i] == v[v.size()-1]) {
                while(s[i] == v[v.size()-1]) {
                    pos++;
                    i--;
                }
            break;
        }
        pos = n - pos - 1;

        FOR(i, 0, t) {
            if(i==t-1) {
                ns+=FindNext(s[i]);
                break;
            }

            if(i+1!=pos) {
                ns+=s[i];
                continue;
            }

            if(i+1==pos) {
                ns+=FindNext(s[i]);
                FOR(j, i+1, t)
                    ns+=v[0];
                break;
            }
        }
    }

    cout << ns;

return 0;
}


#include <iostream>
#include <cstdio>
#include <cassert>
#include <vector>
#include <string>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <map>

using namespace std;

#define FOR(i, a, b) for (int i=(a); i<(b); i++)
#define REP(i, n) FOR(i, 0, n)
#define TRACE(x) cerr << #x << " = " << x << endl
#define _ << " _ " <<
#define X first
#define Y second
#define pb push_back
#define ll long long int
#define INF 0x3F3F3F3F
#define M_PI 3.14159265358979323846
#define EPS 1e-9

int P[30];
vector <char> v;

char FindNext(char c) {
    char s = 'a';
    FOR(i, 0, (int)v.size())
        if(c==v[i])
            s=v[i+1];
    return s;
}

int main(){
    int n, t;
    char c;
    string s, ns;
    cin >> n >> t;
    FOR(i, 0, n) {
        cin >> c;
        s+=c;
        P[c-'a']=1;
    }

    FOR(i, 0, 30)
        if(P[i]==1)
            v.pb((char)('a'+i));

    if(t>n) {
        ns=s;
        FOR(i, 0, t-n)
            ns+=v[0];
    }

    else {
        int pos = -1, beg = -1;
        for(int i = t-1; i >= 0; i--) {
            pos ++;
            if(s[i] == v[v.size()-1]) {
                beg = i;
                while(s[i] == v[v.size()-1]) {
                    pos++;
                    i--;
                }
            break;
            }
        }

        pos = t - pos;
        FOR(i, 0, t) {
            if(i == t - 1) {
                ns += FindNext(s[i]);
                break;
            }

            if(i+1 == pos && beg == t-1) {
                ns += FindNext(s[i]);
                FOR(j, i+1, t)
                    ns += v[0];
                break;
            }

            ns += s[i];
        }
    }

    cout << ns;

return 0;
}


#include <iostream>
#include <cstdio>
#include <cassert>
#include <vector>
#include <string>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <map>

using namespace std;

#define FOR(i, a, b) for (int i=(a); i<(b); i++)
#define REP(i, n) FOR(i, 0, n)
#define TRACE(x) cerr << #x << " = " << x << endl
#define _ << " _ " <<
#define X first
#define Y second
#define pb push_back
#define ll long long int
#define INF 0x3F3F3F3F
#define M_PI 3.14159265358979323846
#define EPS 1e-9

int P[30];
vector <char> v;

char FindNext(char c) {
    char s = 'a';
    FOR(i, 0, (int)v.size())
        if(c==v[i])
            s=v[i+1];
    return s;
}

int main(){
    int n, t;
    char c;
    string s, ns;
    cin >> n >> t;
    FOR(i, 0, n) {
        cin >> c;
        s+=c;
        P[c-'a']=1;
    }

    FOR(i, 0, 30)
        if(P[i]==1)
            v.pb((char)('a'+i));

    if(t>n) {
        ns=s;
        FOR(i, 0, t-n)
            ns+=v[0];
    }

    else {
        int pos = -1;
        for(int i = n-1; i >= 0; i--)
            if(s[i] == v[v.size()-1]) {
                while(s[i] == v[v.size()-1]) {
                    pos++;
                    i--;
                }
            break;
        }
        pos = n - pos - 1;

        FOR(i, 0, t) {
            if(i==t-1)
                ns+=FindNext(s[i]);

            if(s[i+1]!=v[v.size()-1]) {
                ns+=s[i];
                continue;
            }

            if(i+1==pos) {
                ns+=FindNext(s[i]);
                FOR(j, i+1, t)
                    ns+=v[0];
                break;
            }
        }
    }

    cout << ns;

return 0;
}


#include <iostream>
#include <cstdio>
#include <cassert>
#include <vector>
#include <string>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <map>

using namespace std;

#define FOR(i, a, b) for (int i=(a); i<(b); i++)
#define REP(i, n) FOR(i, 0, n)
#define TRACE(x) cerr << #x << " = " << x << endl
#define _ << " _ " <<
#define X first
#define Y second
#define pb push_back
#define ll long long int
#define INF 0x3F3F3F3F
#define M_PI 3.14159265358979323846
#define EPS 1e-9

int P[30];
vector <char> v;

char FindNext(char c) {
    char s = 'a';
    FOR(i, 0, (int)v.size())
        if(c==v[i])
            s=v[i+1];
    return s;
}

int main(){
    int n, t;
    char c;
    string s, ns;
    cin >> n >> t;
    FOR(i, 0, n) {
        cin >> c;
        s+=c;
        P[c-'a']=1;
    }

    FOR(i, 0, 30)
        if(P[i]==1)
            v.pb((char)('a'+i));

    if(t>n) {
        ns=s;
        FOR(i, 0, t-n)
            ns+=v[0];
    }

    else {
        int pos = -1;
        for(int i = n-1; i >= 0; i--)
            if(s[i] == v[v.size()-1]) {
                while(s[i] == v[v.size()-1]) {
                    pos++;
                    i--;
                }
            break;
        }
        pos = n - pos - 1;

        FOR(i, 0, t) {
            if(i==t-1) {
                ns+=FindNext(s[i]);
                break;
            }

            if(s[i+1]!=v[v.size()-1]) {
                ns+=s[i];
                continue;
            }

            if(i+1==pos) {
                ns+=FindNext(s[i]);
                FOR(j, i+1, t)
                    ns+=v[0];
                break;
            }
        }
    }

    cout << ns;

return 0;
}


#include <iostream>
#include <cstdio>
#include <cassert>
#include <vector>
#include <string>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <map>

using namespace std;

#define FOR(i, a, b) for (int i=(a); i<(b); i++)
#define REP(i, n) FOR(i, 0, n)
#define TRACE(x) cerr << #x << " = " << x << endl
#define _ << " _ " <<
#define X first
#define Y second
#define pb push_back
#define ll long long int
#define INF 0x3F3F3F3F
#define M_PI 3.14159265358979323846
#define EPS 1e-9

int P[30];
vector <char> v;

char FindNext(char c) {
    char s = 'a';
    FOR(i, 0, (int)v.size())
        if(c==v[i])
            s=v[i+1];
    return s;
}

int main(){
    int n, t;
    char c;
    string s, ns;
    cin >> n >> t;
    FOR(i, 0, n) {
        cin >> c;
        s+=c;
        P[c-'a']=1;
    }

    FOR(i, 0, 30)
        if(P[i]==1)
            v.pb((char)('a'+i));

    if(t>n) {
        ns=s;
        FOR(i, 0, t-n)
            ns+=v[0];
    }

    else {
        int pos = -1;
        for(int i = n-1; i >= 0; i--)
            if(s[i] == v[v.size()-1]) {
                while(s[i] == v[v.size()-1]) {
                    pos++;
                    i--;
                }
            break;
        }
        pos = n - pos - 1;

        FOR(i, 0, t) {
            if(s[i+1]!=v[v.size()-1]) {
                ns+=s[i];
                continue;
            }

            if(i+1==pos) {
                ns+=FindNext(s[i]);
                FOR(j, i+1, t)
                    ns+=v[0];
                break;
            }

            if(i==t-1)
                ns+=FindNext(s[i]);
        }
    }

    cout << ns;

return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define st string
#define fr first
#define se second
#define ld long double

const long double PI=3.1415926535897932384626433832795;
const long double EPS=0.00000000000000000000001;

using namespace std;

st s;
vector<char> v;
map<char, ll>  us;

int main()
{
    //freopen("veryhard.in", "r", stdin);
    //freopen("veryhard.out", "w", stdout);
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    cout.tie(NULL);

    ll n, k, i, j;
    cin>>n>>k>>s;

    //if("aaa"<"aaaa") cout<<1;
    int mx=1000000000;
    for(i=0; i<n; i++)
    {
        mx=min(int(s[i]), mx);
        if(us[s[i]]==0) { v.push_back(s[i]), us[s[i]]=1; }
    }
    sort(v.begin(), v.end());

    if(k>n)
    {

        cout<<s;
        for(i=1; i<=k-n; i++)
        {
            cout<<char(mx);
        }
        return 0;
    }

    //if(k==n)
        {
            st ans="";
            for(i=0; i<k; i++) ans=ans+s[i];

            ll x=k-1, y=v.size()-1;

            while(ans[x]==v[y]) { x--; }
            //s[x]=v[y];
            ans[x]=v[y];
            //cout<<s<<'\n';
            for(i=x+1; i<k; i++) {  ans[i]=v[0]; }

            cout<<ans;
            return 0;
        }

    return 0;
}
/**
1 2 4

*/


#include<bits/stdc++.h>
#define ll long long
#define st string
#define fr first
#define se second
#define ld long double

const long double PI=3.1415926535897932384626433832795;
const long double EPS=0.00000000000000000000001;

using namespace std;

st s;
vector<char> v;
map<char, ll>  us;

int main()
{
    //freopen("veryhard.in", "r", stdin);
    //freopen("veryhard.out", "w", stdout);
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    cout.tie(NULL);

    ll n, k, i, j;
    cin>>n>>k>>s;

    //if("aaa"<"aaaa") cout<<1;
    int mx=1000000000;
    for(i=0; i<n; i++)
    {
        mx=min(int(s[i]), mx);
        if(us[s[i]]==0) { v.push_back(s[i]), us[s[i]]=1; }
    }

    if(v.size()==1)
    {
        for(i=0; i<min(k, n)-1; i++)
        {
            cout<<s[i];
        }
        cout<<char(int(s[0])+1);
        return 0;
    }

    sort(v.begin(), v.end());

    if(k>n)
    {

        cout<<s;
        for(i=1; i<=k-n; i++)
        {
            cout<<char(mx);
        }
        return 0;
    }

    //if(k==n)
        {
            st ans="";
            for(i=0; i<k; i++) ans=ans+s[i];

            ll x=k-1, y=v.size()-1;

            while(ans[x]==v[y]) { x--; }
            //s[x]=v[y];

            mx=int(v[y]);
            for(i=x+1; i<n; i++)
                mx=min(int(s[i]), mx);

            ans[x]=char(mx);
            //cout<<s<<'\n';
            for(i=x+1; i<k; i++) {  ans[i]=v[0]; }

            cout<<ans;
            return 0;
        }

    return 0;
}
/**
1 2 4

*/


#include<bits/stdc++.h>
#define ll long long
#define st string
#define fr first
#define se second
#define ld long double

const long double PI=3.1415926535897932384626433832795;
const long double EPS=0.00000000000000000000001;

using namespace std;

st s;
vector<char> v;
map<char, ll>  us;

int main()
{
    //freopen("veryhard.in", "r", stdin);
    //freopen("veryhard.out", "w", stdout);
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    cout.tie(NULL);

    ll n, k, i, j;
    cin>>n>>k>>s;

    //if("aaa"<"aaaa") cout<<1;
    int mx=1000000000;
    for(i=0; i<n; i++)
    {
        mx=min(int(s[i]), mx);
        if(us[s[i]]==0) { v.push_back(s[i]), us[s[i]]=1; }
    }

    //cout<<v.size()<<'\n';
/*
    if(v.size()==1)
    {
        for(i=0; i<min(k, n)-1; i++)
        {
            cout<<s[i];
        }
        cout<<char(int(s[0])+1);
        return 0;
    }
*/
    sort(v.begin(), v.end());

    if(k>n)
    {

        cout<<s;
        for(i=1; i<=k-n; i++)
        {
            cout<<char(mx);
        }
        return 0;
    }

    //if(k==n)
        {
            st ans="";
            for(i=0; i<k; i++) ans=ans+s[i];

            ll x=k-1, y=v.size()-1;

            while(ans[x]==v[y]) { x--; }
            //s[x]=v[y];

            mx=int(v[y]);
            for(i=x+1; i<n; i++)
                mx=min(int(s[i]), mx);

            ans[x]=char(mx);
            //cout<<s<<'\n';
            for(i=x+1; i<k; i++) {  ans[i]=v[0]; }

            cout<<ans;
            return 0;
        }

    return 0;
}
/**
1 2 4

*/


#include<bits/stdc++.h>
#define ll long long
#define st string
#define fr first
#define se second
#define ld long double

const long double PI=3.1415926535897932384626433832795;
const long double EPS=0.00000000000000000000001;

using namespace std;

char s[1001001], a[1001001];

int main()
{
    //freopen("veryhard.in", "r", stdin);
    //freopen("1.txt", "w", stdout);
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    cout.tie(NULL);

    ll n, k, i, j;
    cin>>n>>k>>s;
    for(i=0; i<n; i++) a[i]=s[i];

    sort(a, a+n);

    if(k>n)
    {
        cout<<s;
        for(i=1; i<=k-n; i++) cout<<a[0];
        return 0;
    }

    ll x=k-1;
    while(s[x]==a[n-1]) x--;
    //cout<<x<<'\n';

    ll y=0;
    while(a[y]<=s[x]) y++;
    //cout<<y<<'\n';

    s[x]=a[y];

    for(i=0; i<=x; i++)
        cout<<s[i];

    for(i=x+1; i<k; i++)
        cout<<a[0];
    return 0;
}
/**

*/


#include<bits/stdc++.h>
#define ll long long
#define st string
#define fr first
#define se second
#define ld long double

const long double PI=3.1415926535897932384626433832795;
const long double EPS=0.00000000000000000000001;

using namespace std;

char s[1001001], a[1001001];

int main()
{
    //freopen("veryhard.in", "r", stdin);
    //freopen("1.txt", "w", stdout);
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    cout.tie(NULL);

    ll n, k, i, j;
    cin>>n>>k>>s;
    for(i=0; i<n; i++) a[i]=s[i];

    sort(a, a+n);

    if(k>n)
    {
        cout<<s;
        for(i=1; i<=k-n; i++) cout<<a[0];
        return 0;
    }

    ll x=k-1;
    while(s[x]==a[n-1]) x--;
    //cout<<x<<'\n';

    ll y=0;
    while(a[y]!=s[x]) y++;
    //cout<<y<<'\n';

    s[x]=a[y+1];

    for(i=0; i<=x; i++)
        cout<<s[i];

    for(i=x+1; i<k; i++)
        cout<<a[0];
    return 0;
}
/**

*/


#include<bits/stdc++.h>
#define ll long long
#define st string
#define fr first
#define se second
#define ld long double

const long double PI=3.1415926535897932384626433832795;
const long double EPS=0.00000000000000000000001;

using namespace std;

st s;
vector<char> v;
map<char, ll>  us;

int main()
{
    //freopen("veryhard.in", "r", stdin);
    //freopen("veryhard.out", "w", stdout);
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    cout.tie(NULL);

    ll n, k, i, j;
    cin>>n>>k>>s;

    //if("aaa"<"aaaa") cout<<1;
    int mx=1000000000;
    for(i=0; i<n; i++)
    {
        mx=min(int(s[i]), mx);
        if(us[s[i]]==0) { v.push_back(s[i]), us[s[i]]=1; }
    }

    if(v.size()==1)
    {
        for(i=0; i<min(k, n)-1; i++)
        {
            cout<<s[i];
        }
        cout<<char(int(s[0])+1);
        return 0;
    }

    sort(v.begin(), v.end());

    if(k>n)
    {

        cout<<s;
        for(i=1; i<=k-n; i++)
        {
            cout<<char(mx);
        }
        return 0;
    }

    //if(k==n)
        {
            st ans="";
            for(i=0; i<k; i++) ans=ans+s[i];

            ll x=k-1, y=v.size()-1;

            while(ans[x]==v[y]) { x--; }
            //s[x]=v[y];
            ans[x]=v[y];
            //cout<<s<<'\n';
            for(i=x+1; i<k; i++) {  ans[i]=v[0]; }

            cout<<ans;
            return 0;
        }

    return 0;
}
/**
1 2 4

*/


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <time.h>
#include <stdio.h>
#include <queue>
#include <unordered_map>
using namespace std;
#define ll long long
#define INF 1e9
#define pb push_back
#define pii pair<int,int>
vector < pair<int, int>> g;
bool cmp(vector<char> a, vector<char> b) {
	for (int i = 0; i < a.size();i++) {
		if (a[i] < b[i])
			return true;
		if (a[i] > b[i])
			return false;
	}
	return false;
}
int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	int n, k;
	cin >> n >> k;
	vector<char> a;
	vector<char> is;
	a.resize(n);
	set<char> s;
	for (int i = 0; i < n; i++) {
		cin >> a[i];
		s.insert(a[i]);
	}
	vector<char> pr;
	for (set<char>::iterator it = s.begin(); it != s.end(); it++) {
		pr.pb(*it);
	}
	if (k > n) {
		for (int i = 0; i < n; i++) {
			cout << a[i];
		}
		for (int i = n; i < k; i++) {
			cout << pr[0];
		}
		return 0;
	}
	if (n == k) {
		vector<char> is;
		is = a;
		bool cm = false;
		next_permutation(a.begin(), a.end());
		for (int i = 0;i < n; i++) {
			if (cm) {
				cout << pr[0];
			}
			else  {
				cout << a[i];
				if (a[i] > is[i])
					cm = true;
			}
		}
		return 0;
	}
	int g = 1e9;
	for (int i = k - 1; i >= 0; i--) {
		if (a[i] != pr[pr.size() - 1]) {
			for (int j = 0; j < pr.size(); j++) {
				if (pr[j] > a[i]) {
					a[i] = pr[j];
					g = i;
					break;
				}
			}
			break;
		}
	}
	for (int i = 0; i < k; i++) {
		if (i <= g)
			cout << a[i];

		else
			cout << pr[0];
	}
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <time.h>
#include <stdio.h>
#include <queue>
#include <unordered_map>
using namespace std;
#define ll long long
#define INF 1e9
#define pb push_back
#define pii pair<int,int>
vector < pair<int, int>> g;
bool cmp(vector<char> a, vector<char> b) {
	for (int i = 0; i < a.size();i++) {
		if (a[i] < b[i])
			return true;
		if (a[i] > b[i])
			return false;
	}
	return false;
}
int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	int n, k;
	cin >> n >> k;
	vector<char> a;
	vector<char> is;
	a.resize(n);
	set<char> s;
	for (int i = 0; i < n; i++) {
		cin >> a[i];
		s.insert(a[i]);
	}
	vector<char> pr;
	for (set<char>::iterator it = s.begin(); it != s.end(); it++) {
		pr.pb(*it);
	}
	if (k > n) {
		for (int i = 0; i < n; i++) {
			cout << is[i];
		}
		for (int i = n; i < k; i++) {
			cout << pr[0];
		}
		return 0;
	}
	if (n == k) {
		vector<char> is;
		is = a;
		bool cm = false;
		next_permutation(a.begin(), a.end());
		for (int i = 0;i < n; i++) {
			if (cm) {
				cout << pr[0];
			}
			else  {
				cout << a[i];
				if (a[i] > is[i])
					cm = true;
			}
		}
		return 0;
	}
	int g = 1e9;
	for (int i = k - 1; i >= 0; i--) {
		if (a[i] != pr[pr.size() - 1]) {
			for (int j = 0; j < pr.size(); j++) {
				if (pr[j] > a[i]) {
					a[i] = pr[j];
					g = i;
				}
			}
			break;
		}
	}
	for (int i = 0; i < k; i++) {
		if (i <= g)
			cout << a[i];

		else
			cout << pr[0];
	}
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <time.h>
#include <stdio.h>
#include <queue>
#include <unordered_map>
using namespace std;
#define ll long long
#define INF 1e9
#define pb push_back
#define pii pair<int,int>
vector < pair<int, int>> g;
bool cmp(vector<char> a, vector<char> b) {
	for (int i = 0; i < a.size();i++) {
		if (a[i] < b[i])
			return true;
		if (a[i] > b[i])
			return false;
	}
	return false;
}
int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	int n, k;
	cin >> n >> k;
	vector<char> a;
	vector<char> is;
	a.resize(n);
	set<char> s;
	for (int i = 0; i < n; i++) {
		cin >> a[i];
		s.insert(a[i]);
	}
	vector<char> pr;
	for (set<char>::iterator it = s.begin(); it != s.end(); it++) {
		pr.pb(*it);
	}
	is = a;
	bool cm = false;
	next_permutation(a.begin(), a.end());
	if (k > n) {
		for (int i = 0; i < n; i++) {
			cout << is[i];
		}
		for (int i = n; i < k; i++) {
			cout << pr[0];
		}
		return 0;
	}
	if (n == k) {
		next_permutation(a.begin(), a.end());
		for (int i = 0;i < n; i++) {
			cout << a[i];
		}
	}int g = 1e9;
	for (int i = k - 1; i >= 0; i++) {
		if (a[i] != pr[pr.size() - 1]) {
			for (int j = 0; j < pr.size(); j++) {
				if (pr[j] > a[i]) {
					a[i] = pr[j];
					g = i;
				}
			}
			break;
		}
	}
	for (int i = 0; i < k; i++) {
		if (i <= g)
			cout << a[i];

		else
			cout << pr[0];
	}
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <time.h>
#include <stdio.h>
#include <queue>
#include <unordered_map>
using namespace std;
#define ll long long
#define INF 1e9
#define pb push_back
#define pii pair<int,int>
vector < pair<int, int>> g;
bool cmp(vector<char> a, vector<char> b) {
	for (int i = 0; i < a.size();i++) {
		if (a[i] < b[i])
			return true;
		if (a[i] > b[i])
			return false;
	}
	return false;
}
int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	int n, k;
	cin >> n >> k;
	vector<char> a;
	vector<char> is;
	a.resize(n);
	set<char> s;
	for (int i = 0; i < n; i++) {
		cin >> a[i];
		s.insert(a[i]);
	}
	vector<char> pr;
	for (set<char>::iterator it = s.begin(); it != s.end(); it++) {
		pr.pb(*it);
	}
	if (k > n) {
		for (int i = 0; i < n; i++) {
			cout << a[i];
		}
		for (int i = n; i < k; i++) {
			cout << pr[0];
		}
		return 0;
	}
	if (n == k) {
		vector<char> is;
		is = a;
		bool cm = false;
		next_permutation(a.begin(), a.end());
		for (int i = 0;i < n; i++) {
			if (cm) {
				cout << pr[0];
			}
			else  {
				cout << a[i];
				if (a[i] > is[i])
					cm = true;
			}
		}
		return 0;
	}
	int g = 1e9;
	for (int i = k - 1; i >= 0; i--) {
		if (a[i] != pr[pr.size() - 1]) {
			for (int j = 0; j < pr.size(); j++) {
				if (pr[j] > a[i]) {
					a[i] = pr[j];
					g = i;
				}
			}
			break;
		}
	}
	for (int i = 0; i < k; i++) {
		if (i <= g)
			cout << a[i];

		else
			cout << pr[0];
	}
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <time.h>
#include <stdio.h>
#include <queue>
#include <unordered_map>
using namespace std;
#define ll long long
#define INF 1e9
#define pb push_back
#define pii pair<int,int>
vector < pair<int, int>> g;
bool cmp(vector<char> a, vector<char> b) {
	for (int i = 0; i < a.size();i++) {
		if (a[i] < b[i])
			return true;
	}
	return false;
}
int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	int n, k;
	cin >> n >> k;
	vector<char> a;
	vector<char> is;
	a.resize(n);
	set<char> s;
	for (int i = 0; i < n; i++) {
		cin >> a[i];
		s.insert(a[i]);
	}
	vector<char> pr;
	for (set<char>::iterator it = s.begin(); it != s.end(); it++) {
		pr.pb(*it);
	}
	is = a;
	bool cm = false;
	next_permutation(a.begin(), a.end());
	if (cmp(a, is))
		a = is;
	for (int i = 0; i < min(n,k); i++) {
		if (cm) {
			cout << pr[0];
		}
		else {
			char c = a[i];
			for (int j = 0; j < pr.size(); j++) {
				if (pr[j] >= c) {
					cout << pr[j];
					if (pr[j]>is[i])
						cm = true;
					break;
				}
			}
		}
	}
	for (int i = n; i < k; i++) {
		cout << pr[0];
	}

}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <time.h>
#include <stdio.h>
#include <queue>
#include <unordered_map>
using namespace std;
#define ll long long
#define INF 1e9
#define pb push_back
#define pii pair<int,int>
vector < pair<int, int>> g;
bool cmp(vector<char> a, vector<char> b) {
	for (int i = 0; i < a.size();i++) {
		if (a[i] < b[i])
			return true;
		if (a[i] > b[i])
			return false;
	}
	return false;
}
int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	int n, k;
	cin >> n >> k;
	vector<char> a;
	vector<char> is;
	a.resize(n);
	set<char> s;
	for (int i = 0; i < n; i++) {
		cin >> a[i];
		s.insert(a[i]);
	}
	vector<char> pr;
	for (set<char>::iterator it = s.begin(); it != s.end(); it++) {
		pr.pb(*it);
	}
	if (k > n) {
		for (int i = 0; i < n; i++) {
			cout << a[i];
		}
		for (int i = n; i < k; i++) {
			cout << pr[0];
		}
		return 0;
	}
	if (n == k) {
		is = a;
		bool cm = false;
			for (int i = n - 1; i >= 0; i--) {
				if (a[i] < pr[pr.size() - 1]) {
					for (int j = 0; j < pr.size();j++) {
						if (pr[j] > a[i]) {
							a[i] = pr[j];
							break;
						}
					}
					break;
				}
			}
			for (int i = 0; i < n; i++) {
				if (cm) {
					cout << pr[0];
				}
				else {
					cout << a[i];
					if (a[i] > is[i])
						cm = true;
				}
			}
		return 0;
	}
	int g = 1e9;
	for (int i = k - 1; i >= 0; i--) {
		if (a[i] != pr[pr.size() - 1]) {
			for (int j = 0; j < pr.size(); j++) {
				if (pr[j] > a[i]) {
					a[i] = pr[j];
					g = i;
					break;
				}
			}
			break;
		}
	}
	for (int i = 0; i < k; i++) {
		if (i <= g)
			cout << a[i];

		else
			cout << pr[0];
	}
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <time.h>
#include <stdio.h>
#include <queue>
#include <unordered_map>
using namespace std;
#define ll long long
#define INF 1e9
#define pb push_back
#define pii pair<int,int>
vector < pair<int, int>> g;
bool cmp(vector<char> a, vector<char> b) {
	for (int i = 0; i < a.size();i++) {
		if (a[i] < b[i])
			return true;
		if (a[i] > b[i])
			return false;
	}
	return false;
}
int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	int n, k;
	cin >> n >> k;
	vector<char> a;
	vector<char> is;
	a.resize(n);
	set<char> s;
	for (int i = 0; i < n; i++) {
		cin >> a[i];
		s.insert(a[i]);
	}
	vector<char> pr;
	for (set<char>::iterator it = s.begin(); it != s.end(); it++) {
		pr.pb(*it);
	}
	is = a;
	bool cm = false;
	next_permutation(a.begin(), a.end());
	if (cmp(a, is))
		a = is;
	for (int i = 0; i < min(n,k); i++) {
		if (cm) {
			cout << pr[0];
		}
		else {
			char c = a[i];
			for (int j = 0; j < pr.size(); j++) {
				if (pr[j] >= c) {
					cout << pr[j];
					if (pr[j]>is[i])
						cm = true;
					break;
				}
			}
		}
	}
	for (int i = n; i < k; i++) {
		cout << pr[0];
	}

}