#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e6 + 6;
bool ok(int id, int g, vector<int> &vec) {
  if (vec[id] == -1) return 0;
  while (1) {
    if (vec[id << 1] == -1 && vec[id << 1 | 1] == -1) {
      return (id >= (1 << g));
    }
    if (vec[id << 1] > vec[id << 1 | 1])
      id = id << 1;
    else
      id = id << 1 | 1;
  }
}
void operate(int id, vector<int> &vec) {
  while (vec[id] != -1) {
    if (vec[id << 1] > vec[id << 1 | 1]) {
      vec[id] = vec[id << 1];
      id = id << 1;
    } else {
      vec[id] = vec[id << 1 | 1];
      id = id << 1 | 1;
    }
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int h, g;
    cin >> h >> g;
    vector<int> vec(1 << (h + 1)), rec;
    for (auto &x : vec) x = -1;
    for (int i = 1; i < (1 << h); i++) cin >> vec[i];
    for (int i = 1; i < (1 << h); i++) {
      while (ok(i, g, vec)) {
        operate(i, vec);
        rec.push_back(i);
      }
    }
    long long int ans = 0;
    for (int i = 1; i < (1 << g); i++) ans += vec[i];
    cout << ans << '\n';
    for (auto x : rec) cout << x << " ";
    cout << '\n';
  }
  return 0;
}
