#include <bits/stdc++.h>
using namespace std;
template <class T>
inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
const long long INF = 1e18;
long long a[1 << 21];
long long b[1 << 21];
bool use[1 << 21];
void solve() {
  long long g, h;
  cin >> h >> g;
  long long N = (1 << h) - 1;
  for (int i = 1; i <= N; i++) {
    cin >> a[i];
    b[i] = a[i];
    use[i] = false;
  }
  for (int i = 1; i < (1 << (g)); i++) {
    b[i] = -1;
  }
  vector<pair<long long, long long> > v;
  for (int i = 1; i <= N; i++) {
    v.push_back({a[i], i});
  }
  sort(v.begin(), v.end());
  for (int i = 0; i < v.size(); i++) {
    if (b[1] != -1) break;
    int now = v[i].second;
    while (now >= (1 << g)) now /= 2;
    while (b[now] != -1) now /= 2;
    if (b[now * 2] != -1 and b[now * 2 + 1] != -1) {
      b[now] = 1;
      use[v[i].second] = true;
    }
  }
  vector<long long> ans;
  long long ansval = 0;
  for (int i = N; i >= 1; i--) {
    if (use[i])
      ansval += a[i];
    else
      ans.push_back(i);
  }
  cout << ansval << "\n";
  for (int i = 0; i < ans.size(); i++) {
    if (i != 0) cout << " ";
    cout << ans[i];
  }
  cout << "\n";
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  long long T;
  cin >> T;
  while (T--) solve();
  return 0;
}
