#include<iostream>
#include<vector>
using namespace std;
const int N=5e5+10;
int n,q;
int l,r,k;
int u,v;
vector<int> nod[N];
int idx[N];
int main(){
	freopen("query.in","r",stdin);
	freopen("query.out","w",stdout);
	cin>>n;
	for(int i=1;i<n;i++){
		cin>>u>>v;
		nod[u].push_back(v);
		nod[v].push_back(u);
		//idx
	}
	cin>>q;
	while(q--){
		cin>>l>>r>>k;
		int len=(r-l+1);
		cout<<l+int(len/k)*k-k<<endl;
	}
	return 0;
}
