#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  cin.tie(nullptr)->sync_with_stdio(false);
  int t;
  cin >> t;
  for (; t; --t) {
    int n, m;
    cin >> n >> m;
    n = 1 << n;
    m = 1 << m;
    vector<int> v(n + 1);
    for (int i = 1; i < n; ++i) cin >> v[i];
    function<bool(int, bool)> go = [&](int i, bool f) -> bool {
      if (i * 2 + 1 >= n || (!v[i * 2] && !v[i * 2 + 1])) {
        if (f) v[i] = 0;
        return i >= m;
      }
      int j = v[i * 2] > v[i * 2 + 1] ? i * 2 : i * 2 + 1;
      if (f) v[i] = v[j];
      return go(j, f);
    };
    vector<int> res;
    ll s = accumulate(v.begin(), v.end(), 0ll);
    for (int i = 1; i < n; ++i) {
      if (!v[i]) continue;
      while (go(i, false)) {
        s -= v[i];
        res.push_back(i);
        go(i, true);
      }
    }
    cout << s << '\n';
    for (auto &u : res) cout << u << ' ';
    cout << '\n';
  }
}
