#include <bits/stdc++.h>
using namespace std;
long long int fastMax(long long int x, long long int y) {
  return (((y - x) >> (32 - 1)) & (x ^ y)) ^ y;
}
long long int fastMin(long long int x, long long int y) {
  return (((y - x) >> (32 - 1)) & (x ^ y)) ^ x;
}
const long long int MAXN = (1 << 20), INF = 1e9;
long long int a[MAXN], depth[MAXN];
void solve() {
  vector<long long int> path, sol;
  long long int sum = 0;
  long long int h, g;
  cin >> h >> g;
  for (long long int i = (1); i <= ((1 << h) - 1); ++i) cin >> a[i];
  sol.clear();
  long long int tot = (1 << h) - (1 << g);
  for (long long int node = 1; node < (1 << g); node++) {
    while (tot > 0) {
      bool flag = true;
      path.clear();
      long long int i = node;
      for (long long int d = depth[node]; d <= h; d++) {
        if (d <= g && a[i] == 0) {
          flag = false;
          break;
        }
        path.push_back(i);
        if (d == h) break;
        if (a[2 * i] == 0 && a[2 * i + 1] == 0) {
          if (d <= g) flag = false;
          break;
        }
        i = a[2 * i] > a[2 * i + 1] ? 2 * i : 2 * i + 1;
      }
      if (!flag) break;
      sol.push_back(node);
      tot--;
      for (long long int j = 0; j + 1 < (long long int)path.size(); j++)
        a[path[j]] = a[path[j + 1]];
      a[path.back()] = 0;
    }
    sum += a[node];
  }
  cout << sum << '\n';
  for (long long int x : sol) cout << x << " ";
  cout << '\n';
}
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  depth[1] = 1;
  for (long long int i = (2); i <= (MAXN - 1); ++i)
    depth[i] = 1 + depth[i >> 1];
  long long int t = 1;
  cin >> t;
  for (long long int i = (1); i <= (t); ++i) {
    solve();
  }
}
