#include <bits/stdc++.h>
using namespace std;
int n, h, g, a[2300005];
vector<int> ans;
void del(int rt) {
  int lv = a[rt << 1], rv = a[rt << 1 | 1];
  if (lv == 0 && rv == 0) {
    a[rt] = 0;
  } else if (lv >= rv) {
    a[rt] = a[rt << 1];
    del(rt << 1);
  } else {
    a[rt] = a[rt << 1 | 1];
    del(rt << 1 | 1);
  }
}
int getnxt(int rt) {
  int lv = a[rt << 1], rv = a[rt << 1 | 1];
  if (lv == 0 && rv == 0)
    return rt;
  else if (lv >= rv)
    return getnxt(rt << 1);
  else
    return getnxt(rt << 1 | 1);
}
int main() {
  int t;
  cin >> t;
  for (int tt = 1; tt <= t; tt++) {
    scanf("%d%d", &h, &g);
    ans.clear();
    for (int i = 1; i <= (1 << h + 1); i++) a[i] = 0;
    for (int i = 1; i < (1 << h); i++) scanf("%d", &a[i]);
    for (int i = 1; i < (1 << g); i++)
      while (1) {
        int nxt = getnxt(i);
        if (nxt <= (1 << g) - 1) break;
        del(i);
        ans.push_back(i);
      }
    long long sum = 0;
    for (int i = 1; i < (1 << g); i++) sum += a[i];
    cout << sum << '\n';
    for (auto x : ans) cout << x << " ";
    puts("");
  }
}
