#include<bits/stdc++.h>
using namespace std;
//#include <ext/pb_ds/assoc_container.hpp> // Common file
//#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
//using namespace __gnu_pbds; //  order of key(keys strictly less than)  // find_by_order
//typedef tree<long long,null_type,less<>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
//typedef tree<long long, null_type, less_equal<>, rb_tree_tag, tree_order_statistics_node_update> indexed_multiset;
//IF WA CHECK FOR : -
// 1 > EDGE CASES LIKE N=1 , N=0
// 2 > SIGNED INTEGER OVERFLOW IN MOD
// 3 > CHECK THE CODE FOR LOGICAL ERRORS AND SEG FAULTS
// 4 > READ THE PS ONCE AGAIN , if having double diff less than 1e-8 is same.
// 5 > You Have got AC .
#define ll long long
#define NUM (ll)1000000007
#define inf (long long)(1e12)
#define ff first
#define ss second
#define f(i,a,b) for(ll i=a;(i)<long(b);(i)++)
#define fr(i,a,b) for(ll i=a;(i)>=(long long)(b);(i)--)
#define it(b)  for(auto &it:(b))
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
typedef vector<ll> vll;
typedef pair<ll,ll> pll;
ll binpow( ll base , ll ex,ll mod=NUM) {
    ll ans = 1;base = base % mod;
    if(base==0){
        return 0;
    }
    while (ex > 0) {
        if (ex % 2 == 1) {
            ans = (ans * base) % mod;
        }
        base = (base * base) % mod;
        ex = ex / 2;
    }
    return ans;
}
void read(vll &arr,ll n) {
    if (arr.size() != n) { arr.assign(n, 0); }for (int i = 0; i < n; i++)cin >> arr[i];
}
inline ll min(ll a,ll b){
    if(a>b)return b;return a;
}
inline ll max(ll a, ll b){
    if(a>b)return a;return b;
}
inline ll dif(ll a,ll b) {
    if (a > b)return a - b;return b - a;
}
long long gcd(long long a,long long b) {
    if (b == 0)return a;return gcd(b, a % b);
}
long long lcm(long long a,long long b) {
    long long k = gcd(a, b);
    return (a * b) / k;
}
vll fun(ll z){
    vll res;f(i,0,21){
        res.pb(z%2);z/=2;
    }
    reverse(res.begin(),res.end());
    return res;
}
ll fun2(vector<vector<ll>>&dp,ll l,ll x)
{
    if(l<0){
        return 0;
    }
    //from 0 to l and i|x;
    if(x<=l) {
        return dp[x][20];
    }
    else{
        // string of x and l+1
        vll a = fun(x);vll b = fun(l+1);
        ll res=0;
        ll p = 1<<20;
        ll pref=x;
        for(int i=0;i<=20;i++){
            if(b[i]!=a[i]){
                if(b[i]==1){
                    res+=dp[pref][20-i];
                    break;
                }
                pref^=(1<<(20-i));a[i]=0;
            }
            if(a[i]){
                // that means we can set it to 0.
                res+=dp[pref^(1<<(20-i))][20-i];
            }
            p/=2;
        }
        return res;
    }
}
void solve() {
    int n;cin>>n;
    ll z = 1;
    while(z<n){
        z*=2;
    }
    vector<ll>arr;read(arr,n);
    // l to r sum of elements which have i|x = x
    vector<vector<ll>>dp(n,vector<ll>(21));
    for(int i=0;i<n;i++){
        dp[i][0]=arr[i];
        for(int k=1;k<=20;k++){
            ll p = (1<<(k-1));
            if((i^p)<i){
                // it's here dude .
                dp[i][k]=dp[i^p][k-1]+dp[i][k-1];
            }
            else{
                dp[i][k]=dp[i][k-1];
            }
        }
    }
    int q;cin>>q;
    ll prev=0;
    while(q--){
        ll l,x,r;cin>>l>>r>>x;
        x += prev;x%=z;
        //from 0 to l and i|x;
        ll k = fun2(dp,r,x)-fun2(dp,l-1,x);
        cout<<k<<endl;prev=k;
    }
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
    cout << fixed << showpoint;
    cout << setprecision(12);
    long long test_m = 1;
    cin >> test_m;
    //I WILL WIN .
    while (test_m--) {
        solve();
    }
}