#include <bits/stdc++.h>
using namespace std;
typedef long long Long;

//const Long MX = ;

void solve(){
	Long n, m, x;
	cin >> n >> m;
	vector<pair<Long,Long> > ord;
	for(Long i = 0; i < n; i++){
		cin >> x;
		x--;
		ord.push_back({x,i});
	}
	sort(ord.begin(),ord.end());
	vector<Long> c(m);
	for(Long i = 0; i < m; i++) cin >> c[i];
	
	multiset<Long> ms;
	Long ans = 0;
	Long pos = 0;
	for(Long i = 0; i < n; i++){
		Long id = ord[i].first;
		Long cc = c[id];
		ms.insert(cc);
		while(pos < m && pos <= id){
			ms.insert(c[pos]);
			pos++;
		}
		/*cout << "==> ";
		for(Long xx : ms) cout << xx << " ";
		cout << endl;*/
		auto it = ms.begin();
		ans += (*it);
		ms.erase(it);
	}
	cout << ans << "\n";
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	cout.precision(10);
	cout << fixed;
	
	Long t;
	cin >> t;
	while(t--) solve();
	return 0;
}
