#include <bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
long long int M = 1000000007;
long long int inv(long long int a, long long int b) {
  return 1 < a ? b - inv(b % a, a) * b / a : 1;
}
long long int gcd(long long int x, long long int y) {
  if (y == 0) return x;
  return gcd(y, x % y);
}
pair<long long int, long long int> neigh[4] = {
    {-1, 0}, {1, 0}, {0, 1}, {0, -1}};
long long int a[2100000];
long long int loc[2100000];
long long int n;
long long int lower;
vector<long long int> insert(vector<long long int> &a,
                             vector<long long int> &b) {
  vector<long long int> c(a.size() + b.size());
  long long int c1 = 0, c2 = 0, c3 = 0;
  while (c1 < a.size() && c2 < b.size()) {
    if (a[c1] < b[c2])
      c[c3++] = a[c1++];
    else
      c[c3++] = b[c2++];
  }
  while (c1 < a.size()) c[c3++] = a[c1++];
  while (c2 < b.size()) c[c3++] = b[c2++];
  return c;
}
pair<vector<long long int>, vector<long long int> > solve(long long int i) {
  vector<long long int> v;
  v.push_back(a[i]);
  if (i * 2 + 1 >= n) {
    return {vector<long long int>(), v};
  }
  auto one = solve(i * 2 + 1);
  auto two = solve(i * 2 + 2);
  vector<long long int> m1 = one.first;
  vector<long long int> m2 = two.first;
  vector<long long int> s1 = one.second;
  vector<long long int> s2 = two.second;
  if (i >= lower) {
    m1 = insert(m1, m2);
    s1 = insert(s1, s2);
    s1 = insert(s1, v);
    while (s1.size() > 1) {
      vector<long long int> tmp;
      tmp.push_back(s1[s1.size() - 1]);
      m1 = insert(m1, tmp);
      s1.erase(prev(s1.end()));
    }
    return {m1, s1};
  }
  long long int largest1 = *prev(s1.end());
  long long int largest2 = *prev(s2.end());
  auto nex1 = lower_bound(m1.begin(), m1.end(), largest2);
  auto nex2 = lower_bound(m2.begin(), m2.end(), largest1);
  bool e1 = nex1 == m1.end();
  bool e2 = nex2 == m2.end();
  long long int val1 = LLONG_MAX;
  if (!e1) val1 = *nex1;
  if (val1 < largest1) val1 = *lower_bound(m1.begin(), m1.end(), largest1);
  long long int val2 = LLONG_MAX;
  if (!e2) val2 = *nex2;
  if (val2 < largest2) val2 = *lower_bound(m2.begin(), m2.end(), largest2);
  m1 = insert(m1, m2);
  s1 = insert(s1, s2);
  if (val2 < val1) {
    m1.erase(lower_bound(m1.begin(), m1.end(), val2));
    vector<long long int> tmp;
    tmp.push_back(val2);
    s1 = insert(s1, tmp);
  } else {
    m1.erase(lower_bound(m1.begin(), m1.end(), val1));
    vector<long long int> tmp;
    tmp.push_back(val1);
    s1 = insert(s1, tmp);
  }
  m1 = insert(m1, v);
  return {m1, s1};
}
void prod(long long int i) {
  long long int left = 2 * i + 1;
  long long int right = 2 * i + 2;
  if (a[left] == 0 && a[right] == 0)
    a[i] = 0;
  else {
    if (a[left] > a[right]) {
      a[i] = a[left];
      loc[a[left]] = i;
      prod(left);
    } else {
      a[i] = a[right];
      loc[a[right]] = i;
      prod(right);
    }
  }
}
long long int pow2[2100000];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  pow2[0] = 1;
  for (long long int i = 1; i < (23); i++) {
    pow2[i] = pow2[i - 1] * 2;
  }
  long long int _t;
  cin >> _t;
  for (long long int _t1 = 0; _t1 < (_t); _t1++) {
    long long int h;
    cin >> h;
    long long int g;
    cin >> g;
    n = pow2[h] - 1;
    lower = pow2[g - 1] - 1;
    for (long long int i = 0; i < (n); i++) cin >> a[i], loc[a[i]] = i;
    for (long long int i = n; i < (2 * n + 1); i++) a[i] = 0;
    vector<long long int> res;
    long long int sum = 0;
    auto ret = solve(0);
    vector<long long int> free = ret.first;
    vector<long long int> used = ret.second;
    for (long long int i = 0; i < (used.size()); i++) sum += used[i];
    for (long long int i = 0; i < (free.size()); i++) {
      long long int u = free[i];
      res.push_back(loc[u] + 1);
      prod(loc[u]);
    }
    cout << sum << endl;
    for (long long int _i = 0; _i < (res.size()); _i++)
      cout << (res)[_i] << " ";
    cout << endl;
  }
}
