#include<bits/stdc++.h>
#define For(i,a,b) for(int i=(a);i<=(b);++i)
#define Rep(i,a,b) for(int i=(a);i>=(b);--i)
using namespace std;
inline int read()
{
	char c=getchar();int x=0,f=0;
	for(;!isdigit(c);c=getchar())if(c=='-')f=1;
	for(;isdigit(c);c=getchar())x=(x<<1)+(x<<3)+(c^48);
	if(f)x=-x;return x;
}

#define mod 998244353
struct modint{
	int x;
	modint(int o=0){x=o;}
	modint&operator =(int o){return x=o,*this;}
	modint&operator +=(modint o){return x=x+o.x>=mod?x+o.x-mod:x+o.x,*this;}
	modint&operator -=(modint o){return x=x-o.x<0?x-o.x+mod:x-o.x,*this;}
	modint&operator *=(modint o){return x=1ll*x*o.x%mod,*this;}
	modint&operator ^=(int b){
		modint a=*this,c=1;
		for(;b;b>>=1,a*=a)if(b&1)c*=a;
		return x=c.x,*this;
	}
	modint&operator /=(modint o){return *this*=(o^=(mod-2));}
	friend modint operator +(modint a,modint b){return a+=b;}
	friend modint operator -(modint a,modint b){return a-=b;}
	friend modint operator *(modint a,modint b){return a*=b;}
	friend modint operator /(modint a,modint b){return a/=b;}
	modint operator -(){return x?mod-x:0;}
};

#define fi first
#define se second
#define pb push_back
#define mkp make_pair
typedef pair<int,int>pii;
typedef vector<int>vi;

#define maxn 200005
#define inf 0x3f3f3f3f

int n,rt,a[maxn],deg[maxn];
vi e[maxn];

void flower()
{
	modint res=0;
	For(i,1,n)res+=a[i];
	For(i,1,n)if(i!=rt)a[i]=min(a[i],a[rt]);
	
}

int b[maxn],len,top;
int tl[maxn],tr[maxn],tb[maxn];
void dfs(int u,int pa){
	b[++len]=a[u];
	for(auto v:e[u])if(v!=pa)dfs(v,u);
}

void chain()
{
	len=top=0;
	dfs(rt,0);
	// sum_l,r min(a[l]..a[r])
//	For(i,1,n)cout<<b[i]<<" ";puts("");
	modint res=0,now=0;
//	For(i,1,n)For(j,i,n){int mn=a[i];For(k,i+1,j)mn=min(mn,a[k]);res+=mn;}cout<<res.x<<endl;res=0;
	Rep(i,n,1){
		while(top && tb[top]>=b[i]){
			now-=(modint(tb[top]))*(tr[top]-tl[top]+1);
			--top;
		}
		if(!top) ++top,tl[top]=i,tr[top]=n,tb[top]=b[i];
		else ++top,tl[top]=i,tr[top]=tl[top-1]-1,tb[top]=b[i];
		now+=(modint(tb[top]))*(tr[top]-tl[top]+1);
	//	For(j,1,top)cout<<"("<<tl[j]<<' '<<tr[j]<<' '<<tb[j]<<") ";puts("");
	//	cout<<"now "<<now.x<<endl;
		res+=now;
	}
	cout<<res.x<<'\n';
}

void work()
{
	n=read(),rt=read();
	For(i,1,n)a[i]=read(),e[i].clear(),deg[i]=0;
	For(i,2,n){
		int u=read(),v=read();
		e[u].pb(v),e[v].pb(u);
	}
	int mxdeg=0;
	For(i,1,n)mxdeg=max(mxdeg,(int)e[i].size());
	
	if(mxdeg==2&&e[rt].size()==1) return chain();
	if(e[rt].size()==n-1) return flower();
}

signed main()
{
	freopen("dfs.in","r",stdin);
	freopen("dfs.out","w",stdout);
	int T=read();
	while(T--)work();
	return 0;
}
/*
3 12 27 48

1 
5 1
3 2 5 1 4
1 2
2 3
3 4
4 5
*/ 
