#include <bits/stdc++.h>
using namespace std;
vector<int> st;
int g, h, mg, mh;
int leaffrom(int i) {
  int l = 2 * i;
  int r = 2 * i + 1;
  if (r < mh) {
    if (st[l] > st[r]) return leaffrom(l);
    if (st[l] < st[r]) return leaffrom(r);
  }
  return i;
}
int remove(int i) {
  int l = 2 * i;
  int r = 2 * i + 1;
  if (r < mh) {
    if (st[l] > st[r]) {
      st[i] = st[l];
      return remove(l);
    }
    if (st[l] < st[r]) {
      st[i] = st[r];
      return remove(r);
    }
  }
  st[i] = 0;
  return 0;
}
int main() {
  int ___T, case_n = 1;
  scanf("%d ", &___T);
  while (___T-- > 0) {
    scanf("%d%d", &(h), &(g));
    mg = 1 << g;
    mh = 1 << h;
    st = vector<int>(mh, 0);
    long long r(0);
    for (int i = 0; i < (mh - 1); ++i) {
      scanf("%d", &(st[i + 1]));
      r += st[i + 1];
    }
    int nbe = mh - 1;
    vector<int> res(0);
    for (int curr = 1; curr < mg; curr++) {
      while (nbe >= mg) {
        int lf = leaffrom(curr);
        if (lf >= mg) {
          r -= st[curr];
          remove(curr);
          res.push_back(curr);
          nbe--;
        } else {
          break;
        }
      }
    }
    printf("%I64d\n", r);
    for (int i = 0; i < (((int)(res).size())); ++i) {
      printf("%d%c", res[i], (i == ((int)(res).size()) - 1) ? '\n' : ' ');
    }
  }
}
