#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
void err() { cout << "\033[39;0m" << endl; }
template <class T, class... Ts>
void err(const T& arg, const Ts&... args) {
  cout << arg << " ";
  err(args...);
}
const int N = (1 << 21) + 5;
int T, n, g, a[N];
vector<int> res;
int getMinDepID(int x) {
  int lval = a[x << 1], rval = a[x << 1 | 1];
  if (lval == 0 && rval == 0) return x;
  if (lval > rval)
    return getMinDepID(x << 1);
  else
    return getMinDepID(x << 1 | 1);
}
void del(int x) {
  int lval = a[x << 1], rval = a[x << 1 | 1];
  if (lval == 0 && rval == 0) {
    a[x] = 0;
    return;
  }
  if (lval > rval) {
    a[x] = lval;
    del(x << 1);
  } else {
    a[x] = rval;
    del(x << 1 | 1);
  }
}
int main() {
  scanf("%d", &T);
  while (T--) {
    res.clear();
    scanf("%d%d", &n, &g);
    for (int i = 1; i <= (1 << (n + 1)); i++) a[i] = 0;
    for (int i = 1; i < (1 << n); i++) scanf("%d", &a[i]);
    for (int i = 1; i < (1 << g); i++) {
      while (true) {
        int id = getMinDepID(i);
        if (id < (1 << g)) break;
        del(i);
        res.push_back(i);
      }
    }
    long long sum = 0;
    for (int i = 1; i < (1 << g); i++) sum += a[i];
    printf("%lld\n", sum);
    for (auto x : res) {
      printf("%d ", x);
    }
    puts("");
  }
  return 0;
}
