#include <bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL)
#define ll long long
#define ld long double
#define pb push_back
#define fe first
#define se second
#define st(x) sort(x.begin(),x.end())
#define rst(x) sort(x.rbegin(), x.rend())
#define all(x) x.begin(),x.end()
long double pi = 3.14159265358979323;
#define int long long
const double EPS = 1e-12;
const int N = 1e5 + 5;
const int mod = 998244353;
vector < int > v[N];
int deg[N], let[N], inp[N];
bool vis[N];
int a[N];
void fr(int n) {
for(int i = 1; i <= n; ++i) {
v[i].clear();
a[i] = 0;
vis[i] = 0;
deg[i] = 0;
let[i] = 0;
}
}
void solve() {
int n, m, l, r, ans(0);
cin >> n >> m;
for(int i = 1; i <= m; ++i) {
cin >> l >>r;
v[l].pb(r);
v[r].pb(l);
deg[l] = v[l].size();
deg[r] = v[r].size();
}
for(int i = 1; i <= n; ++i) {
cin >> a[i];
}
sort(a + 1, a + 1 + n, greater < int > ());
set < pair < int, int > > s;
for(int i = 1; i <= n; ++i){
s.insert({deg[i], i});
}
/*for(auto i : s)
cout << i.fe << ' ' << i.se << '\n';cout << '\n';*/
int i = 1;
while(!s.empty()) {
auto it = --s.end();
if(vis[it->se]) {
s.erase(it);
continue;
}
for(auto i : v[it->se]) {
if(deg[i]) {
if(vis[i])
continue;
s.erase({deg[i], i});
deg[i]--;
if(deg[i])
s.insert({deg[i], i});
}
}
vis[it->se] = 1;
let[it->se] = a[i++];
s.erase(it);
}
for(int i = 1; i <= n; ++i) {
// cout << let[i] << '\n';
for(auto it : v[i]) {
if(it > i)
ans += max(let[i], let[it]);
}
}
cout << ans << '\n';
fr(n);
}
signed main(){
fast;
ll t ;
cin >> t;
while(t--)
{
solve();
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CiNkZWZpbmUgZmFzdCBpb3NfYmFzZTo6c3luY193aXRoX3N0ZGlvKDApO2Npbi50aWUoTlVMTCk7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