hdu6110
#include <cstdio>
#include <iostream>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef pair<int,int> pii;
struct fastio{
char s[100005];
int it,len;
fastio(){it=len=0;}
inline char get(){
if(it<len)return s[it++];it=0;
len=fread(s,1,100000,stdin);
if(len==0)return EOF;else return s[it++];
}
bool notend(){
char c=get();
while(c==' '||c=='\n')c=get();
if(it>0)it--;
return c!=EOF;
}
}BUFF;
#define read(x) x=getnum()
#define write(x) putnum(x),putchar(' ')
#define writeln(x) putnum(x),putchar('\n')
inline LL getnum(){
LL r=0;bool ng=0;char c;c=BUFF.get();
while(c!='-'&&(c<'0'||c>'9'))c=BUFF.get();
if(c=='-')ng=1,c=BUFF.get();
while(c>='0'&&c<='9')r=r*10+c-'0',c=BUFF.get();
return ng?-r:r;
}
template<class T> inline void putnum(T x){
if(x<0)putchar('-'),x=-x;
register short a[20]={},sz=0;
while(x)a[sz++]=x%10,x/=10;
if(sz==0)putchar('0');
for(int i=sz-1;i>=0;i--)putchar('0'+a[i]);
}
inline char getreal(){char c=BUFF.get();while(c<=32)c=BUFF.get();return c;}
const int MXN = 5e5 + 5;
const int mod = 1e9 + 7;
const int INF = 0x3f3f3f3f;
int n, m, s;
int head[MXN], tot;
int up[MXN][21], dep[MXN], sum[MXN];
struct lp{
int v, nex, w;
}cw[MXN*3];
void add_1(int a,int b,int c){
cw[++tot].v=b;cw[tot].nex=head[a];cw[tot].w=c;
head[a]=tot;
cw[++tot].v=a;cw[tot].nex=head[b];cw[tot].w=c;
head[b]=tot;
}
void search(int u,int ba,int d) {
dep[u] = d;
up[u][0] = ba;
for(int i = 1; i < 20; ++i) {
up[u][i] = up[up[u][i-1]][i-1];
}
for(int i = head[u]; ~i; i = cw[i].nex) {
int v = cw[i].v;
if(v == ba) continue;
up[v][0] = u;
sum[v] = sum[u] + cw[i].w;
search(v, u, d + 1);
}
}
int LCA(int x,int y) {
if(dep[x] < dep[y]) swap(x, y);
int k = dep[x] - dep[y];
for(int i = 0; i < 20; ++i) {
if((1<<i)&k) {
x = up[x][i];
}
}
if(x == y) return x;
for(int i = 19; i >= 0; --i) {
if(up[x][i] != up[y][i]) {
x = up[x][i];
y = up[y][i];
}
}
return up[x][0];
}
pair<int,int> tree[MXN<<2];
bool cmp(int a,int b) {
return dep[a] > dep[b];
}
pii UN(pii x, pii y) {
int a[4];
a[0] = LCA(x.fi,y.fi), a[1] = LCA(x.fi,y.se);
a[2] = LCA(x.se,y.fi), a[3] = LCA(x.se,y.se);
sort(a, a + 4, cmp);
return pii(a[0], a[1]);
}
void build(int l,int r,int rt) {
if(l == r) {
scanf("%d%d",&tree[rt].fi,&tree[rt].se);
return;
}
int mid=(l+r)>>1;
build(l,mid,rt<<1);build(mid+1,r,rt<<1|1);
tree[rt] = UN(tree[rt<<1], tree[rt<<1|1]);
}
pii query(int L,int R,int l,int r,int rt) {
if(L <= l && r <= R) {
return tree[rt];
}
int mid = (l + r) >> 1;
if(L > mid) return query(L, R, mid+1, r, rt<<1|1);
else if(R <= mid) return query(L,R,l,mid,rt<<1);
return UN(query(L,mid,l,mid,rt<<1),query(mid+1,R,mid+1,r,rt<<1|1));
}
int main(){
while(~scanf("%d", &n)) {
for(int i = 1; i <= n; ++i) {
sum[i] = 0;dep[i] = 0;
for(int j = 0; j < 20; ++j) up[i][j] = 0;
head[i] = -1;
}
tot = -1;
for(int i = 1, a, b, c; i < n; ++i) {
scanf("%d%d%d", &a, &b, &c);
add_1(a, b,c);
}
search(1, 1, 1);
scanf("%d", &m);
build(1,m,1);
scanf("%d", &s);
for(int tim = 0, l, r; tim < s; ++ tim) {
scanf("%d%d", &l, &r);
pii ans = query(l,r,1,m,1);
printf("%d\n", sum[ans.fi]+sum[ans.se]-2*sum[LCA(ans.fi,ans.se)]);
}
}
return 0;
}
ACMer,无怨无悔