#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 vector<ll> 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];
if(ob.size()==0)return;
//size_t ns = oa.size() + ob.size() +8;
//if(ns>oa.capacity()) oa.reserve(ns);
for(auto &i:ob){ i+=diff; }
move(ob.begin(),ob.end(),back_inserter(oa));
}
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);
}
bool sorted=false;
for(auto i:vq[v]){
if(i.fi>o.size()){
ans[i.se]=-1;
}else{
if(!sorted)sort(o.begin(),o.end()),sorted=true;
ans[i.se]=o[i.fi-1]+dw[v];
}
}
//o.insert({-dw[v],v});
o.pb(-dw[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+IHZsbDsK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xxO2krKyl7CiAgICAgICAgdj1naSgpLTE7az1naSgpOwogICAgICAgIGlmKGs+bilhbnNbaV09LTE7CiAgICAgICAgZWxzZXsKICAgICAgICAgIGlmKHZxW3ZdLnNpemUoKT09MCl2cVt2XS5yZXNlcnZlKDgpOwogICAgICAgICAgdnFbdl0ucGIoe2ssaX0pOwogICAgICAgIH0KICAgICAgfQogICAgfQogICAgdm9pZCBtZXJnZShvcGkgJm9hLCBvcGkgJm9iLCBpbnQgaWEsIGludCBpYil7CiAgICAgIGlmKG9hLnNpemUoKTxvYi5zaXplKCkpewogICAgICAgIHN3YXAob2Esb2IpOwogICAgICAgIHN3YXAoZHdbaWFdLGR3W2liXSk7CiAgICAgIH0KICAgICAgbGwgZGlmZj1kd1tpYl0tZHdbaWFdOwogICAgICBpZihvYi5zaXplKCk9PTApcmV0dXJuOwogICAgICAvL3NpemVfdCBucyA9IG9hLnNpemUoKSArIG9iLnNpemUoKSArODsKICAgICAgLy9pZihucz5vYS5jYXBhY2l0eSgpKSBvYS5yZXNlcnZlKG5zKTsKICAgICAgZm9yKGF1dG8gJmk6b2IpeyBpKz1kaWZmOyB9CiAgICAgIG1vdmUob2IuYmVnaW4oKSxvYi5lbmQoKSxiYWNrX2luc2VydGVyKG9hKSk7CiAgICB9CiAgICBvcGkgZGZzKGludCB2PTAsIGludCBwYT0tMSl7CiAgICAgIGJvb2wgbGVhZj10cnVlOwogICAgICBvcGkgbzsKICAgICAgZm9yKGF1dG8gaTplZFt2XSkKICAgICAgICBpZihpLmZpIT1wYSl7CiAgICAgICAgICBsZWFmPWZhbHNlOwogICAgICAgICAgb3BpIHQ9ZGZzKGkuZmksdik7CiAgICAgICAgICBkd1tpLmZpXSs9aS5zZTsKICAgICAgICAgIG1lcmdlKG8sdCx2LGkuZmkpOwogICAgICAgIH0KICAgICAgYm9vbCBzb3J0ZWQ9ZmFsc2U7CiAgICAgIGZvcihhdXRvIGk6dnFbdl0pewogICAgICAgIGlmKGkuZmk+by5zaXplKCkpewogICAgICAgICAgYW5zW2kuc2VdPS0xOwogICAgICAgIH1lbHNlewogICAgICAgICAgaWYoIXNvcnRlZClzb3J0KG8uYmVnaW4oKSxvLmVuZCgpKSxzb3J0ZWQ9dHJ1ZTsKICAgICAgICAgIGFuc1tpLnNlXT1vW2kuZmktMV0rZHdbdl07CiAgICAgICAgfQogICAgICB9CiAgICAgIC8vby5pbnNlcnQoey1kd1t2XSx2fSk7CiAgICAgIG8ucGIoLWR3W3ZdKTsKICAgICAgcmV0dXJuIG87CiAgICB9CiAgICB2b2lkIHByaW50KCl7CiAgICAgIGZvcihpbnQgaT0wO2k8cTtpKyspewogICAgICAgIHByaW50ZigiJWxsZFxuIixhbnNbaV0pOwogICAgICB9CiAgICB9Cn07CmludCBtYWluKCkgewogIGludCBuPWdpKCk7CiAgZ3JhcGh3YWwgZyhuKTsKICBnLmxvYWQobi0xKTsKICBnLmxvYWRxKGdpKCkpOwogIGcuZGZzKCk7CiAgZy5wcmludCgpOwoKICByZXR1cm4gMDsKfQo=