#include <bits/stdc++.h>
int a[2000006];
int q[2000006];
int is_leaf(int *a, int k, int n) {
  int l, r;
  l = (k << 1);
  r = (k << 1) + 1;
  if (l > n || (a[l] == 0 && a[r] == 0)) return 1;
  return 0;
}
int find_leaf(int *a, int i, int n) {
  int k = i;
  while (1) {
    if (is_leaf(a, k, n)) return k;
    int l, r;
    l = (k << 1);
    r = (k << 1) + 1;
    if (a[l] > a[r])
      k = l;
    else
      k = r;
  }
}
void do_shuffle(int *a, int i, int n) {
  int k = i;
  while (1) {
    if (is_leaf(a, k, n)) {
      a[k] = 0;
      break;
    }
    int l, r;
    l = (k << 1);
    r = (k << 1) + 1;
    if (a[l] > a[r]) {
      a[k] = a[l];
      k = l;
    } else {
      a[k] = a[r];
      k = r;
    }
  }
}
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    int h, g, n, cut_off;
    scanf("%d%d", &h, &g);
    n = (1 << h) - 1;
    long long total = 0;
    for (int i = 1; i <= n; ++i) {
      scanf("%d", a + i);
      total += a[i];
    }
    cut_off = (1 << g) - 1;
    long long sum = 0;
    int len = 0;
    for (int i = 1; i <= cut_off; ++i) {
      while (1) {
        int k = find_leaf(a, i, n);
        if (k <= cut_off) break;
        sum += a[i];
        do_shuffle(a, i, n);
        q[len++] = i;
      }
    }
    printf("%lld\n", total - sum);
    for (int i = 0; i < len; ++i) printf(i + 1 == len ? "%d\n" : "%d ", q[i]);
  }
  return 0;
}
