#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAXN = 3 * 1e6 + 15;
int a[MAXN], dep[MAXN], maxdep[MAXN], res[MAXN];
void push_up(int p) {
  maxdep[p] = max(dep[p], a[(p << 1)] > a[(p << 1 | 1)] ? maxdep[(p << 1)]
                                                        : maxdep[(p << 1 | 1)]);
}
void Del(int p) {
  if (a[(p << 1)] == 0 && a[(p << 1 | 1)] == 0) {
    a[p] = 0;
    maxdep[p] = 0;
    return;
  } else if (a[(p << 1)] == 0) {
    a[p] = a[(p << 1 | 1)];
    Del((p << 1 | 1));
  } else if (a[(p << 1 | 1)] == 0) {
    a[p] = a[(p << 1)];
    Del((p << 1));
  } else {
    int maxlr = a[(p << 1)] > a[(p << 1 | 1)] ? (p << 1) : (p << 1 | 1);
    a[p] = a[maxlr];
    Del(maxlr);
  }
  push_up(p);
}
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    int h, g;
    long long sum = 0;
    scanf("%d%d", &h, &g);
    dep[0] = 0;
    for (int i = 1; i < (1 << h); i++) {
      scanf("%d", &a[i]);
      a[i << 1] = a[i << 1 | 1] = 0;
      dep[i] = dep[i >> 1] + 1;
      sum += a[i];
    }
    for (int i = (1 << h) - 1; i >= 1; i--) push_up(i);
    int t = 0;
    for (int i = 1; i < (1 << h); i++) {
      while (a[i] && maxdep[i] > g) {
        sum -= a[i];
        res[t++] = i;
        Del(i);
      }
    }
    printf("%lld\n", sum);
    for (int i = 0; i < t; i++) printf("%d ", res[i]);
    printf("\n");
  }
  return 0;
}
