/**
* @Date: 2019-03-19T14:51:21+08:00
* @Last modified time: 2019-03-19T17:04:34+08:00
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <complex>
#include <algorithm>
#include <climits>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#include <ext/rope>
#define
#define
using namespace std;
using namespace __gnu_cxx;
typedef long long ll;
crope q;
char s[11], ch[A];
int T, now, x;
int main(int argc, char const *argv[]) {
cin >> T;
while (T--) {
cin >> s;
switch (s[0]) {
case 'M' : cin >> now; break;
case 'I' : cin >> x; ch[x] = 0;
for (int i = 0; i < x; i++) {
ch[i] = getchar();
while (ch[i] == '\n' or ch[i] == '\r') ch[i] = getchar();
}
q.insert(now, ch); break;
case 'D' : cin >> x; q.erase(now, x); break;
case 'G' : cin >> x; q.copy(now, x, ch); ch[x] = 0; puts(ch); break;
case 'P' : now--; break;
case 'N' : now++; break;
default : break;
}
}
}