#include<bits/stdc++.h>
using namespace std;
#define fo(v,a,b) for(int v=a; v<=b; v++)
#define fr(v,a,b) for(int v=a; v>=b; v--)
#define cl(a,v) memset(a,v,sizeof(a))

typedef long long ll;

const ll P = 998244353;
const int N = 400010;

ll power(ll a, ll x) {
	ll res = 1;
	while(x) {
		if(x & 1LL) res = res * a % P;
		a = a * a % P, x >>= 1;
	} return res;
}

int n, a[N], deg[N], rt; vector<int> g[N];

namespace BF {
	void dfs(int x, int fa) {
		for(int y : g[x]) if(y != fa) {
			dfs(y, x);
		}
	}
	void solve() {
		
	}
}
namespace Star {
	ll ans = 0; int v[N], cnt;
	void solve() {
		fo(i,1,n) ans = (ans + a[i]) % P;
		fo(i,1,n) if(i != rt) v[++cnt] = a[i];
		sort(v + 1, v + cnt + 1, greater<int>());
		fo(i,1,n) ans = (ans + (ll)min(a[rt], v[i]) * i % P
			* power(n - 1, P - 2) % P) % P;
		printf("%lld\n", ans);
	}
}

void reset() {
	fo(i,1,n) g[i].clear(), deg[i] = 0;
}
void solve()
{
	scanf("%d%d", &n, &rt), reset();
	fo(i,1,n) scanf("%d", &a[i]);
	fo(i,2,n) {
		int u, v; scanf("%d%d", &u, &v);
		g[u].push_back(v), g[v].push_back(u);
		deg[u]++, deg[v]++;
	} bool chk = true;
	fo(i,1,n) if(deg[i] > 2) chk = false;
//	if(n <= 5000) BF :: solve();
	if(deg[rt] == n - 1) Star :: solve();
//	else if(chk) Chain :: solve();
}

int main()
{
	freopen("dfs.in", "r", stdin);
	freopen("dfs.out", "w", stdout);
	
	int T; cin >> T;
	while(T--) solve();
	
	return 0;
}
