#include <bits/stdc++.h>
using namespace std;
const int maxn = (1 << 21) + 5;
const int INF = 0x3f3f3f3f;
const int mod = 998244353;
int a[maxn];
int get_id(int x) {
  if (!a[x << 1] && !a[x << 1 | 1]) return x;
  if (a[x << 1] > a[x << 1 | 1])
    return get_id(x << 1);
  else
    return get_id(x << 1 | 1);
}
void dfs(int x) {
  if (!a[x << 1] && !a[x << 1 | 1])
    a[x] = 0;
  else if (a[x << 1] > a[x << 1 | 1]) {
    a[x] = a[x << 1];
    dfs(x << 1);
  } else {
    a[x] = a[x << 1 | 1];
    dfs(x << 1 | 1);
  }
}
void solve() {
  int h, g;
  scanf("%d%d", &h, &g);
  for (int i = 1; i < 1 << (h + 1); i++) a[i] = 0;
  for (int i = 1; i < 1 << h; i++) scanf("%d", &a[i]);
  vector<int> ans;
  int limit = (1 << g) - 1;
  for (int i = 1; i < 1 << g; i++)
    while (get_id(i) > limit) {
      ans.push_back(i);
      dfs(i);
    }
  long long sum = 0;
  for (int i = 1; i < 1 << g; i++) sum += a[i];
  printf("%lld\n", sum);
  for (auto i : ans) printf("%d ", i);
  puts("");
}
int main() {
  int t;
  for (scanf("%d", &t); t >= 1; t--) solve();
}
