#include<bits/stdc++.h>
using namespace std;
const long long inf = 1e18;
const int mininf = 1e9 + 13;
#define pb emplace_back
#define int long long
inline int read(){int x=0,f=1;char ch=getchar();while(ch<'0' or ch>'9'){if(ch=='-'){f=-1;}ch=getchar();}while(ch>='0' and ch<='9'){x=(x<<3)+(x<<1)+(ch^48);ch=getchar();} return x * f;}
inline void write(int x){if(x<0){putchar('-');x=-x;}if(x>=10){write(x/10);}putchar(x%10+'0');}
#define put() putchar(' ')
#define endl puts("")
const int MAX = 5e5 + 10;

vector <int> g[MAX];
int n, q;

int siz[MAX], dep[MAX], top[MAX], son[MAX];
int Fa[MAX];

void dfs(int u, int fa){
	Fa[u] = fa;
	dep[u] = dep[fa]+ 1;
	siz[u] = 1;
	for(int v : g[u]){
		if(v == fa)	continue;
		dfs(v, u);
		siz[u] += siz[v];
		if(siz[son[u]] < siz[v])	son[u] = v;
	}
}

void dfs2(int u, int topu){
	top[u] = topu;
	if(son[u])	dfs2(son[u], topu);
	for(int v : g[u]){
		if(v == Fa[u] or v == son[u])	continue;
		dfs2(v, v);
	}
}

int lca(int u, int v){
	if(u == 1 or v == 1)	return 1;
	while(top[u] != top[v]){
		if(dep[top[u]] < dep[top[v]])	swap(u, v);
		u = Fa[top[u]];
	}
	if(dep[u] < dep[v])	return u;
	return v;
}

int f[30][MAX];
int lg2[MAX];

int qry(int l, int r){
	int len = r - l + 1;
	len = lg2[len];
	return lca(f[len][l], f[len][r - (1ll << len) + 1]);
}

namespace subtask1{
	void solve(){
		for(int i = 1; i <= n; i++)	f[0][i] = i;
		for(int i = 2; i <= n; i++)	lg2[i] = lg2[i >> 1] + 1;
		for(int i = 1; i < 20; i++){
			for(int j = 1; j + (1ll << i) - 1 <= n; j++){
				f[i][j] = lca(f[i - 1][j], f[i - 1][j + (1ll << (i - 1))]);
			}
		}
		for(int i = 1; i <= q; i++){
			int ans = 0;
			int l = read(), r = read(), k = read();
			for(int j = l; j + k -1 <= r; j++){
				ans = max(ans, dep[qry(j, j + k - 1)]);
			}
			write(ans), endl;
		}
	}
}

namespace subtask2{
	
	struct node{
		int l, r, k;
	}; node a[MAX];
	
	vector <int> g[MAX];
	
	void solve(){
		for(int i = 1; i <= n; i++)	f[0][i] = i;
		for(int i = 2; i <= n; i++)	lg2[i] = lg2[i >> 1] + 1;
		for(int i = 1; i < 20; i++){
			for(int j = 1; j + (1ll << i) - 1 <= n; j++){
				f[i][j] = lca(f[i - 1][j], f[i - 1][j + (1ll << (i - 1))]);
			}
		}
		for(int i = 1; i <= q; i++){
			int ans = 0;
			int l = read(), r = read(), k = read();
			for(int j = l; j + k - 1 <= r; j++){
				ans = max(ans, dep[qry(j, j + k - 1)]);
			}
			write(ans), endl;
		}
	}
}

void solve(){
	n = read();
	for(int i = 1; i < n; i++){
		int u = read(), v = read();
		g[u].pb(v), g[v].pb(u);
	}
	dfs(1, 1);
	dfs2(1, 1);
	q = read();
	if(n <= 5000 and q <= 5000)	subtask1::solve();
	else subtask2::solve();
}

signed main(){
	freopen("query.in", "r", stdin);
	freopen("query.out", "w", stdout);
	int t = 1;
	while(t--)	solve();
	return 0;
}


// 【】