program day2_2;
var
n,gub: longint;
s,edit: ansistring;
ch: char;
function getnum(s:ansistring):longint;
var
num,i,k: longint;
begin
num :=0;
for i :=1 to length(S) do if s[i]=' ' then k :=i;
for i :=k+1 to length(s) do num :=num*10+ord(s[i])-48;
exit(num);
end;
procedure Main;
var
i,j,num: longint;
ss: ansistring;
begin
readln(n); gub:=0;
for i :=1 to n do begin
readln(s); ch :=s[1];
case ch of
'I': begin
readln(ss);
edit :=copy(edit,1,gub)+ss+copy(edit,gub+1,length(edit)-gub);
end;
'M': begin
num :=getnum(s);
gub :=num;
end;
'D': begin
num :=getnum(s);
delete(edit,gub+1,num);
end;
'P': begin
dec(gub);
end;
'N': begin
inc(gub);
end;
'G': begin
writeln(edit[gub+1]);
end;
'R': begin
num :=getnum(s); ss:=copy(edit,gub+1,num);
for j :=1 to num do edit[j+gub] :=ss[num-j+1];
end;
end; //case;
end; //for;
end;
begin
assign(input,'editor.in'); reset(input);
assign(output,'editor.out'); rewrite(output);
Main;
close(input); close(output);
end.