#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma comment(linker, "/STACK:2000000")
using namespace std;
using ll = long long;
using db = long double;
using ii = pair<int, int>;
const int N = 3e5 + 5, LG = 19, MOD = 998244353;
const int SQ = 320;
const long double EPS = 1e-7;
int d, m, t;
bool deletable[1 << 22];
int h, g, a[1 << 22];
int pos[1 << 22];
void dfs(int node, int height) {
  if (height > g) return;
  deletable[node] = false;
  dfs(node << 1, height + 1);
  dfs(node << 1 | 1, height + 1);
}
int get(int x) {
  if (a[x << 1] == 0 && a[x << 1 | 1] == 0)
    return x;
  else {
    if (a[x << 1] > a[x << 1 | 1])
      return get(x << 1);
    else
      return get(x << 1 | 1);
  }
  return -1;
}
void apply(int x) {
  if (a[x << 1] == 0 && a[x << 1 | 1] == 0) {
    a[x] = 0;
  } else {
    a[x] = max(a[x << 1], a[x << 1 | 1]);
    pos[a[x]] = x;
    if (a[x << 1] > a[x << 1 | 1])
      return apply(x << 1);
    else
      return apply(x << 1 | 1);
  }
}
vector<int> vt;
int ptr;
vector<int> ans;
bool kill() {
  assert(ptr < vt.size());
  int x = vt[ptr++];
  int y = get(pos[x]);
  if (y == -1) {
    cout << -1 << '\n';
    exit(0);
  }
  if (deletable[y]) {
    ans.push_back(pos[x]);
    apply(pos[x]);
    return true;
  } else
    return false;
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> t;
  while (t--) {
    cin >> h >> g;
    for (int i = 1; i < 1 << h; i++) {
      cin >> a[i];
      a[i + i] = a[i + i + 1] = 0;
      pos[a[i]] = i;
      vt.push_back(a[i]);
    }
    sort(vt.rbegin(), vt.rend());
    fill(deletable, deletable + (1 << h), 1);
    dfs(1, 1);
    int cur = (1 << h);
    while (cur != (1 << g)) {
      cur -= kill();
    }
    cout << accumulate(a, a + (1 << h), 0ll) << '\n';
    for (auto x : ans) cout << x << ' ';
    cout << '\n';
    ans.clear();
    vt.clear();
    ptr = 0;
  }
  return 0;
}
