#include<bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
#include <ext/pb_ds/detail/standard_policies.hpp>
using namespace __gnu_pbds;

#define BOOM ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define PI 3.14159265358979323846
#define NEWLINE cout<<"\n";
#define ll long long
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define all(v) v.begin(), v.end()
#define ff first
#define ss second
#define pii pair<int,int>
#define mset(a,b) memset(a, b, sizeof(a))
#define BHAAGO {cout<<"NO\n";continue;}
using namespace std;
#define MAXN 200
#define MOD  1000000007
int posfastscan() { 
	register int c;  int number = 0; 
	for(c = getchar(); !(c>47 && c<58); c= getchar()); 
	for (; (c>47 && c<58); c=getchar())  number = (number<<1) + (number<<3) + (c ^ 48); 
	return number;
} 
int fastscan() { 
	bool negative = false;  register int c;  int number = 0; 
	for(c = getchar(); !(c>47 && c<58) && c!= '-'; c= getchar()); 
	if (c=='-')  c = getchar(), negative = true; 
	for (; (c>47 && c<58); c=getchar())  number = (number<<1) + (number<<3) + (c ^ 48); 
	if (negative) return -number; else return number;
} 
void write(int x){
	if(x>9) write(x/10);
	putchar(x%10 + 48);
}
//LARGE POWERS
ll power(long long x, long long y, long long p)  
{  
	ll res = 1; 
	x = x % p; 
	if (x == 0) return 0;
	while (y > 0){  
		if (y & 1)  res = (res*x) % p;  
		y = y>>1; 
		x = (x*x) % p;  
	}  
	return res;  
} 
//Fermat Inverse
ll fermatinv(int n, int p) { return power(n,p-2,p); }

//Combination nCr
int fact[MAXN], ifact[MAXN];
void Cinit() {
	fact[0] = 1;
	for (int i = 1; i < MAXN; ++i) fact[i] = 1LL * fact[i - 1] * i % MOD;
	ifact[MAXN - 1] = power(fact[MAXN - 1], MOD - 2, MOD);
	for (int i = MAXN - 2; i >= 0; --i) ifact[i] = 1LL * ifact[i + 1] * (i + 1) % MOD;
}
int nCr(int n, int r) {
  if (r < 0 || r > n) return 0;
  return (1LL * fact[n] * ifact[r] % MOD * ifact[n - r] % MOD)%MOD;
}
long long int sumXOR(ll arr[], int n)
{
    long long int sum = 0;
    for (int i = 0; i < 64; i++) 
    {
        //  Count of zeros and ones
        ll zc = 0, oc = 0; 
        // Individual sum at each bit position
        long long int idsum = 0; 
        for (int j = 0; j < n; j++)
        {
            if (arr[j] % 2 == 0)
                zc++;
            else
                oc++;
            arr[j] /= 2;
        }
          
        // calculating individual bit sum 
        idsum = ((oc * zc)%MOD) * (1LL << (i/2)) % MOD * (1LL << (i-i/2)) % MOD; 
  
        // final sum    
        sum += idsum;
    }
    return sum%MOD;
}
int main()
{	
	// BOOM
	int  _, n, m, k, l=0, r=0, i, j, tt=1, sum=0, c, x,y,z=1;
	bool flag=0;
	string s, ss;
	// Cinit();
	// cin>>tt;
	while(tt--){
		// int q;
		cin>>n;
		// int a[n];
		// for(i=0;i<n;i++)  cin>>a[i];
		std::vector<pair<ll,ll>> adj[n];
		for(i=0;i<n-1;i++) {
			ll x,y,z;
			cin>>x>>y>>z;
			adj[x-1].emplace_back(y-1, z);
			adj[y-1].emplace_back(x-1, z);
		}
		ll a[n]={};
		bool vis[n]={};
		queue<int> q;
		q.push(0);
		while(!q.empty()) {
			int u = q.front(); q.pop();
			vis[u]=1;
			for(auto p : adj[u])
				if(!vis[p.ff])
					a[p.ff] = p.ss ^ a[u],
					q.push(p.ff);
			// cout<<"u = "<<u<<endl;
		}
		// cout<<"Y"<<endl;
		cout<<sumXOR(a, n);
		// if(a[1] + a[1] == a[0] + a[2]) 
			// cout<<"Yes"<<'\n';
			// for(i=0;i<n;i++) cout<<a[i]<<' '; cout<<'\n';
		// }
		// else 
			// cout<<ans+1<<'\n';
			// cout<<"No"<<'\n';
	}
	//cout<<ans;
	/*for(i=0;i<n;i++){
		cin>>;
	}
	*/
	// for(i=0;i<=n;i++){
	// 	cout<<"\nadj["<<i<<"] = ";
	// 	for(j=0;j<adj[i].size();j++)
	// 		cout<<adj[i][j]<<" ";
	// }
	// for(i=0;i<d.size();i++)
	// cout<<d[i]<<" ";NEWLINE
	/*v = n*m;
	Graph G(v);
	char a[v], b[n][m];
	vector<bool> visited(v, false); 
	vector<pair<int, unsigned long int>> level(v);
	for(i=0;i<v;i++){
		level[i].first = i; level[i].second = 0;
	}*/
	
	return 0;
}
#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<int,ll> pil;
typedef pair<ll,int> pli;
typedef pair<ll,ll> pll;
typedef pair<pair<int, int>, int> ppi;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef bitset<60> Bitset;
const ll INFL = 1LL << 60;
const int INF = 1000000005;
const int MOD = 1000000007;
int dx[4] = {1,0,-1,0};
int dy[4] = {0,1,0,-1};
ll GCD(ll a, ll b) { return b ? GCD(b, a % b) : a; }
ll LCM(ll a, ll b) { return (a * b) / GCD(a, b); }
bool range(int x,int y,int X,int Y){if(0<=x&&x<X&&0<=y&&y<Y){return true;} return false;}
//------------------------------------------------------------------------------------------------------------------------------

int main(void) {
    ios::sync_with_stdio(false);cin.tie(nullptr);
    int n;cin>>n;
    vector<pll> p(n);
    for(int i = 0; i < n; i++) {
        ll x,y;cin>>p[i].first>>p[i].second;
    }
    int m;cin>>m;
    vvl v(m+1,vl(6,0));
    v[0][0] = v[0][3] = 1;
    for(int i = 1; i <= m; i++) {
        int op;cin>>op;
        ll p;
        if(op>=3) cin>>p;
        switch(op){
            case 1:
                v[i][0] = v[i-1][2]; v[i][1] = v[i-1][3]; v[i][2] = -v[i-1][0]; v[i][3] = -v[i-1][1];
                v[i][4] = v[i-1][5]; v[i][5] = -v[i-1][4];
                break;
            case 2:
                v[i][0] = -v[i-1][2]; v[i][1] = -v[i-1][3]; v[i][2] = v[i-1][0]; v[i][3] = v[i-1][1];
                v[i][4] = -v[i-1][5]; v[i][5] = v[i-1][4];
                break;
            case 3:
                v[i][0] = -v[i-1][0]; v[i][1] = -v[i-1][1]; v[i][2] = v[i-1][2]; v[i][3] = v[i-1][3];
                v[i][4] = -v[i-1][4] + 2*p; v[i][5] = v[i-1][5];
                break;
            case 4:
                v[i][0] = v[i-1][0]; v[i][1] = v[i-1][1]; v[i][2] = -v[i-1][2]; v[i][3] = -v[i-1][3];
                v[i][4] = v[i-1][4]; v[i][5] = -v[i-1][5] + 2*p;
                break;
        }
        /*
        for(int j = 0; j < 6; j++) cout<<v[i][j]<<' ' ;
        cout<<endl;
        */
    }
    int q;cin>>q;
    for(int i = 0; i < q; i++) {
        int a,b;cin>>a>>b;b--;
        ll x = p[b].first,y = p[b].second;
        cout<<v[a][0]*x+v[a][1]*y+v[a][4]<<' '<<
              v[a][2]*x+v[a][3]*y+v[a][5]<<endl;
    }
}