传送门
T < = 5 , 1 < = n , m < = 2000 , 1 < = l i , r i , h i < = 1 e 9 T<=5,1<=n,m<=2000,1<=li,ri,hi<=1e9 T<=5,1<=n,m<=2000,1<=li,ri,hi<=1e9
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#define maxn 2005
#define ll long long
#define mo 998244353
using namespace std;
int T,n,m,i,j,k,x,y,u,v,tot;
int em,e[maxn*2],nx[maxn*2],ls[maxn],L[maxn*2],R[maxn*2];
int hv[maxn][maxn];
struct zom{int s,h;} z[maxn];
int cmp(zom a,zom b){return a.h<b.h;}
ll sum,ans,f[maxn][maxn],g[maxn],s[maxn];
ll ksm(ll x,ll y){
ll s=1;
for(;y;y/=2,x=x*x%mo) if (y&1)
s=s*x%mo;
return s;
}
ll Get(int i,int k,int t){
if (t==1) return min(max(0,z[k].h-L[i]),R[i]-L[i]+1);
return min(max(0,R[i]-z[k].h+1),R[i]-L[i]+1);
}
void dg(int x,int p){
int i,j,y; ll s;
for(i=ls[x];i;i=nx[i]) if (e[i]!=p)
dg(e[i],x);
int tp=1;
for(i=m;i>=1;i--) {
f[x][i]=tp;
if (hv[x][i]) tp=0;
}
for(i=ls[x];i;i=nx[i]) if (e[i]!=p){
y=e[i];
for(j=1;j<=m;j++) hv[x][j]|=hv[y][j];
memcpy(g,f[x],sizeof(g));
memset(f[x],0,sizeof(f[x]));
for(j=1;j<=m;j++) f[x][j]+=g[j]*f[y][j]%mo*Get(i,j,1)%mo;
for(s=0,j=m;j>=1;j--){
if (!hv[y][j]) f[x][j]+=s*g[j]%mo;
if (hv[y][j]) (s+=f[y][j]*Get(i,j,0)%mo)%=mo;
}
for(s=0,j=1;j<=m;j++) {
if (!hv[y][j]) f[x][j]+=s*g[j]%mo*Get(i,j,0)%mo;
if (hv[y][j]) (s+=f[y][j])%=mo;
}
}
for(i=1;i<=m;i++) f[x][i]%=mo;
}
int main(){
scanf("%d",&T);
while (T--){
scanf("%d%d",&n,&m);
em=0,memset(ls,0,sizeof(ls));
sum=1;
for(i=1;i<n;i++){
scanf("%d%d%d%d",&x,&y,&u,&v);
em++; e[em]=y; nx[em]=ls[x]; ls[x]=em; L[em]=u,R[em]=v;
em++; e[em]=x; nx[em]=ls[y]; ls[y]=em; L[em]=u,R[em]=v;
sum=sum*(v-u+1)%mo;
}
for(i=1;i<=m;i++) scanf("%d%d",&z[i].s,&z[i].h);
sort(z+1,z+1+m,cmp);
memset(hv,0,sizeof(hv));
for(i=1;i<=m;i++) hv[z[i].s][i]=1;
dg(1,0);
for(ans=0,i=1;i<=m;i++) ans+=f[1][i];
printf("%lld\n",((sum-ans%mo)%mo*ksm(sum,mo-2)%mo+mo)%mo);
}
}