#include<bits/stdc++.h>
using namespace std;
const int N=5e3+10,M=2*N;
int h[N],ne[M],e[M],idx=1;
void add(int a,int b)
{
	e[idx]=b,ne[idx]=h[a],h[a]=idx++;
}
int son[N],sz[N],top[N];
int dep[N],fa[N];
void dfs1(int u,int father,int depth)
{
	sz[u]=1;
	dep[u]=depth;
	fa[u]=father;
	for(int i=h[u];i;i=ne[i])
	{
		int v=e[i];
		if(v==father)continue;
		dfs1(v,u,depth+1);
		sz[u]+=sz[v];
		if(sz[son[u]]<sz[v])son[u]=v;
	}
}
void dfs2(int u,int t)
{
	top[u]=t;
	if(!son[u])return ;
	dfs2(son[u],t);
	for(int i=h[u];i;i=ne[i])
	{
		int v=e[i];
		if(v==son[u]||v==fa[u])continue;
		dfs2(v,v);
	}
}
int lca(int u,int v)
{
	while(top[u]!=top[v])
	{
		if(dep[top[u]]<dep[top[v]])swap(u,v);
		u=fa[top[u]];
	}
	return dep[u]<dep[v]?u:v;
}
int pre[N];
int res[5010][5010];
int nid[500010];
int main()
{
	freopen("query.in","r",stdin);
	freopen("query.out","w",stdout);
	int n;
	cin>>n;
	bool flag=true;
	for(int i=1;i<n;i++)
	{
		int u,v;
		cin>>u>>v;
		if(u+1!=v)flag=false;
		add(u,v),add(v,u);
	}
	dfs1(1,-1,1);
	dfs2(1,1);
	if(flag==true)
	{
		int q;
		cin>>q;
		while(q--)
		{
			int l,r,k;
			cin>>l>>r>>k;
			int ans=-1;
			for(int i=l;i+k-1<=r;i++)
			{
				ans=max(ans,dep[res[i][i+k-1]]);
	//			cout<<a
			}
			cout<<ans<<endl;
		}
	}

	memset(res,0,sizeof res);
	
	for(int i=1;i<=n;i++)
	{
		res[i][i]=i;
		for(int j=i+1;j<=n;j++)
		{
			res[i][j]=lca(res[i][j-1],j);
		}
	}
			

	int q;
	cin>>q;
	while(q--)
	{
		int l,r,k;
		cin>>l>>r>>k;
		int ans=-1;
		for(int i=l;i+k-1<=r;i++)
		{
			ans=max(ans,dep[res[i][i+k-1]]);
//			cout<<a
		}
		cout<<ans<<endl;
	}
	return 0;
}

