#include <bits/stdc++.h>
using namespace std;
void merge_pq(std::priority_queue<int, vector<int>, greater<int>>& dest,
              std::priority_queue<int, vector<int>, greater<int>>& src) {
  if (dest.size() < src.size()) {
    std::swap(dest, src);
  }
  while (!src.empty()) {
    dest.push(src.top());
    src.pop();
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  int t;
  cin >> t;
  for (int testIter = 0; testIter < t; testIter++) {
    int h, g;
    unordered_set<int> notDelete;
    cin >> h >> g;
    int n = 1;
    int k = 1;
    for (int i = 0; i < h; i++) {
      n = 2 * n;
    }
    vector<priority_queue<int, vector<int>, greater<int>>> minheaps(n);
    vector<int> a(n);
    for (int i = 0; i < g; i++) {
      k = 2 * k;
    }
    n--;
    k--;
    long long sum = 0;
    vector<int> ans(k + 1);
    for (int i = 1; i <= n; i++) {
      cin >> a[i];
    }
    for (int i = n; i > 0; i--) {
      minheaps[i].push(a[i]);
    }
    for (int i = n / 2; i > k; i--) {
      merge_pq(minheaps[2 * i], minheaps[2 * i + 1]);
      merge_pq(minheaps[i], minheaps[2 * i]);
    }
    for (int i = k; i > k / 2; i--) {
      merge_pq(minheaps[2 * i], minheaps[2 * i + 1]);
      merge_pq(minheaps[i], minheaps[2 * i]);
      ans[i] = minheaps[i].top();
      minheaps[i].pop();
      sum = sum + ans[i];
    }
    for (int i = k / 2; i > 0; i--) {
      merge_pq(minheaps[2 * i], minheaps[2 * i + 1]);
      while (minheaps[2 * i].top() < max(ans[2 * i], ans[2 * i + 1])) {
        minheaps[2 * i].pop();
      }
      merge_pq(minheaps[i], minheaps[2 * i]);
      ans[i] = minheaps[i].top();
      minheaps[i].pop();
      sum = sum + ans[i];
    }
    cout << sum << endl;
    for (int i = 1; i <= k; i++) {
      notDelete.insert(ans[i]);
    }
    for (int i = n; i >= 1; i--) {
      if (notDelete.find(a[i]) == notDelete.end()) {
        cout << i << " ";
      }
    }
    cout << endl;
  }
}
