# 树

$\$
$\$
$\$
$\$
$\$
$\$
$\$
$\$

# 解：

$ans\left(v,u\right)=qrt\left(u\right)+2\ast unqrt\left(u\right)\ast dis\left(u,v\right)+size\left(u\right)\ast dis\left(u,v{\right)}^{2}$$ans(v,u)=qrt(u)+2*unqrt(u)*dis(u,v)+size(u)*dis(u,v)^2$

$ans\left(v,w\right)=qrt\left(w\right)+2\ast unqrt\left(w\right)\ast x+size\left(w\right)\ast {x}^{2}-qrt\left(v\right)-2\ast unqrt\left(v\right)\ast 2x-size\left(v\right)\ast \left(2x{\right)}^{2}+ans\left(v,v\right)$$ans(v,w)=qrt(w)+2*unqrt(w)*x+size(w)*x^2-qrt(v)-2*unqrt(v)*2x-size(v)*(2x)^2+ans(v,v)$

$贡献=qrt\left(s\right)+2\ast unqrt\left(s\right)\ast dis\left(v,s\right)+size\left(s\right)\ast dis\left(v,s{\right)}^{2}-qrt\left(s\right)-2\ast unqrt\left(s\right)\ast \left(2x+dis\left(v,s\right){\right)}^{2}-size\left(s\right)\ast \left(2x+dis\left(v,s\right){\right)}^{2}$$贡献=qrt(s)+2*unqrt(s)*dis(v,s)+size(s)*dis(v,s)^2-qrt(s)-2*unqrt(s)*(2x+dis(v,s))^2-size(s)*(2x+dis(v,s))^2$

$贡献=-4x\ast unqrt\left(s\right)-size\left(s\right)\ast \left(4{x}^{2}+4x\ast dis\left(v,s\right)\right)$$贡献=-4x*unqrt(s)-size(s)*(4x^2+4x*dis(v,s))$

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
struct lxy{
int to,next;
long long len;
}b[200005];

long long const mod=1000000007;

int n,x,y,z,cnt,q,ui,vi;
int wson[100005];
int size[100005];
int dep[100005];
long long fro[100005];
bool vis[100005];
int fa[100005];
int tp[100005];
long long qrt[100005];
long long unqrt[100005];
long long rc[100005];
long long xi[100005];
long long cooold[100005];
long long dis[100005];

{
b[cnt].len=len;
b[cnt].to=ed;
}

void dfs2(int u,int las)
{
tp[u]=las;
vis[u]=1;
if(wson[u]!=0) dfs2(wson[u],las);
if(vis[b[i].to]==0&&b[i].to!=wson[u])
dfs2(b[i].to,b[i].to);
vis[u]=0;
}

void dfs1(int u,int dp)
{
dep[u]=dp;
int weigh=0;
size[u]=1;vis[u]=1;
if(vis[b[i].to]==0)
{
fro[b[i].to]=fro[u]+b[i].len;
fa[b[i].to]=u;
xi[b[i].to]=b[i].len;
dfs1(b[i].to,dp+1);
size[u]+=size[b[i].to];
if(size[b[i].to]>weigh)
weigh=size[b[i].to],wson[u]=b[i].to;
}
vis[u]=0;
}

void dfs3(int u)
{
vis[u]=1;
if(vis[b[i].to]==0)
{
dfs3(b[i].to);
unqrt[u]=(unqrt[u]+unqrt[b[i].to]+(size[b[i].to]*b[i].len)%mod)%mod;
qrt[u]=(qrt[b[i].to]+qrt[u]+2*unqrt[b[i].to]*b[i].len%mod+size[b[i].to]*b[i].len%mod*b[i].len)%mod;
}
vis[u]=0;
}

long long dfs4(int u)
{
vis[u]=1;
long long ret=0;
if(vis[b[i].to]==0)
{
if(b[i].to==wson[u])
{
ret=dfs4(wson[u]);
ret=(ret+b[i].len)%mod;
rc[u]=(4*unqrt[u]*xi[u]%mod+size[u]*(4*ret*xi[u]%mod+4*xi[u]*xi[u]%mod)%mod+rc[wson[u]])%mod;
cooold[u]=(4*xi[u]*size[u]+cooold[wson[u]])%mod;
dis[u]=ret;
}
else dfs4(b[i].to);
}
if(wson[u]==0)
{
rc[u]=(4*xi[u]*xi[u])%mod;
cooold[u]=(4*xi[u]*size[u])%mod;
}
vis[u]=0;
return ret;
}

int main()
{
scanf("%d",&n);
for(int i=1;i<n;i++)
{
scanf("%d%d%d",&x,&y,&z);
}
dfs1(1,1);dfs2(1,1);dfs3(1);dfs4(1);
scanf("%d",&q);
for(int i=1;i<=q;i++)
{
scanf("%d%d",&x,&y);
ui=x,vi=y;
while(tp[x]!=tp[y])
{
if(dep[tp[x]]>=dep[tp[y]])
{
ans=(ans+rc[tp[x]]-rc[wson[x]]+(len-dis[x])*(cooold[tp[x]]-cooold[wson[x]]))%mod;
len=(len+fro[x]-fro[fa[tp[x]]])%mod;
x=tp[x];
x=fa[x];
}
else if(dep[tp[x]]<dep[tp[y]])
{
y=fa[tp[y]];
}
}
if(dep[y]>=dep[x])
{
ans=(ans+rc[x]-rc[wson[x]]+(len-dis[x])*(cooold[x]-cooold[wson[x]]))%mod;
lca=x,y=x;
}
else{
ans=(ans+rc[y]-rc[wson[x]]+(len-dis[x])*(cooold[y]-cooold[wson[x]]))%mod;
len=(len+fro[x]-fro[y])%mod;
x=y;lca=y;
}
if(lca!=vi)
{
len=(len+fro[x]-fro[fa[x]]);
x=fa[x];
while(tp[x]!=0)
{
ans=(ans+rc[tp[x]]-rc[wson[x]]+(len-dis[x])*(cooold[tp[x]]-cooold[wson[x]]))%mod;
len=(len+fro[x]-fro[fa[tp[x]]])%mod;
x=fa[tp[x]];
}
ans=(qrt[1]+2*unqrt[1]%mod*len+size[1]*len%mod*len-ans)%mod;
ret=2*ret-ans;
ret=ret%mod;
if(ret<0) ret=(ret+mod)%mod;
printf("%lld\n",ret);
continue;
}
if(lca==vi)
{
ret=(qrt[lca]+2*unqrt[lca]*(len+2*xi[lca])%mod+size[lca]*(len+2*xi[lca])%mod*(len+2*xi[lca])-ans)%mod;
len=(len+fro[x]-fro[fa[x]]);
x=fa[x];
while(tp[x]!=0)
{
ans=(ans+rc[tp[x]]-rc[wson[x]]+(len-dis[x])*(cooold[tp[x]]-cooold[wson[x]]))%mod;
len=(len+fro[x]-fro[fa[tp[x]]])%mod;
x=fa[tp[x]];
}
ans=(qrt[1]+2*unqrt[1]%mod*len+size[1]*len%mod*len-ans)%mod;
ret=2*ret-ans;
ret=ret%mod;
if(ret<0) ret=(ret+mod)%mod;
printf("%lld\n",ret);
continue;
}
}
}