splay 练手用;
杭电的oj要手动开栈;
#include<cstdio>
#pragma comment(linker, "/STACK:102400000,102400000")
#include<cstring>
#include<algorithm>
#define inf 999999
#define maxn 1500009
#define lch(rt) son[rt][0]
#define rch(rt) son[rt][1]
using namespace std; int son[maxn][],fa[maxn];
int val[maxn],size[maxn],flg[maxn];
int cnt,root;
int num[maxn];
int n,m; void push_up(int rt)
{
size[rt]=size[lch(rt)]+size[rch(rt)]+;
} void push_down(int rt)
{
if(flg[rt])
{
swap(lch(rt),rch(rt));
if(lch(rt))
flg[lch(rt)]^=;
if(rch(rt))
flg[rch(rt)]^=;
flg[rt]=;
}
} void newnode(int &rt,int f,int v)
{
rt=++cnt;
lch(rt)=rch(rt)=;
val[rt]=v;
fa[rt]=f;
size[rt]=;
flg[rt]=;
} void build(int l,int r,int &rt,int f)
{
if(l>r)return;
int mid=(l+r)>>;
newnode(rt,f,num[mid]);
build(l,mid-,lch(rt),rt);
build(mid+,r,rch(rt),rt);
push_up(rt);
} void ini()
{
cnt=root=;
lch()=rch()=;
fa[]=val[]=flg[]=size[]=;
newnode(root,,);
newnode(rch(root),root,inf);
build(,n,lch(rch(root)),rch(root));
push_up(rch(root));
push_up(root);
} void rotate(int x,int kind)//0->left,1->right
{
push_down(x);
int y=fa[x];
son[y][kind^]=son[x][kind];
fa[son[x][kind]]=y;
if(fa[y])
son[fa[y]][son[fa[y]][]==y]=x;
fa[x]=fa[y];
son[x][kind]=y;
fa[y]=x;
push_up(y);
} void splay(int rt,int goal)//将rt节点旋转到goal的右子节点
{
if(rt!=goal)
{
push_down(rt);
while(fa[rt]!=goal)
{
if(lch(fa[rt])==rt)
rotate(rt,);
else rotate(rt,);
}
push_up(rt);
if(!goal)root=rt;
}
} int select(int k)
{
int rt=root;
push_down(rt);
while(size[lch(rt)]+!=k)
{
if(size[lch(rt)]+>=k)
rt=lch(rt);
else
{
k-=(size[lch(rt)]+);
rt=rch(rt);
}
push_down(rt);//不加就超时;
}
return rt;
} int cntt;
void dfs(int rt)
{
push_down(rt);
if(lch(rt))
dfs(lch(rt));
num[cntt++]=val[rt];
if(rch(rt))
dfs(rch(rt));
} void flip(int a,int b)
{
a=select(a-);
splay(a,);
b=select(b+);
splay(b,a);
flg[lch(b)]^=;
} char s[],t[]; int main()
{
int tt;
int ld,rd;
scanf("%d",&tt);
while(tt--)
{
scanf("%d",&n);
for(int i=; i<=n; i++)
scanf("%d",&num[i]);
ini();
scanf("%d%d",&ld,&rd);
ld++;
rd++;
int cot=;
int a,b;
scanf("%d",&m);
int dat;
while(m--)
{
scanf("%s",s);
if(s[]=='M')
{
scanf("%s",t);
if(t[]=='R'&&s[]=='R')
rd++;
else if(t[]=='R'&&s[]=='L')
rd--;
else if(t[]=='L'&&s[]=='R')
ld++;
else ld--;
}
else if(s[]=='I')
{
cot++;
scanf("%s%d",t,&dat);
if(t[]=='L')
{
a=select(ld-);
b=select(ld);
}
else
{
a=select(rd);
b=select(rd+);
}
rd++;
splay(a,);
splay(b,a);
newnode(lch(b),b,dat);
fa[lch(b)]=b;
push_up(b);
push_up(a);
}
else if(s[]=='R')
{
flip(ld,rd);
}
else if(s[]=='D')
{
cot--;
scanf("%s",t);
if(t[]=='L')
{
a=select(ld-);
b=select(ld+);
}
else
{
a=select(rd-);
b=select(rd+);
}
rd--;
splay(a,);
splay(b,a);
push_down(a);
push_down(b);
lch(b)=;
push_up(b);
push_up(a);
}
}
a=select();
b=select(n+cot+);
splay(a,);
splay(b,a);
n+=cot;
cntt=;
dfs(root);
for(int i=;i<n;i++)
printf("%d ",num[i]);
printf("%d\n",num[n]);
}
return ;
}