#include <bits/stdc++.h>
using namespace std;
const int K = 22;
const int N = (1 << K);
int h, g, n;
int a[N];
vector<int> ans;
bool ok(int pos, int c) {
  if (!a[2 * pos] && !a[2 * pos + 1]) {
    return false;
  } else if (c == g) {
    return true;
  } else {
    return ok(2 * pos + (a[2 * pos + 1] > a[2 * pos]), c + 1);
  }
}
void kek(int pos) {
  a[pos] = max(a[2 * pos], a[2 * pos + 1]);
  if (a[2 * pos] || a[2 * pos + 1]) {
    kek(2 * pos + (a[2 * pos + 1] > a[2 * pos]));
  }
}
long long f(int pos, int c = 1) {
  if (c == g + 1) {
    return 0;
  }
  long long res = 0;
  while (ok(pos, c)) {
    res += a[pos];
    ans.push_back(pos);
    kek(pos);
  }
  return res + f(2 * pos, c + 1) + f(2 * pos + 1, c + 1);
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    cin >> h >> g;
    n = (1 << h) - 1;
    long long s = 0;
    for (int i = 1; i <= n; ++i) {
      cin >> a[i];
      s += a[i];
    }
    cout << s - f(1) << '\n';
    for (int i : ans) {
      cout << i << ' ';
    }
    cout << '\n';
    fill(a, a + n, 0);
    ans.clear();
  }
  return 0;
}
