//T4(32)
#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define ull unsigned long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define lowbit(x) ((x)&(-(x)))
using namespace std;
const int N = 500010, L = 18;

int n, Q, lg[N];
vector<int> e[N];

int idx, dfn[N], dep[N], p[N];
int pos[N][20], lc[N][20];
void dfs(int u, int pr){
	p[u] = pr, dfn[u] = ++idx, dep[u] = dep[pr] + 1;
	pos[idx][0] = u;
	for (int v : e[u])
		if (v != pr)
			dfs(v, u);
}
int getlc(int x, int y){
	if (x == y)
		return x;
	if (dfn[x] > dfn[y])
		swap(x, y);
	int l = dfn[x] + 1, r = dfn[y], k = lg[r - l + 1];
	int a = pos[l][k], b = pos[r - (1 << k) + 1][k];
	return dep[a] < dep[b] ? p[a] : p[b];
} 
void init(){
	for (int j = 1; j <= L; j++)
		for (int i = 1; i + (1 << j) - 1 <= n; i++){
			int a = pos[i][j - 1], b = pos[i + (1 << (j - 1))][j - 1];
			pos[i][j] = (dep[a] < dep[b] ? a : b);
		}
	for (int i = 1; i <= n; i++)
		lc[i][0] = i;
	for (int j = 1; j <= L; j++)
		for (int i = 1; i + (1 << j) - 1 <= n; i++){
			int a = lc[i][j - 1], b = lc[i + (1 << (j - 1))][j - 1];
			lc[i][j] = getlc(a, b);
		}
	
}

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);
		e[u].push_back(v);
		e[v].push_back(u);
	}
	
	for (int i = 2; i <= n; i++)
		lg[i] = lg[i / 2] + 1;
	dfs(1, 0), init();
	
	scanf("%d", &Q);
	for (int i = 1, l, r, k; i <= Q; i++){
		scanf("%d %d %d", &l, &r, &k);
		if ((n <= 5000 && Q <= 5000) || k == r - l + 1){
			int t = lg[k], ans = 0;
			for (int j = l; j + k - 1 <= r; j++){
				int a = lc[j][t], b = lc[j + k - 1 - (1 << t) + 1][t];
				ans = max(ans, dep[getlc(a, b)]);
			}
			printf("%d\n", ans);
		}
	}
	return 0;
}
