#include <bits/stdc++.h>
using namespace std;
const int N = 5e6 + 5;
int a[N * 2], b[N], dep[N], siz[N], n, m, pos[N], vis[N];
vector<int> vec;
int p;
int del(int x) {
  int now = x, f = 0;
  while (a[now]) {
    if (a[now << 1] < a[now << 1 | 1])
      now = now << 1 | 1;
    else
      now = now << 1;
  }
  now >>= 1;
  p = now;
  if (dep[now] == m && siz[now] == 1) return 0;
  while (p) siz[p]--, p >>= 1;
  while (a[x]) {
    if (a[x << 1] < a[x << 1 | 1])
      a[x] = a[x << 1 | 1], pos[a[x]] = x, x = x << 1 | 1;
    else
      a[x] = a[x << 1], pos[a[x]] = x, x = x << 1;
  }
  return 1;
}
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    vec.clear();
    scanf("%d%d", &n, &m);
    long long sum = 0;
    for (int i = 1; i < (1 << n); i++) {
      scanf("%d", &a[i]), b[i] = a[i];
      sum += a[i];
      pos[a[i]] = i;
      dep[i] = log2(i) + 1 + 1e-6;
      siz[i] = (1 << (n - dep[i] + 1)) - 1;
    }
    sort(b + 1, b + 1 + (1 << n) - 1);
    int res = 1 << n;
    res--;
    int num = res;
    while (res >= (1 << m)) {
      int ans = pos[b[num]];
      while (!del(pos[b[num]])) {
        while (p) vis[a[p]] = 1, p >>= 1;
        num--;
        while (vis[b[num]]) num--;
        ans = pos[b[num]];
      }
      vec.push_back(ans);
      sum -= b[num--];
      res--;
    }
    printf("%lld\n", sum);
    for (auto i : vec) printf("%d ", i);
    printf("\n");
    for (int i = 1; i < (1 << n); i++) vis[a[i]] = 0, a[i] = 0;
  }
  return 0;
}
