#include <bits/stdc++.h>
using namespace std;
const long long int N = (1 << 22) + 10;
long long int h, g, kata;
long long int a[N];
vector<long long int> v;
void dfs(long long int root, long long int ht) {
  if (!a[2 * root] && !a[2 * root + 1]) {
    kata = ht;
    return;
  }
  if (a[2 * root] > a[2 * root + 1])
    dfs(2 * root, ht + 1);
  else
    dfs(2 * root + 1, ht + 1);
}
void change(long long int root) {
  if (!a[2 * root] && !a[2 * root + 1]) {
    a[root] = 0;
    return;
  }
  if (a[2 * root] > a[2 * root + 1])
    a[root] = a[2 * root], change(2 * root);
  else
    a[root] = a[2 * root + 1], change(2 * root + 1);
}
void go(long long int root, long long int ht) {
  if (!a[root]) return;
  while (1) {
    dfs(root, ht);
    if (kata >= g)
      v.push_back(root), change(root);
    else
      break;
  }
  go(2 * root, ht + 1);
  go(2 * root + 1, ht + 1);
}
void solve() {
  cin >> h >> g;
  for (long long int i = 1; i < (1 << (h + 1)); i++) a[i] = 0;
  v.clear();
  for (long long int i = 1; i < (1 << h); i++) cin >> a[i];
  go(1, 0);
  long long int ans = 0;
  for (long long int i = 1; i < (1 << g); i++) ans += a[i];
  cout << ans << '\n';
  for (auto i : v) cout << i << " ";
  cout << '\n';
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  cout.precision(20);
  long long int t = 1;
  cin >> t;
  for (long long int i = 1; i <= t; i++) {
    if (0) cout << "Case #" << i << ": ";
    solve();
  }
}
