#include<bits/stdc++.h>
using namespace std;
#define N 400005
#define Mod 998244353
#define For(i,x,y)for(i=x;i<=(y);i++)
#define Down(i,x,y)for(i=x;i>=(y);i--)
struct node
{
	int next,to;
}e[800005];
int l[N],r[N],a[N],head[N],b[N],sta[N],n,sum,rt,g;
int read()
{
	int A;
	bool K;
	char C;
	C=A=K=0;
	while(C<'0'||C>'9')K|=C=='-',C=getchar();
	while(C>'/'&&C<':')A=(A<<3)+(A<<1)+(C^48),C=getchar();
	return(K?-A:A);
}
void add(int u,int v)
{
	e[++g].to=v;
	e[g].next=head[u];
	head[u]=g;
}
void work()
{
	int i,top;
	For(i,1,n)b[i]=a[b[i]];
	sta[0]=top=0;
	For(i,1,n)
	{
		while(top&&b[i]<b[sta[top]])top--;
		l[i]=sta[top]+1;
		sta[++top]=i;
	}
	top=0;
	sta[0]=n+1;
	Down(i,n,1)
	{
		while(top&&b[i]<=b[sta[top]])top--;
		r[i]=sta[top]-1;
		sta[++top]=i;
	}
	For(i,1,n)sum=(sum+1LL*b[i]*(r[i]-i+1)%Mod*(i-l[i]+1))%Mod;
	sum=(sum-b[1]+Mod)%Mod;
	b[1]=rt;
}
void dfs(int u,int fa)
{
	int i,v;
	b[++n]=u;
	for(i=head[u];i;i=e[i].next)
	{
		v=e[i].to;
		if(v==fa)continue;
		dfs(v,u);
		if(u==rt)work(),n=1;
	}
}
int main()
{
	freopen("dfs.in","r",stdin);
	freopen("dfs.out","w",stdout);
	int T,u,v,i,m;
	T=read();
	while(T--)
	{
		m=read(),rt=read();
		For(i,1,m)a[i]=read();
		For(i,1,m)head[i]=0;
		g=sum=n=0;
		For(i,1,m-1)
		{
			u=read(),v=read();
			add(u,v),add(v,u);
		}
		dfs(rt,0);
		cout<<(sum+a[rt])%Mod<<endl;
	}
	return 0;
}
