#include <bits/stdc++.h>
#define fst first
#define scd second
#define pb emplace_back
#define mkp make_pair
#define uint unsigned
#define mems(a, x) memset((a), (x), sizeof(a))

using namespace std;
typedef long long ll;
typedef double db;
typedef long double ldb;
typedef unsigned long long ull;
typedef pair<int, int> pii;

inline int read() {
	int x = 0;
	char c = getchar();
	bool f = 0;
	while (c < '0' || c > '9') {
		f |= (c == '-');
		c = getchar();
	}
	while ('0' <= c && c <= '9') {
		x = x * 10 + c - '0';
		c = getchar();
	}
	return f ? -x : x;
}

const int maxn = 500100;
const int logn = 21;

int n, m, hd[maxn], len, to[maxn << 1], nxt[maxn << 1], K, dep[maxn], ans[maxn], f[logn][maxn];
set<pii> S[maxn];

inline void add_edge(int u, int v) {
	to[++len] = v;
	nxt[len] = hd[u];
	hd[u] = len;
}

struct node {
	int x, y, z, i, v;
} a[maxn * 15], b[maxn * 15];

inline int qmax(int l, int r) {
	int k = __lg(r - l + 1);
	return max(f[k][l], f[k][r - (1 << k) + 1]);
}

void dfs(int u, int fa) {
	S[u].emplace(u, u);
	dep[u] = dep[fa] + 1;
	for (int i = hd[u]; i; i = nxt[i]) {
		int v = to[i];
		if (v == fa) {
			continue;
		}
		dfs(v, u);
		if (S[u].size() < S[v].size()) {
			swap(S[u], S[v]);
		}
		for (pii p : S[v]) {
			auto it = S[u].lower_bound(p);
			vector<pii> vc;
			int l = p.fst, r = p.scd;
			if (it != S[u].end() && it->fst == p.scd + 1) {
				vc.pb(*it);
				r = it->scd;
			}
			if (it != S[u].begin()) {
				--it;
				if (it->scd == p.fst - 1) {
					vc.pb(*it);
					l = it->fst;
				}
			}
			for (pii p : vc) {
				S[u].erase(p);
			}
			if (l != p.fst || r != p.scd) {
				a[++K].x = l;
				a[K].y = r;
				a[K].z = r - l + 1;
				a[K].i = 0;
				a[K].v = dep[u];
			}
			S[u].emplace(l, r);
		}
		S[v].clear();
	}
}

namespace BIT {
	int c[maxn];
	
	inline void update(int x, int d) {
		for (int i = x; i; i -= (i & (-i))) {
			c[i] = max(c[i], d);
		}
	}
	
	inline int query(int x) {
		int res = 0;
		for (int i = x; i <= n; i += (i & (-i))) {
			res = max(res, c[i]);
		}
		return res;
	}
	
	inline void clear(int x) {
		for (int i = x; i; i -= (i & (-i))) {
			c[i] = 0;
		}
	}
}

void cdq(int l, int r) {
	if (l == r) {
		return;
	}
	int mid = (l + r) >> 1;
	cdq(l, mid);
	cdq(mid + 1, r);
	int i = l, j = mid + 1, k = 0;
	while (i <= mid && j <= r) {
		if (a[i].y >= a[j].y) {
			if (a[i].v) {
				BIT::update(a[i].z, a[i].v);
			}
			b[++k] = a[i++];
		} else {
			if (a[j].i) {
				ans[a[j].i] = max(ans[a[j].i], BIT::query(a[j].z));
			}
			b[++k] = a[j++];
		}
	}
	while (i <= mid) {
		b[++k] = a[i++];
	}
	while (j <= r) {
		if (a[j].i) {
			ans[a[j].i] = max(ans[a[j].i], BIT::query(a[j].z));
		}
		b[++k] = a[j++];
	}
	for (int i = l; i <= mid; ++i) {
		BIT::clear(a[i].z);
	}
	for (int i = 1; i <= k; ++i) {
		a[l + i - 1] = b[i];
	}
}

void solve() {
	n = read();
	for (int i = 1, u, v; i < n; ++i) {
		u = read();
		v = read();
		add_edge(u, v);
		add_edge(v, u);
	}
	m = read();
	dfs(1, 0);
	for (int i = 1; i <= n; ++i) {
		f[0][i] = dep[i];
	}
	for (int j = 1; (1 << j) <= n; ++j) {
		for (int i = 1; i + (1 << j) - 1 <= n; ++i) {
			f[j][i] = max(f[j - 1][i], f[j - 1][i + (1 << (j - 1))]);
		}
	}
	for (int i = 1, l, r, k; i <= m; ++i) {
		l = read();
		r = read();
		k = read();
		if (k == 1) {
			ans[i] = qmax(l, r);
		}
		a[++K].x = r - k + 1;
		a[K].y = l + k - 1;
		a[K].z = k;
		a[K].i = i;
		a[K].v = 0;
	}
	sort(a + 1, a + K + 1, [&](const node &a, const node &b) {
		if (a.x != b.x) {
			return a.x < b.x;
		} else if (a.y != b.y) {
			return a.y > b.y;
		} else if (a.z != b.z) {
			return a.z > b.z;
		} else {
			return a.i < b.i;
		}
	});
	cdq(1, K);
	for (int i = 1; i <= m; ++i) {
		printf("%d\n", ans[i]);
	}
}

int main() {
	freopen("query.in", "r", stdin);
	freopen("query.out", "w", stdout);
	int T = 1;
	// scanf("%d", &T);
	while (T--) {
		solve();
	}
	return 0;
}