#include <bits/stdc++.h>
using namespace std;
#define F(i,a,b) for(int i=(a);i<=(b);i++)
#define F_(i,a,b) for(int i=(a);i>=(b);i--)
#define pb push_back
typedef long long ll;
const int N = 5e3+10,M = 5e5+10;
int n,q;
//int s[N];
int kans[N][N];
int lg[N],fa[N][20],dep[N];
vector<int> v[N];
void dfs1(int x,int fu){
	fa[x][0] = fu;
	dep[x] = dep[fu]+1;
	for (auto y:v[x]){
		if (y==fu) continue;
		dfs1(y,x);
	}
}
int LCA(int x,int y){
	if (dep[x]<dep[y]) swap(x,y);
	while (dep[x]!=dep[y]) x = fa[x][lg[dep[x]-dep[y]]];
	if (x==y) return x;
	F_(i,19,0) if (fa[x][i]!=fa[y][i]) x=fa[x][i],y=fa[y][i];
	return fa[x][0];
}
void init(){
	F(i,2,n) lg[i] = lg[i/2]+1;
	F(j,1,19) F(i,1,n) fa[i][j] = fa[fa[i][j-1]][j-1];
	F(x,1,n) F(y,x,n){
		int k=y-x+1;
		if (x==y) kans[k][x] = x;
		else kans[k][x] = LCA(kans[k-1][x],y);
	}
}
void solve1(){
	dfs1(1,0);
	init();
	scanf("%d",&q);
	F(i,1,q){
		int l,r,k,ans=0;
		scanf("%d%d%d",&l,&r,&k);
		for (int x=l,y=l+k-1;y<=r;x++,y++){
			ans = max(ans,dep[kans[k][x]]);
		}
		//s[i]=ans;
		printf("%d\n",ans);
	}
}
int main(){
	freopen("query.in","r",stdin);
	freopen("query.out","w",stdout);
	scanf("%d",&n);
	F(i,1,n-1){
		int x,y;
		scanf("%d%d",&x,&y);
		v[x].pb(y),v[y].pb(x);
	}
	solve1();
//	fclose(stdin);
//	freopen("query3.ans","r",stdin);
//	int x,p=0;
//	while (cin>>x){
//		if (x!=s[++p]) printf("%d\n",p);
//	}
	return 0;
}
