#include <bits/stdc++.h>
using namespace std;
const int N = 4e6 + 10;
int n, m, a[N];
int chk(int x) {
  if (!a[x]) return 0;
  if (!a[2 * x] && !a[2 * x + 1]) return 32 - __builtin_clz(x) > m;
  if (a[2 * x] > a[2 * x + 1]) return chk(2 * x);
  return chk(2 * x + 1);
}
void f(int x) {
  if (!a[2 * x] && !a[2 * x + 1]) return a[x] = 0, void();
  if (a[2 * x] > a[2 * x + 1])
    a[x] = a[2 * x], f(2 * x);
  else
    a[x] = a[2 * x + 1], f(2 * x + 1);
}
void work() {
  scanf("%d%d", &n, &m);
  int mx = (1 << n) - 1;
  for (int i = 1; i <= mx; ++i) scanf("%d", a + i);
  vector<int> v;
  for (int i = 1; i <= mx; ++i)
    while (chk(i)) f(i), v.push_back(i);
  long long ans = 0;
  for (int i = 1; i <= mx; ++i) ans += a[i];
  printf("%lld\n", ans);
  for (int t : v) printf("%d ", t);
  puts("");
  for (int i = 1; i <= mx; ++i) a[i] = 0;
}
int main() {
  int t;
  scanf("%d", &t);
  while (t--) work();
}
