#include<bits/stdc++.h>
using namespace std;
//#define int long long
inline int read() {
	int x = 0, f = 1;
	char ch = getchar();
	while(ch < '0' || ch > '9') {
		if(ch == '-') f = -1;
		ch = getchar();
	}
	while(ch >= '0' && ch <= '9') {
		x = (x << 1) + (x << 3) + (ch ^ 48);
		ch = getchar();
	}
	return x * f;
}
#define pb push_back
#define fi first
#define se second
#define N 500010
//#define M
//#define mo
int n, m, i, j, k, T;
int dep[N], f[N][22], ans, q; 
int u, v, l, r, a[N]; 
vector<int>G[N]; 

void dfs(int x, int fa) {
	dep[x] = dep[fa] + 1; 
	for(int y : G[x]) if(y != fa) dfs(y, x), f[y][0] = x; 
}

struct Segment_tree {
#define ls (k << 1)
#define rs (k << 1 | 1)
#define mid ((l + r ) >> 1)
	int mn[N << 2]; 
	void add(int k, int l, int r, int x, int y) {
		if(l == r) return mn[k] = y, void(); 
		if(x <= mid) add(ls, l, mid, x, y); 
		else add(rs, mid + 1, r, x, y); 
		mn[k] = min(mn[ls], mn[rs]); 
	}
	int qry(int k, int l, int r, int x, int y) {
		if(l >= x && r <= y) return mn[k]; 
		int ans = n; 
		if(x <= mid) ans = min(ans, qry(ls, l, mid, x, y)); 
		if(y >= mid + 1) ans = min(ans, qry(rs, mid + 1, r, x, y)); 
		return ans; 
	}
}Seg; 

int lca(int x, int y) {
	if(x == y) return x; 
	if(dep[x] < dep[y]) swap(x, y); 
	for(int k = 20; k >= 0; --k)
	 	if(dep[f[x][k]] >= dep[y]) x = f[x][k]; 
	if(x == y) return x; 
	for(int k = 20; k >= 0; --k)
		if(f[x][k] != f[y][k]) x = f[x][k], y = f[y][k]; 
	return f[x][0]; 
}

signed main() {
//	srand(time(0));
	freopen("query.in", "r", stdin);
	freopen("query.out", "w", stdout);
	n = read(); 
	for(i = 1; i < n; ++i) {
		u = read(); v = read(); 
		G[u].pb(v); G[v].pb(u); 
	}
	dfs(1, 0); 
	for(k = 1; k <= 20; ++k)
		for(i = 1; i <= n; ++i) 
			f[i][k] = f[f[i][k - 1]][k - 1]; 
	for(i = 1; i < n; ++i) a[i] = dep[lca(i, i + 1)], Seg.add(1, 1, n, i, a[i]); 
//	for(i = 1; i < n; ++i) printf("%d ", a[i]); printf("\n"); 
	q = read(); 
	while(q--) {
		l = read(); r = read(); k = read(); ans = 0; 
		if(k == 1) {
			for(i = l; i <= r; ++i) ans = max(ans, dep[i]); 
			printf("%d\n", ans); continue; 
		}
		--r; --k; 
		for(i = l; i + k - 1 <= r; ++i) {
			ans = max(ans, Seg.qry(1, 1, n, i, i + k - 1)); 
//			printf("[%d %d] %d\n", i, i + k - 1, Seg.qry(1, 1, n, i, i + k - 1)); 
		}
		printf("%d\n", ans); 
	}
	return 0;
}

