#include<bits/stdc++.h>
using namespace std;
const int N = 5001;
int n,lca[N][N];
int fa[N],siz[N],dep[N],top[N],son[N];
vector<int> g[N];
int read()
{
	int x=0,w=1;
	char ch = getchar();
	while(ch<'0' || ch>'9')
	{
		if(ch == '-') w=-1;
		ch =  getchar();
	}
	while(ch>='0' && ch<='9')
	{
		x = x*10+ch-'0';
		ch = getchar();
	}
	return x*w;
}
void dfs1(int u,int f)
{
	fa[u] = f;
	dep[u] = dep[f]+1;
	siz[u] = 1;
	for(int i = 0;i < g[u].size();i++)
	{
		int v = g[u][i];
		if(v == f) continue;
		dfs1(v,u);
		siz[u] += siz[v];
		if(!son[u] || siz[son[u]]<siz[v])
		{
			son[u] = v;
		}
	}
}
void dfs2(int u,int tp)
{
	top[u] = tp;
	if(!son[u]) return;
	dfs2(son[u],tp);
	for(int i = 0;i < g[u].size();i++)
	{
		int v = g[u][i];
		if(v!=fa[u] && v!=son[u])
		dfs2(v,v);
	}
}
int LCA(int x,int y)
{
	while(top[x] != top[y])
	{
		if(dep[top[x]] < dep[top[y]]) swap(x,y);
		x = fa[top[x]];
	}
	return (dep[x]<dep[y])?x:y;
}
signed main()
{
	freopen("query.in","r",stdin);
	freopen("query.out","w",stdout);
	scanf("%d",&n);
	for(int i = 1;i < n;i++)
	{
		int u=read(),v=read();
		g[u].push_back(v);
		g[v].push_back(u);
	}
	dfs1(1,0);
	dfs2(1,1);
	for(int i = 1;i <= n;i++)
	{
		lca[i][i] = i;
		for(int j = i+1;j <= n;j++)
		{
			lca[i][j] = LCA(lca[i][j-1],j);
//			printf("lca[%d][%d]=%d\n",i,j,lca[i][j]);
		}
	}
	int q;
	scanf("%d",&q);
	while(q--)
	{
		int ans=0;
		int l=read(),r=read(),k=read();
		for(int len = k;len <= r-l+1;len++)
		{
			for(int i=l,j=i+len-1;j <= r;i++,j++)
			{
				ans = max(ans,dep[lca[i][j]]);
			}
		}
		printf("%d\n",ans);
	}
}
