1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. void solve() {
  6. int n, m;
  7. cin >> n >> m;
  8. vector<int> cnt(n), c(m);
  9. for (int i = 0; i < n; ++i) {
  10. int a;
  11. cin >> a;
  12. ++cnt[a - 1];
  13. }
  14. for (int i = 0; i < m; ++i) cin >> c[i];
  15. sort(cnt.begin(), cnt.end());
  16. sort(c.rbegin(), c.rend());
  17. int ans = 0;
  18. for (int x : c) {
  19. bool found = false;
  20. for (int i = 0; i < n; ++i) {
  21. if (cnt[i] >= x) {
  22. cnt[i] -= x;
  23. ans += x;
  24. found = true;
  25. break;
  26. }
  27. }
  28. if (!found) {
  29. ans += cnt.back();
  30. int y = x - cnt.back();
  31. cnt.back() = 0;
  32. }
  33. sort(cnt.begin(), cnt.end());
  34. }
  35. cout << ans;
  36. }
  37.  
  38. int main() {
  39. ios::sync_with_stdio(0);
  40. cin.tie(0), cout.tie(0);
  41.  
  42. int t;
  43. cin >> t;
  44. while (t--) {
  45. solve();
  46. cout << '\n';
  47. }
  48.  
  49. return 0;
  50. }