// 0pts
// Won't be any hope. Face the truth.
// AFO.
#include <cstdio>
#include <cctype>
#include <cstring>
#include <algorithm>
using namespace std;

typedef long long ll;
const int max_n = 5000, mod = 998244353, ti = 499122177;

struct st
{
	int id, vl;
}
a[max_n];
int hd[max_n], des[max_n<<1], nxt[max_n<<1], val[max_n], fa[max_n], tar, e_cnt = 0;
ll ans = 0;

void add(int s, int t)
{
	des[e_cnt] = t;
	nxt[e_cnt] = hd[s];
	hd[s] = e_cnt++;
}

void chmin(int &a, int b) { if (b < a) a = b; }

void dfs(int id, int f)
{
	fa[id] = f;
	for (int p = hd[id]; p != -1; p = nxt[p])
		if (des[p] != f)
			dfs(des[p], id);
}

inline int read()
{
	int c = getchar(), t = 1, n = 0;
	while (isspace(c)) c = getchar();
	if (c == '-') t = -1, c = getchar();
	while (isdigit(c)) n = n * 10 + c - '0', c = getchar();
	return n * t;
}

int main()
{
	freopen("dfs.in", "r", stdin);
	freopen("dfs.out", "w", stdout);
	
	int cas = read(), n, rt, cur;
	ll ans;
	
	while (cas--)
	{
		n = read(), rt = read() - 1, ans = 0;
		
		memset(hd, -1, sizeof(int) * n);
		
        for (int i = 0; i < n; i++)
            val[i] = read();
		for (int i = 1, x, y; i < n; i++)
		{
			x = read() - 1, y = read() - 1;
			add(x, y), add(y, x);
		}
		dfs(rt, -1);
		
		for (int i = 0, tp; i < n; i++)
		{
			cur = val[i], tp = i;
			ans += val[i];
			
			while (tp != rt)
			{
				tp = fa[tp];
				chmin(cur, val[tp]);
				ans += cur;
			}
		}
		
		printf("%lld\n", ans);
	}
	
	return 0;
}