#include <bits/stdc++.h>
using namespace std;
int N, M, K, T;
int H, G;
int v[1 << 21];
int cnt[1 << 21];
void build(int cur) {
  if (cur < H) {
    build(cur * 2);
    build(cur * 2 + 1);
    cnt[cur] = v[cur * 2] > v[cur * 2 + 1] ? cnt[cur * 2] : cnt[cur * 2 + 1];
    cnt[cur] += G <= cur;
  } else {
    v[cur] = cnt[cur] = 0;
  }
}
void pop(int cur) {
  if (!v[cur * 2] && !v[cur * 2 + 1]) {
    v[cur] = 0;
    cnt[cur] = 0;
  } else {
    int child = cur * 2;
    if (v[cur * 2] < v[cur * 2 + 1]) child++;
    v[cur] = v[child];
    pop(child);
    cnt[cur] = v[cur * 2] > v[cur * 2 + 1] ? cnt[cur * 2] : cnt[cur * 2 + 1];
    cnt[cur] += G <= cur;
  }
}
int main() {
  scanf("%d", &T);
  while (T--) {
    scanf("%d %d", &H, &G);
    H = 1 << H;
    G = 1 << G;
    vector<int> ans;
    long long tot = 0;
    for (int i = 1; i < H; i++) {
      scanf("%d", v + i);
      tot += v[i];
    }
    build(1);
    for (int cur = 1; cur < H; cur++)
      while (cnt[cur]) {
        tot -= v[cur];
        pop(cur);
        ans.push_back(cur);
      }
    printf("%lld\n", tot);
    for (int x : ans) {
      printf("%d ", x);
    }
    puts("");
  }
}
