// WA

#include <bits/stdc++.h>
typedef unsigned uint;
typedef unsigned long long ullt;typedef long long llt;
typedef bool bol;typedef char chr;typedef void voi;
const ullt Mod=998244353;
struct modint
{
	ullt v;
	modint():v(){}
	modint(ullt v):v(v%Mod){}
	friend modint operator+(modint a,modint b){return a()+b();}
	friend modint operator-(modint a,modint b){return a()+Mod-b();}
	friend modint operator*(modint a,modint b){return a()*b();}
	voi read(){scanf("%llu",&v),v%=Mod;}
	voi println(){printf("%llu\n",v);}
	inline ullt&operator()(){return v;}
	modint&operator+=(modint v){return*this=*this+v;}
	modint&operator-=(modint v){return*this=*this-v;}
	modint&operator*=(modint v){return*this=*this*v;}
};
typedef std::vector<modint>modvec;
typedef modvec Beg;
std::vector<uint>Way[5005],Son[5005];
bol Ok[5005];uint Fath[5005],Siz[5005];
voi dfs1(uint p,uint f){Fath[p]=f;for(auto s:Way[p])if(s!=f)dfs1(s,p),Son[p].push_back(s);}
uint A[5005],Now[5005],Back[5005],Biggest[5005];
modint Get[5005][5005];
const modint inv2=(Mod+1)/2;
uint n;
int main()
{
	freopen("dfs.in","r",stdin);
	freopen("dfs.out","w",stdout);
	uint t;scanf("%u",&t);
	while(t--)
	{
		uint r;scanf("%u%u",&n,&r),r--;
		for(uint i=0;i<n;i++)
		{
			scanf("%u",A+i);
			Way[i].clear();
			Son[i].clear();
			Ok[i]=false;
			Now[i]=i;
			Siz[i]=0;
		}
		for(uint i=1,u,v;i<n;i++){
			scanf("%u%u",&u,&v);
			Way[--u].push_back(--v);
			Way[v].push_back(u);
		}
		dfs1(r,-1);
		std::sort(Now,Now+n,[&](uint a,uint b){return A[a]>A[b];});
		modint ans;
		for(uint i=0;i<n;i++)
		{
			Ok[Now[i]]=true;
			uint p=Now[i],last=-1;modint w,f;
			while(~p&&Ok[p]){
				Siz[p]++;
				if(p==Now[i])f=Siz[p];
				else f=(Siz[p]-Siz[last])*inv2+f;
				w+=f;
				last=p;p=Fath[p];
			}
			ans+=w*A[Now[i]];
		}
		ans.println();
	}
	return 0;
}

