#include <bits/stdc++.h>
using namespace std;
const int MAX = (1 << 20) + 20;
int h, g, n;
long long total;
priority_queue<int> cola[MAX];
int arr[MAX], opt[MAX], val[MAX];
bool used[MAX];
int aux;
void merge(int node) {
  while (!cola[node << 1].empty()) {
    aux = cola[node << 1].top();
    cola[node << 1].pop();
    cola[node].push(aux);
  }
  while (!cola[node << 1 | 1].empty()) {
    aux = cola[node << 1 | 1].top();
    cola[node << 1 | 1].pop();
    cola[node].push(aux);
  }
}
void query(int node, int hh) {
  if (hh == h) {
    cola[node].push(arr[node] * -1);
    return;
  }
  query(node << 1, hh + 1);
  query(node << 1 | 1, hh + 1);
  merge(node);
  cola[node].push(arr[node] * -1);
  int lim;
  if (hh <= g) {
    if (hh < g) {
      lim = max(opt[node << 1], opt[node << 1 | 1]);
      while ((cola[node].top() * -1) < lim) cola[node].pop();
    }
    opt[node] = cola[node].top() * -1;
    used[val[cola[node].top() * -1]] = true;
    cola[node].pop();
    total += opt[node];
  }
}
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    scanf("%d %d", &h, &g);
    n = 1 << h;
    n--;
    for (long long i = (long long)(1); i < (long long)(n + 1); i++)
      used[i] = false;
    total = 0;
    for (long long i = (long long)(1); i < (long long)(n + 1); i++) {
      scanf("%d", &arr[i]);
      val[arr[i]] = i;
    }
    query(1, 1);
    while (!cola[1].empty()) cola[1].pop();
    printf("%lld\n", total);
    bool pri = true;
    for (int i = n; i >= 1; i--)
      if (!used[i]) {
        if (!pri)
          printf(" ");
        else
          pri = false;
        printf("%d", i);
      }
    printf("\n");
  }
}
