#include<bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define MIN(x,y) (st[x] < st[y] ? x : y)
using namespace std;
typedef long long ll;
const int N = 500005;
int n,q,d[N],ti,st[N],ed[N],f[22][N],tot,head[N],ver[N<<1],Next[N<<1];
void add(int x,int y){
	tot++;
	ver[tot] = y;
	Next[tot] = head[x];
	head[x] = tot;
}
void dfs(int fa,int x){
	st[x] = ++ti;
	f[0][st[x]] = fa;
	for(int i = head[x];i;i = Next[i]){
		int y = ver[i];
		if(y == fa)continue;
		d[y] = d[x] + 1;
		dfs(x,y);
	}
	ed[x] = ti;
}
int lca(int u,int v){
	if(u == v)return u;
	if((u = st[u]) > (v = st[v]))swap(u,v);
	int k = __lg(v - u++);
	return MIN(f[k][u],f[k][v - (1 << k) + 1]);
}
namespace sol1{
	const int M = 5005;
	int ans[M][M];
	void solve(){
		for(int i = 1;i <= n;i++){
			ans[i][i] = i;
			for(int j = i + 1;j <= n;j++)
				ans[i][j] = lca(ans[i][j - 1],j);
		}
		cin >> q;
		while(q--){
			int l,r,k,res = 0;
			cin >> l >> r >> k;
			for(int i = l + k - 1;i <= r;i++)
				res = max(res,d[ans[i - k + 1][i]]);
			cout << res << '\n';
		}
	}
}
namespace sol2{
	#define mid ((l + r) >> 1)
	struct node{
		int lx,rx,mx,ln;
		node operator + (const node &b)const{
			node c = {lx,b.rx,max(mx,b.mx),ln + b.ln};
			if(lx == ln)c.lx = lx + b.lx;
			if(b.rx == b.ln)c.rx = rx + b.rx;
			c.mx = max(c.mx,rx + b.lx);
			return c; 
		}
	}tr[22*N];
	int num,rt[N],ls[22*N],rs[22*N],rk[N];
	int upd(int id,int l,int r,int x){
		int t = ++num;
		if(l == r)tr[t] = {1,1,1,1};
		else{
			ls[t] = ls[id],rs[t] = rs[id];
			if(x <= mid)ls[t] = upd(ls[id],l,mid,x);
			else rs[t] = upd(rs[id],mid + 1,r,x);
			tr[t] = tr[ls[t]] + tr[rs[t]];
			tr[t].ln = r - l + 1;
		}
		return t;
	}
	node qry(int id,int l,int r,int x,int y){
		if(!id)return {0,0,0,min(y,r) - max(x,l) + 1};
		if(x <= l && r <= y)return tr[id];
		if(y <= mid)return qry(ls[id],l,mid,x,y);
		else if(x > mid)return qry(rs[id],mid + 1,r,x,y);
		else return qry(ls[id],l,mid,x,y) + qry(rs[id],mid + 1,r,x,y);
	}
	#undef mid
	void solve(){
		for(int i = 1;i <= n;i++)
			rk[d[i]] = i;
		for(int i = n;i >= 1;i--)
			rt[i] = upd(rt[i + 1],1,n,rk[i]);
		/*for(int i = n;i >= 1;i--)
			cout << rk[i] << ' ';
		cout << endl;
		cout << "F" << qry(rt[5],1,n,1,n).mx << endl; */
		cin >> q;
		while(q--){
			int l,r,k;
			cin >> l >> r >> k;
			int L = 1,R = n,ans = 0;
			while(L <= R){
				int mid = (L + R) / 2;
//				cout << L << ' ' << R << ' ' << mid << endl;
				if(qry(rt[mid],1,n,l,r).mx >= k)ans = mid,L = mid + 1;
				else R = mid - 1;
			}
			cout << ans << '\n';
		}
	}
}
int main(){
	freopen("query.in","r",stdin);
	freopen("query.out","w",stdout);
	cin.tie(0);
	ios::sync_with_stdio(0);
	cin >> n;
	for(int i = 1;i < n;i++){
		int x,y;
		cin >> x >> y;
		add(x,y);
		add(y,x);
	}
	d[1] = 1;
	dfs(0,1);
	for(int i = 1;i <= __lg(n);i++)
		for(int j = 1;j + (1 << i) - 1 <= n;j++)
			f[i][j] = MIN(f[i - 1][j],f[i - 1][j + (1 << i - 1)]);
	if(n <= 5000)sol1::solve();
	else sol2::solve();
	return 0;
}
/*
8
1 3
3 4
4 5
5 6
6 2
2 8
8 7
5
1 8 4
2 5 3
3 8 3
3 4 1
2 4 3
4
2
5
3
2
*/
