#include <bits/stdc++.h>
#pragma GCC optimize "03"
using namespace std;
const long long int N = 3e6 + 5;
const long long int mod = 1e9 + 7;
const long long int inf = 1e9 + 9;
long long int a[N];
long long int h, g, sum;
vector<long long int> t, res;
void dfs2(long long int x) {
  t.push_back(x);
  long long int l = x << 1, r = x << 1 | 1;
  if (a[l] == 0 && a[r] == 0) return;
  if (a[l] > a[r])
    dfs2(l);
  else
    dfs2(r);
}
void dfs(long long int u, long long int hh) {
  while (true) {
    t.clear();
    dfs2(u);
    if (hh + (long long int)t.size() - 1 <= g) break;
    sum -= a[u];
    res.push_back(u);
    for (long long int i = 0; i < (long long int)t.size() - 1; i++)
      a[t[i]] = a[t[i + 1]];
    a[t[(long long int)t.size() - 1]] = 0;
  }
  long long int l = u << 1, r = u << 1 | 1;
  if (a[l] != 0) dfs(l, hh + 1);
  if (a[r] != 0) dfs(r, hh + 1);
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long int t;
  cin >> t;
  while (t--) {
    cin >> h >> g;
    sum = 0;
    for (long long int i = 1; i < (1 << h); i++) cin >> a[i], sum += a[i];
    for (long long int i = (1 << h); i <= (1 << (h + 1)); i++) a[i] = 0;
    dfs(1, 1);
    cout << sum << '\n';
    for (auto i : res) cout << i << " ";
    cout << '\n';
    res.clear();
  }
  return 0;
}
