#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
const int N = 5e5 + 5;
int T, n;
struct edge{
	int v, nxt;
}E[N << 1];
int cnt, p[N];
void ins(int u, int v){
	E[++cnt].v = v, E[cnt].nxt = p[u], p[u] = cnt;
}
int dis[N], fa[N], lg[N], anc[N][20];
void dfs(int u, int f){
	fa[u] = f;
	for(int i = p[u]; i; i = E[i].nxt){
		int v = E[i].v;
		if(f == v) continue;
		anc[v][0] = u, dis[v] = dis[u] + 1;
		dfs(v, u);
	}
}
void init(){
	for(int i = 1; i <= 18; ++i){
		for(int j = 1; j <= n; ++j){
			anc[j][i] = anc[anc[j][i - 1]][i - 1];
		}
	}
}
void check(){
	for(int j = 0; j <= 18; ++j){
		cout << j << ":\n";
		for(int i = 1; i <= n; ++i){
			cout << i << ' ' << anc[i][j] << endl;
		}
	}
}
int lca(int u, int v){
	if(dis[u] < dis[v]) swap(u, v);
	while(dis[u] > dis[v]) u = fa[u];
	if(u == v) return u;
	while(fa[u] != fa[v]) u = fa[u], v = fa[v];
//	for(int i = 18; i >= 0; --i){
//		if(anc[u][i] != anc[v][i]) {
//			u = anc[u][i], v = anc[v][i];
//			cout << i << ' ' << u << ' ' << v << endl;
//		}
//	}
	return fa[u];
}
int main(){
	freopen("query.in", "r", stdin);
	freopen("query.out", "w", stdout);
	IOS;
	cin >> n;
	for(int i = 1; i < n; ++i){
		int u, v; cin >> u >> v;
		ins(u, v); ins(v, u);
	}
	dis[1] = 1, anc[1][0] = 1; dfs(1, 0);
//	init();
	int T; cin >> T;
	while(T--){
		int l, r, k, ans = 0; cin >> l >> r >> k;
		if(k == 1){
			for(int i = l; i <= r; ++i) ans = max(ans, dis[i]);
		}
		else {
			for(int i = l; i <= r; ++i){
				int lst = i, num = 0;
				for(int j = 1; j < k && i + j <= r; ++j){
					num = lca(lst, i + j);
//					cout << i << ' ' << i + j << ' ' << num << endl;
					lst = num;
				}
				ans = max(ans, dis[num]);
			}
		}
		cout << ans << endl;
	}
	return 0;
}
