#include <bits/stdc++.h>
using namespace std;
template <class T>
bool ckmin(T& a, const T& b) {
  return b < a ? a = b, 1 : 0;
}
template <class T>
bool ckmax(T& a, const T& b) {
  return a < b ? a = b, 1 : 0;
}
int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cin.exceptions(cin.failbit);
  int t;
  cin >> t;
  while (t--) {
    int h, g;
    cin >> h >> g;
    vector<long long> a(1 << (h + 1));
    for (int i = (1); i < (1 << h); ++i) cin >> a[i];
    int loc = 1;
    auto works = [&]() {
      int i = loc;
      int hi = 32 - __builtin_clz(i);
      while (a[i] != 0) {
        if (a[i * 2] == 0 and a[i * 2 + 1] == 0)
          break;
        else if (a[i * 2] > a[i * 2 + 1]) {
          i = i * 2;
          ++hi;
        } else {
          i = i * 2 + 1;
          ++hi;
        }
      }
      return hi > g;
    };
    auto go = [&]() {
      int i = loc;
      while (a[i] != 0) {
        if (a[i * 2] == 0 and a[i * 2 + 1] == 0)
          a[i] = 0;
        else if (a[i * 2] > a[i * 2 + 1]) {
          a[i] = a[i * 2];
          i = i * 2;
        } else {
          a[i] = a[i * 2 + 1];
          i = i * 2 + 1;
        }
      }
    };
    vector<long long> ans;
    for (int i = (0); i < ((1 << h) - (1 << g)); ++i) {
      while (not works()) {
        ++loc;
      }
      go();
      ans.push_back(loc);
    }
    long long res = 0;
    for (int i = (1); i < (1 << g); ++i) res += a[i];
    cout << res << '\n';
    for (auto& k : ans) cout << k << ' ';
    cout << '\n';
  }
}
