#include<bits/stdc++.h>
#define ll long long
#define max(a,b) (a>b?a:b)
#define min(a,b) (a<b?a:b)
using namespace std;
inline int read()
{
	char c=getchar();int x=0;bool f=0;
	for(;!isdigit(c);c=getchar())f^=!(c^45);
	for(;isdigit(c);c=getchar())x=(x<<1)+(x<<3)+(c^48);
	return (f?-x:x);
}
const int M=5e5+5,N=5005;
int n,q,x,y,dep[M],da[N][N],f[M][25],ans[M][25],l,r,k,sub;
vector<int> g[M];
void dfs(int x,int fa)
{
	f[x][0]=fa;
	dep[x]=dep[fa]+1;
	if(dep[x]==n-1)sub=1;
	for(int v:g[x])
		if(v!=fa)dfs(v,x);
}
int LCA(int x,int y)
{
	if(dep[x]<dep[y])swap(x,y);
	for(int i=20;i>=0;--i)
		if(dep[f[x][i]]>=dep[y])x=f[x][i];
	if(x==y)return x;
	for(int i=20;i>=0;--i)
		if(f[x][i]!=f[y][i])x=f[x][i],y=f[y][i];
	return f[x][0]; 
}
int main()
{
	freopen("query.in","r",stdin);
	freopen("query.out","w",stdout);
	n=read();
	for(int i=1;i<n;++i)
	{
		x=read();y=read();
		g[x].push_back(y);
		g[y].push_back(x);
	}
	dfs(1,0);
	for(int j=1;j<=20;++j)
		for(int i=1;i<=n;++i)
			f[i][j]=f[f[i][j-1]][j-1];
	for(int i=1;i<=n;++i)ans[i][0]=i;
	for(int j=1;j<=20;++j)
		for(int i=1;i+(1<<j)<=n;++i)
			ans[i][j]=LCA(ans[i][j-1],ans[i+(1<<(j-1))][j-1]);
	if(n<=5000)
	{
		for(int i=1;i<=n;++i)
		{
			int fq=i;
			da[i][1]=dep[fq];
			for(int j=i+1;j<=n;++j)
			{
				fq=LCA(fq,j);
				da[i][j-i+1]=dep[fq];
			}
		}
	}
	q=read();
	while(q--)
	{
		l=read();r=read();k=read();
		if(r-l+1==k)
		{
			int cd=log2(k);
			printf("%d\n",dep[LCA(ans[l][cd],ans[r-(1<<cd)+1][cd])]);
			continue;
		}
		if(n<=5000)
		{
			int maxx=0;
			for(int i=l;i<=r-k+1;++i)
				maxx=max(maxx,da[i][k]);
			printf("%d\n",maxx);
			continue;
		}
	}
	return 0;
}
/*
6
5 6
6 1
6 2
2 3
2 4
3
2 4 3
1 4 4
1 6 6

*/
