#include <bits/stdc++.h>
using namespace std;
long long gesas;
long long inf = 100000000000000000LL;
long long modulo = pow(10, 9) + 7;
double eps = 1e-6;
const long long int mod = 998244353;
long long n;
const int kons = 500001;
vector<long long> values(2000, 0);
long long mini = 10000000;
vector<vector<int> > dp;
vector<pair<pair<int, int>, int> > queries;
vector<int> adj[200];
long long a[kons];
vector<long long> birincitree;
long long min(long long a, long long b) { return a < b ? a : b; }
const int N = 3e5 + 7;
bool ifpossible(int node) {
  while (node) {
    if (((2 * node >= (int)(birincitree.size()) ||
          birincitree[2 * node] == 0) &&
         2 * node + 1 < (int)(birincitree.size()) &&
         birincitree[2 * node + 1] != 0) ||
        ((2 * node + 1 >= (int)(birincitree.size()) ||
          birincitree[2 * node + 1] == 0) &&
         2 * node < (int)(birincitree.size()) && birincitree[2 * node] != 0))
      return 1;
    if ((2 * node >= (int)(birincitree.size()) || birincitree[2 * node] == 0) &&
        (2 * node + 1 >= (int)(birincitree.size()) ||
         birincitree[2 * node + 1] == 0)) {
      if (node < gesas)
        return 0;
      else
        return 1;
    } else {
      node = (birincitree[2 * node + 1] >= birincitree[2 * node]) ? 2 * node + 1
                                                                  : 2 * node;
    }
  }
}
bool justdoit(int node) {
  if ((2 * node >= (int)(birincitree.size()) || birincitree[2 * node] == 0) &&
      (2 * node + 1 >= (int)(birincitree.size()) ||
       birincitree[2 * node + 1] == 0)) {
    birincitree[node] = 0;
    return 1;
  }
  birincitree[node] = (birincitree[2 * node + 1] >= birincitree[2 * node])
                          ? birincitree[2 * node + 1]
                          : birincitree[2 * node];
  return (birincitree[2 * node + 1] >= birincitree[2 * node])
             ? justdoit(2 * node + 1)
             : justdoit(2 * node);
}
int main() {
  long long k, z, h, g, m, t, b, c, e, f, x, y, x1, y1, x2, y2;
  string he = "YES", yox = "NO";
  cin >> t;
  while (t--) {
    cin >> h >> g;
    int hesas = 1;
    gesas = 1;
    for (int i1 = 0; i1 < h; ++i1) {
      hesas *= 2;
    }
    for (int k1 = 0; k1 < g; ++k1) {
      gesas *= 2;
    }
    birincitree.resize(hesas, 0);
    for (long long j = 1; j < hesas; ++j) scanf("%d", &birincitree[j]);
    int counter = 0;
    vector<long long> operations;
    for (int j = 1; j < hesas; j++) {
      if (counter >= hesas - gesas) break;
      while (ifpossible(j)) {
        operations.push_back(j);
        justdoit(j);
        counter++;
      }
    }
    long long sum = 0;
    for (int i = 0; i < hesas; i++) {
      sum += birincitree[i];
    }
    printf("%lld\n", sum);
    for (int k = 0; k < (int)(operations.size()); k++)
      printf("%d ", operations[k]);
    printf("\n");
  }
}
