#include <bits/stdc++.h>
using namespace std;
long long min_tirar, soma;
vector<long long> v;
bool can(int id) {
  int fl = 2 * id + 1;
  int fr = 2 * id + 2;
  long long vall = 0;
  if (fl < v.size()) vall = v[fl];
  long long valr = 0;
  if (fr < v.size()) valr = v[fr];
  if (vall == 0 and valr == 0) {
    return id >= min_tirar;
  } else if (vall > valr) {
    return can(fl);
  } else
    return can(fr);
}
void f(long long id) {
  int fl = 2 * id + 1;
  int fr = 2 * id + 2;
  long long vall = 0;
  if (fl < v.size()) vall = v[fl];
  long long valr = 0;
  if (fr < v.size()) valr = v[fr];
  if (vall == 0 and valr == 0) {
    v[id] = 0;
    return;
  } else if (vall > valr) {
    v[id] = vall;
    f(fl);
  } else {
    v[id] = valr;
    f(fr);
  }
}
int main() {
  int t;
  cin >> t;
  for (int tt = 0; tt < t; tt++) {
    long long h, g;
    scanf("%lld%lld", &h, &g);
    long long n = (1 << h) - 1;
    v.clear();
    v.resize(n);
    soma = 0;
    for (int i = 0; i < n; i++) scanf("%lld", &v[i]), soma += v[i];
    vector<long long> ans;
    set<pair<long long, long long>> s;
    s.emplace(v[0], 0);
    min_tirar = (1 << g) - 1;
    while (!s.empty()) {
      long long val, id;
      tie(val, id) = *(--s.end());
      s.erase(--s.end());
      if (can(id)) {
        soma -= val;
        f(id);
        ans.push_back(id + 1);
        s.emplace(v[id], id);
      } else {
        int fi = 2 * id + 1;
        if (fi < v.size() and v[fi]) {
          s.emplace(v[fi], fi);
        }
        fi = 2 * id + 2;
        if (fi < v.size() and v[fi]) {
          s.emplace(v[fi], fi);
        }
      }
    }
    printf("%lld\n", soma);
    for (int i = 0; i < (int)(ans).size(); i++) {
      printf("%lld%c", ans[i], " \n"[i == (int)(ans).size() - 1]);
    }
  }
}
