#include <bits/stdc++.h>
using namespace std;
long long mod = 998244353;
const int maxn = (1 << 20) + 7;
vector<pair<int, int> > ve[maxn];
long long sum;
int ans[maxn], a[maxn], book[maxn], g, h;
void dfs(int x) {
  if (x * 2 > (1 << h) - 1) {
    ve[x].push_back(make_pair(a[x], x));
    return;
  }
  dfs(x * 2);
  dfs(x * 2 + 1);
  int st = 0, en = 0;
  while (st < ((int)(ve[x * 2]).size()) && en < ((int)(ve[x * 2 + 1]).size())) {
    if (ve[x * 2][st] < ve[x * 2 + 1][en])
      ve[x].push_back(ve[x * 2][st]), st++;
    else
      ve[x].push_back(ve[x * 2 + 1][en]), en++;
  }
  while (st < ((int)(ve[x * 2]).size())) ve[x].push_back(ve[x * 2][st]), st++;
  while (en < ((int)(ve[x * 2 + 1]).size()))
    ve[x].push_back(ve[x * 2 + 1][en]), en++;
  ve[x * 2].clear();
  ve[x * 2 + 1].clear();
  ve[x].push_back(make_pair(a[x], x));
  if (x <= (1 << g) - 1) {
    int v = max(ans[x * 2], ans[x * 2 + 1]);
    pair<int, int> zz =
        *upper_bound(ve[x].begin(), ve[x].end(), make_pair(v, (int)1e9));
    ans[x] = zz.first;
    book[zz.second] = 1;
    sum += ans[x];
  }
}
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    scanf("%d %d", &h, &g);
    for (int i = (1); i <= ((1 << h) - 1); i++) scanf("%d", &a[i]);
    dfs(1);
    printf("%lld\n", sum);
    int fla = 0;
    for (int i = ((1 << h) - 1); i >= (1); i--) {
      if (book[i] == 0) {
        if (fla == 0)
          printf("%d", i), fla = 1;
        else
          printf(" %d", i);
      } else
        book[i] = 0;
    }
    printf("\n");
    for (int i = (1); i <= ((1 << h) - 1); i++) ans[i] = 0;
    ve[1].clear();
    sum = 0;
  }
  return 0;
}
