#include <bits/stdc++.h>
using namespace std;
const int maxn = 4e6 + 2000;
long long sum;
int cnt, h, g, b[maxn], a[maxn];
void del(int x) {
  if (!a[x << 1] && !a[x << 1 | 1])
    a[x] = 0;
  else if (a[x << 1] > a[x << 1 | 1])
    a[x] = a[x << 1], del(x << 1);
  else
    a[x] = a[x << 1 | 1], del(x << 1 | 1);
}
int gdep(int x, int deep) {
  if (!a[x]) return deep;
  if (a[x << 1] > a[x << 1 | 1])
    return gdep(x << 1, deep + 1);
  else
    return gdep(x << 1 | 1, deep + 1);
}
void dfs(int x, int de) {
  if (!a[x]) return;
  while (gdep(x, de) > g) b[++cnt] = x, sum -= a[x], del(x);
  dfs(x << 1, de + 1);
  dfs(x << 1 | 1, de + 1);
}
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    sum = 0, cnt = 0;
    scanf("%d%d", &h, &g);
    int k = (1 << h) - 1;
    for (int i = k + 1; i <= 2 * k + 1; i++) a[i] = 0;
    for (int i = 1; i <= k; i++) {
      scanf("%d", &a[i]);
      sum += a[i];
    }
    dfs(1, 0);
    printf("%lld\n", sum);
    for (int i = 1; i <= cnt; i++) {
      printf("%d ", b[i]);
    }
    printf("\n");
  }
  return 0;
}
