#include <bits/stdc++.h>
#pragma GCC optimize("Ofast", "O3", "unroll-loops")
using namespace std;
const long long INF = (long long)(1e18);
const long long inf = 1e9 + 7;
long double eps = 1e-15;
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long lcm(long long a, long long b) { return a * b / gcd(a, b); }
long long mod = 1e9 + 7;
long long pows(long long a, long long b) {
  if (b == 0) return 1;
  if (b % 2 == 0) {
    long long c = pows(a, b / 2);
    return (c * c);
  }
  if (b % 2 == 1) return (a * pows(a, b - 1));
}
int n;
vector<vector<int>> tree;
vector<int> aa, bb;
void maketree(int v) {
  if (v + v + 1 < n) {
    maketree(v + v + 1);
    maketree(v + v + 2);
    merge((tree[v + v + 1]).begin(), (tree[v + v + 1]).end(),
          (tree[v + v + 2]).begin(), (tree[v + v + 2]).end(),
          back_inserter(tree[v]));
  }
  tree[v].push_back(aa[v]);
  if (v + v + 1 < n) {
  }
}
signed main() {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(0);
  int t;
  cin >> t;
  while (t--) {
    int h, g;
    cin >> h >> g;
    n = (1 << h) - 1;
    tree.clear();
    tree.resize(n);
    aa.clear();
    aa.assign(n, 0);
    bb.clear();
    bb.assign(n, 0);
    for (int i = 0; i < n; i++) cin >> aa[i];
    maketree(0);
    long long sum = 0;
    for (int i = (1 << g) - 2; i >= 0; i--) {
      int u = max(bb[i + i + 1], bb[i + i + 2]);
      bb[i] = *(upper_bound((tree[i]).begin(), (tree[i]).end(), u));
      sum += bb[i];
    }
    cout << sum << endl;
    for (int i = 0; i < (1 << g) - 1; i++) {
      while (aa[i] != bb[i]) {
        int j = i;
        cout << i + 1 << " ";
        while (aa[j] != 0) {
          if (j + j + 1 >= n) {
            aa[j] = 0;
            break;
          }
          aa[j] = max(aa[j + j + 1], aa[j + j + 2]);
          if (aa[j + j + 1] > aa[j + j + 2])
            j = j + j + 1;
          else
            j = j + j + 2;
        }
      }
    }
    cout << endl;
  }
}
