#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1 << 21 | 1;
int h, g;
int A[MAXN];
vector<int> Ans;
int idx(int nd) {
  if (!A[(nd << 1)] && !A[(nd << 1 | 1)]) return nd;
  return (A[(nd << 1)] > A[(nd << 1 | 1)]) ? idx((nd << 1))
                                           : idx((nd << 1 | 1));
}
void pop(int nd) {
  if (!A[(nd << 1)] && !A[(nd << 1 | 1)]) return void(A[nd] = 0);
  if (A[(nd << 1)] > A[(nd << 1 | 1)])
    return A[nd] = A[(nd << 1)], pop((nd << 1));
  return A[nd] = A[(nd << 1 | 1)], pop((nd << 1 | 1));
}
int main() {
  int Ti;
  scanf("%d", &Ti);
  while (Ti--) {
    scanf("%d%d", &h, &g);
    for (int i = 1; i < (1 << h); ++i) scanf("%d", A + i);
    Ans.clear();
    for (int i = (1 << h); i < (1 << (h + 1)); ++i) A[i] = 0;
    for (int i = 1; i < (1 << g); ++i)
      while (idx(i) >= (1 << g)) Ans.push_back(i), pop(i);
    long long s = 0;
    for (int i = 1; i < (1 << g); ++i) s += A[i];
    printf("%lld\n", s);
    for (size_t i = 0; i < Ans.size(); ++i)
      printf("%d%c", Ans[i], " \n"[i == Ans.size() - 1]);
  }
  return 0;
}
