#include <bits/stdc++.h>
using namespace std;
const int N = 2e6 + 2e5 + 5;
int lim;
int T, h, g;
long long a[N] = {0};
int mx[N] = {0};
int he[N] = {0};
void calc(int u) {
  if (u >= lim) {
    a[u] = 0;
    mx[u] = u;
    return;
  }
  he[u] = he[u / 2] + 1;
  if (u == 1) he[u] = 1;
  calc(u * 2);
  calc(u * 2 + 1);
  if (!a[u * 2] && !a[u * 2 + 1])
    mx[u] = u;
  else
    mx[u] = (a[u * 2] > a[u * 2 + 1] ? mx[u * 2] : mx[u * 2 + 1]);
}
void del(int u) {
  if ((!a[u * 2]) && (!a[u * 2 + 1])) {
    a[u] = 0;
    mx[u] = 0;
  } else if (a[u * 2] > a[u * 2 + 1]) {
    a[u] = a[u * 2];
    del(u * 2);
  } else {
    a[u] = a[u * 2 + 1];
    del(u * 2 + 1);
  }
  if (!a[u * 2] && !a[u * 2 + 1])
    mx[u] = u;
  else
    mx[u] = (a[u * 2] > a[u * 2 + 1] ? mx[u * 2] : mx[u * 2 + 1]);
}
int ans[N] = {0};
long long res = 0;
int main() {
  scanf("%d", &T);
  while (T--) {
    he[0] = 998244353;
    scanf("%d%d", &h, &g);
    lim = 1 << h;
    for (int i = 1; i < (1 << h); i++) scanf("%lld", &a[i]);
    calc(1);
    int cc = 0;
    for (int i = 1; i < (1 << h); i++) {
      while (he[mx[i]] > g && a[i]) {
        del(i);
        ans[++cc] = i;
      }
    }
    res = 0;
    for (int i = 1; i < (1 << g); i++) res += a[i], a[i] = 0;
    printf("%lld\n", res);
    for (int i = 1; i <= (lim - (1 << g)); i++) printf("%d ", ans[i]);
    printf("\n");
  }
  return 0;
}
