#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
template <class c>
struct rge {
  c b, e;
};
template <class c>
rge<c> range(c i, c j) {
  return rge<c>{i, j};
}
template <class c>
auto dud(c* x) -> decltype(cerr << *x, 0);
template <class c>
char dud(...);
struct debug {
  template <class c>
  debug& operator<<(const c&) {
    return *this;
  }
};
mt19937_64 rng(134569);
int rd(int l, int r) { return uniform_int_distribution<int>(l, r)(rng); }
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vll = vector<ll>;
const int MOD = 1e9 + 7;
int a[(1 << 22) + 3];
int m[1 << 22];
int check_leaf(int x) {
  if (a[2 * x] == 0 && a[2 * x + 1] == 0) return x;
  if (a[2 * x] > a[2 * x + 1]) return check_leaf(2 * x);
  return check_leaf(2 * x + 1);
}
void go(int x) {
  if (a[2 * x] == 0 && a[2 * x + 1] == 0) {
    a[x] = 0;
    return;
  }
  if (a[2 * x] > a[2 * x + 1]) {
    a[x] = a[2 * x];
    m[a[x]] = x;
    go(2 * x);
    return;
  }
  if (a[2 * x] < a[2 * x + 1]) {
    a[x] = a[2 * x + 1];
    m[a[x]] = x;
    go(2 * x + 1);
    return;
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout << fixed << setprecision(11);
  cerr << fixed << setprecision(6);
  int t;
  cin >> t;
  while (t--) {
    vi v;
    int g, h;
    cin >> h >> g;
    for (int i = 0; i < (1 << (h + 1)) + 1; i++) a[i] = 0;
    for (int i = 0; i < (1 << h) - 1; i++) {
      cin >> a[i + 1];
      v.push_back(a[i + 1]);
      m[a[i + 1]] = i + 1;
    }
    sort((v).begin(), (v).end());
    reverse((v).begin(), (v).end());
    ll sum = 0;
    vi odp;
    for (auto w : v) {
      int ind = m[w];
      if (check_leaf(ind) >= (1 << (g))) {
        odp.push_back(ind);
        go(ind);
      } else
        sum += w;
    }
    cout << sum << "\n";
    for (int i = 0; i < ((int)(odp).size()); i++) cout << odp[i] << " ";
    cout << "\n";
  }
  return 0;
}
