#include <bits/stdc++.h>
using namespace std;
const int maxn = (1 << 21);
int a[maxn + 5];
int b[maxn + 5], POS[maxn + 5];
int lg[maxn + 5];
int main() {
  lg[1] = 1;
  for (int i = (2); i <= (maxn); i++) lg[i] = lg[i >> 1] + 1;
  int CAS;
  scanf("%d", &CAS);
  while (CAS--) {
    int h, g;
    scanf("%d%d", &h, &g);
    int n = (1 << h) - 1;
    long long sum = 0;
    for (int i = (1); i <= (n * 2 + 1); i++) a[i] = 0;
    for (int i = (1); i <= (n); i++) scanf("%d", &a[i]), sum += a[i];
    for (int i = (1); i <= (n); i++) b[i] = a[i], POS[a[i]] = i;
    sort(b + 1, b + n + 1);
    int cnt = 0;
    vector<int> ANS;
    for (int i = (n); i >= (1); i--) {
      if (cnt == (1 << h) - (1 << g)) break;
      int x = b[i], pos = POS[x];
      int now = pos;
      while (a[now * 2] || a[now * 2 + 1]) {
        if (a[now * 2] > a[now * 2 + 1])
          now = now * 2;
        else
          now = now * 2 + 1;
      }
      if (now >= (1 << g)) {
        cnt++;
        now = pos;
        sum -= x;
        ANS.push_back(now);
        a[now] = 0;
        POS[x] = 0;
        while (a[now * 2] || a[now * 2 + 1]) {
          int nxt = -1;
          if (a[now * 2] > a[now * 2 + 1])
            nxt = now * 2;
          else
            nxt = now * 2 + 1;
          int X = a[nxt];
          swap(a[now], a[nxt]);
          POS[X] = now;
          now = nxt;
        }
      }
    }
    printf("%lld\n", sum);
    assert((int)ANS.size() == (1 << h) - (1 << g));
    for (int i = (1); i <= ((1 << h) - (1 << g)); i++)
      printf("%d%c", ANS[i - 1], " \n"[i == (1 << h) - (1 << g)]);
  }
  return 0;
}
