#include <bits/stdc++.h>
using namespace std;
vector<int> input, pt(1 << 21, -1);
int search(int x) {
  int lc = x * 2;
  int rc = x * 2 + 1;
  if (input[lc] == 0 && input[rc] == 0) return x;
  if (input[lc] > input[rc])
    return search(lc);
  else
    return search(rc);
}
void f(int x) {
  int lc = x * 2;
  int rc = x * 2 + 1;
  if (input[lc] == 0 && input[rc] == 0) {
    input[x] = 0;
  } else if (input[lc] > input[rc]) {
    input[x] = input[lc];
    f(lc);
    pt[input[x]] = x;
  } else {
    input[x] = input[rc];
    f(rc);
    pt[input[x]] = x;
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    int h, g;
    cin >> h >> g;
    int n = 1 << h, m = 1 << g;
    long long sum = 0;
    input.assign(n << 1, 0);
    pt.clear();
    for (int i = 1; i < n; i++) {
      cin >> input[i];
      sum += input[i];
      pt[input[i]] = i;
    }
    vector<int> cpy = input;
    sort((cpy).begin(), (cpy).end());
    reverse((cpy).begin(), (cpy).end());
    vector<int> ans;
    for (int i = 0; i < n - 1; i++) {
      int a = cpy[i];
      if (search(pt[a]) < m) continue;
      sum -= a;
      ans.push_back(pt[a]);
      f(pt[a]);
    }
    cout << sum << endl;
    for (auto i : ans) cout << i << ' ';
    cout << endl;
  }
  return 0;
}
