#include<bits/stdc++.h>
//#pragma GCC optimize "trapv"
//#include<ext/pb_ds/assoc_container.hpp>
//#include<ext/pb_ds/tree_policy.hpp>
#define fast_az_fuk      ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define ll               long long
#define lll              __int128
#define ull              unsigned ll
#define ld               long double 
#define pb               push_back 
#define pf               push_front
#define dll              deque<ll> 
#define vll              vector<ll>
#define vvll             vector<vll> 
#define pll              pair<ll,ll> 
#define vpll             vector<pll>
#define dpll             deque<pll>
#define mapll            map<ll,ll>
#define umapll           umap<ll,ll>
// #define endl             "\n" 
#define all(v)           v.begin(),v.end() 
#define ms(a,x)          memset(a,x,sizeof(a))
#define random(l,r,T)    uniform_int_distribution<T>(l,r)(rng)


//#define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>

using namespace std;


mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//using namespace __gnu_pbds;

template<typename T> istream& operator >>(istream &in,vector<T> &v){ for(auto &x:v) in>>x; return in;}
template<typename T> ostream& operator <<(ostream &out,const vector<T> &v){ for(auto &x:v) out<<x<<' '; return out;}
template<typename T1,typename T2> istream& operator >>(istream &in,pair<T1,T2> &p){ in>>p.first>>p.second; return in;}
template<typename T1,typename T2> ostream& operator <<(ostream &out,const pair<T1,T2> &p){ out<<p.first<<' '<<p.second; return out;}
ll sumn,sumq;
const bool tests = 1;
void solve_case(){
    ll n; cin>>n; vll a(n); cin>>a; assert(n >= 1 && n <= 1e5);
    sumn+=n;
    for(ll x : a){
        assert(x>=0 && x<=1e9);
    }
    int bt = 0; while((1<<bt) < n) ++bt;
    vvll dp((1<<bt),vll(bt+1));
    for(int i=0;i<(1<<bt);i++){
        dp[i][0] = (i<n?a[i]:0);
    }
    for(int j=1;j<=bt;j++){
        for(int i=0;i<(1<<bt);i++){
            dp[i][j] += dp[i][j-1];
            if(i & (1<<(j-1))){
                dp[i][j] += dp[i^(1<<(j-1))][j];
            }
        }
    }
    auto solve = [&](ll l,ll x){
        if(x <= l){
            return dp[x][bt];
        }
        ll ans = 0;
        for(int i=bt-1;i>=0;i--){
            if((x&(1<<i))){
                if((l&(1<<i)) == 0) {
                    x ^= (1<<i);
                }
                else{
                    ans += dp[x^(1<<i)][i];
                }
            }
            if(x <= l) {
                ans += dp[x][i]; break;
            }
        }
        return ans;
    };
    ll q; cin>>q; ll ans = 0; assert(q>0 && q<=1e5);
    sumq += q;
    while(q--){
        ll l,r,x; cin>>l>>r>>x;
        assert(r >= l);
        assert(l>=0&&l<n && r>=0 && r<n && x>=0 && x<=1e9);
        x = (x + ans) % (1<<bt);
        ans = solve(r,x) - (l>0?solve(l-1,x):0);
        cout<<ans<<endl;
    }
}

int32_t main()
{
    #ifdef LOCAL
        freopen("error.txt", "w", stderr);
        clock_t clk = clock();
    #endif
    fast_az_fuk
    sumn = sumq = 0;
    ll testcase=1; if(tests) cin>>testcase;
    cout<<fixed<<setprecision(10);
    for(ll test=1;test<=testcase;test++)
    {//cout<<"Case #"<<test<<": ";
        solve_case();
    }
    assert(sumn >= 1 && sumn <= 1e5);
    assert(sumq >= 1 && sumq <= 1e5);
    #ifdef LOCAL
        cerr << '\n'<<"Time (in s): " << double(clock() - clk) * 1.0 / CLOCKS_PER_SEC << '\n';
    #endif
    return 0;
}