#include <iostream>
#include <cstdio>
#include <vector>
#define N 1000001

using namespace std;

vector<int> G[N];
int n,dep[N],fa[N][21],g[6001][6001],t[N * 4],ans,mx;

void dfs( int u , int f )
{
	dep[u] = dep[f] + 1;
	mx = max( mx , dep[u] );
	fa[u][0] = f;
	for( int i = 1 ; i <= 19 ; i ++ )
		fa[u][i] = fa[fa[u][i - 1]][i - 1];
	for( auto v : G[u] )
	{
		if( v == f ) continue;
		dfs( v , u );
	}
	return;
}

int LCA( int u , int v )
{
	if( dep[u] < dep[v] )
		swap( u , v );
	for( int i = 19 ; i >= 0 ; i -- )
		if( dep[fa[u][i]] >= dep[v] )
			u = fa[u][i];
	if( u == v ) return u;
	for( int i = 19 ; i >= 0 ; i -- )
		if( fa[u][i] != fa[v][i] )
			u = fa[u][i],v = fa[v][i];
	return fa[u][0];
}

void pushup( int u )
{
	t[u] = LCA( t[u << 1] , t[u << 1 | 1] );
}

void build( int u , int l , int r )
{
	if( l == r )
	{
		t[u] = l;
		return;
	}
	int mid = ( l + r ) >> 1;
	build( u << 1 , l , mid );
	build( u << 1 | 1 , mid + 1 , r );
	pushup( u );
}

int que( int u , int l , int r , int L , int R )
{
	if( L <= l && r <= R )
		return t[u];
	int mid = ( l + r ) >> 1,lc = 0,x;
	if( L <= mid )
	{
		x = que( u << 1 , l , mid , L , R );
		if( !lc ) lc = x;
		else lc = LCA( lc , x );
	}
	if( R > mid )
	{
		x = que( u << 1 | 1 , mid + 1 , r , L , R );
		if( !lc ) lc = x;
		else lc = LCA( lc , x );
	}
	return lc;
}

int main()
{
	ios::sync_with_stdio( false );
	cin.tie( 0 );
	cout.tie( 0 );
	freopen( "query.in" , "r" , stdin );
	freopen( "query.out" , "w" , stdout );
	int u,v,T,l,r,k,j;
	cin >> n;
	for( int i = 1 ; i < n ; i ++ )
	{
		cin >> u >> v;
		G[u].push_back( v );
		G[v].push_back( u );
	}
	dfs( 1 , 0 );
	cin >> T;
	if( n <= 5000 && T <= 5000 )
	{
		for( int i = 1 ; i <= n ; i ++ )
		{
			g[i][i] = i;
			for( int j = i + 1 ; j <= n ; j ++ )
				g[i][j] = LCA( g[i][j - 1] , j );
		}
		while( T -- )
		{
			cin >> l >> r >> k;
			ans = 1;
			for( int i = l ; i + k - 1 <= r ; i ++ )
			{
				j = i + k - 1;
				ans = max( ans , dep[g[i][j]] );
			}
			cout << ans << '\n';
		}
	}
	else
	{
		build( 1 , 1 , n );
		while( T -- )
		{
			cin >> l >> r >> k;
			cout << dep[que( 1 , 1 , n , l , r )] << '\n';
		}
	} 
	return 0;
} 
