#include <bits/stdc++.h>
using namespace std;
const int maxn = (1 << 21) + 10;
long long a[maxn];
int func(int i) {
  int lv = log2(i) + 1;
  while (a[i * 2] != 0 || a[i * 2 + 1] != 0) {
    if (a[i * 2] > a[i * 2 + 1])
      lv++, i = i * 2;
    else
      lv++, i = i * 2 + 1;
  }
  return lv;
}
void f(int i) {
  if (a[i * 2] == 0 && a[i * 2 + 1] == 0)
    a[i] = 0;
  else if (a[i * 2] > a[i * 2 + 1]) {
    a[i] = a[i * 2];
    f(i * 2);
  } else {
    a[i] = a[i * 2 + 1];
    f(i * 2 + 1);
  }
}
int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  int test;
  cin >> test;
  int h, g;
  while (test--) {
    cin >> h >> g;
    for (int i = 0; i <= (1 << (h + 1)); i++) a[i] = 0;
    for (int i = 1; i < (1 << h); i++) cin >> a[i];
    priority_queue<pair<long long, int> > q;
    q.push(pair<long long, int>(a[1], 1));
    vector<int> s;
    for (int i = 1; i <= (1 << h) - (1 << g);) {
      auto p = q.top();
      q.pop();
      int v = func(p.second);
      if (v <= g) {
        q.push(pair<long long, int>(a[p.second * 2], p.second * 2));
        q.push(pair<long long, int>(a[p.second * 2 + 1], p.second * 2 + 1));
        continue;
      }
      i++;
      s.push_back(p.second);
      f(p.second);
      q.push(pair<long long, int>(a[p.second], p.second));
    }
    long long sol = 0;
    for (int i = 1; i < (1 << h); i++) sol += a[i];
    cout << sol << "\n";
    for (int i = 0; i < (int)s.size(); i++)
      cout << s[i] << " \n"[i == (int)s.size() - 1];
  }
  return 0;
}
