#include <cstring>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <cstdio>
#include <fstream>
#include <iostream>
#define rep(i, l, r) for(int i = l; i <= r; i++)
#define down(i, l, r) for(int i = l; i >= r; i--)
#define MS 2345678
#define MAX 1037471823
#define Q 1
using namespace std;
int m, n, a, b, s[MS], l[MS], r[MS], h[MS], next[MS], last, now;
char c[MS], q[10];
void Cal(int o) { s[o] = s[l[o]] + s[r[o]] + 1; }
int New() { int o = last; last = next[last], s[o] = 1, l[o] = r[o] = h[o] = c[o] = 0; return o; }
void Splay(int x)
{
if (!x) return; int o = h[x];
while (o)
{
if (l[o] == x) { l[h[o]] == o?l[h[o]] = x: r[h[o]] = x; h[x] = h[o]; l[o] = r[x]; h[r[x]] = o; h[o] = x; r[x] = o; Cal(o); }
else { l[h[o]] == o?l[h[o]] = x: r[h[o]] = x; h[x] = h[o]; r[o] = l[x]; h[l[x]] = o; h[o] = x; l[x] = o; Cal(o); }
o = h[x];
}
Cal(x);
}
int FindRW(int rr)
{
int o = l[0];
while (rr != s[l[o]]) if (rr < s[l[o]]) o = l[o]; else rr -= s[l[o]] + 1, o = r[o];
return o;
}
void InsertP(int o, int n)
{
int ln = (n-1)/2, rn = n-1-ln;
if (ln) { int now = New(); h[now] = o, l[o] = now; InsertP(now, ln); }
c[o] = 0;
while (c[o] < 32 || 126 < c[o])
scanf("%c", &c[o]);
if (rn) { int now = New(); h[now] = o, r[o] = now; InsertP(now, rn); }
Cal(o);
}
void Insert(int b, int n) { Splay(FindRW(b)); Splay(FindRW(b+1)); int now = New(); h[now] = l[l[0]]; r[l[l[0]]] = now; InsertP(now, n); Cal(l[l[0]]); Cal(l[0]); }
void DeleteP(int o) { if (l[o]) DeleteP(l[o]); if (r[o]) DeleteP(r[o]); next[o] = last, last = o; }
void Delete(int b, int n) { Splay(FindRW(b-1)); Splay(FindRW(b+n)); DeleteP(r[l[l[0]]]); r[l[l[0]]] = 0; Cal(l[l[0]]); Cal(l[0]); }
void PrintP(int o) { if (l[o]) PrintP(l[o]); printf("%c", c[o]); if (r[o]) PrintP(r[o]); }
void Print(int b, int n) { Splay(FindRW(b-1)); Splay(FindRW(b+n)); PrintP(r[l[l[0]]]); printf("\n"); }
int main()
{
scanf("%d", &m); now = 0; l[0] = 1, l[1] = 2, h[2] = 1; s[1] = 2, s[2] = 1;
rep(i, 3, MS-1) next[i] = i+1; last = 3;
rep(i, 1, m)
{
scanf("%s", q);
if (q[0] == 'M') { scanf("%d", &now); }
else if (q[0] == 'I') { scanf("%d", &n); Insert(now, n); }
else if (q[0] == 'D') { scanf("%d", &n); if (n > s[l[0]]-2-now) Delete(now+1, s[l[0]]-2-now); else Delete(now+1, n); }
else if (q[0] == 'G') { scanf("%d", &n); Print(now+1, n); }
else if (q[0] == 'P') now--;
else now++;
}
return 0;
}
I2luY2x1ZGUgPGNzdHJpbmc+CiNpbmNsdWRlIDxjbWF0aD4KI2luY2x1ZGUgPGFsZ29yaXRobT4KI2luY2x1ZGUgPGNzdGRsaWI+CiNpbmNsdWRlIDxjc3RkaW8+CiNpbmNsdWRlIDxmc3RyZWFtPgojaW5jbHVkZSA8aW9zdHJlYW0+CgojZGVmaW5lIHJlcChpLCBsLCByKSBmb3IoaW50IGkgPSBsOyBpIDw9IHI7IGkrKykKI2RlZmluZSBkb3duKGksIGwsIHIpIGZvcihpbnQgaSA9IGw7IGkgPj0gcjsgaS0tKQojZGVmaW5lIE1TIDIzNDU2NzgKI2RlZmluZSBNQVggMTAzNzQ3MTgyMwojZGVmaW5lIFEgMQoKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCmludCBtLCBuLCBhLCBiLCBzW01TXSwgbFtNU10sIHJbTVNdLCBoW01TXSwgbmV4dFtNU10sIGxhc3QsIG5vdzsKY2hhciBjW01TXSwgcVsxMF07Cgp2b2lkIENhbChpbnQgbykgeyBzW29dID0gc1tsW29dXSArIHNbcltvXV0gKyAxOyB9CgppbnQgTmV3KCkgeyBpbnQgbyA9IGxhc3Q7IGxhc3Q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