#include<bits/stdc++.h>
using namespace std;
const int N = 5e5 + 5;
int n, q, od[N], fA = 1, d[N], f[N], sz[N], son[N], md[N * 4], id[N], cnt = 0, top[N], mind[N * 4];
vector<int> e[N];
inline void dfs1(int x, int fa, int deep) {
	f[x] = fa; d[x] = deep;
	sz[x] = 1;
	int maxson = 0;
	for (int y : e[x]) {
		if (y != fa) {
			dfs1(y, x, deep + 1);
			sz[x] += sz[y];
			if (maxson < sz[y]) {
				maxson = sz[y];
				son[x] = y;
			}
		}
	}
}
inline void dfs2(int x, int to) {
	top[x] = to;
	id[x] = ++cnt;
	if (!son[x]) return;
	dfs2(son[x], to);
	for (int y : e[x]) {
		if (y != f[x] && y != son[x]) {
			dfs2(y, y);
		}
	}
}
int lca(int x, int y) {
	while(top[x] != top[y]) {
		if (d[top[x]] < d[top[y]]) {
			swap(x, y);
		}
		x = f[top[x]]; 
	}
	return (y > x ? y : x);
}
void build(int k, int l, int r) {
	if (l == r) {
		md[k] = d[l];
		mind[k] = d[l];
		return;
	}
	int mid = (l + r) >> 1;
	build(k * 2, l, mid);
	build(k * 2 + 1, mid + 1, r);
	md[k] = max(md[k * 2], md[k * 2 + 1]);
	mind[k] = min(mind[k * 2], mind[k * 2 + 1]);
}
int query(int k, int l, int r, int x, int y, int opt) {
	if (l == x && r == y) return (opt ? md[k] : mind[k]);
	int mid = (l + r) >> 1;
	if (x <= mid && y <= mid) return query(k * 2, l, mid, x, y, opt);
	else if (x > mid && y > mid) return query(k * 2 + 1, mid + 1, r, x, y, opt);
	else return (opt ? (max(query(k * 2, l, mid, x, mid, opt), query(k * 2 + 1, mid + 1, r, mid + 1, y, opt))) : (min(query(k * 2, l, mid, x, mid, opt), query(k * 2 + 1, mid + 1, r, mid + 1, y, opt))));
}
int main() {
	
	freopen("query.in", "r", stdin);
	freopen("query.out", "w", stdout);
	scanf("%d", &n);
	for (int i = 1; i < n; i++) {
		int u, v;
		scanf("%d%d", &u, &v);
		e[u].push_back(v);
		if (!fA) e[v].push_back(u);
		od[u]++;
		if (od[u] > 1) fA = 0;
	} 
	if (!fA) dfs1(1, 0, 1);
	else {
		d[1] = 1;
		for (int i = 1, fa = 0; i; i = e[i][0]) {
			d[i] = d[fa] + 1;
			fa = i;
			if (e[i].size() == 0) break;
		}
	}
	build(1, 1, n);
	scanf("%d", &q);
	while(q--) {
		int l, r, k;
		int ans = 0;
		scanf("%d%d%d", &l, &r, &k);
		if (k == 1) {
			printf("%d\n", query(1, 1, n, l, r, 1));
		} else if (fA) {
			printf("%d\n", query(1, 1, n, l, r, 1));
		} else if (k == r - l + 1) {
			printf("%d\n", query(1, 1, n, l, r, 0));
		} else {
			printf("1\n");
		}
	}
		
	
	
	return 0;
}
/*
	//freopen("assign.in", "r", stdin);
	//freopen("assign.out", "w", stdout);
*/
