#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int max_n = (1 << 21) + 100;
vector<int> ans;
int a[max_n];
void de(int i) {
  int l = i << 1;
  int r = (i << 1) | 1;
  if (a[l] == 0 && a[r] == 0) {
    a[i] = 0;
    return;
  } else {
    if (a[l] > a[r]) {
      a[i] = a[l];
      de(l);
    } else {
      a[i] = a[r];
      de(r);
    }
  }
  return;
}
int n, m;
long long sum;
int find(int i, int dep) {
  int l = i << 1;
  int r = (i << 1) | 1;
  if (a[l] > a[r]) {
    return find(l, dep + 1);
  } else if (a[r] > a[l]) {
    return find(r, dep + 1);
  } else {
    return dep;
  }
}
void dfs(int x, int dep) {
  if (a[x] == 0) {
    return;
  }
  while (find(x, dep) > m) {
    ans.push_back(x);
    sum -= a[x];
    de(x);
  }
  dfs(x << 1, dep + 1);
  dfs((x << 1) | 1, dep + 1);
}
int main() {
  int t;
  int h, g;
  scanf("%d", &t);
  while (t--) {
    sum = 0;
    scanf("%d%d", &h, &m);
    n = (1 << h) - 1;
    for (int i = 1; i <= n; ++i) {
      scanf("%d", &a[i]);
      sum += a[i];
    }
    for (int i = n + 1; i <= (1 << (h + 1)) - 1; ++i) {
      a[i] = 0;
    }
    dfs(1, 1);
    printf("%lld\n", sum);
    for (int i = 0; i < ans.size(); ++i) {
      printf("%d ", ans[i]);
    }
    printf("\n");
    ans.clear();
  }
  return 0;
}
