#include <bits/stdc++.h>
using namespace std;
vector<int> v;
int t, g, h, n, m, removed;
int tree[(1 << 21) + 2];
inline bool valid(int indx) {
  int l = indx << 1, r = l + 1;
  while ((tree[l] || tree[r]) && indx <= m) {
    indx = tree[l] > tree[r] ? l : r, l = indx << 1, r = (indx << 1) + 1;
  }
  return indx > m;
}
void f(int indx) {
  int l = indx << 1, r = (indx << 1) + 1;
  while (tree[l] || tree[r]) {
    int nxt = tree[l] > tree[r] ? l : r;
    tree[indx] = tree[nxt], indx = nxt, l = indx << 1, r = l + 1;
  }
  tree[indx] = 0;
}
void solve(int i = 1) {
  if (i > m) return;
  while (valid(i)) f(i), v.push_back(i);
  solve(i << 1), solve((i << 1) + 1);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL), cout.tie(NULL);
  cin >> t;
  v.reserve((1 << 20) + 10);
  while (t-- && cin >> h >> g) {
    n = (1 << h) - 1, m = (1 << g) - 1, v.clear();
    for (int i = 1; i <= n; i++) {
      cin >> tree[i];
    }
    solve(1);
    long long ans = 0;
    for (int i = 1; i <= m; i++) ans += tree[i];
    cout << ans << "\n";
    for (int i : v) cout << i << " ";
    cout << "\n";
    if (t) memset(tree, 0, (m + 1) * sizeof(int));
  }
  return 0;
}
