#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int n, kk, wscl, ditt, dps[N];
vector<int> vva;
void gtbts(int x);
int doits(int x);
int main() {
  std::ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> ditt;
  while (ditt--) {
    cin >> kk >> wscl;
    n = (1 << kk) - 1;
    vva = vector<int>(3 * n + 1);
    for (int i = 1; i <= n; i++) cin >> vva[i];
    vector<int> v;
    for (int i = 1; i <= n; i++) {
      if (!vva[i]) continue;
      while (doits(i) > wscl) {
        gtbts(i);
        v.emplace_back(i);
      }
    }
    cout << accumulate(vva.begin(), vva.end(), 0ll) << endl;
    for (int &i : v) cout << i << " \n"[&i == &v.back()];
    puts("");
  }
  return 0;
}
void gtbts(int x) {
  if (x > n) return;
  if (vva[x * 2] == 0 && vva[x * 2 + 1] == 0)
    vva[x] = 0;
  else if (vva[x * 2] > vva[x * 2 + 1]) {
    vva[x] = vva[x * 2];
    gtbts(x * 2);
  } else {
    vva[x] = vva[x * 2 + 1];
    gtbts(x * 2 + 1);
  }
  return;
}
int doits(int x) {
  int lv = 32 - __builtin_clz(x);
  while (true) {
    if (vva[x * 2] == 0 && vva[x * 2 + 1] == 0) break;
    lv++;
    if (vva[x * 2] > vva[x * 2 + 1])
      x = x * 2;
    else
      x = x * 2 + 1;
  }
  return lv;
}
