#include <bits/stdc++.h>
#define MP make_pair
#define PB push_back
#define int long long
#define st first
#define nd second
#define rd third
#define FOR(i, a, b) for(int i =(a); i <=(b); ++i)
#define RE(i, n) FOR(i, 1, n)
#define FORD(i, a, b) for(int i = (a); i >= (b); --i)
#define REP(i, n) for(int i = 0;i <(n); ++i)
#define VAR(v, i) __typeof(i) v=(i)
#define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define __builtin_ctz __builtin_ctzll
#define __builtin_clz __builtin_clzll
#define __builtin_popcount __builtin_popcountll
using namespace std;
template<typename TH> void _dbg(const char* sdbg, TH h) { cerr<<sdbg<<"="<<h<<"\n"; }
template<typename TH, typename... TA> void _dbg(const char* sdbg, TH h, TA... t) {
while(*sdbg != ',') { cerr<<*sdbg++; } cerr<<"="<<h<<","; _dbg(sdbg+1, t...);
}
#ifdef LOCAL
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#define debugv(x) {{cerr <<#x <<" = "; FORE(itt, (x)) cerr <<*itt <<", "; cerr <<"\n"; }}
#else
#define debug(...) (__VA_ARGS__)
#define debugv(x)
#define cerr if(0)cout
#endif
#define next ____next
#define prev ____prev
#define left ____left
#define hash ____hash
typedef long long ll;
typedef long double LD;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VLL;
typedef vector<pair<int, int> > VPII;
typedef vector<pair<ll, ll> > VPLL;
template<class C> void mini(C&a4, C b4){a4=min(a4, b4); }
template<class C> void maxi(C&a4, C b4){a4=max(a4, b4); }
template<class T1, class T2>
ostream& operator<< (ostream &out, pair<T1, T2> pair) { return out << "(" << pair.first << ", " << pair.second << ")";}
template<class A, class B, class C> struct Triple { A first; B second; C third;
bool operator<(const Triple& t) const { if (st != t.st) return st < t.st; if (nd != t.nd) return nd < t.nd; return rd < t.rd; } };
template<class T> void ResizeVec(T&, vector<int>) {}
template<class T> void ResizeVec(vector<T>& vec, vector<int> sz) {
vec.resize(sz[0]); sz.erase(sz.begin()); if (sz.empty()) { return; }
for (T& v : vec) { ResizeVec(v, sz); }
}
typedef Triple<int, int, int> TIII;
template<class A, class B, class C>
ostream& operator<< (ostream &out, Triple<A, B, C> t) { return out << "(" << t.st << ", " << t.nd << ", " << t.rd << ")"; }
template<class T> ostream& operator<<(ostream& out, vector<T> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; }
template<class T> ostream& operator<<(ostream& out, set<T> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; }
template<class L, class R> ostream& operator<<(ostream& out, map<L, R> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; }
struct Sol {
void Test() {
int h, g;
cin>>h>>g;
VI a(1 << h);
VPII ps;
int n = (1 << h) - 1;
int imp = (1 << g) - 1;
RE (i, n) {
int x;
cin>>x;
ps.PB({x, i});
a[i] = x;
}
VI taken(n + 1);
sort(ALL(ps));
VI gotten(n + 1);
int sum = 0;
for (auto p : ps) {
int wh = p.nd;
while (wh > imp || gotten[wh]) {
wh /= 2;
}
if (wh == 0) { continue; }
if (gotten[2 * wh] == gotten[2 * wh + 1]) {
gotten[wh] = 1;
sum += p.st;
taken[p.nd] = 1;
}
}
cout<<sum<<"\n";
VI sum_gotten(n + 1);
FORD (i, n, 1) {
if (!taken[i]) {
cout<<i<<" ";
}
}
cout<<"\n";
}
};
int32_t main() {
ios_base::sync_with_stdio(0);
cout << fixed << setprecision(10);
cerr << fixed << setprecision(10);
cin.tie(0);
//double beg_clock = 1.0 * clock() / CLOCKS_PER_SEC;
int t;
cin>>t;
RE (i, t) {
Sol sol;
sol.Test();
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiNkZWZpbmUgTVAgbWFrZV9wYWlyCiNkZWZpbmUgUEIgcHVzaF9iYWNrCiNkZWZpbmUgaW50IGxvbmcgbG9uZwojZGVmaW5lIHN0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AgIHRha2VuW3AubmRdID0gMTsKICAgICAgfQogICAgfQogICAgY291dDw8c3VtPDwiXG4iOwogICAgVkkgc3VtX2dvdHRlbihuICsgMSk7CiAgICBGT1JEIChpLCBuLCAxKSB7CiAgICAgIGlmICghdGFrZW5baV0pIHsKICAgICAgICBjb3V0PDxpPDwiICI7CiAgICAgIH0KICAgICAgCiAgICB9CiAgICBjb3V0PDwiXG4iOwogICAgCiAgfQp9OwogICAgCiAgICAKICAgICAgCgppbnQzMl90IG1haW4oKSB7CgogIGlvc19iYXNlOjpzeW5jX3dpdGhfc3RkaW8oMCk7CiAgY291dCA8PCBmaXhlZCA8PCBzZXRwcmVjaXNpb24oMTApOwogIGNlcnIgPDwgZml4ZWQgPDwgc2V0cHJlY2lzaW9uKDEwKTsKICBjaW4udGllKDApOwogIC8vZG91YmxlIGJlZ19jbG9jayA9IDEuMCAqIGNsb2NrKCkgLyBDTE9DS1NfUEVSX1NFQzsKICAKICBpbnQgdDsKICBjaW4+PnQ7CiAgUkUgKGksIHQpIHsKICAgIFNvbCBzb2w7CiAgICBzb2wuVGVzdCgpOwogIH0KICAKICAKICAKICAKICAKICAKICAKICAKICAKICAKICAKICAKICAKICAKICAKICAKICAKICAKICAKICAKICAKICAKICAKICAKICAKICByZXR1cm4gMDsKfQo=