#include <iostream>
#include<iomanip>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <queue>
#include <deque>
#include <iterator>
#include <stack>
#include <map>
#include <set>
#include <numeric>
#include <limits>
#include <math.h>
#include <stdio.h>
#include <string.h>
#define MOD 1000000007
#define EPS 1e-15
#define ll long long
#define ld long double
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define F first
#define S second
#define endl '\n'
#define FOREACH(it,x) for(__typeof__((x).begin()) it = (x).begin(); it != (x).end(); ++it)
#define FOR(i,a,b) for(ll i=(ll)(a);i<=(ll)(b);++i)
#define ROF(i,a,b) for(ll i=(ll)(a);i>=(ll)(b);--i)
const ld PI=3.141592653589793238L;
const ll N=100005;
using namespace std;
// <<>>
ll nb[(1<<15)+5]; // nb[i] how many occurences of i are there in our current query.We only insert every number once in the trie.
ll a[N] , ans[50005];
pair<pair<ll,ll>,pair<ll,ll> >qu[50005];
ll block;
struct node{
struct node *z , *o;
ll n;
node(){
z=NULL;
o=NULL;
n=0;
}
};
node* root = new node();
void add(ll x){
++nb[x];
if(nb[x] > 1) return;
node* c = root;
++c->n;
ROF(i,15,0){
if(x&(1<<i)){
if(c->o == NULL) c->o = new node();
c=c->o;
}else{
if(c->z == NULL) c->z = new node();
c=c->z;
}
++c->n;
}
}
void del(ll x){
--nb[x];
if(nb[x] > 0) return;
node *c = root , *prv;
--c->n;
ROF(i,15,0){
prv = c;
if(x&(1<<i)) c = c->o;
else c = c->z;
--c->n;
if(c->n == 0){
if(x&(1<<i)) prv->o = NULL;
else prv->z = NULL;
return;
}
}
}
ll query(ll x){
node* c = root;
ROF(i,15,0){
if(x&(1<<i)){
if(c->z != NULL) c=c->z;
else x-=(1<<i) , c=c->o;
}else{
if(c->o != NULL) x+=(1<<i) , c=c->o;
else c=c->z;
}
}
return x;
}
bool cmp(pair<pair<ll,ll>,pair<ll,ll> >a,pair<pair<ll,ll>,pair<ll,ll> >b){
ll x = a.F.F / block , y = b.F.F / block;
if(x != y) return x < y;
return a.F.S < b.F.S;
}
int main(){
ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
//freopen("input.txt","r",stdin); freopen("output.txt","w",stdout);
ll tc; cin >> tc;
while(tc--){
FOR(i,0,1<<15) if(nb[i]) nb[i]=1 , del(i);
ll n,q; cin >> n >> q;
FOR(i,1,n) cin >> a[i];
block = sqrt(n);
FOR(i,1,q){
ll x,l,r; cin >> x >> l >> r;
qu[i] = mp(mp(l,r),mp(x,i));
}
sort(qu+1,qu+q+1,cmp);
ll cl = qu[1].F.F , cr=qu[1].F.S;
FOR(i,cl,cr) add(a[i]);
ans[qu[1].S.S] = query(qu[1].S.F);
FOR(i,2,q){
ll l = qu[i].F.F , r=qu[i].F.S;
while(cl < l) del(a[cl]) , ++cl;
while(cl > l) --cl , add(a[cl]);
while(cr < r) ++cr , add(a[cr]);
while(cr > r) del(a[cr]) , --cr;
ans[qu[i].S.S] = query(qu[i].S.F);
}
FOR(i,1,q) cout << ans[i] << endl;
}
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZTxpb21hbmlwPgojaW5jbHVkZSA8c3RyaW5nPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8YWxnb3JpdGhtPgojaW5jbHVkZSA8c3N0cmVhbT4KI2luY2x1ZGUgPHF1ZXVlPgojaW5jbHVkZSA8ZGVxdWU+CiNpbmNsdWRlIDxpdGVyYXRvcj4KI2luY2x1ZGUgPHN0YWNrPgojaW5jbHVkZSA8bWFwPgojaW5jbHVkZSA8c2V0PgojaW5jbHVkZSA8bnVtZXJpYz4KI2luY2x1ZGUgPGxpbWl0cz4KI2luY2x1ZGUgPG1hdGguaD4KI2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdHJpbmcuaD4KI2RlZmluZSBNT0QgMTAwMDAwMDAwNwojZGVmaW5lIEVQUyAxZS0xNQojZGVmaW5lIGxsIGxvbmcgbG9uZwojZGVmaW5lIGxkIGxvbmcgZG91Ymxl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iBjID0gcm9vdDsKICAgICsrYy0+bjsKICAgIFJPRihpLDE1LDApewogICAgICAgIGlmKHgmKDE8PGkpKXsKICAgICAgICAgICAgaWYoYy0+byA9PSBOVUxMKSBjLT5vID0gbmV3IG5vZGUoKTsKICAgICAgICAgICAgYz1jLT5vOwogICAgICAgIH1lbHNlewogICAgICAgICAgICBpZihjLT56ID09IE5VTEwpIGMtPnogPSBuZXcgbm9kZSgpOwogICAgICAgICAgICBjPWMtPno7CiAgICAgICAgfQogICAgICAgICsrYy0+bjsKICAgIH0KfQoKdm9pZCBkZWwobGwgeCl7CiAgICAtLW5iW3hdOwogICAgaWYobmJbeF0gPiAwKSByZXR1cm47CgogICAgbm9kZSAqYyA9IHJvb3QgLCAqcHJ2OwogICAgLS1jLT5uOwogICAgUk9GKGksMTUsMCl7CiAgICAgICAgcHJ2ID0gYzsKCiAgICAgICAgaWYoeCYoMTw8aSkpIGMgPSBjLT5vOwogICAgICAgIGVsc2UgYyA9IGMtPno7CgogICAgICAgIC0tYy0+bjsKICAgICAgICBpZihjLT5uID09IDApewogICAgICAgICAgICBpZih4JigxPDxpKSkgcHJ2LT5vID0gTlVMTDsKICAgICAgICAgICAgZWxzZSBwcnYtPnogPSBOVUxMOwogICAgICAgICAgICByZXR1cm47CiAgICAgICAgfQogICAgfQp9CgpsbCBxdWVyeShsbCB4KXsKICAgIG5vZGUqIGMgPSByb290OwogICAgUk9GKGksMTUsMCl7CiAgICAgICAgaWYoeCYoMTw8aSkpewogICAgICAgICAgICBpZihjLT56ICE9IE5VTEwpIGM9Yy0+ejsKICAgICAgICAgICAgZWxzZSB4LT0oMTw8aSkgLCBjPWMtPm87CiAgICAgICAgfWVsc2V7CiAgICAgICAgICAgIGlmKGMtPm8gIT0gTlVMTCkgeCs9KDE8PGkpICwgYz1jLT5vOwogICAgICAgICAgICBlbHNlICBjPWMtPno7CiAgICAgICAgfQogICAgfQoKICAgIHJldHVybiB4Owp9Cgpib29sIGNtcChwYWlyPHBhaXI8b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