#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9 + 7;
const long long INF = 1e18L + 7;
template <class n, class second>
ostream &operator<<(ostream &p, pair<n, second> x) {
  return p << "<" << x.first << ", " << x.second << ">";
}
template <class n>
auto operator<<(ostream &p, n y) ->
    typename enable_if<!is_same<n, string>::value,
                       decltype(y.begin(), p)>::type {
  int o = 0;
  p << "{";
  for (auto c : y) {
    if (o++) p << ", ";
    p << c;
  }
  return p << "}";
}
void dor() { cerr << '\n'; }
template <class n, class... second>
void dor(n p, second... y) {
  cerr << p << " ";
  dor(y...);
}
template <class n, class second>
void mini(n &p, second y) {
  if (p > y) p = y;
}
template <class n, class second>
void maxi(n &p, second y) {
  if (p < y) p = y;
}
const int M = 1024 * 1024 * 2;
int n, m, T[M], dep[M];
long long sum;
vector<int> V;
int getdep(int v) {
  if (T[2 * v] == 0 && T[2 * v + 1] == 0) {
    return dep[v];
  }
  if (T[2 * v] > T[2 * v + 1]) {
    return getdep(2 * v);
  }
  return getdep(2 * v + 1);
}
void wyjeb(int v) {
  if (T[2 * v] == 0 && T[2 * v + 1] == 0) {
    T[v] = 0;
    return;
  }
  if (T[2 * v] > T[2 * v + 1]) {
    T[v] = T[2 * v];
    wyjeb(2 * v);
  } else {
    T[v] = T[2 * v + 1];
    wyjeb(2 * v + 1);
  }
}
void jebaj(int v) {
  if (dep[v] > m) {
    return;
  }
  int x = getdep(v);
  while (x > m) {
    sum -= T[v];
    ;
    wyjeb(v);
    V.push_back(v);
    x = getdep(v);
  }
  jebaj(2 * v);
  jebaj(2 * v + 1);
}
void solve() {
  scanf("%d%d", &n, &m);
  for (int i = (0); i <= (int)((1 << (n + 1)) - 1); ++i) {
    T[i] = 0;
  }
  sum = 0;
  for (int i = (1); i <= (int)((1 << n) - 1); ++i) {
    scanf("%d", T + i);
    sum += T[i];
  }
  V.clear();
  jebaj(1);
  printf("%lld\n", sum);
  for (auto x : V) {
    printf("%d ", x);
  }
  printf("\n");
}
int32_t main() {
  dep[1] = 1;
  for (int i = (1); i <= (int)(M / 2 - 1); ++i) {
    dep[2 * i] = dep[i] + 1;
    dep[2 * i + 1] = dep[i] + 1;
  }
  int t;
  scanf("%d", &t);
  while (t--) {
    solve();
  }
}
