#include <bits/stdc++.h>

using namespace std;

const int N1 = 5000 + 5;
const int N2 = (int) 5E5 + 5;

int n;
vector<int> e[N2];
int p[N2], d[N2], s[N2], h[N2];
int l[N2], t[N2], c;

void DFS1(int u) {
	s[u] = 1;
	h[u] = 0 - 1;
	for (auto v : e[u]) {
		if (v != p[u]) {
			p[v] = u;
			d[v] = d[u] + 1;
			DFS1(v);
			s[u] = s[u] + s[v];
			if (h[u] < 0 || s[v] > s[h[u]]) {
				h[u] = v;
			}
		}
	}
}

void DFS2(int u) {
	l[u] = c;
	c = c + 1;
	if (h[u] >= 0) {
		t[h[u]] = t[u];
		DFS2(h[u]);
	}
	for (auto v : e[u]) {
		if (v != p[u] && v != h[u]) {
			t[v] = v;
			DFS2(v);
		}
	}
}

int LCA(int u, int v) {
	while (t[u] != t[v]) {
		if (d[t[u]] > d[t[v]]) {
			swap(u, v);
		}
		v = p[t[v]];
	}
	return d[u] < d[v] ? u : v;
}

struct node {
	int x;
	
	node(int u = 0 - 1) : x(u) {
		
	}
	
	friend node operator + (node u, node v) {
		if (u.x < 0) {
			return v;
		}
		if (v.x < 0) {
			return u;
		}
		node w;
		w.x = LCA(u.x, v.x);
		return w;
	}
};

struct segment_tree {
	int n;
	vector<node> a;
	
	segment_tree(int p = 0) {
		n = 1;
		while (n < p) {
			n = n * 2;
		}
		a.resize(n * 2 - 1);
	}
	
	void pull(int x) {
		a[x] = a[x * 2 + 1] + a[x * 2 + 2];
	}
	
	void assign(int x, int l, int r, int p, node u) {
		if (l + 1 == r) {
			a[x] = u;
		} else {
			int m = (l + r) / 2;
			if (p < m) {
				assign(x * 2 + 1, l, m, p, u);
			} else {
				assign(x * 2 + 2, m, r, p, u);
			}
			pull(x);
		}
	}
	
	void assign(int p, node u) {
		assign(0, 0, n, p, u);
	}
	
	node query(int x, int l, int r, int s, int t) {
		if (l >= s && r <= t) {
			return a[x];
		} else if (l >= t || r <= s) {
			return node();
		} else {
			int m = (l + r) / 2;
			return query(x * 2 + 1, l, m, s, t) + query(x * 2 + 2, m, r, s, t);
		}
	}
	
	node query(int s, int t) {
		return query(0, 0, n, s, t);
	}
};

namespace A {
	int q;
	int f[N1][N1];
//	segment_tree T;
	
	void solve() {
//		T = segment_tree(n);
//		for (int i = 0; i < n; i++) {
//			T.assign(i, {i});
//		}
		for (int i = 0; i < n; i++) {
			f[i][i] = i;
			for (int j = i + 1; j < n; j++) {
				f[i][j] = LCA(f[i][j - 1], j);
//				f[i][j] = T.query(i, j + 1).x;
			}
		}
		
		cin >> q;
		
		while (q--) {
			int l, r, k;
			cin >> l >> r >> k;
			l = l - 1;
			r = r - 1;
			int x = 0;
			for (int i = l; i <= r - k + 1; i++) {
				x = max(x, d[f[i][i + k - 1]]);
			}
			cout << x << "\n";
		}
	}
};

namespace B {
	int q;
	segment_tree T;
	
	void solve() {
		T = segment_tree(n);
		for (int i = 0; i < n; i++) {
			T.assign(i, {i});
		}
		
		cin >> q;
		
		while (q--) {
			int l, r, k;
			cin >> l >> r >> k;
			l = l - 1;
			r = r - 1;
			int x = T.query(l, r + 1).x;
			cout << d[x] << "\n";
		}
	}
}

void solve() {
	cin >> n;
	for (int i = 0; i < n - 1; i++) {
		int u, v;
		cin >> u >> v;
		u = u - 1;
		v = v - 1;
		e[u].push_back(v);
		e[v].push_back(u);
	}
	
	p[0] = 0 - 1;
	d[0] = 1;
	DFS1(0);
	DFS2(0);
	
	if (n <= 5000) {
		A::solve();
	} else {
		B::solve();
	}
}

int main() {
	freopen("query.in", "r", stdin);
	freopen("query.out", "w", stdout); 
	ios::sync_with_stdio(0);
	cin.tie(nullptr);
	solve();
	return 0;
}
