#include<bits/stdc++.h>
using namespace std;
const int N=5e5+5;
vector<int>f[N];
int n,dep[N],fa[N][25];
bool spe=1;
void pre1(int x){
	for (int i=0;i<f[x].size();i++){
		int y=f[x][i];
		if (y==fa[x][0])  continue;
		dep[y]=dep[x]+1,fa[y][0]=x;
		for (int j=1;j<25;j++)  fa[y][j]=fa[fa[y][j-1]][j-1];
		pre1(y);
	}
	return;
}
void pre2(int x){
	for (int i=0;i<f[x].size();i++){
		int y=f[x][i];
		if (dep[y])  continue;
		dep[y]=dep[x]+1;
		pre2(y);
	}
	return;
}
int lca(int x,int y){
	if (dep[x]<dep[y])  swap(x,y);
	for (int i=24;i>=0;i--){
		if (dep[fa[x][i]]>=dep[y])  x=fa[x][i];
		if (x==y)  return x;
	}
	for (int i=24;i>=0;i--)
		if (fa[x][i]!=fa[y][i])  x=fa[x][i],y=fa[y][i];
	return fa[x][0];
}
void work1(){
	int l,r,k,ans=-2100000000;
	scanf("%d %d %d",&l,&r,&k);
	for (int i=l;i<=r-k+1;i++){
		int j=i+k-1,tmp=i;
		for (int m=i+1;m<=j;m++)  tmp=lca(tmp,m);
		ans=max(ans,dep[tmp]);
	}
	printf("%d\n",ans);
	return;
}
void work2(){
	int l,r,k,ans=-2100000000;
	scanf("%d %d %d",&l,&r,&k);
	for (int i=l;i<=r-k+1;i++){
		int j=i+k-1,tmp=i;
		for (int m=i+1;m<=j;m++)
			if (dep[tmp]>dep[m])  tmp=m;
		ans=max(ans,dep[tmp]);
	}
	printf("%d\n",ans);
	return;
}
int main(){
	freopen("query.in","r",stdin);
	freopen("query.out","w",stdout);
	scanf("%d",&n);
	for (int i=1;i<n;i++){
		int x,y;
		scanf("%d %d",&x,&y);
		f[x].push_back(y);f[y].push_back(x);
		if (f[x].size()>2||f[y].size()>2)  spe=0;
	}
	dep[1]=1;
	if (!spe)  pre1(1);
	else  pre2(1);
	int t;
	scanf("%d",&t);
	while (t--){
		if (!spe)  work1();
		else  work2();
	}
	return 0;
}/*
6
5 6
6 1
6 2
2 3
2 4
3
2 5 2
1 4 1
1 6 3

*/
