#include<algorithm>
#include<stdio.h>
#define ll long long
#define N 500005
using namespace std;
ll read() {
	char c=getchar(); ll x=0, y=1;
	while(!(c >= '0' && c <= '9' || c == '-')) c=getchar();
	if(c == '-') y=-1, c=getchar();
	while(c >= '0' && c <= '9') x=x*10+c-'0', c=getchar();
	return x*y;
}
int n;
int head[N], to[N<<1], nex[N<<1], m1;
void add(int u, int v) {to[++m1]=v, nex[m1]=head[u], head[u]=m1;}
int q;
struct ques {
	int l, r, k;
} Q[N];
int dep[N], f[N][25];
void dfs(int now, int last) {
	dep[now]=dep[last]+1, f[now][0]=last;
	for(int i=1; i<=20; i++) f[now][i]=f[f[now][i-1]][i-1];
	for(int i=head[now]; i; i=nex[i]) {
		int st=to[i]; if(st == last) continue;
		dfs(st, now);
	}
}
int lca(int u, int v) {
	if(dep[u] < dep[v]) swap(u, v);
	for(int i=20; i>=0; i--) if(dep[f[u][i]] >= dep[v]) u=f[u][i];
	if(u == v) return u;
	for(int i=20; i>=0; i--) if(f[u][i] != f[v][i]) u=f[u][i], v=f[v][i];
	return f[u][0];
}

int a[5005][5005];
void case0() {
	int i, j;
	for(i=1; i<=n; i++) {
		int lc=i;
		for(j=i; j>0; j--) {
			lc=lca(lc, j);
			a[j][i]=lc;
//			printf("%d %d %d\n", j, i, a[j][i]);
		}
	}	
	q=read();
	for(i=1; i<=q; i++) {
		Q[i].l=read(), Q[i].r=read(), Q[i].k=read();
		int ans=0;
		for(j=Q[i].l; j+Q[i].k-1 <= Q[i].r; j++) {
			ans=max(ans, dep[a[j][j+Q[i].k-1]]);
		}
		printf("%d\n", ans);
	}
}
int main() {
	freopen("query.in", "r", stdin);
	freopen("query.out", "w", stdout);
	n=read();
	int i, j, k;
	for(i=1; i<n; i++) {
		int u=read(), v=read();
		add(u, v), add(v, u);
	}
	dfs(1, 0);
	if(n <= 5000) {case0(); return 0;}
	q=read();
	for(i=1; i<=q; i++) {
		Q[i].l=read(), Q[i].r=read(), Q[i].k=read();
	}	
	return 0;
}

