#include <bits/stdc++.h>
using namespace std;
int a[1 << 20], b[1 << 20];
bool vis[1 << 20];
int n, m;
pair<int, int> calc(int x, int lim) {
  if (x > n || a[x] <= lim) {
    return make_pair(1e9, x);
  }
  pair<int, int> res = make_pair(a[x], x);
  res = min(res, calc(x << 1, lim));
  res = min(res, calc(x << 1 | 1, lim));
  return res;
}
int main() {
  ios::sync_with_stdio(0);
  int t;
  cin >> t;
  while (t--) {
    int h, g;
    cin >> h >> g;
    n = (1 << h) - 1;
    m = (1 << g) - 1;
    for (int i = 1; i <= n; ++i) {
      cin >> a[i];
      vis[i] = 0;
    }
    long long int ans = 0;
    for (int i = m; i >= 1; --i) {
      int lim;
      if (i * 2 > m) {
        lim = 0;
      } else {
        lim = max(b[i << 1], b[i << 1 | 1]);
      }
      pair<int, int> res = calc(i, lim);
      b[i] = res.first;
      ans += b[i];
      vis[res.second] = true;
    }
    cout << ans << "\n";
    for (int i = n; i >= 1; --i) {
      if (!vis[i]) {
        cout << i << " ";
      }
    }
    cout << "\n";
  }
  return 0;
}
