#include<bits/stdc++.h>
using namespace std;
const int mod=998244353;
namespace wxh666{
#define getchar getchar_unlocked
#define putchar putchar_unlocked
#define sqrt(a) __builtin_sqrt(a)
#define f(a,b,c) for(int a=b;a<=c;a++)
#define ff(a,b,c) for(int a=b;a>=c;a--)
#define g(n) f(i,1,n)
#define df(n,m) g(n) f(j,1,m)
#define max max_by_wxh
template<typename T>
inline T max_by_wxh(T x,T y) {return ((x)>(y)?(x):y);}
template<typename T,typename ...Args>
inline T max_by_wxh(T x,Args ...xs) {return max_by_wxh(x,max_by_wxh(xs...));}
#define min min_by_wxh
template<typename T>
inline T min_by_wxh(T x,T y) {return ((x)<(y)?(x):y);}
template<typename T,typename ...Args>
inline T min_by_wxh(T x,Args ...xs) {return min_by_wxh(x,min_by_wxh(xs...));}
template<typename T>
inline void read(T &ans)
{
char ch=getchar();int f=1;ans=0;
for(;!isdigit(ch);ch=getchar()) ch=='-'?f=-1:f=1;
for(;isdigit(ch);ch=getchar()) ans=(ans<<3)+(ans<<1)+(ch&15);
ans*=f;
return;
}
template<typename T,typename ...Args>
inline void read(T &tmp,Args &...tmps){read(tmp);read(tmps...);}
inline int read()
{
char ch=getchar();int f=1,ans=0;
for(;!isdigit(ch);ch=getchar()) ch=='-'?f=-1:f=1;
for(;isdigit(ch);ch=getchar()) ans=(ans<<3)+(ans<<1)+(ch&15);
return f*ans;
}
#define in read()
template<typename T>
inline void pu(T x)
{
ios::sync_with_stdio(false);
cout<<x;
ios::sync_with_stdio(true);
return;
}
template<typename T>
inline void pk(T x)
{
ios::sync_with_stdio(false);
cout<<x<<" ";
ios::sync_with_stdio(true);
return;
}
inline void pk(char x)
{
ios::sync_with_stdio(false);
cout<<x;
if(x!='\n') cout<<' ';
ios::sync_with_stdio(true);
return;
}
template<typename T,typename ...Args>
inline void pu(T x,Args ...xs){pu(x);pu(xs...);}
template<typename T,typename ...Args>
inline void ppu(T x,Args ...xs){pu(x,xs...,'\n');}
template<typename T,typename ...Args>
inline void pk(T x,Args ...xs){pk(x);pk(xs...);}
template<typename T,typename ...Args>
inline void ppk(T x,Args ...xs){pk(x,xs...,'\n');}
#define cs(dt,n,m) \
for(int i=1;i<=n;i++)\
{\
for(int j=1;j<=m;j++)\
printf("%d ",dt[i][j]);\
cout<<endl;\
}
};using namespace wxh666;
namespace FILES{
// #define FILE
#ifdef FILE
string file="";
struct File{File(){freopen((file+".in").c_str(),"r",stdin);freopen((file+".out").c_str(),"w",stdout);}}files;
#endif
};using namespace FILES;
namespace lsq {
typedef int lsqxx;
struct lq {
struct lqbz {
lsqxx v,w,nxt;
} e[400005];
lsqxx h[200005],cnt;
inline void add(lsqxx u,lsqxx v,lsqxx w=1) {
e[++cnt].v=v;
e[cnt].w=w;
e[cnt].nxt=h[u];
h[u]=cnt;
}
void erase() {cnt=0;memset(h,0,sizeof(h));return;}
#define F(z,u) for(int j=z.h[u],v=z.e[j].v,w=z.e[j].w;j;j=z.e[j].nxt,v=z.e[j].v,w=z.e[j].w)
}q;
};using namespace lsq;
vector<pair<int,int> >qu[200005];
int n,m,p;
int sz[200005],d[200005];
int f[200005],dp[200005];
int ans;
void getsz(int u=1,int fa=1)
{
sz[u]=1;d[u]=d[fa]+1;
F(q,u)
{
if(v==fa) continue;
getsz(v,u);
sz[u]+=sz[v];
f[u]=(f[u]+f[v]+sz[v]*w)%mod;
ans=(ans+w*sz[v]*(n-sz[v]))%mod;
}
}
int anss[200005];
void dfs(int u=1,int fa=1)
{
for(auto i:qu[u]) anss[i.second]=(dp[u]+i.first*n+ans)%mod;
F(q,u)
{
if(v==fa) continue;
dp[v]=((dp[u]-w*sz[v]+w*(n-sz[v]))%mod+mod)%mod;
dfs(v,u);
}
}
int main()
{
cin>>n>>p;
g(n-1)
{
int x,y,z;
read(x,y,z);
q.add(x,y,z);
q.add(y,x,z);
m+=z;
}
g(p)
{
int k,w;
read(k,w);
qu[k].push_back(make_pair(w,i));
}
getsz();
dp[1]=f[1];
dfs();
g(p) printf("%lld\n",2*anss[i]);
return 0;
}
样例过了,但0pts.