#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
long long expo_pow(long long x, long long y) {
  y = y % (mod - 1);
  if (y == 0) y = mod - 1;
  long long res = 1;
  while (y) {
    if (y & 1) res = (res * x) % mod;
    x = (x * x) % mod;
    y >>= 1;
  }
  return res;
}
vector<long long> heap;
long long g, h;
long long func(long long idx) {
  long long tmp = heap[idx];
  long long left = 2 * idx;
  long long right = 2 * idx + 1;
  heap[idx] = 0;
  vector<long long> qq;
  qq.push_back(idx);
  while (idx < (1 << h) and heap[idx] < max(heap[left], heap[right])) {
    if (heap[left] > heap[right]) {
      swap(heap[left], heap[idx]);
      idx = left;
    } else {
      swap(heap[right], heap[idx]);
      idx = right;
    }
    left = 2 * idx;
    right = 2 * idx + 1;
    qq.push_back(idx);
  }
  if (idx < (1 << g)) {
    for (long long i = qq.size() - 1; i > 0; i--) {
      heap[qq[i]] = heap[qq[i - 1]];
    }
    heap[qq[0]] = tmp;
    return 0;
  }
  return tmp;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long test;
  cin >> test;
  while (test--) {
    cin >> h >> g;
    heap.clear();
    heap.assign(2 * (1 << h) + 2, 0);
    long long sum = 0;
    for (long long i = 1; i < (1 << h); i++) {
      cin >> heap[i];
      sum += heap[i];
    }
    vector<long long> ans;
    for (long long i = 1; i < (1 << g); i++) {
      while (1) {
        long long tmp = func(i);
        if (tmp) {
          sum -= tmp;
          ans.push_back(i);
        } else
          break;
      }
    }
    cout << sum << "\n";
    for (auto u : ans) cout << u << " ";
    cout << "\n";
  }
}
