#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define int long long
const int N = 5e5+5;
inline void read(int &x)
{
int s=0,w=1;
char c=getchar();
while(c<'0'||c>'9')c=getchar();
while(c>='0'&&c<='9')
{
s=(s<<3)+(s<<1)+c-'0';
c=getchar();
}
x=s*w;
}
struct node{
int ls, rs;
int pri, siz;
char val;
}tr[N];
int tsiz = 0, root = 0;
void update(int x){
tr[x].siz = tr[tr[x].ls].siz + tr[tr[x].rs].siz + 1;
}
void newnode(char v){
++tsiz;
tr[tsiz].siz = 1;
tr[tsiz].ls = tr[tsiz].rs = 0;
tr[tsiz].pri = rand();
tr[tsiz].val = v;
}
void split(int rt, int x, int &l, int &r){
if (rt == 0){
l = r = 0;
return;
}
if (tr[tr[rt].ls].siz < x) l = rt, split(tr[rt].rs, x - tr[tr[rt].ls].siz - 1, tr[rt].rs, r);
else r = rt, split(tr[rt].ls, x, l, tr[rt].ls);
update(rt);
}
int merge(int l, int r){
if (l == 0 || r == 0) return l + r;
if (tr[l].pri > tr[r].pri){
tr[l].rs = merge(tr[l].rs, r);
update(l);
return l;
}
else{
tr[r].ls = merge(l, tr[r].ls);
update(r);
return r;
}
}
void print(int x){
if (!x) return;
print(tr[x].ls);
printf("%c", tr[x].val);
print(tr[x].rs);
}
int q;
int gb;
char op[10];
signed main(){
srand(time(NULL));
read(q);
while (q--){
scanf("%s", op);
if (op[0] == 'M'){
read(gb);
}
else if (op[0] == 'I'){
int n, a, b;
read(n);
char ch;
split(root, gb, a, b);
for (int i = 1;i <= n;i++){
ch = getchar();
if (ch < 32 || ch > 126) --i;
else{
newnode(ch);
a = merge(a, tsiz);
}
}
root = merge(a, b);
}
else if (op[0] == 'D'){
int n;
read(n);
int a, b, c;
split(root, gb, a, b);
split(b, n, b, c);
root = merge(a, c);
}
else if (op[0] == 'G'){
int n;
int a, b, c;
read(n);
split(root, gb, a, b);
split(b, n, b, c);
print(b);
puts("");
root = merge(a, merge(b, c));
}
else if (op[0] == 'P'){
--gb;
}
else{
++gb;
}
}
return 0;
}