#include <bits/stdc++.h>
using namespace std;
const int MAXN = (1 << 21) + 5;
int T, H, G, a[MAXN], dep[MAXN], S[MAXN], Slen;
long long sum;
inline int son(int x) {
  return a[x << 1] > a[x << 1 | 1] ? (x << 1) : (x << 1 | 1);
}
int leaf(int x) {
  int y;
  for (y = 0; a[x]; y = x, x = son(x))
    ;
  return y;
}
void func(int x) {
  if (!a[son(x)])
    a[x] = 0;
  else
    a[x] = a[son(x)], func(son(x));
}
int main() {
  scanf("%d", &T);
  while (T--) {
    scanf("%d%d", &H, &G);
    int i;
    for (i = 1; i < (1 << H); ++i) scanf("%d", &a[i]);
    for (i = 1; i < (1 << H); ++i) dep[i] = dep[i >> 1] + 1;
    for (i = 1; i < (1 << H); ++i)
      if (dep[leaf(i)] > G) func(i), S[++Slen] = i, --i;
    for (i = 1; i < (1 << G); ++i) sum += a[i];
    printf("%lld\n", sum);
    for (i = 1; i <= Slen; ++i) printf("%d ", S[i]);
    putchar('\n');
    Slen = sum = 0;
    for (i = 1; i < (1 << H); ++i) a[i] = 0;
  }
  return 0;
}
