#include <bits/stdc++.h>
using namespace std;
const int N = 23, M = 1 << N;
int a[M], h, g;
int ans[M], sz;
long long sum;
bool ok(int it) {
  if (it >= (1 << g)) return true;
  if (a[2 * it] == 0 && a[2 * it + 1] == 0) return false;
  if (a[2 * it] < a[2 * it + 1]) return ok(2 * it + 1);
  return ok(2 * it);
}
void rem(int it) {
  if (2 * it >= (1 << h)) {
    a[it] = 0;
    return;
  }
  if (a[2 * it] == a[2 * it + 1] && a[2 * it] == 0) {
    a[it] = 0;
    return;
  }
  if (a[2 * it] > a[2 * it + 1]) {
    a[it] = a[2 * it];
    rem(2 * it);
  } else {
    a[it] = a[2 * it + 1];
    rem(2 * it + 1);
  }
}
void calc(int it) {
  if (it >= (1 << g)) return;
  while (ok(it)) {
    ans[sz++] = it;
    sum -= a[it];
    rem(it);
  }
  calc(2 * it);
  calc(2 * it + 1);
}
void solve() {
  sz = 0;
  sum = 0;
  cin >> h >> g;
  for (int i = 1; i < (1 << h); i++) {
    cin >> a[i];
    sum += a[i];
  }
  calc(1);
  cout << sum << "\n";
  for (int i = 0; i < sz; i++) cout << ans[i] << " \n"[i == sz - 1];
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  cin >> t;
  while (t--) solve();
}
