#include <bits/stdc++.h>
using namespace std;
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
class DebugStream {
} LOG;
template <typename T>
DebugStream &operator<<(DebugStream &s, const T &) {
  return s;
}
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
const int maxN = 1e5 + 9, maxV = 1e6 + 9, MOD = 1e9 + 7, SQ = 335, lg = 20,
          bs = 29;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int h, g;
    cin >> h >> g;
    h = 1 << h;
    g = 1 << g;
    vector<long long> arr(h);
    for (int i = 1; i < h; i++) cin >> arr[i];
    vector<int> ans;
    long long suma = accumulate(arr.begin(), arr.end(), 0LL);
    function<int(int)> fn;
    fn = [&](int x) {
      assert(arr[x]);
      if (x * 2 >= h || (!arr[x * 2] && !arr[x * 2 + 1])) return x;
      return fn(arr[x * 2] > arr[x * 2 + 1] ? x * 2 : x * 2 + 1);
    };
    for (int i = 1; i < g; i++) {
      while (1) {
        int w = fn(i);
        if (w < g) break;
        ans.push_back(i);
        suma -= arr[i];
        int he = 22;
        while (!(w >> he)) he--;
        for (; he > 0; he--) {
          int c = w >> (he - 1);
          arr[c >> 1] = arr[c];
        }
        arr[w] = 0;
      }
    }
    cout << suma << '\n';
    for (auto el : ans) cout << el << ' ';
    cout << '\n';
  }
}
