#include <bits/stdc++.h>
using namespace std;
long long power(long long x, long long y, long long z) {
  long long ret = 1;
  while (y > 0) {
    if (y & 1) ret = (ret * x) % z;
    x = (x * x) % z;
    y >>= 1;
  }
  return ret;
}
int main() {
  long long t;
  scanf("%lld", &t);
  while (t--) {
    long long h, g, tot = 0;
    scanf("%lld%lld", &h, &g);
    long long n = (1LL << h) - 1, m = (1LL << g) - 1;
    vector<long long> A(n + 1), ans;
    for (long long i = 1; i <= n; ++i) scanf("%lld", &A[i]), tot += A[i];
    for (long long i = 1; i <= n;) {
      if (!A[i]) {
        ++i;
        continue;
      }
      long long rem = i;
      while (true) {
        long long lc = rem << 1, rc = rem << 1 | 1;
        if (lc > n)
          break;
        else if (!A[lc] && !A[rc])
          break;
        else {
          if (A[lc] > A[rc])
            rem = lc;
          else
            rem = rc;
        }
      }
      if (rem > m) {
        tot -= A[i];
        ans.push_back(i);
        rem = i;
        while (true) {
          long long lc = rem << 1, rc = rem << 1 | 1;
          if (lc > n)
            break;
          else if (!A[lc] && !A[rc])
            break;
          else {
            if (A[lc] > A[rc])
              rem = lc;
            else
              rem = rc;
            A[rem / 2] = A[rem];
          }
        }
        A[rem] = 0;
      } else
        ++i;
    }
    printf("%lld\n", tot);
    for (auto u : ans) printf("%lld ", u);
    printf("\n");
  }
  return 0;
}
