#include <bits/stdc++.h>
using namespace std;
template <typename T>
T &read(T &x) {
  x = 0;
  bool f = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') {
      f = 1;
    }
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = (x << 1) + (x << 3) + (ch ^ 48);
    ch = getchar();
  }
  if (f) {
    x = -x;
  }
  return x;
}
void fp() {
  freopen(".in", "r", stdin);
  freopen(".out", "w", stdout);
}
const double eps = 1e-8;
inline int sgn(double x) {
  if (x < -eps) {
    return -1;
  }
  return x > eps;
}
int ans[1048580], a[1048580], mxdep[1048580], h, depth[1048580];
void dfs(int x, int dep) {
  depth[x] = dep - 1;
  if (dep == h) {
    mxdep[x] = 1;
    return;
  }
  dfs(x << 1, dep + 1);
  dfs(x << 1 | 1, dep + 1);
  if (a[x << 1] > a[x << 1 | 1]) {
    mxdep[x] = mxdep[x << 1] + 1;
  } else {
    mxdep[x] = mxdep[x << 1 | 1] + 1;
  }
}
void pop(int x) {
  if (mxdep[x] == 1) {
    mxdep[x] = a[x] = 0;
    return;
  }
  if (a[x << 1] > a[x << 1 | 1]) {
    a[x] = a[x << 1];
    pop(x << 1);
  } else {
    a[x] = a[x << 1 | 1];
    pop(x << 1 | 1);
  }
  if (a[x << 1] > a[x << 1 | 1]) {
    mxdep[x] = mxdep[x << 1] + 1;
  } else {
    mxdep[x] = mxdep[x << 1 | 1] + 1;
  }
}
int main() {
  int t = read(t);
  while (t--) {
    read(h);
    int g = read(g), i, now = 1, cnt = 0;
    long long sum = 0;
    for (i = 1; i < 1 << h; i++) {
      sum += read(a[i]);
    }
    dfs(1, 1);
    for (i = 1; i < 1 << g; i++) {
      while (mxdep[i] > g - depth[i]) {
        sum -= a[i];
        ans[++cnt] = i;
        pop(i);
      }
    }
    printf("%lld\n", sum);
    for (i = 1; i <= (1 << h) - (1 << g); i++) {
      printf("%d ", ans[i]);
    }
    puts("");
  }
  return 0;
}
