#include <bits/stdc++.h>
const int MAXN = 1 << 22;
int val[MAXN], flag[MAXN], ans[MAXN], idx, gg, need[MAXN];
long long sum;
bool found;
void DFS(int i) {
  if (!val[i << 1] && !val[i << 1 | 1]) {
    sum -= val[i];
    val[i] = 0;
    flag[i] = false;
    need[i] = false;
    return;
  }
  if (val[i << 1] > val[i << 1 | 1] && flag[i << 1]) {
    if (!found) {
      ans[idx] = i;
      ++idx;
      found = true;
    }
    std::swap(val[i], val[i << 1]);
    DFS(i << 1);
    flag[i] = val[i << 1] > val[i << 1 | 1] ? flag[i << 1] : flag[i << 1 | 1];
    need[i] = i >= gg || need[i << 1] || need[i << 1 | 1];
    if (i >= gg && val[i << 1] == val[i << 1 | 1]) flag[i] = true;
  } else if (val[i << 1 | 1] > val[i << 1] && flag[i << 1 | 1]) {
    if (!found) {
      ans[idx] = i;
      ++idx;
      found = true;
    }
    std::swap(val[i], val[i << 1 | 1]);
    DFS(i << 1 | 1);
    flag[i] = val[i << 1] > val[i << 1 | 1] ? flag[i << 1] : flag[i << 1 | 1];
    need[i] = i >= gg || need[i << 1] || need[i << 1 | 1];
    if (i >= gg && val[i << 1] == val[i << 1 | 1]) flag[i] = true;
  } else if (need[i << 1]) {
    DFS(i << 1);
    flag[i] = val[i << 1] > val[i << 1 | 1] ? flag[i << 1] : flag[i << 1 | 1];
    need[i] = i >= gg || need[i << 1] || need[i << 1 | 1];
    if (i >= gg && val[i << 1] == val[i << 1 | 1]) flag[i] = true;
  } else {
    DFS(i << 1 | 1);
    flag[i] = val[i << 1] > val[i << 1 | 1] ? flag[i << 1] : flag[i << 1 | 1];
    need[i] = i >= gg || need[i << 1] || need[i << 1 | 1];
    if (i >= gg && val[i << 1] == val[i << 1 | 1]) flag[i] = true;
  }
}
int main() {
  int g, h, t;
  scanf("%d", &t);
  while (t--) {
    scanf("%d%d", &h, &g);
    int hh = 1 << h;
    gg = 1 << g;
    sum = 0;
    for (int i = 0; i <= hh; ++i) {
      val[hh + i] = 0;
      flag[hh + i] = false;
      need[hh + i] = false;
    }
    for (int i = 1; i < hh; ++i) {
      flag[i] = true;
      need[i] = true;
      scanf("%d", val + i);
      sum += val[i];
    }
    int cnt = hh - gg;
    idx = 0;
    while (cnt--) {
      found = false;
      DFS(1);
    }
    printf("%lld\n%d", sum, ans[0]);
    for (int i = 1; i < idx; ++i) printf(" %d", ans[i]);
    puts("");
  }
  return 0;
}
