#include<bits/stdc++.h>
#define int long long 
#define rep(i,a,b)	for(int i = (a);i<=(b);i++)
#define lep(i,a,b)	for(int i = (a);i>=(b);i--)
using namespace std;
const int N = 5e3+10;
int n,m,u,v,ans,l,r,k;
int f[N][30],dep[N],a[N][N];
vector<int> g[N];
inline void dfs(int u,int fa){
	dep[u] = dep[fa]+1;
	f[u][0] = fa;
	for(int i = 1;(1ll<<i)<=dep[u];i++)	f[u][i] = f[f[u][i-1]][i-1];
	for(int i = 0;i<(int)g[u].size();i++){
		int v = g[u][i];
		if(v == fa)	continue;
		dfs(v,u);
	}
}
inline int LCA(int x,int y){
	if(dep[x]<dep[y]){
		swap(x,y);
	}
	lep(i,20,0){
		if(dep[f[x][i]]>=dep[y])	x = f[x][i];
	}
	if(x == y)	return x;
	lep(i,20,0){
		if(f[x][i] != f[y][i]){
			x = f[x][i];
			y = f[y][i];
		}
	}
	return f[x][0];
}
signed main(){
	freopen("query.in","r",stdin);
	freopen("query.out","w",stdout);
	cin>>n;
	rep(i,1,n-1){
		cin>>u>>v;
		g[u].push_back(v);
		g[v].push_back(u);
	}
	dfs(1,0);
	rep(i,1,n){
		int lca = i;
		rep(j,i,n){
			lca = LCA(lca,j);
			a[i][j] = dep[lca];
		}
	}
	cin>>m;
	rep(i,1,m){
		cin>>l>>r>>k;
		ans = 0;
		rep(j,k,(r-l+1)){
			rep(p,l,r){
				int L = p+j-1;
				if(L>r)	break;
				ans = max(ans,a[p][L]);
			}
		}
		cout<<ans<<"\n";
	}
	return 0;
}
