#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <fstream>
#define rep( i, l, r ) for (int i = l; i <= r; i++)
#define down( i, l, r ) for (int i = l; i >= r; i--)
const int DS = 4000000;
using namespace std;
int h[DS], l[DS], r[DS], n[DS], s[DS];
char key[DS];
bool rev[DS], same[DS];
int q, m, w, last;
char que[50];
int NewTree()
{
int a = last;
last = n[a];
h[a] = l[a] = r[a] = n[a] = s[a] = 0;
rev[a] = same[a] = false;
key[a] = '$';
return a;
}
void Calculate(int w)
{
s[w] = s[l[w]] + 1 + s[r[w]];
}
void InsertR(int w)
{
int zuo = s[w] / 2;
int you = s[w] - zuo - 1;
if (zuo > 0)
{
int a = l[w] = NewTree(); h[a] = w; s[a] = zuo;
InsertR(a);
}
key[w] = getchar();
while (key[w] < 32 || key[w] > 126) scanf("%c", &key[w]);
if (you > 0)
{
int a = r[w] = NewTree(); h[a] = w; s[a] = you;
InsertR(a);
}
Calculate(w);
}
void Insert(int m)
{
int a = l[r[l[0]]] = NewTree();
h[a] = r[l[0]];
s[a] = m;
InsertR(a);
Calculate(r[l[0]]);
Calculate(l[0]);
}
void DeleteR(int w)
{
if (l[w] != 0) DeleteR(l[w]);
if (r[w] != 0) DeleteR(r[w]);
n[w] = last;
last = w;
}
void Delete()
{
int a = l[r[l[0]]];
l[r[l[0]]] = 0;
DeleteR(a);
Calculate(r[l[0]]);
Calculate(l[0]);
}
void Reverse(int w)
{
rev[w] = !rev[w];
}
void Down(int w)
{
if (rev[w])
{
rev[w] = false;
int a = l[w];
l[w] = r[w];
r[w] = a;
if (l[w] != 0) Reverse(l[w]);
if (r[w] != 0) Reverse(r[w]);
}
}
int Find(int m)
{
int w = l[0];
Down(w);
while (m != s[l[w]]+1)
{
if (m < s[l[w]]+1) w = l[w];
else
{
m -= s[l[w]]+1;
w = r[w];
}
Down(w);
}
return w;
}
void RotateL(int w)
{
int o = h[w];
r[o] = l[w]; h[r[o]] = o;
l[w] = o; h[w] = h[o];
h[o] = w;
if (l[h[w]] == o) l[h[w]] = w; else r[h[w]] = w;
Calculate(o);
}
void RotateR(int w)
{
int o = h[w];
l[o] = r[w]; h[l[o]] = o;
r[w] = o; h[w] = h[o];
h[o] = w;
if (l[h[w]] == o) l[h[w]] = w; else r[h[w]] = w;
Calculate(o);
}
void Splay(int w)
{
while (h[w] != 0)
{
if (l[h[w]] == w) RotateR(w); else RotateL(w);
}
Calculate(w);
}
void FindAll(int b, int e)
{
Splay(Find(e));
Splay(Find(b));
}
int main()
{
rep(i, 0, DS-1) n[i] = i+1; last = 0;
rep(i, 1, 3) q = NewTree();
l[0] = 1; h[1] = 0;
r[1] = 2; h[2] = 1;
s[1] = 2; s[2] = 1;
scanf("%d", &q);
w = 1;
while (q > 0)
{
q --;
scanf("%s", que);
if (que[0] == 'I')
{
scanf("%d", &m);
FindAll(w, w+1);
Insert(m);
}
else if (que[0] == 'M')
{
scanf("%d", &w); w ++;
}
else if (que[0] == 'D')
{
scanf("%d", &m);
FindAll(w, w+m+1);
Delete();
}
else if (que[0] == 'R')
{
scanf("%d", &m);
FindAll(w, w+m+1);
Reverse(l[r[l[0]]]);
Calculate(r[l[0]]);
Calculate(l[0]);
}
else if (que[0] == 'G') printf("%c\n", key[Find(w+1)]);
else if (que[0] == 'P') w --;
else if (que[0] == 'N') w ++;
}
return 0;
}
I2luY2x1ZGUgPGNzdGRpbz4KI2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8Y3N0cmluZz4KI2luY2x1ZGUgPGFsZ29yaXRobT4KI2luY2x1ZGUgPGZzdHJlYW0+CgojZGVmaW5lIHJlcCggaSwgbCwgciApIGZvciAoaW50IGkgPSBsOyBpIDw9IHI7IGkrKykKI2RlZmluZSBkb3duKCBpLCBsLCByICkgZm9yIChpbnQgaSA9IGw7IGkgPj0gcjsgaS0tKQoKY29uc3QgaW50IERTID0gNDAwMDAwMDsKCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CmludCBoW0RTXSwgbFtEU10sIHJbR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