#include<bits/stdc++.h>
using namespace std;
int u,i,n,t,l,r,fa[500001],d[500001];
vector<int>e[500001];
bitset<200040002>p;
void init1(int now,int pre,int dep){
	d[now]=dep;p[now*20002]=1;
	for(int i=0;i<e[now].size();++i){
		int nxt=e[now][i];
		if(nxt==pre)continue;
		init1(nxt,now,dep+1);
		for(int j=0;j<=2e4;++j)p[now*20001+p[nxt*20001+j]]=1;}}
void init2(int now,int pre,int dep){
	d[now]=dep;
	for(int i=0;i<e[now].size();++i){
		int nxt=e[now][i];
		if(nxt==pre)continue;
		init2(nxt,now,dep+1);}}
int find(int now,int tgt,int pre,int ans){
	if(now==tgt)return ans;
	if(!e[now].size())return 0x3f3f3f3f;
	int s=ans;
	for(int i=0;i<e[now].size();++i){
		int nxt=e[now][i];
		if(nxt==pre)continue;
		s=min(ans,min(d[nxt],find(nxt,tgt,now,s)));
	}
	printf(" (%d,%d):%d",now,tgt,s);
	return s;}
int find2(int now,int tgt){
	int s=0x3f3f3f3f;
	for(int i=now;i<=tgt;++i)s=min(s,d[i]);
	printf(" (%d,%d):%d",now,tgt,s);
	return s;}
int ufind(int tmp){
	int s=0x3f3f3f3f;
	for(int i=tmp;i<tmp+u;++i)
		for(int j=tmp+u;j>i;--j)
			s=min(s,find(i,j,0,d[i]));
	printf("\nu(%d):%d\n",tmp,s);
	return s;}
int ufind2(int tmp){
	int s=0x3f3f3f3f;
	for(int i=tmp;i<tmp+u;++i)
		for(int j=tmp+u;j>i;--j)
			s=min(s,find2(i,j));
	printf("\nu(%d):%d\n",tmp,s);
	return s;}
void solve1(){
	scanf("%d%d%d",&l,&r,&u);
	int ans=0;
	for(i=l;i<=r-u;++i)ans=max(ans,ufind(i));
	printf("%d\n",ans);}
void solve2(){
	scanf("%d%d%d",&l,&r,&u);
	int ans=0;
	for(i=l;i<=r-u;++i)ans=max(ans,ufind2(i));
	printf("%d\n",ans);}
int main(){
	freopen("query.in","r",stdin);
	freopen("query.out","w",stdout);
	scanf("%d",&n);
	for(i=1;i<n;++i){
		scanf("%d%d",&u,&t);
		e[u].push_back(t);
		e[t].push_back(u);}
	scanf("%d",&t);
	for(i=1;i<=n;++i)if(e[i].size()>l)l=e[i].size();
	if(l<3){init2(1,0,1);
		while(t--)solve2();}
	else{init1(1,0,1);
		while(t--)solve1();}
		
	for(i=1;i<=n;i++)printf("d[%d]:%d ",i,d[i]);
	
	for(i=1;i<n;++i){putchar('\n');
		for(int j=n;j>i;--j)
			printf("%d ",find(i,j,0,d[i]));
		}
	
	return 0;}
