#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define lowbit(x) (x&-x)
#define INF 0x3f3f3f3f
#define N 5010
namespace graph{
	int head[N],nxt[N<<1],to[N<<1];
	int deg[N];
	int cnt_edge,maxdeg;
	inline void connect_head(const int &x,const int &y){
		++cnt_edge;
		nxt[cnt_edge] = head[x];
		to[cnt_edge] = y;
		head[x] = cnt_edge;
		++deg[x],maxdeg = max(maxdeg,deg[x]);
	}
}
using namespace graph;
int n,q,a,b;
int ap[N],tp;
int st[20][N<<1];
int vt[20][N];
int dep[N];
int lg[N<<1];
bool flag;
inline int sel(const int &x,const int &y){
	if(dep[x] < dep[y])return x;
	return y;
}
inline int lca(const int &x,const int &y){
	int px = ap[x],py = ap[y];
	if(py < px)swap(px,py);
	int g = lg[py - px + 1];
	return sel(st[g][px],st[g][py - (1 << g) + 1]);
}
void dfs(const int &idx,const int &dad){
	dep[idx] = dep[dad] + 1;
	ap[idx] = ++tp;
	st[0][tp] = idx;
	for(int edg = head[idx];edg;edg = nxt[edg]){
		if(to[edg] == dad)continue;
		st[0][++tp] = idx;
		dfs(to[edg],idx);
	}
}
struct ask{
	int l,r,k;
}p[N];
inline void input(){
	scanf("%d",&n);
	for(int i = 1;i < n;++i){
		scanf("%d%d",&a,&b);
		connect_head(a,b);
		connect_head(b,a);
	}
	scanf("%d",&q);
	flag = 1;
	
	dfs(1,0);
	// 建立表 
	for(int gap = 1;(1 << gap) <= tp;++gap){
		for(int i = 1;i + (1<<gap) - 1 <= tp;++i){
			st[gap][i] = sel(st[gap-1][i],st[gap-1][i + (1 << gap-1)]);
		}
	}
	for(int i = 2;i <= tp;++i)lg[i] = lg[i>>1] + 1; 
	for(int i = 1;i <= n;++i)vt[0][i] = i;
	for(int gap = 1;(1 << gap) <= n;++gap){
		for(int i = 1;i + (1<<gap) - 1 <= n;++i){
			vt[gap][i] = lca(vt[gap-1][i],vt[gap-1][i + (1 << gap-1)]);
		}
	}
}
// 判断部分分类型 
// 第一档部分分 
inline void solve(){
	int l,r,k,x,tmp;
	int ans;
	while(q--){
		scanf("%d%d%d",&l,&r,&k);
		ans = -INF;
		x = lg[k];
		// 便利然后求 lca
		for(int i = l;i + k - 1 <= r;++i){
			tmp = lca(vt[x][i],vt[x][i + k - (1 << x)]);
//			printf("[%d,%d,%d]tmp = %d\n",l,r,k,tmp);
			ans = max(ans,dep[tmp]);
		} 
		printf("%d\n",ans);
	}
}
inline void output(){
	
}
int main(){
	freopen("query.in","r",stdin);
	freopen("query.out","w",stdout);
	input();
	solve();
	output();
	return 0;
}
/*
6
5 6
6 1
6 2
2 3
2 4
3
2 5 2
1 4 1
1 6 3

*/
