#include <bits/stdc++.h>
using namespace std;
int N, G, tree[1 << 20], cnt[1 << 20];
void solve(int c) {
  --cnt[c];
  if (2 * c >= N || tree[2 * c] == 0 && tree[2 * c + 1] == 0) {
    tree[c] = 0;
    return;
  }
  if (tree[2 * c] > tree[2 * c + 1]) {
    tree[c] = tree[2 * c];
    solve(2 * c);
  } else {
    tree[c] = tree[2 * c + 1];
    solve(2 * c + 1);
  }
  cnt[c] =
      (tree[2 * c] > tree[2 * c + 1] ? cnt[2 * c] : cnt[2 * c + 1]) + (c >= G);
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ((void)0);
  ((void)0);
  ((void)0);
  int T;
  for (cin >> T; T--;) {
    long long res = 0;
    vector<int> ans;
    cin >> N >> G;
    N = 1 << N;
    G = 1 << G;
    for (int i = 1; i < N; i++) {
      cin >> tree[i];
      res += tree[i];
      cnt[i] = i >= G;
    }
    for (int i = N / 2; --i;)
      cnt[i] = (tree[2 * i] > tree[2 * i + 1] ? cnt[2 * i] : cnt[2 * i + 1]) +
               (i >= G);
    for (int i = 1; i < G; i++)
      while (cnt[i]) {
        res -= tree[i];
        ans.push_back(i);
        solve(i);
      }
    cout << res << '\n';
    for (auto a : ans) cout << a << ' ';
    cout << '\n';
  }
  return 0;
}
