#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
void solve() {
  int h, g;
  cin >> h >> g;
  vector<pair<int, int>> q;
  vector<int> a(1 << g);
  vector<int> del;
  for (int i = 1; i < (1 << h); i++) {
    int a;
    cin >> a;
    q.push_back({a, i});
  }
  sort(q.begin(), q.end());
  for (int i = 0; i < q.size(); i++) {
    int pos = q[i].second;
    while (pos >= (1 << g)) {
      pos /= 2;
    }
    while (a[pos] != 0) {
      pos /= 2;
    }
    if (pos == 0) {
      del.push_back(q[i].second);
      continue;
    }
    if (pos >= (1 << (g - 1)) && pos < (1 << g)) {
      a[pos] = q[i].first;
      continue;
    }
    if (a[2 * pos] != 0 && a[2 * pos + 1] != 0) {
      a[pos] = q[i].first;
      continue;
    }
    del.push_back(q[i].second);
  }
  long long ans = 0;
  for (int i = 1; i < a.size(); i++) {
    ans += a[i];
  }
  cout << ans << endl;
  for (int i : del) {
    cout << i << " ";
  }
  cout << endl;
}
int main() {
  ios_base::sync_with_stdio(false);
  int t;
  cin >> t;
  while (t--) {
    solve();
  }
}
