#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin>>n;
cin.ignore();
getline(cin,str);
for(int i=0;i<n;i++)
{
int x;
cin>>x;
if(x==1)
{
string temp;
cin>>temp;
str+=temp;
cout<<str;
cout<<endl;
}
else if(x==2)
{
int x1,y1;
cin>>x1>>y1;
str=str.substr(x1,y1);
cout<<str;
cout<<endl;
}
else if(x==3)
{
int x1;
cin>>x1;
string temp;
cin>>temp;
str=str.insert(x1,temp);
cout<<str;
cout<<endl;
}
else {
string temp;
cin >> temp;
size_t pos = str.find(temp);
if (pos != string::npos) {
cout << pos << endl;
} else {
cout << -1 << endl;
}
}
}
}