#include <bits/stdc++.h>
template <class T>
inline void read(T &res) {
  res = 0;
  bool bo = 0;
  char c;
  while (((c = getchar()) < '0' || c > '9') && c != '-')
    ;
  if (c == '-')
    bo = 1;
  else
    res = c - 48;
  while ((c = getchar()) >= '0' && c <= '9')
    res = (res << 3) + (res << 1) + (c - 48);
  if (bo) res = ~res + 1;
}
const int N = (1 << 20) + 5;
int h, g, a[N], dep[N], tot, res[N];
long long ans;
int get_right(int p) {
  if (p >= (1 << h - 1) || (!a[p << 1] && !a[p << 1 | 1])) return dep[p];
  return a[p << 1] > a[p << 1 | 1] ? get_right(p << 1) : get_right(p << 1 | 1);
}
void del(int p) {
  if (p >= (1 << h - 1) || (!a[p << 1] && !a[p << 1 | 1]))
    return (void)(a[p] = 0);
  if (a[p << 1] > a[p << 1 | 1])
    a[p] = a[p << 1], del(p << 1);
  else
    a[p] = a[p << 1 | 1], del(p << 1 | 1);
}
void dfs(int p) {
  if (dep[p] > g) return;
  while (get_right(p) > g) res[++tot] = p, ans -= a[p], del(p);
  dfs(p << 1);
  dfs(p << 1 | 1);
}
void work() {
  read(h);
  read(g);
  ans = 0;
  tot = 0;
  for (int i = 1; i < (1 << h); i++) read(a[i]), ans += a[i];
  for (int i = 1; i <= h; i++)
    for (int p = 1 << i - 1; p < (1 << i); p++) dep[p] = i;
  dfs(1);
  printf("%lld\n", ans);
  for (int i = 1; i <= tot; i++) printf("%d ", res[i]);
  puts("");
}
int main() {
  int T;
  read(T);
  while (T--) work();
  return 0;
}
