#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e6 + 5e5;
int bin[23] = {1}, a[maxn], p[maxn];
vector<int> ans;
priority_queue<int> que;
int f(int u) {
  if (!a[u << 1] && !a[u << 1 | 1])
    return u;
  else if (a[u << 1] > a[u << 1 | 1])
    return f(u << 1);
  else
    return f(u << 1 | 1);
}
void del(int u) {
  if (!a[u << 1] && !a[u << 1 | 1])
    a[u] = 0;
  else if (a[u << 1] > a[u << 1 | 1])
    a[u] = a[u << 1], p[a[u << 1]] = u, del(u << 1);
  else
    a[u] = a[u << 1 | 1], p[a[u << 1 | 1]] = u, del(u << 1 | 1);
}
int main() {
  int T;
  for (int i = 1; i <= 22; i++) bin[i] = 2 * bin[i - 1];
  for (scanf("%d", &T); T; T--) {
    long long sum = 0;
    int h, g, cnt = 0;
    scanf("%d%d", &h, &g);
    for (int i = 1; i <= bin[h] - 1; i++)
      scanf("%d", &a[i]), p[a[i]] = i, sum += a[i];
    que.push(a[1]);
    while (cnt < bin[h] - bin[g]) {
      int u = que.top();
      que.pop();
      if (f(p[u]) <= bin[g] - 1) {
        que.push(a[p[u] << 1]);
        que.push(a[p[u] << 1 | 1]);
      } else {
        cnt++;
        sum -= u;
        del(p[u]);
        que.push(a[p[u]]);
        ans.push_back(p[u]);
      }
    }
    printf("%lld\n", sum);
    for (int i = 0; i < ans.size(); i++)
      printf("%d%c", ans[i], i == ans.size() - 1 ? '\n' : ' ');
    ans.clear();
    while (!que.empty()) que.pop();
    for (int i = 1; i <= bin[h] - 1; i++) a[i] = 0;
  }
  return 0;
}
