#include <bits/stdc++.h>
using namespace std;
const int maxn = (1 << 21) + 10;
int N, M, a[maxn], ans[maxn], w[maxn];
long long sum;
bool check(int x) {
  if (!a[x]) return 0;
  while (a[x << 1] || a[x << 1 | 1])
    x = a[x << 1] > a[x << 1 | 1] ? x << 1 : x << 1 | 1;
  if (w[x] <= M)
    return 0;
  else
    return 1;
}
void work(int x) {
  if (!a[x]) return;
  while (a[x << 1] || a[x << 1 | 1])
    a[x] = a[x << 1] > a[x << 1 | 1] ? a[x << 1] : a[x << 1 | 1],
    x = a[x << 1] > a[x << 1 | 1] ? x << 1 : x << 1 | 1;
  a[x] = 0;
}
int main() {
  for (int i = 1; i < (1 << 20); i++) w[i] = w[i >> 1] + 1;
  int T;
  scanf("%d", &T);
  while (T--) {
    scanf("%d%d", &N, &M);
    ans[0] = sum = 0;
    for (int i = 1; i < (1 << N); i++) scanf("%d", &a[i]);
    for (int i = 1; i < (1 << N); i++)
      while (check(i)) work(i), ans[++ans[0]] = i;
    for (int i = 1; i < (1 << M); i++) sum += a[i];
    printf("%lld\n", sum);
    for (int i = 1; i <= ans[0]; i++)
      printf("%d%s", ans[i], i == ans[0] ? "\n" : " ");
    for (int i = 1; i < (1 << N); i++) a[i] = 0;
  }
}
