#include <bits/stdc++.h>
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define F(i, x, y) for (int i = (x); i <= (y); i++)
#define R(i, x, y) for (int i = (x); i >= (y); i--)
#define FIO(FILE) freopen(FILE".in", "r", stdin), freopen(FILE".out", "w", stdout);

using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
bool Mbe;

const int N = 5e5 + 5;
const int M = 20;

int n, q, dfc;
int rid[N], id[N], f[N][M], g[N][M], d[N];

struct Q {
	
	int l, r, k;
	
	Q () { }
	Q (int _l, int _r, int _k) : l(_l), r(_r), k(_k) { }

} qr[N];

vector<int> G[N];

void dfs(int u, int fa) {
	rid[id[u] = ++dfc] = u;
	g[dfc][0] = fa, d[u] = d[fa] + 1;
	for (int v : G[u]) {
		if (v == fa) {
			continue;
		}
		dfs(v, u);
	}
}

int gt(int u, int v) {
	return id[u] < id[v] ? u : v;
}

int lca(int u, int v) {
	if (u == v) {
		return u;
	}
	u = id[u], v = id[v];
	if (u > v) {
		swap(u, v);
	}
	u++;
	int len = __lg(v - u + 1);
	return gt(g[u][len], g[v - (1 << len) + 1][len]);
}

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

void Sub1() {
	F (i, 1, q) {
		int l, r, k, res = 0;
		l = qr[i].l, r = qr[i].r, k = qr[i].k;
		F (i, l, r - k + 1) {
			int j = i + k - 1;
			res = max(res, d[qry(i, j)]);
		}
		cout << res << '\n';
	}
}

mt19937 rnd(time(0));

void Sub2() {
	F (i, 1, q) {
		int l, r, k, res = 0;
		l = qr[i].l, r = qr[i].r, k = qr[i].k;
		int T = 20;
		while (T--) {
			int i = rnd() % (r - l - k + 2) + l;
			int j = i + k - 1;
			res = max(res, d[qry(i, j)]);
		}
		cout << res << '\n';
	}
}

void solve() {
	cin >> n;
	F (i, 1, n - 1) {
		int u, v;
		cin >> u >> v;
		G[u].eb(v), G[v].eb(u);
	}
	dfs(1, 0);
	F (j, 1, __lg(n)) {
		F (i, 1, n - (1 << j) + 1) {
			g[i][j] = gt(g[i][j - 1], g[i + (1 << (j - 1))][j - 1]);
		}
	}
	F (i, 1, n) {
		f[i][0] = i;
	}
	F (j, 1, __lg(n)) {
		F (i, 1, n - (1 << j) + 1) {
			f[i][j] = lca(f[i][j - 1], f[i + (1 << (j - 1))][j - 1]);
		}
	}
	cin >> q;
	int fl = 1;
	F (i, 1, q) {
		int l, r, k;
		cin >> l >> r >> k;
		qr[i] = Q(l, r, k);
		fl &= ((r - l + 1) == k);
	}
	if (n <= 5000 || fl) {
		Sub1();
	} else {
		Sub2();
	}
}

bool Med;
int main() {
	FIO("query");
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	cerr << (&Mbe - &Med) / 1048576. << " MB\n";
	int T = 1;
	// cin >> T;
	while (T--) solve();
	cerr << 1. * clock() / CLOCKS_PER_SEC << " ms\n";
	return 0;
}
