#include<cstdio>
#include<algorithm>
#define G *++ptr
typedef long long i64;
const int N=3e5+7,inf=0x3f3f3f3f;
char buf[N*100],*ptr=buf-1;
int _(){
int x=0,c=G;
while(c<48)c=G;
while(c>47)x=x*10+c-48,c=G;
return x;
}
int n,q,rt[N],ch[N*25][2],sz[N*25][2],p=0,xs[N],id[N],ir[N];
i64 sum[N*25];
int ins(int w,int x,int v){
int u=++p,u0=u,a=v&1;
sz[u][a]=sz[w][a]+1;
sz[u][a^1]=sz[w][a^1];
sum[u]=sum[w]+v;
for(int i=20,d;~i;--i){
d=x>>i&1;
ch[u][d^1]=ch[w][d^1];
u=ch[u][d]=++p;w=ch[w][d];
sz[u][a]=sz[w][a]+1;
sz[u][a^1]=sz[w][a^1];
sum[u]=sum[w]+v;
}
return u0;
}
int _a,_x,_v;
void getpv(int w1,int w2,int L,int R){
if(_v||sz[w1][_a]==sz[w2][_a])return;
if(L==R&&R<=_x){
_v=L;
return;
}
int M=L+R>>1;
if(_x>M)getpv(ch[w1][1],ch[w2][1],M+1,R);
getpv(ch[w1][0],ch[w2][0],L,M);
}
void getnx(int w1,int w2,int L,int R){
if(_v||sz[w1][_a]==sz[w2][_a])return;
if(L==R&&L>=_x){
_v=L;
return;
}
int M=L+R>>1;
if(_x<=M)getnx(ch[w1][0],ch[w2][0],L,M);
getnx(ch[w1][1],ch[w2][1],M+1,R);
}
void query(int l,int r,int k){
if(r-l+1<k){
puts("-1");
return;
}
i64 ss=0;
int w1=rt[r],w2=rt[l-1],c0=0,c1=0,v=0;
for(int i=20;~i;--i){
int u1=ch[w1][1],u2=ch[w2][1];
int s0=sz[u1][0]-sz[u2][0];
int s1=sz[u1][1]-sz[u2][1];
if(s0+s1>k){
v|=1<<i;
w1=u1,w2=u2;
}else{
k-=s0+s1;
ss+=sum[u1]-sum[u2];
c0+=s0,c1+=s1;
w1=ch[w1][0],w2=ch[w2][0];
}
}
if(c0&1){
int l0,l1,r0,r1;
_a=0,_x=v;
_v=0,getpv(rt[r],rt[l-1],0,(1<<21)-1),l0=_v;
_x=v+1;
_v=0,getnx(rt[r],rt[l-1],0,(1<<21)-1),l1=_v;
_a=1,_x=v;
_v=0,getpv(rt[r],rt[l-1],0,(1<<21)-1),r0=_v;
_x=v+1;
_v=0,getnx(rt[r],rt[l-1],0,(1<<21)-1),r1=_v;
int d1=r1&&l0?r1[id][xs]-l0[id][xs]:inf,d2=l1&&r0?l1[id][xs]-r0[id][xs]:inf;
if(d1==inf&&d2==inf){
puts("-1");
return;
}
ss-=std::min(d1,d2);
}
printf("%lld\n",ss);
}
bool cmp(int a,int b){
return xs[a]<xs[b];
}
int main(){
fread(buf,1,sizeof(buf),stdin)[buf]=0;
n=_();
for(int i=1,x;i<=n;++i)xs[id[i]=i]=_();
std::sort(id+1,id+n+1,cmp);
for(int i=1,x;i<=n;++i)ir[id[i]]=i;
for(int i=1;i<=n;++i){
rt[i]=ins(rt[i-1],ir[i],xs[i]);
}
for(q=_();q;--q){
int l=_(),r=_(),k=_();
query(l,r,k);
}
return 0;
}