#include <bits/stdc++.h>
using namespace std;
const int MAXN = (2 << 21) + 50;
int a[MAXN];
vector<int> op;
int get_id(int i) {
  int l = 2 * i;
  int r = 2 * i + 1;
  if (!a[l] && !a[r]) return i;
  if (a[l] > a[r])
    return get_id(l);
  else
    return get_id(r);
}
void f(int i) {
  int l = 2 * i;
  int r = 2 * i + 1;
  if (!a[l] && !a[r]) {
    a[i] = 0;
  } else if (a[l] > a[r]) {
    a[i] = a[l];
    f(l);
  } else {
    a[i] = a[r];
    f(r);
  }
}
int main() {
  ios::sync_with_stdio(false);
  int t;
  cin >> t;
  while (t--) {
    int h, g;
    op.clear();
    cin >> h >> g;
    for (int i = 1; i < 1 << (h + 1); i++) a[i] = 0;
    for (int i = 1; i < 1 << h; i++) cin >> a[i];
    int limit = (1 << g) - 1;
    long long ans = 0;
    for (int i = 1; i < 1 << g; i++) {
      while (get_id(i) > limit) {
        op.push_back(i);
        f(i);
      }
      ans += a[i];
    }
    cout << ans << endl;
    for (auto x : op) cout << x << " ";
    cout << '\n';
  }
  return 0;
}
