#include <bits/stdc++.h>
using namespace std;
bool fl1;
struct seg1{//n <= 5000
	int maxn[20005] = {0};
	void push_up(int rt){
		maxn[rt] = max(maxn[rt << 1], maxn[rt << 1 | 1]);
	}
	void update(int rt, int id, int l, int r, int k){
		if (id == l && r == id){
			maxn[rt] = k;
			return;
		}
		int mid = (l + r) >> 1;
		if (id <= mid) update(rt << 1, id, l, mid, k);
		else update(rt << 1 | 1, id, mid + 1, r, k);
		push_up(rt);
	}
	int query(int rt, int L, int R, int l, int r){
		if (L <= l && r <= R){
			return maxn[rt];
		}
		int ans = 0;
		int mid = (l + r) >> 1;
		if (L <= mid) ans = max(ans, query(rt << 1, L, R, l, mid));
		if (R > mid) ans = max(ans, query(rt << 1 | 1, L, R, mid + 1, r));
		return ans;
	}
}t1[5005];
struct seg2{//链 
	int maxn[2000005] = {0};
	void push_up(int rt){
		maxn[rt] = min(maxn[rt << 1], maxn[rt << 1 | 1]);
	}
	void update(int rt, int id, int l, int r, int k){
		if (id == l && r == id){
			maxn[rt] = k;
			return;
		}
		int mid = (l + r) >> 1;
		if (id <= mid) update(rt << 1, id, l, mid, k);
		else update(rt << 1 | 1, id, mid + 1, r, k);
		push_up(rt);
	}
	int query(int rt, int L, int R, int l, int r){
		if (L <= l && r <= R){
			return maxn[rt];
		}
		int ans = 0x3f3f3f3f;
		int mid = (l + r) >> 1;
		if (L <= mid) ans = min(ans, query(rt << 1, L, R, l, mid));
		if (R > mid) ans = min(ans, query(rt << 1 | 1, L, R, mid + 1, r));
		return ans;
	}
}t2;
int n, q;
int tmp1, tmp2, tmp3;
int in[500005];
int dp[500005][25], dep[500005];
int lg[500005];
int ans1[5005][5005];
vector<int> e[500005];
bool fl2;
void dfs(int k, int fath){
	dep[k] = dep[fath] + 1;
	dp[k][0] = fath;
	for (int i = 1; i <= 20; i++){
		dp[k][i] = dp[dp[k][i - 1]][i - 1];
	}
	for (auto x : e[k]){
		if (x != fath){
			dfs(x, k);
		}
	}
}
int lca(int x, int y){
	if (x == 0 || y == 0){
		return 0;
	}
	if (dep[x] < dep[y]){
		swap(x, y);
	}
	while (dep[x] > dep[y]){
		x = dp[x][lg[dep[x] - dep[y]]];
	}
	if (x == y){
		return x;
	}
	for (int i = 20; i >= 0; i--){
		if (dp[x][i] != dp[y][i]){
			x = dp[x][i];
			y = dp[y][i];
		}
	}
	return dp[x][0];
}
struct seg3{//全区间 
	int maxn[2000005];
	void push_up(int rt){
		maxn[rt] = lca(maxn[rt << 1], maxn[rt << 1 | 1]);
	}
	void build(int rt, int L, int R){
		if (L == R){
			maxn[rt] = L;
			return;
		}
		int mid = (L + R) >> 1;
		build(rt << 1, L, mid);
		build(rt << 1 | 1, mid + 1, R);
		push_up(rt);
	}
	int query(int rt, int L, int R, int l, int r){
		if (L <= l && r <= R){
			return maxn[rt];
		}
		int ans1 = 0x3f3f3f3f, ans2 = 0x3f3f3f3f;
		int mid = (l + r) >> 1;
		if (L <= mid) ans1 = query(rt << 1, L, R, l, mid);
		if (R > mid) ans2 = query(rt << 1 | 1, L, R, mid + 1, r);
		if (ans1 == 0x3f3f3f3f){
			return ans2;
		}
		if (ans2 == 0x3f3f3f3f){
			return ans1;
		}
		return lca(ans1, ans2);
	}
}t3;
int main()
{
	freopen("query.in", "r", stdin);
	freopen("query.out", "w", stdout);
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
//	cerr << (&fl2-&fl1)/1024/1024 << endl;
	lg[0] = -1;
	for (int i = 1; i <= 500000; i++){
		lg[i] = lg[i / 2] + 1;
	}
	cin >> n;
	for (int i = 1; i < n; i++){
		cin >> tmp1 >> tmp2;
		e[tmp1].push_back(tmp2);
		e[tmp2].push_back(tmp1);
		in[tmp1]++;
		in[tmp2]++; 
	}
	bool fl = true;
	int cnt1 = 0;
	for (int i = 1; i <= n; i++){
		if (in[i] == 1){
			cnt1++;
		}
	}
	if (cnt1 != 2){
		fl = false;
	}
	dfs(1, 1);
	cin >> q;
	if (n <= 5000 && q <= 5000){
//		cerr << 123 << endl;
		for (int i = 1; i <= n; i++){
			ans1[i][i] = i;
		}
		for (int i = 1; i <= n; i++){
			for (int j = i + 1; j <= n; j++){
				ans1[i][j] = lca(ans1[i][j - 1], j);
			}
		}
		for (int len = 1; len <= n; len++){//区间长度 
			for (int i = 1; i <= n - len + 1; i++){
				int j = i + len - 1;
				t1[len].update(1, i, 1, n, dep[ans1[i][j]]);
			}
		}
		for (int i = 1; i <= q; i++){
			cin >> tmp1 >> tmp2 >> tmp3;
			int len = tmp2 - tmp1 + 1;
			int ans = 0;
			for (int j = tmp3; j <= len; j++){
				ans = max(ans, t1[j].query(1, tmp1, tmp1 + len - j, 1, n));
			}
			cout << ans << '\n';
		}
	}
	else if (fl){
//		cerr << 1234 << endl;
		for (int i = 1; i <= n; i++){
			t2.update(1, i, 1, n, dep[i]);
		}
		for (int i = 1; i <= q; i++){
			cin >> tmp1 >> tmp2 >> tmp3;
			cout << t2.query(1, tmp1, tmp2, 1, n) << '\n';
		}
	}
	else{
//		cerr << 12345 << endl;
		t3.build(1, 1, n); 
		for (int i = 1; i <= q; i++){
			cin >> tmp1 >> tmp2 >> tmp3;
			cout << dep[t3.query(1, tmp1, tmp2, 1, n)] << '\n';
		}
	}
	return 0;
}
/*
珂朵莉保佑我不要挂分~~ 
luogu_uid:373844
*/ 
