#include<bits/stdc++.h>
using namespace std;
#define mid (l+r>>1)
const int N=100002,M=2000002;
int a[N],b[N],sum[M],T[M],L[M],R[M],tot,cas,n,m,i,q,x,y,k;
int update(int pre,int l,int r,int x){
int rt=++tot;
L[rt]=L[pre],R[rt]=R[pre],sum[rt]=sum[pre]+1;
if (l<r){
if (x<=mid) L[rt]=update(L[pre],l,mid,x);
else R[rt]=update(R[pre],mid+1,r,x);
}
return rt;
}
int query(int l,int r,int x,int y,int k){
if (l==r) return l;
int t=sum[L[y]]-sum[L[x]];
if (k<=t) return query(l,mid,L[x],L[y],k);
return query(mid+1,r,R[x],R[y],k-t);
}
int main(){
scanf("%d",&cas);
for (;cas--;){
tot=0;
scanf("%d%d",&n,&q);
for (i=1;i<=n;i++) scanf("%d",&a[i]),b[i]=a[i];
sort(b+1,b+n+1);
m=unique(b+1,b+n+1)-b-1;
for (i=1;i<=n;i++) a[i]=lower_bound(b+1,b+m+1,a[i])-b,T[i]=update(T[i-1],1,m,a[i]);
for (;q--;){
scanf("%d%d%d",&x,&y,&k);
printf("%d\n",b[query(1,m,T[x-1],T[y],k)]);
}
}
}
#include<cstdio>
#include<algorithm>
using namespace std;
const int N=100002;
int tr[19][N],st[N],sum[19][N],x,y,k,n,m,i;
void build(int c,int l,int r){
int mid=l+r>>1,lm=mid-l+1,x=l,y=mid+1;
for (int i=l;i<=mid;i++)
if (st[i]<st[mid]) lm--;
for (int i=l;i<=r;i++){
if (i==l) sum[c][i]=0;
else sum[c][i]=sum[c][i-1];
if (tr[c][i]==st[mid]){
if (lm) lm--,sum[c][i]++,tr[c+1][x++]=tr[c][i];
else tr[c+1][y++]=tr[c][i];
}else if (tr[c][i]<st[mid]) sum[c][i]++,tr[c+1][x++]=tr[c][i];
else tr[c+1][y++]=tr[c][i];
}
if (l==r) return;
build(c+1,l,mid);
build(c+1,mid+1,r);
}
int query(int c,int l,int r,int x,int y,int k){
if (l==r) return tr[c][l];
int mid=l+r>>1,s,ss;
if (l==x) s=0;
else s=sum[c][x-1];
ss=sum[c][y]-s;
if (k<=ss) return query(c+1,l,mid,l+s,l+s+ss-1,k);
return query(c+1,mid+1,r,x+mid-l+1-s,y+mid-l+1-s-ss,k-ss);
}
int main(){
scanf("%d%d",&n,&m);
for (i=1;i<=n;i++) scanf("%d",&st[i]),tr[0][i]=st[i];
sort(st+1,st+n+1);
build(0,1,n);
for (;m--;){
scanf("%d%d%d",&x,&y,&k);
printf("%d\n",query(0,1,n,x,y,k));
}
}
#include<bits/stdc++.h>
using namespace std;
#define mid ((l+r)>>1)
const int N=20002,M=2200002;
int L[M],R[M],sum[M],num[N],tot,cnt,A[N],B[N],x,y,le[20],ri[20],n,m,i,a[N],K[N],j,T[N],t;
char s[2];
#define gc getchar
inline int rd(){
int x=0,fl=1;char ch=gc();
for (;ch<48||ch>57;ch=gc())if(ch=='-')fl=-1;
for (;48<=ch&&ch<=57;ch=gc())x=(x<<3)+(x<<1)+(ch^48);
return x*fl;
}
inline void wri(int a){if(a<0)a=-a,putchar('-');if(a>=10)wri(a/10);putchar(a%10|48);}
inline void wln(int a){wri(a);puts("");}
int update(int pre,int l,int r,int x,int v){
int rt=++cnt;
L[rt]=L[pre],R[rt]=R[pre],sum[rt]=sum[pre]+v;
if (l<r){
if (x<=mid) L[rt]=update(L[pre],l,mid,x,v);
else R[rt]=update(R[pre],mid+1,r,x,v);
}
return rt;
}
int query(int l,int r,int k){
if (l==r) return l;
int s=0;
for (int i=0;i<x;i++) s-=sum[L[le[i]]];
for (int i=0;i<y;i++) s+=sum[L[ri[i]]];
if (k<=s){
for (int i=0;i<x;i++) le[i]=L[le[i]];
for (int i=0;i<y;i++) ri[i]=L[ri[i]];
return query(l,mid,k);
}
for (int i=0;i<x;i++) le[i]=R[le[i]];
for (int i=0;i<y;i++) ri[i]=R[ri[i]];
return query(mid+1,r,k-s);
}
int main(){
tot=n=rd(),m=rd();
for (i=1;i<=n;i++) num[i]=a[i]=rd();
for (i=1;i<=m;i++){
scanf("%s",s),A[i]=rd(),B[i]=rd();
if (s[0]=='Q') K[i]=rd();
else num[++tot]=B[i];
}
sort(num+1,num+tot+1);
tot=unique(num+1,num+tot+1)-num-1;
for (i=1;i<=n;i++){
t=lower_bound(num+1,num+tot+1,a[i])-num;
for (j=i;j<=n;j+=j&(-j)) T[j]=update(T[j],1,tot,t,1);
}
for (i=1;i<=m;i++)
if (K[i]){
x=0,y=0;A[i]--;
for (j=A[i];j;j^=j&(-j)) le[x++]=T[j];
for (j=B[i];j;j^=j&(-j)) ri[y++]=T[j];
wln(num[query(1,tot,K[i])]);
}else{
t=lower_bound(num+1,num+tot+1,a[A[i]])-num;
for (j=A[i];j<=n;j+=j&(-j)) T[j]=update(T[j],1,tot,t,-1);
a[A[i]]=B[i];
t=lower_bound(num+1,num+tot+1,B[i])-num;
for (j=A[i];j<=n;j+=j&(-j)) T[j]=update(T[j],1,tot,t,1);
}
}
#include<bits/stdc++.h>
using namespace std;
const int N=100002,M=4000002;
typedef long long ll;
#define up(rt,l,r) sum[rt]=sum[L[rt]]+sum[R[rt]]+lz[rt]*(r-l+1)
#define mid ((l+r)>>1)
int L[M],R[M],cnt,n,m,rt[N],a[N],i,x,y,z,t;
ll sum[M],lz[M];
char s[2];
void build(int &rt,int l,int r){
rt=++cnt;
if (l==r){
sum[rt]=a[l];
return;
}
build(L[rt],l,mid);
build(R[rt],mid+1,r);
up(rt,l,r);
}
void update(int pre,int &rt,int l,int r,int x,int y,int z){
rt=++cnt;
L[rt]=L[pre],R[rt]=R[pre],sum[rt]=sum[pre],lz[rt]=lz[pre];
if (x<=l && r<=y){
sum[rt]+=1ll*(r-l+1)*z;
lz[rt]+=z;
return;
}
if (x<=mid) update(L[pre],L[rt],l,mid,x,y,z);
if (mid<y) update(R[pre],R[rt],mid+1,r,x,y,z);
up(rt,l,r);
}
ll query(int t,int l,int r,int x,int y){
if (x==l && r==y) return sum[t];
ll ans=1ll*(y-x+1)*lz[t];
if (y<=mid) ans+=query(L[t],l,mid,x,y);
else if (x>mid) ans+=query(R[t],mid+1,r,x,y);
else ans+=query(L[t],l,mid,x,mid)+query(R[t],mid+1,r,mid+1,y);
return ans;
}
int main(){
scanf("%d%d",&n,&m);
for (i=1;i<=n;i++) scanf("%d",&a[i]);
build(rt[0],1,n);
for (;m--;){
scanf("%s%d",s,&x);
if (s[0]=='C') scanf("%d%d",&y,&z),update(rt[t],rt[t+1],1,n,x,y,z),t++;
if (s[0]=='Q') scanf("%d",&y),printf("%lld\n",query(rt[t],1,n,x,y));
if (s[0]=='H') scanf("%d%d",&y,&z),printf("%lld\n",query(rt[z],1,n,x,y));
if (s[0]=='B') t=x;
}
}