#include <bits/stdc++.h>
using namespace std;
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
//0 1->codeforces
#define LL "%lld"
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define PN printf("\n")
#define MODV 1000000007
#define MAXN 100007
typedef long long ll;
typedef double dbl;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pi;
void addmod(int &a, ll b){a=(a+b); if(a>=MODV)a-=MODV;}
void mulmod(int &a, ll b){a=(a*b)%MODV;}
int gi(){int a;scanf("%d",&a);return a;}
ll gll(){ll a;scanf(LL,&a);return a;}
typedef ordered_set<pair<ll,int>> opi;
//ordered_set<opi> os[MAXN];
ll ans[MAXN], dw[MAXN];
vector<pi> vq[MAXN];
class graphwal
{
public:
//1 1->bidirectional, 2->reverse edges
typedef vector<pair<int,int>> vpi;
int n, q;
vector<vpi> ed;
graphwal(int n):n(n),ed(n){}
void add(int a, int b, ll w){
if(ed[a].size()==0)ed[a].reserve(8);
if(ed[b].size()==0)ed[b].reserve(8);
ed[a].pb(mp(b,w));ed[b].pb(mp(a,w));
}
void load(int m){ for(int i=0;i<m;i++){int a=gi(),b=gi();int w=gi();add(a-1,b-1,w);}}
void loadq(int q){
this->q = q;
int v;int k;
for(int i=0;i<q;i++){
v=gi()-1;k=gi();
if(k>n)ans[i]=-1;
else{
if(vq[v].size()==0)vq[v].reserve(8);
vq[v].pb({k,i});
}
}
}
void merge(opi &oa, opi &ob, int ia, int ib){
if(oa.size()<ob.size()){
swap(oa,ob);
swap(dw[ia],dw[ib]);
}
ll diff=dw[ib]-dw[ia];
for(auto it=ob.begin();it!=ob.end();it++){
oa.insert({(*it).fi+diff,(*it).se});
}
}
opi dfs(int v=0, int pa=-1){
bool leaf=true;
opi o;
for(auto i:ed[v])
if(i.fi!=pa){
leaf=false;
opi t=dfs(i.fi,v);
dw[i.fi]+=i.se;
merge(o,t,v,i.fi);
}
int sz=o.size();
for(auto i:vq[v]){
if(i.fi>sz){
ans[i.se]=-1;
}else{
ans[i.se]=(*o.find_by_order(i.fi-1)).fi+dw[v];
}
}
o.insert({-dw[v],v});
return o;
}
void print(){
for(int i=0;i<q;i++){
printf("%lld\n",ans[i]);
}
}
};
int main() {
int n=gi();
graphwal g(n);
g.load(n-1);
g.loadq(gi());
g.dfs();
g.print();
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgojaW5jbHVkZTxleHQvcGJfZHMvYXNzb2NfY29udGFpbmVyLmhwcD4KI2luY2x1ZGU8ZXh0L3BiX2RzL3RyZWVfcG9saWN5LmhwcD4KdXNpbmcgbmFtZXNwYWNlIF9fZ251X3BiZHM7CnRlbXBsYXRlIDx0eXBlbmFtZSBUPgp1c2luZyBvcmRlcmVkX3NldCA9IHRyZWU8VCwgbnVsbF90eXBlLCBsZXNzPFQ+LCByYl90cmVlX3RhZywgdHJlZV9vcmRlcl9zdGF0aXN0aWNzX25vZGVfdXBkYXRlPjsKCi8vMCAxLT5jb2RlZm9yY2VzCiNkZWZpbmUgTEwgIiVsbGQiCiNkZWZpbmUgZmkgZmlyc3QKI2RlZmluZSBzZSBzZWNvbmQKI2RlZmluZSBwYiBwdXNoX2JhY2sKI2RlZmluZSBtcCBtYWtlX3BhaXIKI2RlZmluZSBQTiBwcmludGYoIlxuIikKI2RlZmluZSBNT0RWIDEwMDAwMDAwMDcKI2RlZmluZSBNQVhOIDEwMDAwNwoKdHlwZWRlZiBsb25nIGxvbmcgbGw7CnR5cGVkZWYgZG91YmxlIGRibDsKdHlwZWRlZiB2ZWN0b3I8aW50PiB2aTsKdHlwZWRlZiB2ZWN0b3I8bGw+IHZsbDsKdHlwZWRlZiBwYWlyPGludCwgaW50PiBwaTsKdm9pZCBhZGRtb2QoaW50ICZhLCBsbCBiKXthPShhK2IpOyBpZ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LTE7CiAgICAgICAgZWxzZXsKICAgICAgICAgIGlmKHZxW3ZdLnNpemUoKT09MCl2cVt2XS5yZXNlcnZlKDgpOwogICAgICAgICAgdnFbdl0ucGIoe2ssaX0pOwogICAgICAgIH0KICAgICAgfQogICAgfQogICAgdm9pZCBtZXJnZShvcGkgJm9hLCBvcGkgJm9iLCBpbnQgaWEsIGludCBpYil7CiAgICAgIGlmKG9hLnNpemUoKTxvYi5zaXplKCkpewogICAgICAgIHN3YXAob2Esb2IpOwogICAgICAgIHN3YXAoZHdbaWFdLGR3W2liXSk7CiAgICAgIH0KICAgICAgbGwgZGlmZj1kd1tpYl0tZHdbaWFdOwogICAgICBmb3IoYXV0byBpdD1vYi5iZWdpbigpO2l0IT1vYi5lbmQoKTtpdCsrKXsKICAgICAgICBvYS5pbnNlcnQoeygqaXQpLmZpK2RpZmYsKCppdCkuc2V9KTsKICAgICAgfQogICAgfQogICAgb3BpIGRmcyhpbnQgdj0wLCBpbnQgcGE9LTEpewogICAgICBib29sIGxlYWY9dHJ1ZTsKICAgICAgb3BpIG87CiAgICAgIGZvcihhdXRvIGk6ZWRbdl0pCiAgICAgICAgaWYoaS5maSE9cGEpewogICAgICAgICAgbGVhZj1mYWxzZTsKICAgICAgICAgIG9waSB0PWRmcyhpLmZpLHYpOwogICAgICAgICAgZHdbaS5maV0rPWkuc2U7CiAgICAgICAgICBtZXJnZShvLHQsdixpLmZpKTsKICAgICAgICB9CiAgICAgIGludCBzej1vLnNpemUoKTsKICAgICAgZm9yKGF1dG8gaTp2cVt2XSl7CiAgICAgICAgaWYoaS5maT5zeil7CiAgICAgICAgICBhbnNbaS5zZV09LTE7CiAgICAgICAgfWVsc2V7CiAgICAgICAgICBhbnNbaS5zZV09KCpvLmZpbmRfYnlfb3JkZXIoaS5maS0xKSkuZmkrZHdbdl07CiAgICAgICAgfQogICAgICB9CiAgICAgIG8uaW5zZXJ0KHstZHdbdl0sdn0pOwogICAgICByZXR1cm4gbzsKICAgIH0KICAgIHZvaWQgcHJpbnQoKXsKICAgICAgZm9yKGludCBpPTA7aTxxO2krKyl7CiAgICAgICAgcHJpbnRmKCIlbGxkXG4iLGFuc1tpXSk7CiAgICAgIH0KICAgIH0KfTsKaW50IG1haW4oKSB7CiAgaW50IG49Z2koKTsKICBncmFwaHdhbCBnKG4pOwogIGcubG9hZChuLTEpOwogIGcubG9hZHEoZ2koKSk7CiAgZy5kZnMoKTsKICBnLnByaW50KCk7CgogIHJldHVybiAwOwp9Cg==