#include <bits/stdc++.h>
#define int long long
using namespace std;

struct edge {
    int v, nxt;
}line[500010 * 2];
int cnt;
int head[500010];
int d[500010], dep[500010];

void add(int u, int v) {
    cnt++;
    line[cnt].v = v;
    line[cnt].nxt = head[u];
    head[u] = cnt;
}

void dfs(int node, int depp) {
    dep[node] = depp;
    for(int i = head[node]; i; i = line[i].nxt) {
        int v = line[i].v;
        if(v == node) {
            continue;
        }
        dfs(v, depp + 1);
    }
}

signed main() {
	freopen("query.in", "r", stdin);
	freopen("query.out", "w", stdout);
    int n;
    scanf("%lld", &n);
    for(int i = 1; i <= n; ++i) {
        int u, v;
        scanf("%lld", &u, &v);
        add(u, v);
        add(v, u);
        d[u]++;
        d[v]++;
    }
    int q;
    int root;
    for(int i = 1; i <= n; ++i) {
        if(d[i] == 1) {
            root = i;
        }
    }
    scanf("%d", &q);
    while(q--) {
        int l, r, k;
        scanf("%d %d %d", &l, &r, &k);
        int anss = 0;
        for(int i = l; i <= r; ++i) {
            if(i + k > r) {
                break;
            }
            int ans = INT_MIN;
            for(int j = l; j <= r; ++j) {
                ans = min(dep[j], ans);
                if(j >= l + k) {
                    anss = max(anss, ans);
                }
            }
        }
        printf("%lld\n", anss);
    }
    return 0;
}