#include <bits/stdc++.h>
using namespace std;
const int N = 1 << 20;
int heap[2 * N], pos[N], sol[N];
bool tryPop(int x, int lim) {
  int st = x << 1, dr = st + 1;
  if (!heap[st] && !heap[dr]) {
    if (x < lim) {
      return false;
    }
    heap[x] = 0;
    return true;
  }
  int son = heap[st] > heap[dr] ? st : dr, val = heap[son];
  if (!tryPop(son, lim)) {
    return false;
  }
  heap[x] = val;
  pos[val] = x;
  return true;
}
long long solve(int n, int lim) {
  long long ans = 0;
  for (int i = 1; i < n; i++) {
    pos[heap[i]] = i;
    sol[i] = heap[i];
  }
  for (int i = n; i < 2 * n; i++) {
    heap[i] = 0;
  }
  sort(sol + 1, sol + n, greater<int>());
  sol[0] = 0;
  for (int i = 1; i < n; i++) {
    int p = pos[sol[i]];
    if (p && heap[p] == sol[i] && tryPop(p, lim)) {
      sol[++sol[0]] = p;
    } else {
      ans += sol[i];
    }
  }
  return ans;
}
int main() {
  int t, n, lim;
  scanf("%d", &t);
  while (t--) {
    scanf("%d%d", &n, &lim);
    for (int i = 1; i < (1 << n); i++) {
      scanf("%d", heap + i);
    }
    printf("%lld\n", solve(1 << n, 1 << lim));
    for (int i = 1; i <= sol[0]; i++) {
      printf("%d ", sol[i]);
    }
    printf("\n");
  }
  return 0;
}
