#include <bits/stdc++.h>
using namespace std;
using ll = long long; using ull = unsigned long long;
using pll = pair<ll, ll>; using ld = long double;
void fileIO(const string& x) { freopen((x + ".in").c_str(), "r", stdin), freopen((x + ".out").c_str(), "w", stdout); }
template<typename T1, typename T2> bool chkmin(T1& x, const T2& y) { bool flag = (y < x); x = (y < x) ? y : x; return flag; }
template<typename T1, typename T2> bool chkmax(T1& x, const T2& y) { bool flag = (y > x); x = (y > x) ? y : x; return flag; }
mt19937_64 rnd(random_device{}());

const ll N = 5e5 + 5;

ll n, q;
vector<ll> G[N];

namespace Task1 {
	const ll Task1_N = 5000 + 5;
	ll fa[Task1_N][15], dep[Task1_N], f[Task1_N][Task1_N];
	
	void dfs(ll u) {
		for(int i = 1; i <= 13; i++) fa[u][i] = fa[fa[u][i - 1]][i - 1];
		for(auto v : G[u]) {
			if(v == fa[u][0]) continue;
			fa[v][0] = u, dep[v] = dep[u] + 1;
			dfs(v);
		}
	}
	ll lca(ll x, ll y) {
		if(dep[x] < dep[y]) swap(x, y);
		for(int i = 13; i >= 0; i--) {
			if(dep[fa[x][i]] >= dep[y]) x = fa[x][i];
		}
		if(x == y) return x;
		for(int i = 13; i >= 0; i--) {
			if(fa[x][i] != fa[y][i]) x = fa[x][i], y = fa[y][i];
		}
		return fa[x][0];
	}
	
	void solution() {
		dfs(1);
		for(int i = 1; i <= n; i++) {
			f[i][i] = i;
			for(int j = i + 1; j <= n; j++) {
				f[i][j] = lca(f[i][j - 1], j);
			}
		}
		//cout << lca(4, 5) << " " << dep[4] << "\n";
		cin >> q;
		ll l, r, k;
		while(q--) {
			cin >> l >> r >> k;
			ll res = 0;
			for(int i = l; i + k - 1 <= r; i++) {
				int j = i + k - 1;
				res = max(res, dep[f[i][j]] + 1);
			}
			cout << res << "\n";
		}
	}
};
namespace Task2 {
	ll dep[N];
	struct segT {
		ll t[N];
		ll lc(ll u) { return u * 2; }
		ll rc(ll u) { return u * 2 + 1; }
		bool InRange(ll l, ll r, ll L, ll R) { return L <= l && r <= R; }
		bool OutofRange(ll l, ll r, ll L, ll R) { return r < L || R < l; }
		void pushup(ll u) {
			t[u] = max(t[lc(u)], t[rc(u)]);
		}
		void modify(ll u, ll l, ll r, ll x, ll val) {
			if(l == r) {
				t[u] = val;
				return;
			}
			ll mid = (l + r) / 2;
			if(x <= mid) modify(lc(u), l, mid, x, val);
			else modify(rc(u), mid + 1, r, x, val);
			pushup(u);
		}
		ll query(ll u, ll l, ll r, ll ql, ll qr) {
			if(InRange(l, r, ql, qr)) {
				return t[u];
			} else if(!OutofRange(l, r, ql, qr)) {
				ll mid = (l + r) / 2;
				ll t1 = query(lc(u), l, mid, ql, qr);
				ll t2 = query(rc(u), mid + 1, r, ql, qr);
				return max(t1, t2);
			}
			return 0;
		}
	} T;
	void dfs(ll u, ll fa) {
		for(auto v : G[u]) {
			if(v == fa) continue;
			dep[v] = dep[u] + 1;
			dfs(v, u);
		}
	}
	bool check() {
		ll mx = 0;
		dfs(1, 0);
		for(int i = 1; i <= n; i++) mx = max(mx, dep[i]);
		return mx == n - 1;
	}
	
	void solution() {
		for(int i = 1; i <= n; i++) {
			T.modify(1, 1, n, i, dep[i]);
		}
		cin >> q;
		ll l, r, k;
		while(q--) {
			cin >> l >> r >> k;
			cout << T.query(1, 1, n, l, r) << "\n";
		}
	}
};

void solution() {
	cin >> n;
	for(int i = 1, u, v; i < n; i++) {
		cin >> u >> v;
		G[u].push_back(v); G[v].push_back(u);
	}
	if(Task2::check()) Task2::solution();
	else Task1::solution();
	//Task1::solution();
}

double start_time, end_time;
int main() {
	fileIO("query");
	ios::sync_with_stdio(false), cin.tie(nullptr);
	start_time = clock(); solution(); end_time = clock();
	return 0;
}