#include <bits/stdc++.h>
using namespace std;
const int N = 5e6 + 5;
const long double eps = 1e-12, inf = 1e18;
int h, g, T, a[N], b[N], n, m;
int main() {
  for (scanf("%d", &T); T--;) {
    scanf("%d%d", &h, &g);
    n = (1 << h) - 1, m = (1 << g) - 1;
    for (int i = 1; i <= n; ++i) scanf("%d", a + i);
    for (int i = 1 << h; i < 2 << h; ++i) a[i] = 0;
    int top = 0;
    for (int i = 1; i <= n; ++i)
      for (;;) {
        if (a[i] == 0) break;
        int cur = i;
        for (;;) {
          int L = cur << 1, R = L + 1;
          if (a[L] == 0 && a[R] == 0) break;
          if (a[L] > a[R])
            cur = L;
          else
            cur = R;
        }
        if (cur <= m) break;
        b[++top] = i;
        cur = i;
        for (;;) {
          int L = cur << 1, R = L + 1;
          if (a[L] == 0 && a[R] == 0) break;
          if (a[L] > a[R])
            a[cur] = a[L], cur = L;
          else
            a[cur] = a[R], cur = R;
        }
        a[cur] = 0;
      }
    long long ans = 0;
    for (int i = 1; i <= m; ++i) ans += a[i];
    printf("%lld\n", ans);
    for (int i = 1; i <= n - m; ++i) printf("%d%c", b[i], " \n"[i == n - m]);
  }
  return 0;
}
