#include <cstdio>
#include <cstring>
#include <queue>
#include <cctype>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <bitset>
#include <map>
#include <complex>
#include <ctime>
#include <numeric>
#include <set>
#include <cassert>
#include <ext/rope>
using namespace std;
typedef pair<int, int> pi;
typedef long long ll;
using namespace __gnu_cxx;
char s[(int)1e6 + 5];
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.in", "r", stdin);
#endif
ios::sync_with_stdio(false);
int T, p, a, b;
scanf("%d ", &T);
while(T--){
rope< char > r;
scanf("%s", s);
r.insert(0, s);
while(scanf("%s", s), !(s[0] == 'E' && s[1] == 'N' && s[2] == 'D')){
if(s[0] == 'I'){
scanf("%s %d ", s, &p);
r.insert(p, s);
}else{
scanf("%d %d ", &a, &b);
cout << r.substr(a, b-a+1) << endl;
}
}
}
return 0;
}
I2luY2x1ZGUgPGNzdGRpbz4KI2luY2x1ZGUgPGNzdHJpbmc+CiNpbmNsdWRlIDxxdWV1ZT4KI2luY2x1ZGUgPGNjdHlwZT4KI2luY2x1ZGUgPGNtYXRoPgojaW5jbHVkZSA8YWxnb3JpdGhtPgojaW5jbHVkZSA8aW9zdHJlYW0+CiNpbmNsdWRlIDxiaXRzZXQ+CiNpbmNsdWRlIDxtYXA+CiNpbmNsdWRlIDxjb21wbGV4PgojaW5jbHVkZSA8Y3RpbWU+CiNpbmNsdWRlIDxudW1lcmljPgojaW5jbHVkZSA8c2V0PgojaW5jbHVkZSA8Y2Fzc2VydD4KCiNpbmNsdWRlIDxleHQvcm9wZT4KCnVzaW5nIG5hbWVzcGFjZSBzdGQ7Cgp0eXBlZGVmIHBhaXI8aW50LCBpbnQ+IHBpOwp0eXBlZGVmIGxvbmcgbG9uZyBsbDsKCnVzaW5nIG5hbWVzcGFjZSBfX2dudV9jeHg7IAoKY2hhciBzWyhpbnQpMWU2ICsgNV07CgppbnQgbWFpbigpCnsKI2lmbmRlZiBPTkxJTkVfSlVER0UKICAgIGZyZW9wZW4oImluLmluIiwgInIiLCBzdGRpbik7CiNlbmRpZgogICAgaW9zOjpzeW5jX3dpdGhfc3RkaW8oZmFsc2UpOwogICAgaW50IFQsIHAsIGEsIGI7CiAgICBzY2FuZigiJWQgIiwgJlQpOwogICAgd2hpbGUoVC0tKXsKICAgICAgICByb3BlPCBjaGFyID4gcjsKICAgICAgICBzY2FuZigiJXMiLCBzKTsKICAgICAgICByLmluc2VydCgwLCBzKTsKICAgICAgICB3aGlsZShzY2FuZigiJXMiLCBzKSwgIShzWzBdID09ICdFJyAmJiBzWzFdID09ICdOJyAmJiBzWzJdID09ICdEJykpewogICAgICAgICAgICBpZihzWzBdID09ICdJJyl7CiAgICAgICAgICAgICAgICBzY2FuZigiJXMgJWQgIiwgcywgJnApOwogICAgICAgICAgICAgICAgci5pbnNlcnQocCwgcyk7CiAgICAgICAgICAgIH1lbHNlewogICAgICAgICAgICAgICAgc2NhbmYoIiVkICVkICIsICZhLCAmYik7CiAgICAgICAgICAgICAgICBjb3V0IDw8IHIuc3Vic3RyKGEsIGItYSsxKSA8PCBlbmRsOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgfQogICAKICAgIAogICAgCiAgICByZXR1cm4gMDsKfQo=