#include <bits/stdc++.h>


using namespace std;

using ll = long long;
using pii = pair<int, int>;
using ld = long double;

template <typename T>
int sz(T &x) {
  return (int)(x.size());
}

int power(int x, int y, int mod) {
  if (y == 0) {
    return 1;
  }
  int z = power(x, y >> 1, mod);
  z = z * 1ll * z % mod;
  if (y & 1) {
    z = z * 1ll * x % mod;
  }
  return z;
}

const int mod = 998244353;
const int inf = 1000000007;

void solve() {
  int n, m;
  cin >> n >> m;

  vector<vector<int> > f(m + 1);
  for (int i = 1; i <= n; ++i) {
    int k;
    cin >> k;
    f[k].push_back(i);
  }

  set<pii> s;
  vector<int> c(m + 1);
  for (int i = 1; i <= m; ++i) {
    cin >> c[i];
    for (int j : f[i]) {
      s.insert(make_pair(c[i], j));
    }
  }

  ll ans = 0;
  vector<int> used(n + 1);
  for (int i = 1; i <= m; ++i) {
    if (!s.empty() && s.rbegin() -> first > c[i]) {
      used[s.rbegin() -> second] = 1;
      s.erase(--s.end());
      ans += c[i];
    }
    for (int j : f[i]) {
      if (!used[j]) {
        ans += c[i];
        s.erase(make_pair(c[i], j));
      }
    }
  }

  cout << ans << "\n";
}

int main() {
  #ifndef LOCAL58
    // don't forget to remove if interactive
    ios_base :: sync_with_stdio(false);
    cin.tie(nullptr);
  #endif

  int t = 1;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
