#include <bits/stdc++.h>
using namespace std;
long long arr[20000020];
long long del(long long i, long long k) {
  long long val = arr[i];
  long long left = 2 * i;
  long long right = 2 * i + 1;
  if (arr[left] == 0 && arr[right] == 0) {
    arr[i] = 0;
    if (i < k) {
      return val;
    } else {
      return 0;
    }
  } else {
    if (arr[left] > arr[right]) {
      long long l = arr[left];
      long long x = del(left, k);
      if (x) {
        arr[i] = 0;
        return val + x;
      } else {
        arr[i] = l;
        return 0;
      }
    } else {
      long long l = arr[right];
      long long x = del(right, k);
      if (x) {
        arr[i] = 0;
        return val + x;
      } else {
        arr[i] = l;
        return 0;
      }
    }
  }
}
int main() {
  long long T;
  scanf("%lld", &T);
  ;
  while (T--) {
    long long J, K;
    scanf("%lld", &J);
    ;
    scanf("%lld", &K);
    ;
    long long qq = (1 << (J + 1));
    long long n = 1 << J;
    long long m = 1 << K;
    long long dif = n - m;
    long long sum = 0;
    for (long long i = n; i < qq; i++) {
      arr[i] = 0;
    }
    for (long long i = 1; i <= n - 1; i++) {
      long long in;
      scanf("%lld", &in);
      ;
      arr[i] = in;
    }
    vector<long long> ans;
    for (long long i = 1; i < m;) {
      if (arr[i] == 0) {
        i++;
        continue;
      }
      long long x = del(i, m);
      if (x) {
        sum += x;
        i++;
      } else {
        ans.push_back(i);
      }
    }
    cout << sum << "\n";
    for (auto i : ans) {
      cout << i << " ";
    }
    puts("");
  }
}
