#include <bits/stdc++.h>
using namespace std;

int n,q,lit[500005],dep[500005],f[30][500005];
vector <int> edge[500005];

void dfs(int x,int from,int deep){
	lit[deep] = x;
	dep[x] = deep;
	for(auto y : edge[x])
		if(y != from)
			dfs(y,x,deep + 1);
} 

int llog(int x){
	for(int i = 1;i <= 20;i++)
		if(x < (1 << i))
			return i - 1; 
}

int main(){
//	freopen("query.in","r",stdin);
//	freopen("query.out","w",stdout);
	scanf("%d",&n);
	for(int i = 1;i < n;i++){
		int a,b; scanf("%d%d",&a,&b);
		edge[a].push_back(b);
		edge[b].push_back(a);
	}
	dfs(1,0,1);
	for(int i = 1;i <= n;i++)	
		f[0][i] = dep[i];
	for(int i = 1;i <= 20;i++)
		for(int j = 1;j + (1 << i) - 1 <= n;j++)
			f[i][j] = min(f[i - 1][j],f[i - 1][j + (1 << (i - 1))]);
	scanf("%d",&q);
	while(q--){
		int l,r,k; scanf("%d%d%d",&l,&r,&k);
		int lg = llog(k);
		printf("%d\n",max(f[lg][l],f[lg][r - (1 << lg) + 1]));
	}
}
