#include <bits/stdc++.h>
using namespace std;
int h, g, n, m;
vector<int> a;
int f(int id) {
  int left = 2 * id;
  int right = left + 1;
  int ret = a[id];
  if (a[left] == 0 && a[right] == 0) {
    if (id < m) return 0;
    a[id] = 0;
  } else if (a[left] > a[right]) {
    int nxt = f(left);
    if (!nxt) return 0;
    a[id] = nxt;
  } else {
    int nxt = f(right);
    if (!nxt) return 0;
    a[id] = nxt;
  }
  return ret;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int tt;
  cin >> tt;
  while (tt--) {
    cin >> h >> g;
    n = 1 << h;
    m = 1 << g;
    a = vector<int>(1 << (h + 1));
    for (int i = 1; i < n; i++) cin >> a[i];
    vector<int> ans;
    for (int i = 1; i < m; i++) {
      while (f(i)) {
        ans.push_back(i);
      }
    }
    long long res = 0;
    for (int x : a) res += x;
    cout << res << '\n';
    for (int x : ans) cout << x << ' ';
    cout << '\n';
  }
}
