#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    int h, g;
    cin >> h >> g;
    int n = (int)(pow(2, h) - 1);
    int m = (int)(pow(2, g) - 1);
    vector<int> a(n + 1);
    for (int i = 1; i <= n; i++) {
      cin >> a[i];
    }
    vector<int> ans;
    for (int i = 0; i < g; i += 0) {
      int start = (int)(pow(2, i));
      int end = (int)(pow(2, i + 1) - 1);
      for (int j = start; j <= end; j += 0) {
        long long int flag = 0;
        int z = 1;
        int start1 = (int)(pow(2, g - 1));
        int end1 = (int)(pow(2, g) - 1);
        int m;
        int j1 = j;
        while (z == 1) {
          if (j1 >= start1 && j1 <= end1) {
            m = max(a[2 * j1], a[2 * j1 + 1]);
            z = 0;
            break;
          } else {
            if (a[2 * j1] > a[2 * j1 + 1]) {
              j1 = 2 * j1;
            } else {
              j1 = 2 * j1 + 1;
            }
          }
        }
        if (m == 0) {
          flag = 0;
        } else {
          flag = 1;
        }
        if (flag == 1) {
          ans.push_back(j);
          int z1 = 1;
          int start2 = (int)(pow(2, h - 1));
          int end2 = (int)(pow(2, h) - 1);
          int j2 = j;
          while (z1 == 1) {
            if (j2 >= start2 && j2 <= end2) {
              z1 = 0;
              a[j2] = 0;
              break;
            }
            int mm = max(a[2 * j2], a[2 * j2 + 1]);
            if (mm == 0) {
              z1 = 0;
              a[j2] = 0;
              break;
            } else {
              a[j2] = mm;
              if (a[2 * j2] > a[2 * j2 + 1]) {
                j2 = 2 * j2;
              } else {
                j2 = 2 * j2 + 1;
              }
            }
          }
        } else {
          j += 1;
        }
      }
      if (ans.size() == n - m) {
        break;
      }
      i += 1;
    }
    long long int sum = 0;
    for (int i = 1; i <= (int)(pow(2, g) - 1); i++) {
      sum += a[i];
    }
    cout << sum << endl;
    for (int i = 0; i < ans.size(); i++) {
      cout << ans[i] << " ";
    }
    cout << endl;
  }
  return 0;
}
