#include<bits/stdc++.h>
using namespace std;

const int N = 5e5 + 5,S = N * 4;

int n,q,flg,L[N],R[N],K[N],ans[N];
int sl[N],sr[N],stk[N];
int dfn[N],pnt[N],tot,dep[N],stf[20][N],st[2][20][N];
vector<int> G[N],Q[N];
vector<pair<int,int>> P0[N],P1[N];

#define lc (i * 2)
#define rc (lc + 1)
#define mid ((L + R) / 2)
#define ls lc,L,mid
#define rs rc,mid + 1,R
#define id int i = 1,int L = 1,int R = n

struct sgt{
	int T[S],t[S];
	
	void ass(int x,int i){ t[i] = T[i] = x; }
	
	void psd(int i){ if(t[i]) ass(t[i],lc),ass(t[i],rc),t[i] = 0; }
	
	void upd(int l,int r,int x,id){
		if(l <= L && R <= r) return ass(x,i);
		psd(i);
		if(l <= mid) upd(l,r,x,ls);
		if(r > mid) upd(l,r,x,rs);
		T[i] = max(T[lc],T[rc]);
	}
	
	int qry(int l,int r,id,int s = 0){
		if(l <= L && R <= r) return T[i];
		psd(i);
		if(l <= mid) s = qry(l,r,ls);
		if(r > mid) s = max(s,qry(l,r,rs));
		return s;
	}
} T[2];

void dfs(int u,int ft){
	pnt[dfn[u] = ++tot] = u;
	st[0][0][u] = st[1][0][u] = u;
	stf[0][tot] = ft;
	flg = max(flg,dep[u] = dep[ft] + 1);
	for(int v : G[u]) if(v != ft) dfs(v,u);
}

int cmp2(int x,int y){ return dfn[x] < dfn[y]; }

int lca(int u,int v){
	if(u == v) return u;
	if((u = dfn[u]) > (v = dfn[v])) swap(u,v);
	int i = __lg(v - ++u + 1);
	return min(stf[i][u],stf[i][v - (1 << i) + 1],cmp2);
}

int Qry(int t,int l,int r){
	int i = __lg(r - l + 1);
	if(!t) return min(st[0][i][l],st[0][i][r - (1 << i) + 1],cmp2);
	return max(st[1][i][l],st[1][i][r - (1 << i) + 1],cmp2);
}

int main(){
	freopen("query.in","r",stdin);
	freopen("query.out","w",stdout);
	scanf("%d",&n);
	for(int i = 1,u,v;i < n;++i)
		scanf("%d%d",&u,&v),G[u].push_back(v),G[v].push_back(u);
	dfs(1,0);
	for(int j = 1;(1 << j) <= n;++j){
		for(int i = 1;i + (1 << j) - 1 <= n;++i){
			stf[j][i] = min(stf[j - 1][i],stf[j - 1][i + (1 << (j - 1))],cmp2);
			st[0][j][i] = min(st[0][j - 1][i],st[0][j - 1][i + (1 << (j - 1))],cmp2);
			st[1][j][i] = max(st[1][j - 1][i],st[1][j - 1][i + (1 << (j - 1))],cmp2);
		}
	}
	scanf("%d",&q);
	if(flg == n){
		for(int i = 1,k;i <= q;++i) scanf("%d%d%d",L + i,R + i,&k),Q[k].push_back(i);
		dfn[0] = dfn[n + 1] = N;
		for(int i = 1,tp = 0;i <= n;++i){
			while(tp && dep[stk[tp]] > dep[i]) --tp;
			sl[i] = stk[tp],stk[++tp] = i;
		}
		stk[0] = n + 1;
		for(int i = n,tp = 0;i >= 1;--i){
			while(tp && dep[stk[tp]] >= dep[i]) --tp;
			sr[i] = stk[tp],stk[++tp] = i;
		}
		for(int i = 1;i <= n;++i){
			if(i - sl[i] <= sr[i] - i) for(int j = sl[i] + 1;j <= i;++j) P0[sr[i] - j].emplace_back(j,dep[i]);
			else for(int j = sr[i] - 1;j >= i;--j) P1[j - sl[i]].emplace_back(j,dep[i]);
		}
		for(int i = n;i >= 1;--i){
			for(auto p : P0[i]) T[0].upd(p.first,p.first,p.second);
			for(auto p : P1[i]) T[1].upd(p.first,p.first,p.second);
			for(int j : Q[i]) ans[j] = max(T[0].qry(L[j],R[j] - i + 1),T[1].qry(L[j] + i - 1,R[j]));
		}
		for(int i = 1;i <= q;++i) printf("%d\n",ans[i]);
		return 0;
	}
	for(int l,r,k,s;q--;){
		scanf("%d%d%d",&l,&r,&k),s = 0;
		for(int i = l;i + k - 1 <= r;++i)
			s = max(s,dep[lca(Qry(0,i,i + k - 1),Qry(1,i,i + k - 1))]);
		printf("%d\n",s);
	}
	return 0;
}
