#include <bits/stdc++.h>
using namespace std;
int n, h, g;
int a[3000010];
int dep[3000010], ans[3000010], cnt;
long long res;
int getd(int u) {
  int l = u << 1;
  int r = u << 1 | 1;
  if (!a[l] && !a[r])
    return u;
  else if (a[l] > a[r])
    return getd(l);
  else
    return getd(r);
}
void modify(int u) {
  int l = u << 1;
  int r = u << 1 | 1;
  if (!a[l] && !a[r])
    a[u] = 0;
  else if (a[l] > a[r]) {
    a[u] = a[l];
    modify(l);
  } else {
    a[u] = a[r];
    modify(r);
  }
}
int main() {
  dep[0] = -1;
  for (int i = 1; i <= (1 << 21); i++) dep[i] = dep[i >> 1] + 1;
  int t;
  cin >> t;
  while (t--) {
    scanf("%d%d", &h, &g);
    n = (1 << (h + 1)) - 1;
    for (int i = 1; i <= n; i++) a[i] = 0;
    n = (1 << h) - 1;
    for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
    cnt = 0, res = 0;
    n = (1 << g) - 1;
    for (int i = 1; i <= n; i++)
      while (dep[getd(i)] + 1 > g) modify(i), ans[++cnt] = i;
    for (int i = 1; i <= n; i++) res += a[i];
    cout << res << endl;
    for (int i = 1; i <= cnt; i++) printf("%d ", ans[i]);
    puts("");
  }
  return 0;
}
