#include<cstdio>
#include<algorithm>
#include<cstring>
#define N 80010
#define which(x) (ls[fa[(x)]]==(x))
using namespace std;
int id[N],pos[N],sz[N],ls[N],rs[N],fa[N],tot,n,m,root,a[N];
char op[];
void pushup(int x) {sz[x]=+sz[ls[x]]+sz[rs[x]];}
int Build(int l,int r,int pre)
{
if (l>r) return ;
int mid=l+r>>,u=a[mid];
ls[u]=Build(l,mid-,u);
rs[u]=Build(mid+,r,u);
fa[u]=pre;
pushup(u);
return u;
}
void Rotate(int u){
int v = fa[u], w = fa[v], b = which(u) ? rs[u] : ls[u];
if(w) which(v) ? ls[w] = u : rs[w] = u;
which(u) ? (ls[v] = b, rs[u] = v) : (rs[v] = b, ls[u] = v);
fa[u] = w, fa[v] = u;
if(b) fa[b] = v;
pushup(v), pushup(u);
}
void Splay(int u, int tar){
while(fa[u] != tar){
if(fa[fa[u]] != tar){
if(which(u) == which(fa[u])) Rotate(fa[u]);
else Rotate(u);
}
Rotate(u);
}
if(!tar) root = u;
}
int getrank(int u)
{
Splay(u,);
return sz[ls[u]];
}
int getkth(int k)
{
int cur=root;
while (sz[ls[cur]]+!=k)
{
if (k<=sz[ls[cur]]) cur=ls[cur];
else k-=sz[ls[cur]]+,cur=rs[cur];
}
return cur;
}
void insert(int v,int top)
{
int u=root;
while (top?ls[u]:rs[u])
u=top?ls[u]:rs[u];
fa[v]=u,top?ls[u]=v:rs[u]=v,sz[v]=;
ls[v]=rs[v]=;
Splay(v,);
}
void erase(int u)
{
Splay(u,);
if (sz[u]==) root;
else if (!ls[u] || !rs[u]) root=ls[u]+rs[u],fa[root]=;
else
{
fa[ls[u]]=;
int v=ls[u];
while (rs[v]) v=rs[v];
Splay(v,);
rs[v]=rs[u],fa[rs[u]]=v,pushup(v);
}
}
void swp(int u,int x)
{
Splay(u,);
int v=x<?ls[u]:rs[u];
while (x<?rs[v]:ls[v]) v=x<=?rs[v]:ls[v];
Splay(v,u);
if (x<) swap(rs[u],rs[v]),ls[u]=ls[v],ls[v]=u;
else swap(ls[u],ls[v]),rs[u]=rs[v],rs[v]=u;
fa[v]=,fa[ls[u]]=fa[rs[u]]=u,fa[ls[v]]=fa[rs[v]]=v;
pushup(u),pushup(v);
root=v;
}
int main()
{
scanf("%d%d",&n,&m);
for (int i=,x;i<=n;i++)
scanf("%d",&a[i]);
root=Build(,n,);
for (int i=,t,x;i<=m;i++)
{
scanf("%s%d",op,&x);
if (op[]=='T') erase(x),insert(x,);
if (op[]=='B') erase(x),insert(x,);
if (op[]=='A') printf("%d\n",getrank(x));
if (op[]=='Q') printf("%d\n",getkth(x));
if (op[]=='I')
{
scanf("%d",&t);
if (t) swp(x,t);
}
}
return ;
}