#include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
typedef long long ll;
#define int ll
inline int fastpow(int a, int n)
{
	int ans = 1;
	while(n)
	{
		if(n & 1)
			ans = ans * a % mod;
		a = a * a % mod, n >>= 1;
	}
	return ans;
}
inline int inv(int x)
{
	return fastpow(x, mod - 2);
}
int inv2 = inv(2);
int n, rt;
int a[400010], d[400010];
struct Edge
{
	int to, nxt;
}e[800010];
int ecnt = 0, head[400010];
inline void addedge(int from, int to)
{
	e[ecnt].to = to;
	e[ecnt].nxt = head[from];
	head[from] = ecnt++;
}
int fa[400010], mn[400010];
inline void dfs(int u)
{
	mn[u] = a[u];
	for(int i = head[u]; i != -1; i = e[i].nxt)
	{
		int v = e[i].to;
		if(v == fa[u])
			continue;
		fa[v] = u;
		dfs(v);
		mn[u] = min(mn[u], mn[v]);
	}
}
namespace Subtask1
{
	inline void Solve()
	{
		fa[rt] = rt;
		dfs(rt);
		int ans = 0;
		for(int i = 1; i <= n; i++)
		{
			set<int> st;
			int nmn = a[i], u = fa[i];
			ans += a[i];
//			cout << i << ' ' << i << " = " << a[i] << endl;
			if(i == rt)
				continue;
			int pre = i;
			while(true)
			{
				nmn = min(nmn, a[u]);
				for(int j = head[u]; j != -1; j = e[j].nxt)
					if(e[j].to != fa[u] && e[j].to != pre)
						st.insert(mn[e[j].to]);
				int p2 = 1, res = 0;
				for(auto x: st)
				{
					if(x >= nmn)
						break;
					res = (res + (p2 = p2 * inv2 % mod) * x) % mod;
				}
				res = (res + p2 * nmn) % mod;
				ans = (ans + res) % mod;
//				cout << i << ' ' << u << " = " << res << endl;
				if(u == rt)
					break;
				pre = u, u = fa[u];
			}
		}
		printf("%lld\n", ans);
	}
}
namespace SubtaskChain
{
	inline bool Check()
	{
		for(int i = 1; i <= n; i++)
			if(d[i] >= 3)
				return false;
		return true;
	}
	inline void dfs2(int u, int fa, vector<int> &vec, int nmn, int ano, int &ans)
	{
//		cout << "DFS " << u << " " << nmn << ' ' << ano << endl;
		ans = (ans + (nmn + min(nmn, ano)) * inv2) % mod;
		vec.push_back(u);
		for(int i = head[u]; i != -1; i = e[i].nxt)
		{
			int v = e[i].to;
			if(v == fa)
				continue;
			dfs2(v, u, vec, min(nmn, a[v]), ano, ans);
		}
	}
	int val[400010];
	struct Num
	{
		int x, ind;
		bool operator < (const Num& ano)
		{
			return x < ano.x;
		}
	}nums[400010];
	inline int Work(int n)
	{
//		for(int i = 1; i <= n; i++)
//			cout << val[i] << ' ';
//		cout << endl;
		for(int i = 1; i <= n; i++)
			nums[i] = {val[i], i};
		sort(nums + 1, nums + n + 1);
		set<int> st;
		st.insert(0), st.insert(n + 1);
		int res = 0;
		for(int i = 1; i <= n; i++)
		{
			int u = nums[i].ind;
			set<int> :: iterator it = st.lower_bound(u);
			int R = *it;
			it--;
			int L = *it;
			res = (res + val[u] * (R - u) % mod * (u - L)) % mod;
			st.insert(u);
		}
//		cout << res << endl;
		return res;
	}
	inline void Solve()
	{
//		cout << "QWQ " << Check() << endl;
		fa[rt] = rt;
		dfs(rt);
		int ans = 0, tot = 0;
		for(int i = head[rt]; i != -1; i = e[i].nxt)
			tot += mn[e[i].to];
		for(int i = head[rt]; i != -1; i = e[i].nxt)
		{
			int v = e[i].to; // a chain
			vector<int> b;
			dfs2(v, rt, b, min(a[rt], a[v]), tot == mn[v] ? INT_MAX : tot - mn[v], ans);
//			for(auto x: b)
//				cout << x << ' ';
//			cout << endl;
			int m = b.size();
			for(int j = 1; j <= m; j++)
				val[j] = a[b[j - 1]];
			ans = (ans + Work(m)) % mod;
		}
		ans = (ans + a[rt]) % mod;
		printf("%lld\n", ans);
	}
}
//namespace Subtask2
//{
//	int val[5010];
//	struct Num
//	{
//		int x, ind;
//		bool operator < (const Num& ano)
//		{
//			return x < ano.x;
//		}
//	}nums[5010];
//	inline void Solve()
//	{
//		dfs(rt);
//		for(int i = 1; i <= n; i++)
//		{
//			int VAL = a[i];
//			for(int j = 1; j <= n; j++)
//				if(a[])
//		}
//	}
//}
namespace SubtaskFlower
{
	
}
signed main()
{
	freopen("dfs.in", "r", stdin);
	freopen("dfs.out", "w", stdout);
	int t;
	scanf("%lld", &t);
	while(t--)
	{
		memset(head, -1, sizeof(head));
		ecnt = 0;
		scanf("%lld %lld", &n, &rt);
		for(int i = 1; i <= n; i++)
			scanf("%lld", &a[i]);
		for(int i = 1; i < n; i++)
		{
			int u, v;
			scanf("%lld %lld", &u, &v);
			d[u]++, d[v]++;
			addedge(u, v), addedge(v, u);
		}
		if(n <= 100)
			Subtask1 :: Solve();
//		else if(d[rt] == n - 1)
//			SubtaskFlower :: Solve();
		else if(SubtaskChain :: Check())
			SubtaskChain :: Solve();
	}
	return 0;
}
// G.
