#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
using namespace std;
long long N = 1e9 + 7, M = 1000, N2 = 1234567891;
long long i, j;
long long x, y, z, n, m, k, l, r, t, ma = 0, x2, y2, x3, y3, x4, y4, k2, f,
                                     a[4010000], d[4110000];
long long ans, ans2;
string s, s2, s3;
vector<long long> v2;
struct pos {
  long long x, y;
};
vector<pos> v;
bool operator<(pos l, pos r) { return l.x < r.x; }
void tr(int x) {
  if (!a[x * 2] && !a[x * 2 + 1]) {
    if (d[x] + 1 <= m) f = 1;
    return;
  } else if (a[x * 2] < a[x * 2 + 1])
    tr(x * 2 + 1);
  else
    tr(x * 2);
}
void F(int x) {
  if (!a[x * 2] && !a[x * 2 + 1]) {
    a[x] = 0;
  } else if (a[x * 2] < a[x * 2 + 1]) {
    a[x] = a[x * 2 + 1];
    F(x * 2 + 1);
  } else {
    a[x] = a[x * 2];
    F(x * 2);
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int qu;
  cin >> qu;
  while (qu--) {
    cin >> n >> m;
    for (i = 1; i < (1 << (n + 1)); i++) a[i] = 0, d[i] = 0;
    for (i = 1; i < (1 << n); i++) {
      cin >> a[i];
    }
    k = (1 << n);
    k2 = (1 << m);
    queue<int> q;
    v2.clear();
    l = 1;
    ans = 0;
    d[0] = -1;
    for (i = 1; i < k; i++) {
      d[i] = d[i / 2] + 1;
    }
    for (i = k2; i < k; i++) {
      f = 0;
      tr(l);
      if (f) {
        if (d[l] + 1 < m) {
          q.push(l * 2);
          q.push(l * 2 + 1);
        }
        l = q.front();
        q.pop();
        i--;
        continue;
      }
      if (!f) {
        v2.push_back(l);
        F(l);
      }
    }
    for (i = 1; i < k2; i++) ans += a[i];
    cout << ans << endl;
    for (i = 0; i < v2.size(); i++) cout << v2[i] << ' ';
    cout << endl;
  }
}
