#include<bits/stdc++.h>
using namespace std;

#define F(i, l, r) for(int i = (l); i <= (r); ++i)
#define dF(i, r, l) for(itn i = (r); i >= (l); --i)

inline int ri() {
	int x = 0, f = 1;
	char c = getchar();
	while(c < '0' || c > '9') {
		if(c == '-') f = -1;
		c = getchar();
	}
	while(c >= '0' && c <= '9') {
		x = x * 10 + c - 48;
		c = getchar();
	} return x * f;
}

const int _ = 5e5 + 5;

#define vi vector<int>
#define pb push_back

bool stA;
int n;

vi d[_];

int dfn[_], cnt;

int st2[_][20], Fa[_], dep[_];

void dfs(int u, int fa) {
	dfn[u] = ++cnt;
	dep[u] = dep[fa] + 1;
	Fa[u] = fa;
	for(int v : d[u]) if(v != fa) {
		dfs(v, u);
	}
}

int get(int u, int v) {
	return dfn[Fa[u]] < dfn[Fa[v]] ? u : v;
}

int st[_][20], lg[_];

int LCA(int u, int v) {
	if(u == v) return u;
	u = dfn[u], v = dfn[v];
	if(u > v) swap(u, v);
	u++;
	int t = lg[v - u + 1];
	return Fa[get(st2[u][t], st2[v - (1 << t) + 1][t])];
}
	

bool stB;

int qry(int l, int r) {
	int t = lg[r - l + 1];
	return LCA(st[l][t], st[r - (1 << t) + 1][t]);
}

namespace part1 {
	bool chk() {
		return n <= 5000;
	}
	void solve() {
		int q = ri();
		while(q--) {
			int l = ri(), r = ri(), k = ri();
			int res = 0;
			F(i, l + k - 1, r) {
				int u = qry(i - k + 1, i);
				res = max(res, dep[u]);
			}
			printf("%d\n", res);
		}
	}
}

namespace part3 {
	bool chk() {
		return 1;
	}
	void solve() {
		int q = ri();
		while(q--) {
			int l = ri(), r = ri(), k = ri();
			int res = 0;
			F(i, l + k - 1, r) {
				int u = qry(i - k + 1, i);
				int lca = i;
				F(j, i - k + 1, i) lca = LCA(lca, j);
				res = max(res, dep[lca]);
//				res = max(res, dep[u]);
			}
			printf("%d\n", res);
		}
	}
}

signed main() {
	freopen("query.in", "r", stdin);
	freopen("query.out", "w", stdout);
	fprintf(stderr, "%lf\n", (&stA - &stB) / 1024.0 / 1024.0);
	n = ri();
	F(i, 1, n - 1) {
		int u = ri(), v = ri();
		d[u].pb(v), d[v].pb(u);
	}
	dfs(1, 0);
	F(i, 2, n) lg[i] = lg[i >> 1] + 1;
	F(i, 1, n) st2[dfn[i]][0] = i, st[i][0] = i;
	F(j, 1, 19) {
		F(i, 1, n - (1 << j) + 1) st2[i][j] = get(st2[i][j - 1], st2[i + (1 << (j - 1))][j - 1]);
		F(i, 1, n - (1 << j) + 1) st[i][j] = LCA(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]);
	}
//	if(part1::chk()) return part1::solve(), 0;
	part3::solve();
}

