#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#define debug(...) fprintf(stderr, ##__VA_ARGS__)
#else
#define debug(...) void(0)
#define endl "\n"
#endif
using LL = long long;
constexpr int N = 5e5 + 10;
int n, q, dfn[N], dep[N], st[20][N], rnk[N], fa[N], siz[N], son[N], top[N];
basic_string<int> g[N];
void dfs(int u, int _fa) {
  dep[u] = dep[_fa] + 1, fa[u] = _fa, siz[u] = 1, son[u] = 0;
  for (int v : g[u]) if (v != _fa) dfs(v, u), siz[u] += siz[v], siz[v] > siz[son[u]] && (son[u] = v);
}
void cut(int u, int topf) {
  static int cnt = 0;
  dfn[u] = ++cnt, st[0][cnt] = fa[u], rnk[cnt] = u, top[u] = topf;
  if (son[u]) cut(son[u], topf);
  for (int v : g[u]) if (v != fa[u] && v != son[u]) cut(v, v);
}
bool cmp(int u, int v) { return dfn[u] < dfn[v]; }
int lca(int u, int v) {
  if (u == v) return u;
  int l = min(dfn[u], dfn[v]) + 1, r = max(dfn[u], dfn[v]);
  int k = __lg(r - l + 1);
  return min(st[k][l], st[k][r - (1 << k) + 1], cmp);
}
template <int op>
struct mqueue {
  int q[N], l, r;
  void clr() { l = 1, r = 0; }
  int top(int lim) { while (l <= r && q[l] < lim) ++l; return dfn[q[l]]; }
  void push(int i) { while (l <= r && dfn[q[r]] * op < dfn[i] * op) --r; q[++r] = i; }
};
int sst[20][N];
int main() {
#ifndef LOCAL
#ifndef NF
  freopen("query.in", "r", stdin);
  freopen("query.out", "w", stdout);
#endif
  cin.tie(nullptr)->sync_with_stdio(false);
#endif
  cin >> n;
  for (int i = 1, u, v; i < n; i++) cin >> u >> v, g[u] += v, g[v] += u;
  dfs(1, 0), cut(1, 1);
  for (int j = 1; j < 20; j++) {
    for (int i = 1; i + (1 << j) - 1 <= n; i++) {
      st[j][i] = min(st[j - 1][i], st[j - 1][i + (1 << (j - 1))], cmp);
    }
  }
  for (int i = 1; i <= n; i++) sst[0][i] = i;
  for (int j = 1; j < 20; j++) {
    for (int i = 1; i + (1 << j) - 1 <= n; i++) {
      sst[j][i] = lca(sst[j - 1][i], sst[j - 1][i + (1 << (j - 1))]);
    }
  }
  cin >> q;
  while (q--) {
    int l, r, k;
    cin >> l >> r >> k;
    if (k == r - l + 1) {
      k = __lg(r - l + 1);
      int z = lca(sst[k][l], sst[k][r - (1 << k) + 1]);
      cout << dep[z] << endl;
      continue;
    }
    static mqueue<+1> qmax;
    static mqueue<-1> qmin;
    qmax.clr(), qmin.clr();
    int res = 0;
    for (int i = l; i <= r; i++) {
      qmax.push(i), qmin.push(i);
      if (i - k + 1 >= l) res = max(res, dep[lca(rnk[qmax.top(i - k + 1)], rnk[qmin.top(i - k + 1)])]);
    }
    cout << res << endl;
  }
  return 0;
}

