#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <cctype>
#define inf 0x3f3f3f3f
#define eps 1e-6
using namespace std;
#define clr(x) memset(x,0,sizeof((x)))
const int maxn = 1e4+1;
#define MAX(a,b,c) ((a)>(b)?((a)>(c)?(a):(c)):((b)>(c)?(b):(c)))
#define _max(a,b) ((a) > (b) ? (a) : (b))
#define _min(a,b) ((a) < (b) ? (a) : (b))
#define _for(a,b,c) for(int a = b;a<c;a++)
char s[maxn],tmp[maxn];
void f1() {
char str[100];
clr(str);
scanf("%s",str);
strcat(s,str);
}
void f2() {
int a,b,j = 0;
cin>>a>>b;
for(int i = a;i<a+b;i++) {
tmp[j++] = s[i];
}
clr(s);
strcpy(s,tmp);
}
void f3() {
int a;
char ss[105];
scanf("%d%s",&a,ss);
int len = strlen(ss);
int lens = strlen(s);
for(int i = lens-1;i>=a;i--) {
s[i+len] = s[i];
}
int cnt = 0;
for(int j = a;j<a+len;j++) {
s[j] = ss[cnt++];
}
}
int f4() {
char st[105];
scanf("%s",st);
int i,j = 0,flag = 0;
for(i = 0;i<strlen(s);i++) {
if(s[i]!=st[j]) {
if(j!=0)i--;
j = 0;
continue;
}
j++;
if(j==strlen(st)) {
flag = 1;break;
}
}
if(flag)return (i-strlen(st)+1);
return -1;
}
void _print() {
_for(i,0,strlen(s)){
cout<<s[i];
}
cout<<endl;
}
int main() {
#ifdef LOCAL
freopen("data.in","r",stdin);
freopen("data.out","w",stdout);
#endif
int q;
cin>>q;
scanf("%s",s);
while(q--) {
int cmd;
scanf("%d",&cmd);
switch(cmd) {
case 1 :f1();break;
case 2 :f2();break;
case 3 :f3();break;
case 4 :cout<<f4()<<endl;break;
}
if(cmd!=4)_print();
}
return 0;
}
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <cctype>
#define inf 0x3f3f3f3f
#define eps 1e-6
using namespace std;
#define clr(x) memset(x,0,sizeof((x)))
const int maxn = 1e4+1;
#define MAX(a,b,c) ((a)>(b)?((a)>(c)?(a):(c)):((b)>(c)?(b):(c)))
#define _max(a,b) ((a) > (b) ? (a) : (b))
#define _min(a,b) ((a) < (b) ? (a) : (b))
#define _for(a,b,c) for(int a = b;a<c;a++)
char s[maxn],tmp[maxn];
int n,opc,op1,op2;
string str,s1;
int main()
{
#ifdef LOCAL
freopen("data.in","r",stdin);
freopen("data.out","w",stdout);
#endif
cin>>n;
cin>>str;
for(int i=0;i<n;i++)
{
cin>>opc;
if(opc==1)
{
cin>>s1;
str+=s1;
cout<<str<<endl;
}
else if(opc==2)
{
cin>>op1>>op2;
s1=str.substr(op1,op2);
str=s1;
cout<<str<<endl;
}
else if(opc==3)
{
cin>>op1>>s1;
str.insert(op1,s1);
cout<<str<<endl;
}
else if(opc==4)
{
cin>>s1;
size_t loc = str.find(s1,0);
if(loc!=string::npos)
cout<<loc<<endl;
else cout<<-1<<endl;
}
}
return 0;
}