#include<bits/stdc++.h>
using namespace std;
using i64=long long;
constexpr int mxn=1000000;
int n,q,N,tot,mx[25][mxn+10],fa[mxn+10],dep[mxn+10],dfn[mxn+10],top[mxn+10],sz[mxn+10],tp;
basic_string<int>G[1000010];
i64 val[mxn+10],a[mxn+10];
auto midx=[](int x,int y){return val[x]>val[y]?x:y;};
auto qry=[](int l,int r){
int g=__lg(r-l+1);
return midx(mx[g][l],mx[g][r-(1<<g)+1]);
};
struct seg_t{
int l,r,v;
seg_t(int x,int y):l(x),r(y),v(qry(l,r)){};
bool operator<(const seg_t&o)const{return val[v]<val[o.v];}
};
void dfs1(int u){
dep[u]=dep[fa[u]]+1,sz[u]=1;
if(fa[u])G[u].erase(find(begin(G[u]),end(G[u]),fa[u]));
for(int&v:G[u])if(v!=fa[u]){
fa[v]=u,dfs1(v),sz[u]+=sz[v];
if(sz[v]>sz[G[u][0]])swap(v,G[u][0]);
}
}
void dfs2(int u,int t){
dfn[u]=++tot,val[tot]=a[u],mx[0][tot]=tot,top[u]=t;
for(int v:G[u])dfs2(v,v==G[u][0]?t:v);
}
auto ri=[](auto&x){x=0;
char c=getchar_unlocked();
while(!isdigit(c))c=getchar_unlocked();
while(isidigit(c))x=x*10+c-'0',c=getchar_unlocked();
};
int main(){
ri(n);
for(int i=1,u,v;i<n;i++)ri(u),ri(v),G[u]+=v,G[v]+=u;
for(int i=1;i<=n;i++)ri(a[i]);
dfs1(1),dfs2(1,1);
for(int i=1;n>>i;i++)for(int k=1;k+(1<<i)-1<=n;k++)
mx[i][k]=midx(mx[i-1][k],mx[i-1][k+(1<<i-1)]);
i64 lstans=0;cin>>q;
for(int u,v,k;q--;){
static priority_queue<seg_t>q;
ri(u),ri(v),ri(k);
u=(u^lstans)%n+1,v=(v^lstans)%n+1,lstans=0;
for(;!q.empty();q.pop());
auto nex=[]{
if(q.empty())return 0ll;
auto[l,r,p]=q.top();q.pop();
if(l<p)q.emplace(l,p-1);
if(p<r)q.emplace(p+1,r);
return val[p];
};
for(;top[u]!=top[v];u=fa[top[u]]){
if(dep[top[u]]<dep[top[v]])swap(u,v);
q.emplace(dfn[top[u]],dfn[u]);
}if(dfn[u]>dfn[v])swap(u,v);
q.emplace(dfn[u],dfn[v]);
vector<i64>b;
generate_n(back_inserter(b),k*62,nex);
for(i64 w=1ll<<61;w;w>>=1){
int c=0;
auto it=upper_bound(begin(b),end(b),w,greater<>{});
for_each(begin(b),it,[=](i64&x){x^=w;});
if(it-begin(b)>=k)b.erase(it,end(b)),lstans|=w;
else{
inplace_merge(begin(b),it,end(b),greater<>{});
}
}printf("%lld\n",lstans);
}
return 0;
}