#include <bits/stdc++.h>
using namespace std;
int num[4000005], id[4000005];
pair<int, int> a[2000005];
long long ans;
vector<int> vt;
int query(int x) {
  if (!num[x * 2] && !num[x * 2 + 1]) return x;
  if (num[x * 2] > num[x * 2 + 1])
    return query(x * 2);
  else
    return query(x * 2 + 1);
}
void del(int x) {
  if (!num[x * 2] && !num[x * 2 + 1]) {
    ans -= num[x];
    num[x] = 0;
    return;
  }
  if (num[x * 2] > num[x * 2 + 1]) {
    swap(num[x], num[x * 2]);
    swap(id[num[x]], id[num[x * 2]]);
    del(x * 2);
  } else {
    swap(num[x], num[x * 2 + 1]);
    swap(id[num[x]], id[num[x * 2 + 1]]);
    del(x * 2 + 1);
  }
}
int main() {
  int cases;
  scanf("%d", &cases);
  for (; cases; cases--) {
    int h, g;
    scanf("%d%d", &h, &g);
    memset(num, 0, sizeof(int) * ((1 << (h + 1)) + 5));
    ans = 0;
    for (int i = 1; i < (1 << h); i++) {
      scanf("%d", &num[i]);
      a[i] = pair<int, int>(num[i], i);
      id[num[i]] = i;
      ans += num[i];
    }
    vt.clear();
    sort(a + 1, a + (1 << h));
    int r = (1 << h) - 1;
    for (int i = 1; i <= (1 << h) - (1 << g); i++) {
      while (query(id[a[r].first]) < (1 << g)) r--;
      vt.push_back(id[a[r].first]);
      del(id[a[r].first]);
      r--;
    }
    printf("%lld\n", ans);
    for (int i = 0; i < vt.size(); i++) printf("%d ", vt[i]);
    printf("\n");
  }
  return 0;
}
