#include <bits/stdc++.h>
using namespace std;
int t, h, g, prox, r[2100000];
long long a[2100000], tot;
int main() {
  scanf("%d", &t);
  while (t--) {
    tot = 0;
    scanf("%d %d", &h, &g);
    for (int i = 1; i < 1 << h; i++) scanf("%lld", &a[i]);
    for (int i = 1 << h; i < 1 << (h + 1); i++) a[i] = 0;
    prox = 1;
    for (int i = 0; i < (1 << h) - (1 << g); i++) {
      int j = prox;
      while (j < 1 << (g + 1)) {
        j = prox;
        while (a[j]) {
          if (a[2 * j] > a[2 * j + 1])
            j *= 2;
          else
            j = 2 * j + 1;
        }
        if (j < 1 << (g + 1)) prox++;
      }
      r[i] = prox;
      j = prox;
      while (a[j]) {
        a[j] = max(a[2 * j], a[2 * j + 1]);
        if (a[2 * j] > a[2 * j + 1])
          j *= 2;
        else
          j = 2 * j + 1;
      }
    }
    for (int i = 1; i < 1 << g; i++) tot += a[i];
    printf("%lld\n", tot);
    for (int i = 0; i < (1 << h) - (1 << g) - 1; i++) printf("%d ", r[i]);
    printf("%d\n", r[(1 << h) - (1 << g) - 1]);
  }
  return 0;
}
