Eyjafjalla
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
using namespace std;
const int maxn=8e6+10;
const int maxe=2e7+10;
int n,m;
int e,h[maxn],ne[maxn],ver[maxn],w[maxn],b[maxn],wt[maxn];
int son[maxn],id[maxn],fa[maxn][35],cnt,dep[maxn],siz[maxn],top[maxn];
int T[maxn],L[maxe],R[maxe],sum[maxe],tot;
int nd[maxn],nd1[maxn],nd2[maxn];
void add(int x,int y){
ver[++e]=y;
ne[e]=h[x];
h[x]=e;
}
int build(int l,int r){
int rt=++tot;
int mid=(l+r)>>1;
if(l<r){
L[rt] = build(l, mid);
R[rt] = build(mid+1, r);
}
return rt;
}
int update(int pre, int l, int r, int x)
{
int rt = ++ tot;
L[rt] = L[pre]; R[rt] = R[pre]; sum[rt] = sum[pre] + 1;
int mid=(l+r)>>1;
if (l < r){
if (x <= mid) L[rt] = update(L[pre], l, mid, x);
else R[rt] = update(R[pre], mid+1, r, x);
}
return rt;
}
int query(int u,int v,int l,int r,int ll,int rr){
// cout<<u<<"tt"<<v<<sum[v]<<" "<<sum[u]<<" "<<l<<" "<<r<<endl;
if(ll<=l&&rr>=r){
// cout<<l<<"zz"<<r<<" "<<sum[v]<<" "<<sum[u]<<endl;
return sum[v]-sum[u];
}
int mid=(l+r)>>1;
int ans=0;
if(ll<=mid)ans+=query(L[u],L[v],l,mid,ll,rr);
if(rr>mid)ans+=query(R[u],R[v],mid+1,r,ll,rr);
return ans;
}
void dfs1(int x,int f,int deep){
dep[x]=deep;siz[x]=1;
fa[x][0]=f;
for (int k =1; k<=20; k ++ )
fa[x][k]=fa[fa[x][k - 1]][k - 1];
int maxson=-1;
for(int i=h[x];i;i=ne[i]){
int y=ver[i];
if(y==f)continue;
dfs1(y,x,deep+1);
siz[x]+=siz[y];
if(siz[y]>maxson){
maxson=siz[y],son[x]=y;
}
}
}
void dfs2(int x,int topf){
id[x]=++cnt;
wt[cnt]=w[x];
top[x]=topf;
if(!son[x])return;
dfs2(son[x],topf);
for(int i=h[x];i;i=ne[i]){
int y=ver[i];
if(y==fa[x][0]||y==son[x])continue;
dfs2(y,y);
}
}
int main(){
scanf("%d",&n);
// rr=1;
for(int i=1;i<n;i++){
int a,b;
scanf("%d%d",&a,&b);
add(a,b);add(b,a);
}
for(int i=1;i<=n;i++){
scanf("%d",&w[i]);
b[i]=w[i];
}
// for(int i=1;i<=n;i++)cout<<w[i]<<endl;
// for(int i=1;i<=n;i++)cout<<"GG"<<T[i]<<endl;
int q;
scanf("%d",&q);
int Z=n;
for(int i=1;i<=q;i++){
scanf("%d%d%d",&nd[i],&nd1[i],&nd2[i]);
b[++Z]=nd1[i],b[++Z]=nd2[i];
}
sort(b+1,b+1+Z);
m=unique(b+1,b+1+Z)-1-b;
for(int i=1;i<=n;i++){
w[i]=lower_bound(b+1,b+1+m,w[i])-b;
// cout<<w[i]<<endl;
}
dfs1(1,0,1);
dfs2(1,1);
T[0]=build(1,m);
//for(int i=1;i<=n;i++)cout<<w[i]<<endl;
// for(int i=1;i<=n;i++)cout<<wt[i]<<endl;
// for(int i=1;i<=n;i++)cout<<"ss"<<id[i]<<endl;
// for(int i=1;i<=n;i++)cout<<siz[i]<<endl;
for(int i=1;i<=n;i++){
T[i]=update(T[i-1],1,m,wt[i]);
}
// cout<<fa[0][0]<<"SS"<<endl;
w[0]=2e9;
//cout<<w[0]<<"JOSAS"<<endl;
for(int i=1;i<=q;i++){
int x,y,z;
x=nd[i],y=nd1[i],z=nd2[i];
y=lower_bound(b+1,b+1+m,y)-b;
z=lower_bound(b+1,b+1+m,z)-b;
// x=id[x];
// cout<<wt[id[x]]<<" "<<y<<" "<<z<<endl;
// cout<<wt[x]<<" "<<y<<" eeeeeeeeeeeeeeeee"<<z<<endl;
if(wt[id[x]]<y||wt[id[x]]>z){
printf("0\n");
}else{
int ans=0;
// cout<<"LL"<<endl;
ans=1;
for (int i = 20; i >= 0; i--)
if (fa[x][i] && w[fa[x][i]] <=z) x = fa[x][i];
//cout<<"JJ"<<x<<endl;
//cout<<T[id[x]]<<"ss"<<T[id[x]+siz[x]-1]<<endl;
ans+=query(T[id[x]],T[id[x]+siz[x]-1],1,m,y,z);
printf("%d\n",ans);
}
}
}