#include <bits/stdc++.h>
using namespace std;
const int SIZE = 1 << 20;
int INF = 1000000001;
pair<int, int> a[SIZE];
int final_v[SIZE];
bool used[SIZE];
int h, g;
void pull(int id) {
  while (a[id] > min(a[id << 1], a[(id << 1) | 1])) {
    if (a[id << 1] < a[(id << 1) | 1]) {
      swap(a[id << 1], a[id]);
      id <<= 1;
    } else {
      swap(a[(id << 1) | 1], a[id]);
      id = (id << 1) | 1;
    }
    if (id >= (1 << h)) return;
  }
}
void solve() {
  scanf("%d%d", &h, &g);
  long long an = 0;
  for (int i = 1; i < (1 << h); i++) {
    used[i] = 0;
    scanf("%d", &a[i].first);
    a[i].second = i;
    final_v[i] = 0;
  }
  h--;
  for (int lv = h - 1; lv >= 0; lv--) {
    int ll = 1 << lv;
    int rr = 1 << (lv + 1);
    for (int i = ll; i < rr; i++) {
      pair<int, int> tmp = a[i];
      int bot = i << (h - lv);
      a[i] = a[bot];
      a[bot] = make_pair(INF, 0);
      pull(i);
      if (lv < g) {
        int need_mi = max(final_v[i * 2], final_v[i * 2 + 1]);
        while (a[i].first < need_mi) {
          a[i] = make_pair(INF, 0);
          pull(i);
        }
        an += final_v[i] = a[i].first;
        used[a[i].second] = 1;
        a[i] = tmp;
        pull(i);
      } else {
        a[bot] = tmp;
      }
    }
  }
  printf("%lld\n", an);
  bool first_time = true;
  for (int i = (1 << (h + 1)) - 1; i > 0; i--) {
    if (!used[i]) {
      if (!first_time)
        printf(" ");
      else
        first_time = false;
      printf("%d", i);
    }
  }
  puts("");
}
int main() {
  int T;
  scanf("%d", &T);
  while (T--) solve();
  return 0;
}
