#include <bits/stdc++.h>
using namespace std;
int cansel_sync = (ios::sync_with_stdio(0), cin.tie(0), 0);
const int N = 2100010;
long long read() {
  long long x;
  if (scanf("%lld", &x) == -1) exit(0);
  return x;
}
int a[N], h, g;
int height(int x) {
  if (a[x] == 0) return 0;
  return height((a[(x * 2)] > a[(x * 2 + 1)] ? (x * 2) : (x * 2 + 1))) + 1;
}
void del(int x) {
  if (a[x] == 0) return;
  a[x] = a[(a[(x * 2)] > a[(x * 2 + 1)] ? (x * 2) : (x * 2 + 1))];
  del((a[(x * 2)] > a[(x * 2 + 1)] ? (x * 2) : (x * 2 + 1)));
}
vector<int> ans;
void dfs(int x, int dep) {
  if (a[x] == 0) return;
  while (height(x) + dep > g) del(x), ans.push_back(x);
  dfs((x * 2), dep + 1);
  dfs((x * 2 + 1), dep + 1);
}
signed main() {
  int T = read();
  while (T--) {
    h = read(), g = read();
    fill(a, a + (1 << h) * 2 + 2, 0);
    ans.clear();
    for (int i = (1), _ = ((1 << h)); i < _; i++) a[i] = read();
    dfs(1, 0);
    long long s = 0;
    for (int i = (1), _ = ((1 << g)); i < _; i++) s += a[i];
    cout << s << endl;
    for (auto i : ans) cout << i << ' ';
    cout << endl;
  }
  return 0;
}
