#include <bits/stdc++.h>
const int SIZE = (1 << 21) + 5;
int a[SIZE];
int get_id(int rt) {
  if (!a[rt << 1] && !a[rt << 1 | 1]) return rt;
  if (a[rt << 1] > a[rt << 1 | 1])
    return get_id(rt << 1);
  else
    return get_id(rt << 1 | 1);
}
void dfs(int rt) {
  if (!a[rt << 1] && !a[rt << 1 | 1])
    a[rt] = 0;
  else if (a[rt << 1] > a[rt << 1 | 1]) {
    a[rt] = a[rt << 1];
    dfs(rt << 1);
  } else {
    a[rt] = a[rt << 1 | 1];
    dfs(rt << 1 | 1);
  }
}
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    int h, g;
    scanf("%d%d", &h, &g);
    for (int i = 1; i < (1 << (h + 1)) && i < SIZE; i++) a[i] = 0;
    for (int i = 1; i < (1 << h); i++) scanf("%d", &a[i]);
    std::vector<int> res;
    int limit = (1 << g) - 1;
    long long sum = 0;
    for (int i = 1; i < (1 << g); i++) {
      while (get_id(i) > limit) {
        res.push_back(i);
        dfs(i);
      }
    }
    for (int i = 1; i < (1 << g); i++) sum += a[i];
    printf("%lld\n", sum);
    for (auto e : res) printf("%d ", e);
    printf("\n");
  }
}
