#include<bits/stdc++.h>
using namespace std;
inline long long read(){
	long long x=0,f=1;char ch=getchar();
	while ((ch!='-')&&(!isdigit(ch))) ch=getchar();
	if (ch=='-') f=-1;
	while (isdigit(ch)) x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
	return x*f;
}
vector<long long> E[1001010];
bool vis[1001010],tag[1001010];
long long x,y,MOD,Testing,val[1001010],n,rt,cnt,i,j,G1,G2,ans,a[1001010],b[1001010],d[1001010];
void add(int x,int y){
	cnt++;a[cnt]=y;b[cnt]=d[x];d[x]=cnt;
}
void build(int x,int fa){int y;
	for (int i=0;i<E[x].size();i++){
		y=E[x][i];
		if (y==fa) continue;
		add(x,y);build(y,x);
	}
}
long long pow(long long x,long long y,long long p){
	long long ans=1;
	for (;y;y>>=1,x=x*x % p) if (y&1) ans=ans*x % p;
	return ans;
}
void search(int x){
	vis[x]=true;
	for (int i=d[x];i;i=b[i])
	        {
	     	search(a[i]);
	        }
}
void solve(int x,long long y){
	  
	  if (x==j) 
	     {
	      G1+=y;G2++;
		 return ;}
		if (tag[x]){
			G1+=y;G2++;
		}
	  for (int i=d[x];i;i=b[i])
	        solve(a[i],min(y,val[a[i]]));
}
void Main(){
	n=read();rt=read();
	for (i=1;i<=n;i++) val[i]=read();
	cnt=0;for (i=1;i<=n;i++) d[i]=0;
	for (i=1;i<=n;i++) E[i].clear();
	for (i=1;i<n;i++){
		x=read();y=read();
		E[x].push_back(y);E[y].push_back(x);
	}
	build(rt,0);
	for (i=1;i<=n;i++) tag[i]=0;
	for (i=1;i<=n;i++) if (d[i]==0) tag[i]=true;
	ans=0;MOD=998244353;
    for (i=1;i<=n;i++)
          {
          	for (j=1;j<=n;j++) vis[j]=0;
          	search(i);
          	for (j=1;j<=n;j++)
          	    if (vis[j])
          	       {G1=0;G2=0;
          	        solve(i,val[i]);
          	        ans=(ans+G1*pow(G2,MOD-2,MOD)) % MOD;
          	        }
		  }
	printf("%lld\n",ans);
}
int main(){
	freopen("dfs.in","r",stdin);
	freopen("dfs.out","w",stdout);
	Testing=read();
	for (;Testing;Testing--) Main();
	return 0;
}

