#include <bits/stdc++.h>
using namespace std;
const int N = 1 << 22;
int h, g;
long long a[N];
vector<int> ans;
int path[25];
long long pull(int u) {
  long long tp = a[u];
  int cnt = 0;
  path[cnt++] = u;
  a[u] = 0;
  while ((u << 1) < (1 << h) && max(a[u << 1], a[(u << 1) | 1]) > 0) {
    if (a[u << 1] > a[(u << 1) | 1]) {
      swap(a[u << 1], a[u]);
      u <<= 1;
    } else {
      swap(a[(u << 1) | 1], a[u]);
      u = (u << 1) | 1;
    }
    path[cnt++] = u;
  }
  if (u < (1 << g)) {
    for (int i = cnt - 1; i >= 0; i--) {
      a[path[i]] = a[path[i - 1]];
    }
    a[path[0]] = tp;
    return 0;
  }
  return tp;
}
void solve() {
  ans.clear();
  cin >> h >> g;
  long long sum = 0;
  for (int i = 1; i <= (1 << h) - 1; i++) {
    cin >> a[i];
    sum += a[i];
  }
  for (int i = 1; i <= (1 << g) - 1; i++) {
    while (true) {
      long long v = pull(i);
      if (v) {
        sum -= v;
        ans.push_back(i);
      } else
        break;
    }
  }
  cout << sum << endl;
  for (auto v : ans) cout << v << " ";
  cout << endl;
}
int main() {
  int t;
  ios::sync_with_stdio(0);
  cin >> t;
  while (t--) solve();
  return 0;
}
