#include <bits/stdc++.h>
using namespace std;
const int maxn = 2500 * 1000 + 10;
int a[maxn], t, h, g, lh, lg;
long long sum;
vector<int> m;
bool ok(int x) {
  if (a[2 * x] == 0 && a[2 * x + 1] == 0) {
    if (x <= lg)
      return 0;
    else
      return 1;
  }
  if (a[2 * x] > a[2 * x + 1]) return ok(2 * x);
  return ok(2 * x + 1);
}
void bd(int x) {
  if (a[2 * x] == 0 && a[2 * x + 1] == 0) {
    a[x] = 0;
    return;
  }
  if (a[2 * x] > a[2 * x + 1]) {
    swap(a[x], a[2 * x]);
    bd(2 * x);
  } else {
    swap(a[x], a[2 * x + 1]);
    bd(2 * x + 1);
  }
}
void solve(int x) {
  if (a[x] == 0) return;
  while (ok(x)) {
    sum -= a[x];
    bd(x);
    m.push_back(x);
  }
  solve(2 * x);
  solve(2 * x + 1);
}
int main() {
  ios::sync_with_stdio(0);
  scanf("%d", &t);
  for (int z = 0; z < t; z++) {
    m.clear();
    sum = 0;
    scanf("%d %d", &h, &g);
    lh = (1 << h) - 1;
    lg = (1 << g) - 1;
    for (int i = 1; i <= lh; i++) {
      a[2 * i] = a[2 * i + 1] = 0;
      scanf("%d", &a[i]);
      sum += a[i];
    }
    solve(1);
    printf("%lld\n", sum);
    for (auto &w : m) printf("%d ", w);
    printf("\n");
  }
  return 0;
}
