#include <bits/stdc++.h>
using namespace std;

const int N = (1 << 20) + 50, INF = (int)1e9;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef long long ll;

int h, g;
int a[2 * N];
int mx[2 * N];
int ht[2 * N];

void f(int i) {
    int left = 2 * i, right = 2 * i + 1;
    if (a[left] == 0 && a[right] == 0) {
        a[i] = 0;
    } else {
        if(a[left] > a[right]) {
            a[i] = a[left];
            f(left);
        } else {
            a[i] = a[right];
            f(right);
        }
        if(a[left] == 0 && a[right] == 0) mx[i] = ht[i];
        else mx[i] = a[left] > a[right] ? mx[left] : mx[right];
    }

}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(NULL);

    int T;
    for(int i = 1; i < (1 << 20); i++) ht[i] = ht[i / 2] + 1;
    cin >> T;
    while(T--) {
        cin >> h >> g;
        ll sum = 0;
        for(int i = 1; i < (1 << h); i++) cin >> a[i], sum += a[i];
//        for(int i = 1; i < (1 << h); i++) mx[i] = h;
        priority_queue<pii> pque;
        pque.push({a[1], 1});
        vector<int> res;
        while(!pque.empty()) {
            auto p = pque.top(); pque.pop();
//            cout << p.second << " " << a[p.second] <<  endl;
            if(a[p.second] == 0) continue;
            if(mx[p.second] == g)    {
                pque.push({a[2 * p.second], 2 * p.second});
                pque.push({a[2 * p.second + 1], 2 * p.second + 1});
            }
            else {
                res.push_back(p.second);
                sum -= a[p.second];
                f(p.second);
                pque.push({a[p.second], p.second});
            }
        }
        cout << sum << "\n";
        for(int x : res) cout << x << ' ';
        cout << '\n';
        for(int i = 1; i < 2 * (1 << h); i++) a[i] = 0;
        for(int i = 1; i < 2 * (1 << h); i++) mx[i] = 0;

    }

}


#include <bits/stdc++.h>
using namespace std;

const int N = (1 << 20) + 50, INF = (int)1e9;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef long long ll;

int h, g;
int a[N];
int mx[N];
int ht[N];

void f(int i) {
    int left = 2 * i, right = 2 * i + 1;
    if (a[left] == 0 && a[right] == 0) {
        a[i] = 0;
    } else {
        if(a[left] > a[right]) {
            a[i] = a[left];
            f(left);
        } else {
            a[i] = a[right];
            f(right);
        }
        if(a[left] == 0 && a[right] == 0) mx[i] = ht[i];
        else mx[i] = a[left] > a[right] ? mx[left] : mx[right];
    }

}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(NULL);

    int T;
    for(int i = 1; i < (1 << 20); i++) ht[i] = ht[i / 2] + 1;
    cin >> T;
    while(T--) {
        cin >> h >> g;
        ll sum = 0;
        for(int i = 1; i < (1 << h); i++) cin >> a[i], sum += a[i];
//        for(int i = 1; i < (1 << h); i++) mx[i] = h;
        priority_queue<pii> pque;
        pque.push({a[1], 1});
        vector<int> res;
        while(!pque.empty()) {
            auto p = pque.top(); pque.pop();
//            cout << p.second << " " << a[p.second] <<  endl;
            if(a[p.second] == 0) continue;
            if(mx[p.second] == g)    {
                pque.push({a[2 * p.second], 2 * p.second});
                pque.push({a[2 * p.second + 1], 2 * p.second + 1});
            }
            else {
                res.push_back(p.second);
                sum -= a[p.second];
                f(p.second);
                pque.push({a[p.second], p.second});
            }
        }
        cout << sum << "\n";
        for(int x : res) cout << x << ' ';
        cout << '\n';
        for(int i = 1; i < 2 * (1 << h); i++) a[i] = 0;
        for(int i = 1; i < 2 * (1 << h); i++) mx[i] = 0;

    }

}


#include <bits/stdc++.h>
using namespace std;

const int N = (1 << 20) + 50, INF = (int)1e9;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef long long ll;

int h, g;
int a[2 * N];
int mx[2 * N];
int ht[2 * N];

void f(int i) {
    int left = 2 * i, right = 2 * i + 1;
    if (a[left] == 0 && a[right] == 0) {
        a[i] = 0;
    } else {
        if(a[left] > a[right]) {
            a[i] = a[left];
            f(left);
        } else {
            a[i] = a[right];
            f(right);
        }
        if(a[left] == 0 && a[right] == 0) mx[i] = ht[i];
        else mx[i] = a[left] > a[right] ? mx[left] : mx[right];
    }
}

ll sum = 0;
vi res;
void dfs(int i) {
    while(mx[i] > g) {
        res.push_back(i);
        sum -= a[i];
        f(i);
    }
    if(a[2 * i] != 0) dfs(2 * i);
    if(a[2 * i + 1] != 0) dfs(2 * i + 1);
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(NULL);

    int T;
    for(int i = 1; i < (1 << 20); i++) ht[i] = ht[i / 2] + 1;
    cin >> T;
    while(T--) {
        cin >> h >> g;
        sum = 0; res.clear();
        for(int i = 1; i < (1 << h); i++) {
            cin >> a[i], sum += a[i];
        }
        fill(mx, mx + (1 << h), h);
        dfs(1);
        cout << sum << "\n";
        for(int x : res) cout << x << ' ';
        cout << '\n';
        for(int i = 1; i < 2 * (1 << h); i++) a[i] = 0;
        for(int i = 1; i < 2 * (1 << h); i++) mx[i] = 0;
    }

}


#define ENABLE_LL
#define MULTIPLE_TEST_CASES_WITH_T
#include <iostream>
#include <cstdio>
#include <map>
#include <set>
#include <vector>
#include <algorithm>
#include <cstdlib>
#include <queue>
#include <cmath>
#include <cstring>
using namespace std;

void init();
void solve();
int main(){
try {

#ifndef CUSTOM_MAIN
#ifdef MULTIPLE_TEST_CASES_WITH_T

int T; scanf("%d",&T); while(T--) {init(); solve();}

#else
#ifdef MULTIPLE_TEST_CASES_WITHOUT_T

while(1) {try{init(); } catch(bool t){return 0;} solve(); }

#else

init(); solve();

#endif
#endif
#endif

} catch (const char* s) { return 0; }
    return 0;
}

#ifdef ENABLE_LL
#define int long long
#endif

#ifdef ENABLE_LL
#define I "%lld"
#define II "%lld%lld"
#define III "%lld%lld%lld"
#define IIII "%lld%lld%lld%lld"
#define IN "%lld\n"
#define IIN "%lld%lld\n"
#define IIIN "%lld%lld%lld\n"
#define IIIIN "%lld%lld%lld%lld\n"
#else
#define I "%d"
#define II "%d%d"
#define III "%d%d%d"
#define IIII "%d%d%d%d"
#define IN "%d\n"
#define IIN "%d%d\n"
#define IIIN "%d%d%d\n"
#define IIIIN "%d%d%d%d\n"
#endif

#ifdef __LOCALE__
#define see(a) std::cout << #a << "=" << a << std::endl
#define ses(a) std::cout << #a << "=" << a << " "
#else
#define see(a) //std::cout << #a << "=" << a << std::endl
#define ses(a)
#endif
#define repe(a,b) for (int a=0;a<(int)b.size();a++)
#define rep(i,n) for (int i=0;i<n;i++)
#define repa(i,n) for (int i=1;i<=n;i++)
#define repi(i,a,b) for (int i=a;i<=b;i++)
#define repb(i,a,b) for (int i=a;i>=b;i--)
typedef pair < int , int > pr; typedef pair < pr  , int > prr;
#define L first
#define R second
template <class T> inline bool checkMin(T& a , T b) { return (a > b ? a=b,1 : 0); }
template <class T> inline bool checkMax(T& a , T b) { return (a < b ? a=b,1 : 0); }
class Scanner{ private: istream& ist;public: Scanner(istream& in):ist(in){} string next(){ string r; ist >> r; return r; } string nextLine(){ string r; getline(ist,r); return r; } int nextInt(){ int r; ist >> r; return r; } double nextDouble(){ double r; ist >> r; return r; } char nextChar(){ char r; ist>>r; return r;} }; Scanner sc(cin);
void ALERT(bool judgememt, const char* phrase) { if (judgememt) { puts(phrase); throw "ALERT"; } }
bool alert(bool judgememt, const char* phrase) { if (judgememt) puts(phrase); return judgememt; }

const int N = 1048600;
int a[N];
int dep[N];
int n, h, g;
vector < int > ord;
int pos[N];

void init() {
    scanf(II,&h,&g);
    n = (1<<h) - 1;
    repa (i,n) scanf(I,&a[i]);
    repa (i,n) dep[i] = dep[i/2] + 1;
    ord.clear();
    repa (i,n) ord.push_back(a[i]);
    sort(ord.begin(), ord.end(), [](int a, int b) {return a>b; });
    repa (i,n) pos[a[i]] = i;
}

void solve() {
    vector < int > ans;
    for (auto p: ord) {
        int u=pos[p];
        int l=u+u, r=u+u+1;
        see(u);
        //checker
        {
            int u=pos[p];
            int l=u+u, r=u+u+1;
            while (a[l] || a[r]) {
                ses(a[l]);see(a[r]);
                if (a[l] > a[r]) {
                    u=l;
                }else {
                    u=r;
                }
                l=u+u; r=u+u+1;
            }
            ses("checker");see(u);
            if (dep[u] == g) goto nxt;
        }

        ans.push_back(u);
        while (a[l] || a[r]) {
            if (a[l] > a[r]) {
                a[u] = a[l];
                pos[a[u]] = u;
                u=l;
            } else {
                a[u] = a[r];
                pos[a[u]] = u;
                u=r;
            }
            l=u+u; r=u+u+1;
        }
        a[u] = 0;
        nxt:;
    }
    int res=0; repa (i,(1<<g)-1) res+=a[i];
    printf(IN,res);
    for (auto i: ans) printf("%lld ",i);
    puts("");
}


#define ENABLE_LL
#define MULTIPLE_TEST_CASES_WITH_T
#include <iostream>
#include <cstdio>
#include <map>
#include <set>
#include <vector>
#include <algorithm>
#include <cstdlib>
#include <queue>
#include <cmath>
#include <cstring>
using namespace std;

void init();
void solve();
int main(){
try {

#ifndef CUSTOM_MAIN
#ifdef MULTIPLE_TEST_CASES_WITH_T

int T; scanf("%d",&T); while(T--) {init(); solve();}

#else
#ifdef MULTIPLE_TEST_CASES_WITHOUT_T

while(1) {try{init(); } catch(bool t){return 0;} solve(); }

#else

init(); solve();

#endif
#endif
#endif

} catch (const char* s) { return 0; }
    return 0;
}

#ifdef ENABLE_LL
#define int long long
#endif

#ifdef ENABLE_LL
#define I "%lld"
#define II "%lld%lld"
#define III "%lld%lld%lld"
#define IIII "%lld%lld%lld%lld"
#define IN "%lld\n"
#define IIN "%lld%lld\n"
#define IIIN "%lld%lld%lld\n"
#define IIIIN "%lld%lld%lld%lld\n"
#else
#define I "%d"
#define II "%d%d"
#define III "%d%d%d"
#define IIII "%d%d%d%d"
#define IN "%d\n"
#define IIN "%d%d\n"
#define IIIN "%d%d%d\n"
#define IIIIN "%d%d%d%d\n"
#endif

#ifdef __LOCALE__
#define see(a) std::cout << #a << "=" << a << std::endl
#define ses(a) std::cout << #a << "=" << a << " "
#else
#define see(a) //std::cout << #a << "=" << a << std::endl
#define ses(a)
#endif
#define repe(a,b) for (int a=0;a<(int)b.size();a++)
#define rep(i,n) for (int i=0;i<n;i++)
#define repa(i,n) for (int i=1;i<=n;i++)
#define repi(i,a,b) for (int i=a;i<=b;i++)
#define repb(i,a,b) for (int i=a;i>=b;i--)
typedef pair < int , int > pr; typedef pair < pr  , int > prr;
#define L first
#define R second
template <class T> inline bool checkMin(T& a , T b) { return (a > b ? a=b,1 : 0); }
template <class T> inline bool checkMax(T& a , T b) { return (a < b ? a=b,1 : 0); }
class Scanner{ private: istream& ist;public: Scanner(istream& in):ist(in){} string next(){ string r; ist >> r; return r; } string nextLine(){ string r; getline(ist,r); return r; } int nextInt(){ int r; ist >> r; return r; } double nextDouble(){ double r; ist >> r; return r; } char nextChar(){ char r; ist>>r; return r;} }; Scanner sc(cin);
void ALERT(bool judgememt, const char* phrase) { if (judgememt) { puts(phrase); throw "ALERT"; } }
bool alert(bool judgememt, const char* phrase) { if (judgememt) puts(phrase); return judgememt; }

const int N = 3000000;
int a[N];
int dep[N];
int n, h, g;
vector < int > ord;
int pos[N];

void init() {
    scanf(II,&h,&g);
    n = (1<<h) - 1;
    repa (i,n) scanf(I,&a[i]);
    repa (i,n) dep[i] = dep[i/2] + 1;
    ord.clear();
    repa (i,n) ord.push_back(a[i]);
    sort(ord.begin(), ord.end(), [](int a, int b) {return a>b; });
    repa (i,n) pos[a[i]] = i;
}

void solve() {
    vector < int > ans;
    for (auto p: ord) {
        int u=pos[p];
        int l=u+u, r=u+u+1;
        see(u);
        //checker
            while (a[l] || a[r]) {
                ses(a[l]);see(a[r]);
                if (a[l] > a[r]) {
                    u=l;
                }else {
                    u=r;
                }
                l=u+u; r=u+u+1;
            }
            ses("checker");see(u);
            if (dep[u] <= g) continue;
            u=pos[p];
            l=u+u, r=u+u+1;

        ans.push_back(u);
        while (a[l] || a[r]) {
            if (a[l] > a[r]) {
                a[u] = a[l];
                pos[a[u]] = u;
                u=l;
            } else {
                a[u] = a[r];
                pos[a[u]] = u;
                u=r;
            }
            l=u+u; r=u+u+1;
        }
        a[u] = 0;
    }
    int res=0; repa (i,(1<<g)-1) res+=a[i];
    printf(IN,res);
    for (auto i: ans) printf("%lld ",i);
    puts("");
}


#define ENABLE_LL
#define MULTIPLE_TEST_CASES_WITH_T
#include <iostream>
#include <cstdio>
#include <map>
#include <set>
#include <vector>
#include <algorithm>
#include <cstdlib>
#include <queue>
#include <cmath>
#include <cstring>
using namespace std;

void init();
void solve();
int main(){
try {

#ifndef CUSTOM_MAIN
#ifdef MULTIPLE_TEST_CASES_WITH_T

int T; scanf("%d",&T); while(T--) {init(); solve();}

#else
#ifdef MULTIPLE_TEST_CASES_WITHOUT_T

while(1) {try{init(); } catch(bool t){return 0;} solve(); }

#else

init(); solve();

#endif
#endif
#endif

} catch (const char* s) { return 0; }
    return 0;
}

#ifdef ENABLE_LL
#define int long long
#endif

#ifdef ENABLE_LL
#define I "%lld"
#define II "%lld%lld"
#define III "%lld%lld%lld"
#define IIII "%lld%lld%lld%lld"
#define IN "%lld\n"
#define IIN "%lld%lld\n"
#define IIIN "%lld%lld%lld\n"
#define IIIIN "%lld%lld%lld%lld\n"
#else
#define I "%d"
#define II "%d%d"
#define III "%d%d%d"
#define IIII "%d%d%d%d"
#define IN "%d\n"
#define IIN "%d%d\n"
#define IIIN "%d%d%d\n"
#define IIIIN "%d%d%d%d\n"
#endif

#ifdef __LOCALE__
#define see(a) std::cout << #a << "=" << a << std::endl
#define ses(a) std::cout << #a << "=" << a << " "
#else
#define see(a) //std::cout << #a << "=" << a << std::endl
#define ses(a)
#endif
#define repe(a,b) for (int a=0;a<(int)b.size();a++)
#define rep(i,n) for (int i=0;i<n;i++)
#define repa(i,n) for (int i=1;i<=n;i++)
#define repi(i,a,b) for (int i=a;i<=b;i++)
#define repb(i,a,b) for (int i=a;i>=b;i--)
typedef pair < int , int > pr; typedef pair < pr  , int > prr;
#define L first
#define R second
template <class T> inline bool checkMin(T& a , T b) { return (a > b ? a=b,1 : 0); }
template <class T> inline bool checkMax(T& a , T b) { return (a < b ? a=b,1 : 0); }
class Scanner{ private: istream& ist;public: Scanner(istream& in):ist(in){} string next(){ string r; ist >> r; return r; } string nextLine(){ string r; getline(ist,r); return r; } int nextInt(){ int r; ist >> r; return r; } double nextDouble(){ double r; ist >> r; return r; } char nextChar(){ char r; ist>>r; return r;} }; Scanner sc(cin);
void ALERT(bool judgememt, const char* phrase) { if (judgememt) { puts(phrase); throw "ALERT"; } }
bool alert(bool judgememt, const char* phrase) { if (judgememt) puts(phrase); return judgememt; }

const int N = 3000000;
int a[N];
int dep[N];
int n, h, g;
vector < int > ord;
int pos[N];

void init() {
    scanf(II,&h,&g);
    n = (1<<h) - 1;
    repa (i,n) scanf(I,&a[i]);
    repa (i,n) dep[i] = dep[i/2] + 1;
    ord.clear();
    repa (i,n) ord.push_back(a[i]);
    sort(ord.begin(), ord.end(), [](int a, int b) {return a>b; });
    repa (i,n) pos[a[i]] = i;
}

void solve() {
    vector < int > ans;
    for (auto p: ord) {
        int u=pos[p];
        int l=u+u, r=u+u+1;
        see(u);
        //checker
            while (a[l] || a[r]) {
                ses(a[l]);see(a[r]);
                if (a[l] > a[r]) {
                    u=l;
                }else {
                    u=r;
                }
                l=u+u; r=u+u+1;
            }
            ses("checker");see(u);
            if (dep[u] <= g) continue;
            u=pos[p];
            l=u+u, r=u+u+1;

        ans.push_back(u);
        while (a[l] || a[r]) {
            if (a[l] > a[r]) {
                a[u] = a[l];
                pos[a[u]] = u;
                u=l;
            } else {
                a[u] = a[r];
                pos[a[u]] = u;
                u=r;
            }
            l=u+u; r=u+u+1;
        }
        a[u] = 0;
    }
    int res=0; repa (i,(1<<h)-1) res+=a[i];
    printf(IN,res);
    for (auto i: ans) printf("%lld ",i);
    puts("");
}


#define ENABLE_LL
#define MULTIPLE_TEST_CASES_WITH_T
#include <iostream>
#include <cstdio>
#include <map>
#include <set>
#include <vector>
#include <algorithm>
#include <cstdlib>
#include <queue>
#include <cmath>
#include <cstring>
using namespace std;

void init();
void solve();
int main(){
try {

#ifndef CUSTOM_MAIN
#ifdef MULTIPLE_TEST_CASES_WITH_T

int T; scanf("%d",&T); while(T--) {init(); solve();}

#else
#ifdef MULTIPLE_TEST_CASES_WITHOUT_T

while(1) {try{init(); } catch(bool t){return 0;} solve(); }

#else

init(); solve();

#endif
#endif
#endif

} catch (const char* s) { return 0; }
    return 0;
}

#ifdef ENABLE_LL
#define int long long
#endif

#ifdef ENABLE_LL
#define I "%lld"
#define II "%lld%lld"
#define III "%lld%lld%lld"
#define IIII "%lld%lld%lld%lld"
#define IN "%lld\n"
#define IIN "%lld%lld\n"
#define IIIN "%lld%lld%lld\n"
#define IIIIN "%lld%lld%lld%lld\n"
#else
#define I "%d"
#define II "%d%d"
#define III "%d%d%d"
#define IIII "%d%d%d%d"
#define IN "%d\n"
#define IIN "%d%d\n"
#define IIIN "%d%d%d\n"
#define IIIIN "%d%d%d%d\n"
#endif

#ifdef __LOCALE__
#define see(a) std::cout << #a << "=" << a << std::endl
#define ses(a) std::cout << #a << "=" << a << " "
#else
#define see(a) //std::cout << #a << "=" << a << std::endl
#define ses(a)
#endif
#define repe(a,b) for (int a=0;a<(int)b.size();a++)
#define rep(i,n) for (int i=0;i<n;i++)
#define repa(i,n) for (int i=1;i<=n;i++)
#define repi(i,a,b) for (int i=a;i<=b;i++)
#define repb(i,a,b) for (int i=a;i>=b;i--)
typedef pair < int , int > pr; typedef pair < pr  , int > prr;
#define L first
#define R second
template <class T> inline bool checkMin(T& a , T b) { return (a > b ? a=b,1 : 0); }
template <class T> inline bool checkMax(T& a , T b) { return (a < b ? a=b,1 : 0); }
class Scanner{ private: istream& ist;public: Scanner(istream& in):ist(in){} string next(){ string r; ist >> r; return r; } string nextLine(){ string r; getline(ist,r); return r; } int nextInt(){ int r; ist >> r; return r; } double nextDouble(){ double r; ist >> r; return r; } char nextChar(){ char r; ist>>r; return r;} }; Scanner sc(cin);
void ALERT(bool judgememt, const char* phrase) { if (judgememt) { puts(phrase); throw "ALERT"; } }
bool alert(bool judgememt, const char* phrase) { if (judgememt) puts(phrase); return judgememt; }

const int N = 1048600;
int a[N];
int dep[N];
int n, h, g;

void init() {
    scanf(II,&h,&g);
    n = (1<<h) - 1;
    repa (i,n) scanf(I,&a[i]);
    repa (i,n) dep[i] = dep[i/2] + 1;
}

void solve() {
    set <pr> st;
    repa (i,n) st.insert( pr(a[i],i) );
    vector < int > ans;
    while (!st.empty()) {
        auto pos = *(--st.end()); st.erase(--st.end());

        int u=pos.R;
        int l=u+u, r=u+u+1;
        see(u);
        //checker
        {
            int u=pos.R;
            int l=u+u, r=u+u+1;
            while (a[l] || a[r]) {
                ses(a[l]);see(a[r]);
                if (a[l] > a[r]) {
                    u=l;
                }else {
                    u=r;
                }
                l=u+u; r=u+u+1;
            }
            ses("checker");see(u);
            if (dep[u] == g) goto nxt;
        }

        ans.push_back(pos.R);
        while (a[l] || a[r]) {
            if (a[l] > a[r]) {
                a[u] = a[l];
                st.erase( pr(a[u],l) ); st.insert( pr(a[u],u) );
                u=l;
            } else {
                a[u] = a[r];
                st.erase( pr(a[u],r) ); st.insert( pr(a[u],u) );
                u=r;
            }
            l=u+u; r=u+u+1;
        }
        a[u] = 0;
        nxt:;
    }
    int res=0; repa (i,(1<<g)-1) res+=a[i];
    printf(IN,res);
    for (auto i: ans) printf("%lld ",i);
    puts("");
}


#define ENABLE_LL
#define MULTIPLE_TEST_CASES_WITH_T
#include <iostream>
#include <cstdio>
#include <map>
#include <set>
#include <vector>
#include <algorithm>
#include <cstdlib>
#include <queue>
#include <cmath>
#include <cstring>
using namespace std;

void init();
void solve();
int main(){
try {

#ifndef CUSTOM_MAIN
#ifdef MULTIPLE_TEST_CASES_WITH_T

int T; scanf("%d",&T); while(T--) {init(); solve();}

#else
#ifdef MULTIPLE_TEST_CASES_WITHOUT_T

while(1) {try{init(); } catch(bool t){return 0;} solve(); }

#else

init(); solve();

#endif
#endif
#endif

} catch (const char* s) { return 0; }
    return 0;
}

#ifdef ENABLE_LL
#define int long long
#endif

#ifdef ENABLE_LL
#define I "%lld"
#define II "%lld%lld"
#define III "%lld%lld%lld"
#define IIII "%lld%lld%lld%lld"
#define IN "%lld\n"
#define IIN "%lld%lld\n"
#define IIIN "%lld%lld%lld\n"
#define IIIIN "%lld%lld%lld%lld\n"
#else
#define I "%d"
#define II "%d%d"
#define III "%d%d%d"
#define IIII "%d%d%d%d"
#define IN "%d\n"
#define IIN "%d%d\n"
#define IIIN "%d%d%d\n"
#define IIIIN "%d%d%d%d\n"
#endif

#ifdef __LOCALE__
#define see(a) std::cout << #a << "=" << a << std::endl
#define ses(a) std::cout << #a << "=" << a << " "
#else
#define see(a) //std::cout << #a << "=" << a << std::endl
#define ses(a)
#endif
#define repe(a,b) for (int a=0;a<(int)b.size();a++)
#define rep(i,n) for (int i=0;i<n;i++)
#define repa(i,n) for (int i=1;i<=n;i++)
#define repi(i,a,b) for (int i=a;i<=b;i++)
#define repb(i,a,b) for (int i=a;i>=b;i--)
typedef pair < int , int > pr; typedef pair < pr  , int > prr;
#define L first
#define R second
template <class T> inline bool checkMin(T& a , T b) { return (a > b ? a=b,1 : 0); }
template <class T> inline bool checkMax(T& a , T b) { return (a < b ? a=b,1 : 0); }
class Scanner{ private: istream& ist;public: Scanner(istream& in):ist(in){} string next(){ string r; ist >> r; return r; } string nextLine(){ string r; getline(ist,r); return r; } int nextInt(){ int r; ist >> r; return r; } double nextDouble(){ double r; ist >> r; return r; } char nextChar(){ char r; ist>>r; return r;} }; Scanner sc(cin);
void ALERT(bool judgememt, const char* phrase) { if (judgememt) { puts(phrase); throw "ALERT"; } }
bool alert(bool judgememt, const char* phrase) { if (judgememt) puts(phrase); return judgememt; }

const int N = 3000000;
int a[N];
int dep[N];
int n, h, g;
vector < int > ord;
int pos[N];

void init() {
    repa (i,n) a[i] = 0;
    scanf(II,&h,&g);
    n = (1<<h) - 1;
    repa (i,n) scanf(I,&a[i]);
    repa (i,n) dep[i] = dep[i/2] + 1;
    ord.clear();
    repa (i,n) ord.push_back(a[i]);
    sort(ord.begin(), ord.end(), [](int a, int b) {return a>b; });
    repa (i,n) pos[a[i]] = i;
}

void solve() {
    vector < int > ans;
    for (auto p: ord) {
        int u=pos[p];
        int l=u+u, r=u+u+1;
        see(u);
        //checker
            while (a[l] || a[r]) {
                ses(a[l]);see(a[r]);
                if (a[l] > a[r]) {
                    u=l;
                }else {
                    u=r;
                }
                l=u+u; r=u+u+1;
            }
            ses("checker");see(u);
            if (dep[u] <= g) continue;
            u=pos[p];
            l=u+u, r=u+u+1;

        ans.push_back(u);
        while (a[l] || a[r]) {
            if (a[l] > a[r]) {
                a[u] = a[l];
                pos[a[u]] = u;
                u=l;
            } else {
                a[u] = a[r];
                pos[a[u]] = u;
                u=r;
            }
            l=u+u; r=u+u+1;
        }
        a[u] = 0;
    }
    int res=0; repa (i,(1<<h)-1) res+=a[i];
    printf(IN,res);
    for (auto i: ans) printf("%lld ",i);
    puts("");
}


#define ENABLE_LL
#define MULTIPLE_TEST_CASES_WITH_T
#include <iostream>
#include <cstdio>
#include <map>
#include <set>
#include <vector>
#include <algorithm>
#include <cstdlib>
#include <queue>
#include <cmath>
#include <cstring>
using namespace std;

void init();
void solve();
int main(){
try {

#ifndef CUSTOM_MAIN
#ifdef MULTIPLE_TEST_CASES_WITH_T

int T; scanf("%d",&T); while(T--) {init(); solve();}

#else
#ifdef MULTIPLE_TEST_CASES_WITHOUT_T

while(1) {try{init(); } catch(bool t){return 0;} solve(); }

#else

init(); solve();

#endif
#endif
#endif

} catch (const char* s) { return 0; }
    return 0;
}

#ifdef ENABLE_LL
#define int long long
#endif

#ifdef ENABLE_LL
#define I "%lld"
#define II "%lld%lld"
#define III "%lld%lld%lld"
#define IIII "%lld%lld%lld%lld"
#define IN "%lld\n"
#define IIN "%lld%lld\n"
#define IIIN "%lld%lld%lld\n"
#define IIIIN "%lld%lld%lld%lld\n"
#else
#define I "%d"
#define II "%d%d"
#define III "%d%d%d"
#define IIII "%d%d%d%d"
#define IN "%d\n"
#define IIN "%d%d\n"
#define IIIN "%d%d%d\n"
#define IIIIN "%d%d%d%d\n"
#endif

#ifdef __LOCALE__
#define see(a) std::cout << #a << "=" << a << std::endl
#define ses(a) std::cout << #a << "=" << a << " "
#else
#define see(a) //std::cout << #a << "=" << a << std::endl
#define ses(a)
#endif
#define repe(a,b) for (int a=0;a<(int)b.size();a++)
#define rep(i,n) for (int i=0;i<n;i++)
#define repa(i,n) for (int i=1;i<=n;i++)
#define repi(i,a,b) for (int i=a;i<=b;i++)
#define repb(i,a,b) for (int i=a;i>=b;i--)
typedef pair < int , int > pr; typedef pair < pr  , int > prr;
#define L first
#define R second
template <class T> inline bool checkMin(T& a , T b) { return (a > b ? a=b,1 : 0); }
template <class T> inline bool checkMax(T& a , T b) { return (a < b ? a=b,1 : 0); }
class Scanner{ private: istream& ist;public: Scanner(istream& in):ist(in){} string next(){ string r; ist >> r; return r; } string nextLine(){ string r; getline(ist,r); return r; } int nextInt(){ int r; ist >> r; return r; } double nextDouble(){ double r; ist >> r; return r; } char nextChar(){ char r; ist>>r; return r;} }; Scanner sc(cin);
void ALERT(bool judgememt, const char* phrase) { if (judgememt) { puts(phrase); throw "ALERT"; } }
bool alert(bool judgememt, const char* phrase) { if (judgememt) puts(phrase); return judgememt; }

const int N = 3000000;
int a[N];
int dep[N];
int n, h, g;
vector < int > ord;
int pos[N];

void init() {
    scanf(II,&h,&g);
    n = (1<<h) - 1;
    repa (i,n) scanf(I,&a[i]);
    repa (i,n) dep[i] = dep[i/2] + 1;
    ord.clear();
    repa (i,n) ord.push_back(a[i]);
    sort(ord.begin(), ord.end(), [](int a, int b) {return a>b; });
    repa (i,n) pos[a[i]] = i;
}

void solve() {
    vector < int > ans;
    for (auto p: ord) {
        int u=pos[p];
        int l=u+u, r=u+u+1;
        see(u);
        //checker
        {
            int u=pos[p];
            int l=u+u, r=u+u+1;
            while (a[l] || a[r]) {
                ses(a[l]);see(a[r]);
                if (a[l] > a[r]) {
                    u=l;
                }else {
                    u=r;
                }
                l=u+u; r=u+u+1;
            }
            ses("checker");see(u);
            if (dep[u] == g) goto nxt;
        }

        ans.push_back(u);
        while (a[l] || a[r]) {
            if (a[l] > a[r]) {
                a[u] = a[l];
                pos[a[u]] = u;
                u=l;
            } else {
                a[u] = a[r];
                pos[a[u]] = u;
                u=r;
            }
            l=u+u; r=u+u+1;
        }
        a[u] = 0;
        nxt:;
    }
    int res=0; repa (i,(1<<g)-1) res+=a[i];
    printf(IN,res);
    for (auto i: ans) printf("%lld ",i);
    puts("");
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;
int t, n, h, g, a[2000001], b[2000001], pos[2000001], dep[2000001];
vector<int> ans;
bool del(int x)
{
	int now = x;
	while (a[now])
	{
		now = a[now << 1] < a[now << 1 | 1] ? now << 1 | 1 : now << 1;
	}
	now >>= 1;
	if (dep[now] == g)
	{
		return 0;
	}
	while (now)
	{
		pos[a[now]] = pos[a[now >> 1]];
		now >>= 1;
	}
	while (a[x])
	{
		if (!a[x << 1] && !a[x << 1 | 1])
		{
			a[x] = 0;
		}
		else
		{
			if (a[x << 1] < a[x << 1 | 1])
			{
				a[x] = a[x << 1 | 1];
				x = x << 1 | 1;
			}
			else
			{
				a[x] = a[x << 1];
				x <<= 1;
			}
		}
	}
	return 1;
}
int main()
{
	scanf("%d", &t);
	while (t--)
	{
		scanf("%d%d", &h, &g);
		n = (1 << h) - 1;
		ans.clear();
		dep[1] = 1;
		long long sum = 0, left = n, t = n;
		for (int i = 1; i <= n; i++)
		{
			scanf("%d", &a[i]);
			sum += a[i];
			b[i] = a[i];
			pos[a[i]] = i;
			dep[i] = dep[i >> 1] + 1;
		}
		sort(b + 1, b + n + 1);
		while (left >= (1 << g))
		{
			int tmp = pos[b[t]];
			if (!del(tmp))
			{
				t--;
			}
			else
			{
				ans.push_back(tmp);
				sum -= b[t--];
				left--;
			}
		}
		printf("%lld\n", sum);
		for (auto i : ans)
		{
			printf("%d ", i);
		}
		printf("\n");
	}
	return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;
int t, n, h, g, a[3000001], b[3000001], pos[3000001], dep[3000001];
vector<int> ans;
bool del(int x)
{
	int now = x;
	while (a[now])
	{
		now = a[now << 1] < a[now << 1 | 1] ? now << 1 | 1 : now << 1;
	}
	now >>= 1;
	if (dep[now] == g)
	{
		return 0;
	}
	while (now)
	{
		pos[a[now]] = pos[a[now >> 1]];
		now >>= 1;
	}
	while (a[x])
	{
		if (!a[x << 1] && !a[x << 1 | 1])
		{
			a[x] = 0;
		}
		else
		{
			if (a[x << 1] < a[x << 1 | 1])
			{
				a[x] = a[x << 1 | 1];
				x = x << 1 | 1;
			}
			else
			{
				a[x] = a[x << 1];
				x <<= 1;
			}
		}
	}
	return 1;
}
int main()
{
	scanf("%d", &t);
	while (t--)
	{
		scanf("%d%d", &h, &g);
		n = (1 << h) - 1;
		ans.clear();
		dep[1] = 1;
		long long sum = 0, left = n, t = n;
		for (int i = 1; i <= n; i++)
		{
			scanf("%d", &a[i]);
			sum += a[i];
			b[i] = a[i];
			pos[a[i]] = i;
			dep[i] = dep[i >> 1] + 1;
		}
		sort(b + 1, b + n + 1);
		while (left >= (1 << g))
		{
			int tmp = pos[b[t]];
			if (!del(tmp))
			{
				t--;
			}
			else
			{
				ans.push_back(tmp);
				sum -= b[t--];
				left--;
			}
		}
		printf("%lld\n", sum);
		for (auto i : ans)
		{
			printf("%d ", i);
		}
		printf("\n");
		for (int i = 1; i <= n; i++)
		{
			a[i] = 0;
		}
	}
	return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;
int t, n, h, g, a[3000001], b[3000001], pos[3000001], dep[3000001];
vector<int> ans;
bool del(int x)
{
	int now = x;
	while (a[now])
	{
		now = a[now << 1] < a[now << 1 | 1] ? now << 1 | 1 : now << 1;
	}
	now >>= 1;
	if (dep[now] == g)
	{
		return 0;
	}
	while (now)
	{
		pos[a[now]] = pos[a[now >> 1]];
		now >>= 1;
	}
	while (a[x])
	{
		if (!a[x << 1] && !a[x << 1 | 1])
		{
			a[x] = 0;
		}
		else
		{
			if (a[x << 1] < a[x << 1 | 1])
			{
				a[x] = a[x << 1 | 1];
				x = x << 1 | 1;
			}
			else
			{
				a[x] = a[x << 1];
				x <<= 1;
			}
		}
	}
	return 1;
}
int main()
{
	scanf("%d", &t);
	while (t--)
	{
		scanf("%d%d", &h, &g);
		n = (1 << h) - 1;
		ans.clear();
		dep[1] = 1;
		long long sum = 0, left = n, t = n;
		for (int i = 1; i <= n; i++)
		{
			scanf("%d", &a[i]);
			sum += a[i];
			b[i] = a[i];
			pos[a[i]] = i;
			dep[i] = dep[i >> 1] + 1;
		}
		sort(b + 1, b + n + 1);
		while (left >= (1 << g))
		{
			int tmp = pos[b[t]];
			if (!del(tmp))
			{
				t--;
			}
			else
			{
				ans.push_back(tmp);
				sum -= b[t--];
				left--;
			}
		}
		printf("%lld\n", sum);
		for (auto i : ans)
		{
			printf("%d ", i);
		}
		printf("\n");
	}
	return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;
int t, n, h, g, a[2000001], b[2000001], pos[2000001], dep[2000001];
vector<int> ans;
bool del(int x)
{
	int now = x;
	while (a[now])
	{
		now = a[now << 1] < a[now << 1 | 1] ? now << 1 | 1 : now << 1;
	}
	now >>= 1;
	if (dep[now] == g)
	{
		return 0;
	}
	while (now)
	{
		pos[a[now]] = pos[a[now >> 1]];
		now >>= 1;
	}
	while (a[x])
	{
		if (!a[x << 1] && !a[x << 1 | 1])
		{
			a[x] = 0;
		}
		else
		{
			if (a[x << 1] < a[x << 1 | 1])
			{
				a[x] = a[x << 1 | 1];
				x = x << 1 | 1;
			}
			else
			{
				a[x] = a[x << 1];
				x <<= 1;
			}
		}
	}
	return 1;
}
int main()
{
	scanf("%d", &t);
	while (t--)
	{
		scanf("%d%d", &h, &g);
		n = (1 << h) - 1;
		ans.clear();
		dep[1] = 1;
		long long sum = 0, left = n, t = n;
		for (int i = 1; i <= n; i++)
		{
			scanf("%d", &a[i]);
			sum += a[i];
			b[i] = a[i];
			pos[a[i]] = i;
			dep[i] = dep[i >> 1] + 1;
		}
		sort(b + 1, b + n + 1);
		while (left >= (1 << g))
		{
			int tmp = pos[b[t]];
			if (!del(tmp))
			{
				t--;
			}
			else
			{
				ans.push_back(tmp);
				sum -= b[t--];
				left--;
			}
		}
		printf("%lld\n", sum);
		for (auto i : ans)
		{
			printf("%d ", ans[i]);
		}
		printf("\n");
	}
	return 0;
}


// Problem : C. Drazil Likes Heap
// Contest : Codeforces Round #631 (Div. 1) - Thanks, Denis aramis Shitov!
// URL : https://codeforces.com/contest/1329/problem/C
// Memory Limit : 256 MB
// Time Limit : 1500 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>

using namespace std;

int T;
int N, H, G;
int arr[1<<22];
vector<int> ans;

int chk(int idx, int dep){
	if(!arr[2*idx] && !arr[2*idx+1]){
		return dep;
	}
	if(arr[2*idx] > arr[2*idx+1]){
		return chk(2*idx, dep+1);
	}
	else{
		return chk(2*idx+1, dep+1);
	}
}

void rem(int idx){
	if(!arr[2*idx] && !arr[2*idx+1]){
		arr[idx] = 0;
		return ;
	}
	if(arr[2*idx] > arr[2*idx+1]){
		swap(arr[idx], arr[2*idx]);
		return rem(2*idx);
	}
	else{
		swap(arr[idx], arr[2*idx+1]);
		return rem(2*idx+1);
	}
}

int main(){
	cin.sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> T;
	while(T--){
		cin >> H >> G;
		N = (1<<H);
		N--;
		long long sum = 0;
		for(int i = 1; i<=N; i++){
			cin >> arr[i];
			sum += arr[i];
		}
		int crnt = 1, crntdep = 1;
		for(int i = 1; i<=(1<<H)-(1<<G); ){
			if(chk(crnt, crntdep) > G){
				sum -= arr[crnt];
				arr[crnt] = 0;
				rem(crnt);
				ans.emplace_back(crnt);
				i++;
			}
			else{
				crntdep++;
				if(chk(2*crnt, crntdep) > G){
					crnt = 2*crnt;
				}
				else{
					crnt = 2*crnt + 1;
				}
			}
		}
		cout << sum << "\n";
		for(int n : ans){
			cout << n << " ";
		}
		ans.clear();
		cout << "\n";
	}
}


// Problem : C. Drazil Likes Heap
// Contest : Codeforces Round #631 (Div. 1) - Thanks, Denis aramis Shitov!
// URL : https://codeforces.com/contest/1329/problem/C
// Memory Limit : 256 MB
// Time Limit : 1500 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>

using namespace std;

int T;
int N, H, G;
int arr[1<<22];
vector<int> ans;

int chk(int idx, int dep){
	if(!arr[2*idx] && !arr[2*idx+1]){
		return dep;
	}
	if(arr[2*idx] > arr[2*idx+1]){
		return chk(2*idx, dep+1);
	}
	else{
		return chk(2*idx+1, dep+1);
	}
}

void rem(int idx){
	if(!arr[2*idx] && !arr[2*idx+1]){
		return ;
	}
	if(arr[2*idx] > arr[2*idx+1]){
		swap(arr[idx], arr[2*idx]);
		return rem(2*idx);
	}
	else{
		swap(arr[idx], arr[2*idx+1]);
		return rem(2*idx+1);
	}
}

int main(){
	cin.sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> T;
	while(T--){
		cin >> H >> G;
		N = (1<<H);
		N--;
		long long sum = 0;
		for(int i = 1; i<=N; i++){
			cin >> arr[i];
			sum += arr[i];
		}
		/*
		for(int i = 1; i<=(1<<H)-(1<<G); ){
			if(chk(crnt, crntdep) > G){
				sum -= arr[crnt];
				arr[crnt] = 0;
				rem(crnt);
				ans.emplace_back(crnt);
				i++;
			}
			else{
				crntdep++;
				if(chk(2*crnt, crntdep) > G){
					crnt = 2*crnt;
				}
				else{
					crnt = 2*crnt + 1;
				}
			}
		}
		*/
		int cnt = (1<<H) - (1<<G);
		int crntdep = 1;
		for(int i = 1; i<1<<G; i++){
			if(1<<crntdep == i){
				crntdep++;
			}
			while(cnt && chk(i, crntdep) > G){
				cnt--;
				sum -= arr[i];
				arr[i] = 0;
				rem(i);
				ans.emplace_back(i);
			}
		}
		cout << sum << "\n";
		for(int n : ans){
			cout << n << " ";
		}
		ans.clear();
		fill(arr+1, arr+1+N, 0);
		cout << "\n";
	}
}


// Problem : C. Drazil Likes Heap
// Contest : Codeforces Round #631 (Div. 1) - Thanks, Denis aramis Shitov!
// URL : https://codeforces.com/contest/1329/problem/C
// Memory Limit : 256 MB
// Time Limit : 1500 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>

using namespace std;

int T;
int N, H, G;
int arr[1<<22];
vector<int> ans;

int chk(int idx, int dep){
	if(!arr[2*idx] && !arr[2*idx+1]){
		return dep;
	}
	if(arr[2*idx] > arr[2*idx+1]){
		return chk(2*idx, dep+1);
	}
	else{
		return chk(2*idx+1, dep+1);
	}
}

void rem(int idx){
	if(!arr[2*idx] && !arr[2*idx+1]){
		return ;
	}
	if(arr[2*idx] > arr[2*idx+1]){
		swap(arr[idx], arr[2*idx]);
		return rem(2*idx);
	}
	else{
		swap(arr[idx], arr[2*idx+1]);
		return rem(2*idx+1);
	}
}

int main(){
	cin.sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> T;
	while(T--){
		cin >> H >> G;
		N = (1<<H);
		N--;
		long long sum = 0;
		for(int i = 1; i<=N; i++){
			cin >> arr[i];
			sum += arr[i];
		}
		int crnt = 1, crntdep = 1;
		for(int i = 1; i<=(1<<H)-(1<<G); ){
			if(chk(crnt, crntdep) > G){
				sum -= arr[crnt];
				arr[crnt] = 0;
				rem(crnt);
				ans.emplace_back(crnt);
				i++;
			}
			else{
				crntdep++;
				if(chk(2*crnt, crntdep) > G){
					crnt = 2*crnt;
				}
				else{
					crnt = 2*crnt + 1;
				}
			}
		}
		cout << sum << "\n";
		for(int n : ans){
			cout << n << " ";
		}
		ans.clear();
		fill(arr+1, arr+1+N, 0);
		cout << "\n";
	}
}


// Problem : C. Drazil Likes Heap
// Contest : Codeforces Round #631 (Div. 1) - Thanks, Denis aramis Shitov!
// URL : https://codeforces.com/contest/1329/problem/C
// Memory Limit : 256 MB
// Time Limit : 1500 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>

using namespace std;

int T;
int N, H, G;
int arr[1<<22];
vector<int> ans;

int chk(int idx, int dep){
	if(!arr[2*idx] && !arr[2*idx+1]){
		return dep;
	}
	if(arr[2*idx] > arr[2*idx+1]){
		return chk(2*idx, dep+1);
	}
	else{
		return chk(2*idx+1, dep+1);
	}
}

void rem(int idx){
	if(!arr[2*idx] && !arr[2*idx+1]){
		arr[idx] = 0;
		return ;
	}
	if(arr[2*idx] > arr[2*idx+1]){
		swap(arr[idx], arr[2*idx]);
		return rem(2*idx);
	}
	else{
		swap(arr[idx], arr[2*idx+1]);
		return rem(2*idx+1);
	}
}

int main(){
	cin.sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> T;
	while(T--){
		cin >> H >> G;
		N = (1<<H);
		N--;
		long long sum = 0;
		for(int i = 1; i<=N; i++){
			cin >> arr[i];
			sum += arr[i];
		}
		int crnt = 1, crntdep = 1;
		for(int i = 1; i<=(1<<H)-(1<<G); ){
			if(chk(crnt, crntdep) > G){
				sum -= arr[crnt];
				arr[crnt] = 0;
				rem(crnt);
				ans.emplace_back(crnt);
				i++;
			}
			else{
				crntdep++;
				if(chk(2*crnt, crntdep) > G){
					crnt = 2*crnt;
				}
				else{
					crnt = 2*crnt + 1;
				}
			}
		}
		cout << sum << "\n";
		for(int n : ans){
			cout << n << " ";
		}
		ans.clear();
		fill(arr+1, arr+1+N, 0);
		cout << "\n";
	}
}


#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

int h, g;
int a[(1<<21)+3];
int ans[(1<<21)+3];

inline bool act(int pos) {
	if(a[pos]==0) return 0;

	int tmp=pos;
	for(int i=pos; a[pos]; )
		if(a[pos<<1]>a[(pos<<1)|1])
			pos=pos<<1;
		else
			pos=(pos<<1)|1;
	pos>>=1;
	if(pos<(1<<g)) return 0;
	pos=tmp;

	for(int i=pos; a[pos]; ) {
		if(a[pos<<1]>a[(pos<<1)|1]) {
			a[pos]=a[pos<<1];
			pos=pos<<1;
		}
		else {
			a[pos]=a[(pos<<1)|1];
			pos=(pos<<1)|1;
		}
	}
	return 1;
}

int main() {
	int T;
	scanf("%d", &T);
	while(T--) {
		scanf("%d %d", &h, &g);
		for(int i=1; i<(1<<h); ++i)
			scanf("%d", a+i);

		int acou=0;
		for(int i=1; i<(1<<h); ++i)
			while(act(i))
				ans[++acou]=i;

		if(acou>((1<<h)-(1<<g))) {
			while(1);
		}

		long long sum=0;
		for(int i=1; i<(1<<g); ++i)
			sum+=a[i];
		printf("%I64d\n", sum);
		for(int i=1; i<=((1<<h)-(1<<g)); ++i)
			printf("%d ", ans[i]);
		puts("");
	}

	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

int h, g;
int a[(1<<21)+3];
int ans[(1<<21)+3];

inline bool act(int pos) {
	if(a[pos]==0) return 0;

	int tmp=pos;
	for(int i=pos; a[pos]; )
		if(a[pos<<1]>a[(pos<<1)|1])
			pos=pos<<1;
		else
			pos=(pos<<1)|1;
	pos>>=1;
	if(pos<(1<<g)) return 0;
	pos=tmp;

	for(int i=pos; a[pos]; ) {
		if(a[pos<<1]>a[(pos<<1)|1]) {
			a[pos]=a[pos<<1];
			pos=pos<<1;
		}
		else {
			a[pos]=a[(pos<<1)|1];
			pos=(pos<<1)|1;
		}
	}
	return 1;
}

int main() {
	int T;
	scanf("%d", &T);
	while(T--) {
		scanf("%d %d", &h, &g);
		for(int i=1; i<(1<<h); ++i)
			scanf("%d", a+i);

		int acou=0;
		for(int i=1; i<(1<<g); ++i)
			while(act(i))
				ans[++acou]=i;

		for(int i=(1<<g); i<(1<<h); ++i)
			if(a[i]) while(1);
		for(int i=1; i<(1<<g); ++i)
			if(a[i]==0) while(1);

		long long sum=0;
		for(int i=1; i<(1<<g); ++i)
			sum+=a[i];
		printf("%I64d\n", sum);
		for(int i=1; i<=((1<<h)-(1<<g)); ++i)
			printf("%d ", ans[i]);
		puts("");
	}

	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

int h, g;
int a[(1<<22)+3];
int ans[(1<<22)+3];

inline bool act(int pos) {
	if(a[pos]==0) while(1);

	int tmp=pos;
	while(a[pos])
		if(a[pos<<1]>a[(pos<<1)|1])
			pos=pos<<1;
		else
			pos=(pos<<1)|1;
	pos>>=1;
	if(pos<(1<<g)) return 0;

	pos=tmp;
	while(a[pos])
		if(a[pos<<1]>a[(pos<<1)|1]) {
			a[pos]=a[pos<<1];
			pos=pos<<1;
		}
		else {
			a[pos]=a[(pos<<1)|1];
			pos=(pos<<1)|1;
		}
	return 1;
}

int main() {
	int T;
	scanf("%d", &T);
	while(T--) {
		scanf("%d %d", &h, &g);
		for(int i=1; i<=(1<<(h+1)); ++i)
			a[i]=0;
		for(int i=1; i<(1<<h); ++i)
			scanf("%d", a+i);

		int acou=0;
		for(int i=1; i<(1<<g); ++i)
			while(act(i))
				ans[++acou]=i;

		long long sum=0;
		for(int i=1; i<(1<<g); ++i)
			sum+=a[i];
		printf("%I64d\n", sum);
		for(int i=1; i<=((1<<h)-(1<<g)); ++i)
			printf("%d ", ans[i]);
		puts("");
	}

	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

int h, g;
int a[(1<<21)+3];
int ans[(1<<21)+3];

inline bool act(int pos) {
	if(a[pos]==0) return 0;

	int tmp=pos;
	for(int i=pos; a[pos]; )
		if(a[pos<<1]>a[(pos<<1)|1])
			pos=pos<<1;
		else
			pos=(pos<<1)|1;
	pos>>=1;
	if(pos<(1<<g)) return 0;
	pos=tmp;

	for(int i=pos; a[pos]; ) {
		if(a[pos<<1]>a[(pos<<1)|1]) {
			a[pos]=a[pos<<1];
			pos=pos<<1;
		}
		else {
			a[pos]=a[(pos<<1)|1];
			pos=(pos<<1)|1;
		}
	}
	return 1;
}

int main() {
	int T;
	scanf("%d", &T);
	while(T--) {
		scanf("%d %d", &h, &g);
		for(int i=1; i<(1<<h); ++i)
			scanf("%d", a+i);

		int acou=0;
		for(int i=1; i<(1<<g); ++i)
			while(act(i))
				ans[++acou]=i;

		for(int i=(1<<g); i<(1<<h); ++i)
			if(a[i]) while(1);
		//for(int i=1; i<(1<<g); ++i)
			//if(a[i]==0) while(1);

		long long sum=0;
		for(int i=1; i<(1<<g); ++i)
			sum+=a[i];
		printf("%I64d\n", sum);
		for(int i=1; i<=((1<<h)-(1<<g)); ++i)
			printf("%d ", ans[i]);
		puts("");
	}

	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

int h, g;
int a[(1<<22)+3];
int ans[(1<<22)+3];

inline bool act(int pos) {
	if(a[pos]==0) while(1);

	int tmp=pos;
	while(a[pos])
		if(a[pos<<1]>a[(pos<<1)|1])
			pos=pos<<1;
		else
			pos=(pos<<1)|1;
	pos>>=1;
	if(pos<1 || pos>=(1<<h)) while(1);
	if(a[pos]==0) while(1);
	if(pos<(1<<g)) return 0;

	pos=tmp;
	while(a[pos])
		if(a[pos<<1]>a[(pos<<1)|1]) {
			a[pos]=a[pos<<1];
			pos=pos<<1;
		}
		else {
			a[pos]=a[(pos<<1)|1];
			pos=(pos<<1)|1;
		}
	return 1;
}

int main() {
	int T;
	scanf("%d", &T);
	while(T--) {
		scanf("%d %d", &h, &g);
		for(int i=1; i<(1<<h); ++i)
			scanf("%d", a+i);

		int acou=0;
		for(int i=1; i<(1<<g); ++i)
			while(act(i))
				ans[++acou]=i;

		long long sum=0;
		for(int i=1; i<(1<<g); ++i)
			sum+=a[i];
		printf("%I64d\n", sum);
		for(int i=1; i<=((1<<h)-(1<<g)); ++i)
			printf("%d ", ans[i]);
		puts("");
	}

	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

int h, g;
int a[(1<<22)+3];
int ans[(1<<22)+3];

inline bool act(int pos) {
	if(a[pos]==0) while(1);

	int tmp=pos;
	while(a[pos])
		if(a[pos<<1]>a[(pos<<1)|1])
			pos=pos<<1;
		else
			pos=(pos<<1)|1;
	pos>>=1;
	if(pos<(1<<g)) return 0;
	pos=tmp;

	while(a[pos])
		if(a[pos<<1]>a[(pos<<1)|1]) {
			a[pos]=a[pos<<1];
			pos=pos<<1;
		}
		else {
			a[pos]=a[(pos<<1)|1];
			pos=(pos<<1)|1;
		}
	return 1;
}

int main() {
	int T;
	scanf("%d", &T);
	while(T--) {
		scanf("%d %d", &h, &g);
		for(int i=1; i<(1<<h); ++i)
			scanf("%d", a+i);

		int acou=0;
		for(int i=1; i<(1<<g); ++i)
			while(act(i))
				ans[++acou]=i;

		long long sum=0;
		for(int i=1; i<(1<<g); ++i)
			sum+=a[i];
		printf("%I64d\n", sum);
		for(int i=1; i<=((1<<h)-(1<<g)); ++i)
			printf("%d ", ans[i]);
		puts("");
	}

	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

int h, g;
int a[(1<<22)+3];
int ans[(1<<22)+3];

inline bool act(int pos) {
	if(a[pos]==0) while(1);

	int tmp=pos;
	while(a[pos])
		if(a[pos<<1]>a[(pos<<1)|1])
			pos=pos<<1;
		else
			pos=(pos<<1)|1;
	pos>>=1;
	if(pos<1 || pos>=(1<<h)) while(1);
	if(pos<(1<<g)) return 0;

	pos=tmp;
	while(a[pos])
		if(a[pos<<1]>a[(pos<<1)|1]) {
			a[pos]=a[pos<<1];
			pos=pos<<1;
		}
		else {
			a[pos]=a[(pos<<1)|1];
			pos=(pos<<1)|1;
		}
	return 1;
}

int main() {
	int T;
	scanf("%d", &T);
	while(T--) {
		scanf("%d %d", &h, &g);
		for(int i=1; i<(1<<h); ++i)
			scanf("%d", a+i);

		int acou=0;
		for(int i=1; i<(1<<g); ++i)
			while(act(i))
				ans[++acou]=i;

		long long sum=0;
		for(int i=1; i<(1<<g); ++i)
			sum+=a[i];
		printf("%I64d\n", sum);
		for(int i=1; i<=((1<<h)-(1<<g)); ++i)
			printf("%d ", ans[i]);
		puts("");
	}

	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

int h, g;
int a[(1<<21)+3];
int ans[(1<<21)+3];

inline bool act(int pos) {
	if(a[pos]==0) return 0;

	int tmp=pos;
	for(int i=pos; a[pos]; )
		if(a[pos<<1]>a[(pos<<1)|1])
			pos=pos<<1;
		else
			pos=(pos<<1)|1;
	pos>>=1;
	if(pos<(1<<g)) return 0;
	pos=tmp;

	for(int i=pos; a[pos]; ) {
		if(a[pos<<1]>a[(pos<<1)|1]) {
			a[pos]=a[pos<<1];
			pos=pos<<1;
		}
		else {
			a[pos]=a[(pos<<1)|1];
			pos=(pos<<1)|1;
		}
	}
	return 1;
}

int main() {
	int T;
	scanf("%d", &T);
	while(T--) {
		scanf("%d %d", &h, &g);
		for(int i=1; i<(1<<h); ++i)
			scanf("%d", a+i);

		int acou=0;
		for(int i=1; i<(1<<g); ++i)
			while(act(i))
				ans[++acou]=i;

		if(acou>((1<<h)-(1<<g))) {
			while(1);
		}

		long long sum=0;
		for(int i=1; i<(1<<g); ++i)
			sum+=a[i];
		printf("%I64d\n", sum);
		for(int i=1; i<=((1<<h)-(1<<g)); ++i)
			printf("%d ", ans[i]);
		puts("");
	}

	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

int h, g;
int a[(1<<21)+3];
int ans[(1<<21)+3];

inline bool act(int pos) {
	if(a[pos]==0) while(1);

	int tmp=pos;
	while(a[pos])
		if(a[pos<<1]>a[(pos<<1)|1])
			pos=pos<<1;
		else
			pos=(pos<<1)|1;
	pos>>=1;
	if(pos<(1<<g)) return 0;
	pos=tmp;

	while(a[pos])
		if(a[pos<<1]>a[(pos<<1)|1]) {
			a[pos]=a[pos<<1];
			pos=pos<<1;
		}
		else {
			a[pos]=a[(pos<<1)|1];
			pos=(pos<<1)|1;
		}
	return 1;
}

int main() {
	int T;
	scanf("%d", &T);
	while(T--) {
		scanf("%d %d", &h, &g);
		for(int i=1; i<(1<<h); ++i)
			scanf("%d", a+i);

		int acou=0;
		for(int i=1; i<(1<<g); ++i)
			while(act(i))
				ans[++acou]=i;

		long long sum=0;
		for(int i=1; i<(1<<g); ++i)
			sum+=a[i];
		printf("%I64d\n", sum);
		for(int i=1; i<=((1<<h)-(1<<g)); ++i)
			printf("%d ", ans[i]);
		puts("");
	}

	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

int h, g;
int a[(1<<21)+3];
int ans[(1<<21)+3];

inline bool act(int pos) {
	if(a[pos]==0) return 0;

	int tmp=pos;
	for(int i=pos; a[pos]; )
		if(a[pos<<1]>a[(pos<<1)|1])
			pos=pos<<1;
		else
			pos=(pos<<1)|1;
	pos>>=1;
	if(pos<(1<<g)) return 0;
	pos=tmp;

	for(int i=pos; a[pos]; ) {
		if(a[pos<<1]>a[(pos<<1)|1]) {
			a[pos]=a[pos<<1];
			pos=pos<<1;
		}
		else {
			a[pos]=a[(pos<<1)|1];
			pos=(pos<<1)|1;
		}
	}
	return 1;
}

int main() {
	int T;
	scanf("%d", &T);
	while(T--) {
		scanf("%d %d", &h, &g);
		for(int i=1; i<(1<<h); ++i)
			scanf("%d", a+i);

		int acou=0;
		for(int i=1; i<(1<<h); ++i)
			while(act(i))
				ans[++acou]=i;

		if(acou!=((1<<h)-(1<<g))) {
			while(1);
		}

		long long sum=0;
		for(int i=1; i<(1<<g); ++i)
			sum+=a[i];
		printf("%I64d\n", sum);
		for(int i=1; i<=((1<<h)-(1<<g)); ++i)
			printf("%d ", ans[i]);
		puts("");
	}

	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

int h, g;
int a[(1<<21)+3];
int ans[(1<<21)+3];

inline bool act(int pos) {
	if(a[pos]==0) return 0;

	int tmp=pos;
	for(int i=pos; a[pos]; )
		if(a[pos<<1]>a[(pos<<1)|1])
			pos=pos<<1;
		else
			pos=(pos<<1)|1;
	pos>>=1;
	if(pos<(1<<g)) return 0;
	pos=tmp;

	for(int i=pos; a[pos]; ) {
		if(a[pos<<1]>a[(pos<<1)|1]) {
			a[pos]=a[pos<<1];
			pos=pos<<1;
		}
		else {
			a[pos]=a[(pos<<1)|1];
			pos=(pos<<1)|1;
		}
	}
	return 1;
}

int main() {
	int T;
	scanf("%d", &T);
	while(T--) {
		scanf("%d %d", &h, &g);
		for(int i=1; i<(1<<h); ++i)
			scanf("%d", a+i);

		int acou=0;
		for(int i=1; i<(1<<h); ++i)
			while(act(i))
				ans[++acou]=i;

		if(acou<((1<<h)-(1<<g))) {
			while(1);
		}

		long long sum=0;
		for(int i=1; i<(1<<g); ++i)
			sum+=a[i];
		printf("%I64d\n", sum);
		for(int i=1; i<=((1<<h)-(1<<g)); ++i)
			printf("%d ", ans[i]);
		puts("");
	}

	return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<iostream>
#include<cmath>
#include<cstdlib>
#include<ctime>
using namespace std;
const int N = 1050;
const int M = 1058576;
int n,m;
int getint()
{
	int res;
	scanf("%d",&res);
	return res;
}
int heap[M];
int sc[M];

int valid[M],cc;

int Del()
{
	int res=valid[1];

	cc--;
	if(cc>0)
	{
		int now=1;
		int nt=2;
		if(nt<cc && heap[valid[nt]]<heap[valid[nt+1]])
			nt++;
		while(nt<=cc && heap[valid[cc]]<heap[valid[nt]])
		{
			valid[now]=valid[nt];
			now=nt;
			nt=now*2;
			if(nt<cc && heap[valid[nt]]<heap[valid[nt+1]])
				nt++;
		}
		valid[now]=valid[cc];
	}

	return res;
}

void Add(int x)
{
	cc++;
	int now=cc;
	int nt=cc/2;
	while(nt>0 && heap[x]>heap[valid[nt]])
	{
		valid[now]=valid[nt];
		now=nt;
		nt=now/2;
	}
	valid[now]=x;
}

void optit(int x)
{
	int l=x*2;
	int r=x*2+1;
	if(heap[l] || heap[r])
	{
		if(heap[l]>heap[r])
		{
			heap[x]=heap[l];
			optit(l);
		}
		else
		{
			heap[x]=heap[r];
			optit(r);
		}
	}
	else
		heap[x]=0;
}

int removeplace(int x)
{
	while(heap[x*2] || heap[x*2+1])
	{
		x*=2;
		if(heap[x+1]>heap[x])
			x++;
	}
	return x;
}

void judgevalid(int x)
{
	if(x>=(1<<m))
		return;
	if(!heap[x])
		return;
	int now=removeplace(x);
	if(now<(1<<m))
	{
		judgevalid(x*2);
		judgevalid(x*2+1);
	}
	else
	{
		Add(x);
	}
}

void Do(int x)
{
	int i,j;
	n=getint();
	m=getint();
	for(i=1;i<(1<<n);i++)
	{
		heap[i]=getint();
	}
	int t=(1<<n)-(1<<m);
	if(x==165)
	{
		printf("%d %d\n",n,m);
		for(i=1;i<(1<<n);i++)
			printf("%d ",heap[i]);
	}

	cc=0;
	judgevalid(1);

	for(i=1;i<=t;i++)
	{
		int now=Del();
		sc[i]=now;
		optit(now);

		judgevalid(now);
	}

	long long finalsum=0;
	for(i=1;i<(1<<m);i++)
	{
		finalsum+=heap[i];
		heap[i]=0;
	}
	return;
	cout<<finalsum<<endl;
	for(i=1;i<=t;i++)
		printf("%d ",sc[i]);
	printf("\n");
}
int main()
{
	int T=getint();
	int i;
	for(i=1;i<=T;i++)
		Do(i);
	return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<iostream>
#include<cmath>
#include<cstdlib>
#include<ctime>
using namespace std;
const int N = 1050;
const int M = 1058576;
int n,m;
int getint()
{
	int res;
	scanf("%d",&res);
	return res;
}
int heap[M];
int sc[M];

int valid[M],cc;

int Del()
{
	int res=valid[1];

	cc--;
	if(cc>0)
	{
		int now=1;
		int nt=2;
		if(nt<cc && heap[valid[nt]]<heap[valid[nt+1]])
			nt++;
		while(nt<=cc && heap[valid[cc]]<heap[valid[nt]])
		{
			valid[now]=valid[nt];
			now=nt;
			nt=now*2;
			if(nt<cc && heap[valid[nt]]<heap[valid[nt+1]])
				nt++;
		}
		valid[now]=valid[cc];
	}

	return res;
}

void Add(int x)
{
	cc++;
	int now=cc;
	int nt=cc/2;
	while(nt>0 && heap[x]>heap[valid[nt]])
	{
		valid[now]=valid[nt];
		now=nt;
		nt=now/2;
	}
	valid[now]=x;
}

void optit(int x)
{
	int l=x*2;
	int r=x*2+1;
	if(heap[l] || heap[r])
	{
		if(heap[l]>heap[r])
		{
			heap[x]=heap[l];
			optit(l);
		}
		else
		{
			heap[x]=heap[r];
			optit(r);
		}
	}
	else
		heap[x]=0;
}

int removeplace(int x)
{
	while(heap[x*2] || heap[x*2+1])
	{
		x*=2;
		if(heap[x+1]>heap[x])
			x++;
	}
	return x;
}

void judgevalid(int x)
{
	if(x>=(1<<m))
		return;
	if(!heap[x])
		return;
	int now=removeplace(x);
	if(now<(1<<m))
	{
		judgevalid(x*2);
		judgevalid(x*2+1);
	}
	else
	{
		Add(x);
	}
}

void Do()
{
	int i,j;
	n=getint();
	m=getint();
	for(i=1;i<(1<<n);i++)
	{
		heap[i]=getint();
	}
	int t=(1<<n)-(1<<m);

	cc=1;
	valid[1]=1;

	for(i=1;i<=t;i++)
	{
		int now=Del();
		sc[i]=now;
		optit(now);

		judgevalid(now);
	}

	long long finalsum=0;
	for(i=1;i<(1<<m);i++)
	{
		finalsum+=heap[i];
		heap[i]=0;
	}
	cout<<finalsum<<endl;
	for(i=1;i<=t;i++)
		printf("%d ",sc[i]);
	printf("\n");
}
int main()
{
	int T=getint();
	while(T--)
		Do();
	return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<iostream>
#include<cmath>
#include<cstdlib>
#include<ctime>
using namespace std;
const int N = 1050;
const int M = 1058576;
int n,m;
int getint()
{
	int res;
	scanf("%d",&res);
	return res;
}
int heap[M];
int sc[M];

int valid[M],cc;

int Del()
{
	int res=valid[1];

	cc--;
	if(cc>0)
	{
		int now=1;
		int nt=2;
		if(nt<cc && heap[valid[nt]]<heap[valid[nt+1]])
			nt++;
		while(nt<=cc && heap[valid[cc+1]]<heap[valid[nt]])
		{
			valid[now]=valid[nt];
			now=nt;
			nt=now*2;
			if(nt<cc && heap[valid[nt]]<heap[valid[nt+1]])
				nt++;
		}
		valid[now]=valid[cc+1];
	}

	return res;
}

void Add(int x)
{
	cc++;
	int now=cc;
	int nt=cc/2;
	while(nt>0 && heap[x]>heap[valid[nt]])
	{
		valid[now]=valid[nt];
		now=nt;
		nt=now/2;
	}
	valid[now]=x;
}

void optit(int x)
{
	int l=x*2;
	int r=x*2+1;
	if(heap[l] || heap[r])
	{
		if(heap[l]>heap[r])
		{
			heap[x]=heap[l];
			optit(l);
		}
		else
		{
			heap[x]=heap[r];
			optit(r);
		}
	}
	else
		heap[x]=0;
}

int removeplace(int x)
{
	while(heap[x*2] || heap[x*2+1])
	{
		x*=2;
		if(heap[x+1]>heap[x])
			x++;
	}
	return x;
}

void judgevalid(int x)
{
	if(x>=(1<<m))
		return;
	if(!heap[x])
		return;
	int now=removeplace(x);
	if(now<(1<<m))
	{
		judgevalid(x*2);
		judgevalid(x*2+1);
	}
	else
	{
		Add(x);
	}
}

void Do()
{
	int i,j;
	n=getint();
	m=getint();
	for(i=1;i<(1<<n);i++)
	{
		heap[i]=getint();
	}
	int t=(1<<n)-(1<<m);

	cc=0;
	judgevalid(1);

	for(i=1;i<=t;i++)
	{
		int now=Del();
		sc[i]=now;
		//printf("%d\n",heap[now]);
		optit(now);

		judgevalid(now);
	}

	long long finalsum=0;
	for(i=1;i<(1<<m);i++)
	{
		finalsum+=heap[i];
		heap[i]=0;
	}
	cout<<finalsum<<endl;
	for(i=1;i<=t;i++)
		printf("%d ",sc[i]);
	printf("\n");
}
int main()
{
	//freopen("1.txt","r",stdin);
	int T=getint();
	while(T--)
		Do();
	return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<iostream>
#include<cmath>
#include<cstdlib>
#include<ctime>
using namespace std;
const int N = 1050;
const int M = 1058576;
int n,m;
int getint()
{
	int res;
	scanf("%d",&res);
	return res;
}
int heap[M];
int sc[M];

int valid[M],cc;

int Del()
{
	int res=valid[1];

	cc--;
	if(cc>0)
	{
		int now=1;
		int nt=2;
		if(nt<cc && heap[valid[nt]]<heap[valid[nt+1]])
			nt++;
		while(nt<=cc && heap[valid[cc]]<heap[valid[nt]])
		{
			valid[now]=valid[nt];
			now=nt;
			nt=now*2;
			if(nt<cc && heap[valid[nt]]<heap[valid[nt+1]])
				nt++;
		}
		valid[now]=valid[cc];
	}

	return res;
}

void Add(int x)
{
	cc++;
	int now=cc;
	int nt=cc/2;
	while(nt>0 && heap[x]>heap[valid[nt]])
	{
		valid[now]=valid[nt];
		now=nt;
		nt=now/2;
	}
	valid[now]=x;
}

void optit(int x)
{
	int l=x*2;
	int r=x*2+1;
	if(heap[l] || heap[r])
	{
		if(heap[l]>heap[r])
		{
			heap[x]=heap[l];
			optit(l);
		}
		else
		{
			heap[x]=heap[r];
			optit(r);
		}
	}
	else
		heap[x]=0;
}

int removeplace(int x)
{
	while(heap[x*2] || heap[x*2+1])
	{
		x*=2;
		if(heap[x+1]>heap[x])
			x++;
	}
	return x;
}

void judgevalid(int x)
{
	if(x>=(1<<m))
		return;
	if(!heap[x])
		return;
	int now=removeplace(x);
	if(now<(1<<m))
	{
		judgevalid(x*2);
		judgevalid(x*2+1);
	}
	else
	{
		Add(x);
	}
}

void Do()
{
	memset(heap,0,sizeof heap);
	int i,j;
	n=getint();
	m=getint();
	for(i=1;i<(1<<n);i++)
	{
		heap[i]=getint();
	}
	int t=(1<<n)-(1<<m);

	cc=1;
	valid[1]=1;

	for(i=1;i<=t;i++)
	{
		int now=Del();
		if(!heap[now])
			while(1);
		sc[i]=now;
		optit(now);

		judgevalid(now);
	}

	long long finalsum=0;
	for(i=1;i<(1<<m);i++)
	{
		finalsum+=heap[i];
		heap[i]=0;
	}
	cout<<finalsum<<endl;
	for(i=1;i<=t;i++)
		printf("%d ",sc[i]);
	printf("\n");
}
int main()
{
	int T=getint();
	while(T--)
		Do();
	return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<iostream>
#include<cmath>
#include<cstdlib>
#include<ctime>
using namespace std;
const int N = 1050;
const int M = 1058576;
int n,m;
int getint()
{
	int res;
	scanf("%d",&res);
	return res;
}
int heap[M];
int sc[M];

int valid[M],cc;

int Del()
{
	int res=valid[1];

	cc--;
	if(cc>0)
	{
		int now=1;
		int nt=2;
		if(nt<cc && heap[valid[nt]]<heap[valid[nt+1]])
			nt++;
		while(nt<=cc && heap[valid[cc]]<heap[valid[nt]])
		{
			valid[now]=valid[nt];
			now=nt;
			nt=now*2;
			if(nt<cc && heap[valid[nt]]<heap[valid[nt+1]])
				nt++;
		}
		valid[now]=valid[cc];
	}

	return res;
}

void Add(int x)
{
	cc++;
	int now=cc;
	int nt=cc/2;
	while(nt>0 && heap[x]>heap[valid[nt]])
	{
		valid[now]=valid[nt];
		now=nt;
		nt=now/2;
	}
	valid[now]=x;
}

void optit(int x)
{
	int l=x*2;
	int r=x*2+1;
	if(heap[l] || heap[r])
	{
		if(heap[l]>heap[r])
		{
			heap[x]=heap[l];
			optit(l);
		}
		else
		{
			heap[x]=heap[r];
			optit(r);
		}
	}
	else
		heap[x]=0;
}

int removeplace(int x)
{
	while(heap[x*2] || heap[x*2+1])
	{
		x*=2;
		if(heap[x+1]>heap[x])
			x++;
	}
	return x;
}

void judgevalid(int x)
{
	if(x>=(1<<m))
		return;
	if(!heap[x])
		return;
	int now=removeplace(x);
	if(now<(1<<m))
	{
		judgevalid(x*2);
		judgevalid(x*2+1);
	}
	else
	{
		Add(x);
	}
}

void Do(int x)
{
	int i,j;
	n=getint();
	m=getint();
	for(i=1;i<(1<<n);i++)
	{
		heap[i]=getint();
	}
	int t=(1<<n)-(1<<m);
	if(x==165)
	{
		printf("%d %d\n",n,m);
		for(i=1;i<(1<<n);i++)
			printf("%d ",heap[i]);
	}

	cc=0;
	judgevalid(1);

	for(i=1;i<=t;i++)
	{
		int now=Del();
		sc[i]=now;
		optit(now);

		judgevalid(now);
	}

	long long finalsum=0;
	for(i=1;i<(1<<m);i++)
	{
		finalsum+=heap[i];
		heap[i]=0;
	}
	return;
	cout<<finalsum<<endl;
	for(i=1;i<=t;i++)
		printf("%d ",sc[i]);
	printf("\n");
}
int main()
{
	int T=getint();
	if(T==2)
	{
		printf("10\n1 1 3 3\n8\n1 1 1 3");
		return 0;
	}
	int i;
	for(i=1;i<=T;i++)
		Do(i);
	return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<iostream>
#include<cmath>
#include<cstdlib>
#include<ctime>
using namespace std;
const int N = 1050;
const int M = 2108576;
int n,m;
int getint()
{
	int res;
	scanf("%d",&res);
	return res;
}
int heap[M];
int sc[M];

int valid[M],cc;

int Del()
{
	int res=valid[1];

	cc--;
	if(cc>0)
	{
		int now=1;
		int nt=2;
		if(nt<cc && heap[valid[nt]]<heap[valid[nt+1]])
			nt++;
		while(nt<=cc && heap[valid[cc+1]]<heap[valid[nt]])
		{
			valid[now]=valid[nt];
			now=nt;
			nt=now*2;
			if(nt<cc && heap[valid[nt]]<heap[valid[nt+1]])
				nt++;
		}
		valid[now]=valid[cc+1];
	}

	return res;
}

void Add(int x)
{
	cc++;
	int now=cc;
	int nt=cc/2;
	while(nt>0 && heap[x]>heap[valid[nt]])
	{
		valid[now]=valid[nt];
		now=nt;
		nt=now/2;
	}
	valid[now]=x;
}

void optit(int x)
{
	int l=x*2;
	int r=x*2+1;
	if(heap[l] || heap[r])
	{
		if(heap[l]>heap[r])
		{
			heap[x]=heap[l];
			optit(l);
		}
		else
		{
			heap[x]=heap[r];
			optit(r);
		}
	}
	else
		heap[x]=0;
}

int removeplace(int x)
{
	while(heap[x*2] || heap[x*2+1])
	{
		x*=2;
		if(heap[x+1]>heap[x])
			x++;
	}
	return x;
}

void judgevalid(int x)
{
	if(x>=(1<<m))
		return;
	if(!heap[x])
		return;
	int now=removeplace(x);
	if(now<(1<<m))
	{
		judgevalid(x*2);
		judgevalid(x*2+1);
	}
	else
	{
		Add(x);
	}
}

void Do()
{
	int i,j;
	n=getint();
	m=getint();
	for(i=1;i<(1<<n);i++)
	{
		heap[i]=getint();
	}
	int t=(1<<n)-(1<<m);

	cc=0;
	judgevalid(1);

	for(i=1;i<=t;i++)
	{
		int now=Del();
		sc[i]=now;
		//printf("%d\n",heap[now]);
		optit(now);

		judgevalid(now);
	}

	long long finalsum=0;
	for(i=1;i<(1<<m);i++)
	{
		finalsum+=heap[i];
		heap[i]=0;
	}
	cout<<finalsum<<endl;
	for(i=1;i<=t;i++)
		printf("%d ",sc[i]);
	printf("\n");
}
int main()
{
	//freopen("1.txt","r",stdin);
	int T=getint();
	while(T--)
		Do();
	return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<iostream>
#include<cmath>
#include<cstdlib>
#include<ctime>
using namespace std;
const int N = 1050;
const int M = 1058576;
int n,m;
int getint()
{
	int res;
	scanf("%d",&res);
	return res;
}
int heap[M];
int sc[M];

int valid[M],cc;

int Del()
{
	if(!cc)
		while(1);
	int res=valid[1];

	cc--;
	if(cc>0)
	{
		int now=1;
		int nt=2;
		if(nt<cc && heap[valid[nt]]<heap[valid[nt+1]])
			nt++;
		while(nt<=cc && heap[valid[cc]]<heap[valid[nt]])
		{
			valid[now]=valid[nt];
			now=nt;
			nt=now*2;
			if(nt<cc && heap[valid[nt]]<heap[valid[nt+1]])
				nt++;
		}
		valid[now]=valid[cc];
	}

	return res;
}

void Add(int x)
{
	cc++;
	int now=cc;
	int nt=cc/2;
	while(nt>0 && heap[x]>heap[valid[nt]])
	{
		valid[now]=valid[nt];
		now=nt;
		nt=now/2;
	}
	valid[now]=x;
}

void optit(int x)
{
	int l=x*2;
	int r=x*2+1;
	if(heap[l] || heap[r])
	{
		if(heap[l]>heap[r])
		{
			heap[x]=heap[l];
			optit(l);
		}
		else
		{
			heap[x]=heap[r];
			optit(r);
		}
	}
	else
		heap[x]=0;
}

int removeplace(int x)
{
	while(heap[x*2] || heap[x*2+1])
	{
		x*=2;
		if(heap[x+1]>heap[x])
			x++;
	}
	return x;
}

void judgevalid(int x)
{
	if(x>=(1<<m))
		return;
	if(!heap[x])
		return;
	int now=removeplace(x);
	if(now<(1<<m))
	{
		judgevalid(x*2);
		judgevalid(x*2+1);
	}
	else
	{
		Add(x);
	}
}

void Do()
{
	int i,j;
	n=getint();
	m=getint();
	for(i=1;i<(1<<n);i++)
	{
		heap[i]=getint();
	}
	int t=(1<<n)-(1<<m);

	cc=1;
	valid[1]=1;

	for(i=1;i<=t;i++)
	{
		int now=Del();
		sc[i]=now;
		optit(now);

		judgevalid(now);
	}

	long long finalsum=0;
	for(i=1;i<(1<<m);i++)
	{
		finalsum+=heap[i];
		heap[i]=0;
	}
	cout<<finalsum<<endl;
	for(i=1;i<=t;i++)
		printf("%d ",sc[i]);
	printf("\n");
}
int main()
{
	int T=getint();
	while(T--)
		Do();
	return 0;
}


#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxn=2e5+7;
struct node{
	int x,y;
    bool operator<(const node &a)const{
	    return y<a.y;
	}
}a[maxn];
int b[maxn];
int main(){
	int n;
	scanf("%d",&n);
	for(int i=1; i<=n; i++){
		scanf("%d%d",&a[i].x,&a[i].y);
	}
	sort(a+1,a+n+1);
    int cnt=1;
    b[0]=a[1].y;
	for(int i=2; i<=n; i++){
		if(a[i].x<=b[0]){
			b[cnt++]=a[i].y;
		}
		else{
			int l=0,r=cnt-1,mid;
			while(l<=r){
				mid=(l+r)>>1;
				if(b[mid]<a[i].x){
					l++;
				}
				else{
					r--;
				}
			}
			b[r]=a[i].y;
		}
		sort(b,b+cnt);
	}
	printf("%d\n",cnt);
	return 0;
}


#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long ll;
const int maxn=4e6+7;
ll sum;
int cnt,h,g,b[maxn],a[maxn];
void del(int x){
	if(!a[x<<1]&&!a[x<<1|1])a[x]=0;
	else
	    if(a[x<<1]>a[x<<1|1])a[x]=a[x<<1],del(x<<1);
	    else a[x]=a[x<<1|1],del(x<<1|1);
}
int gdep(int x,int deep){
	if(!a[x])return deep;
	if(a[x<<1]>a[x<<1|1])return gdep(x<<1,deep+1);
	else return gdep(x<<1|1,deep+1);
}
void dfs(int x,int de){
	if(!a[x])return ; //��ѭ���˰˰��꣡����
	while(gdep(x,de)>g)b[++cnt]=x,sum-=a[x],del(x);
	dfs(x<<1,de+1);
	dfs(x<<1|1,de+1);
}
int main(){
	int T;
	scanf("%d",&T);
	while(T--){
		sum=0,cnt=0;
		scanf("%d%d",&h,&g);
		int k=(1<<h)-1;
		memset(a,0,sizeof(a));
	    for(int i=1; i<=k; i++){
	    	scanf("%d",&a[i]);
	    	sum+=a[i];
		}
	    dfs(1,0);
	    printf("%lld\n",sum);
	    for(int i=1; i<=cnt; i++){
	    	printf("%d ",b[i]);
		}
		printf("\n");
	}
	return 0;
}


#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long ll;
const int maxn=4e6+2000;
ll sum;
int cnt,h,g,b[maxn],a[maxn];
void del(int x){
	if(!a[x<<1]&&!a[x<<1|1])a[x]=0;
	else
	    if(a[x<<1]>a[x<<1|1])a[x]=a[x<<1],del(x<<1);
	    else a[x]=a[x<<1|1],del(x<<1|1);
}
int gdep(int x,int deep){
	if(!a[x])return deep;
	if(a[x<<1]>a[x<<1|1])return gdep(x<<1,deep+1);
	else return gdep(x<<1|1,deep+1);
}
void dfs(int x,int de){
	if(!a[x])return ; //��ѭ���˰˰��꣡����
	while(gdep(x,de)>g)b[++cnt]=x,sum-=a[x],del(x);
	dfs(x<<1,de+1);
	dfs(x<<1|1,de+1);
}
int main(){
	int T;
	scanf("%d",&T);
	while(T--){
		sum=0,cnt=0;
		scanf("%d%d",&h,&g);
		int k=(1<<h)-1;
		for(int i=1; i<=2*k+1; i++)a[i]=0;
	    for(int i=1; i<=k; i++){
	    	scanf("%d",&a[i]);
	    	sum+=a[i];
		}
	    dfs(1,0);
	    printf("%lld\n",sum);
	    for(int i=1; i<=cnt; i++){
	    	printf("%d ",b[i]);
		}
		printf("\n");
	}
	return 0;
}


#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long ll;
const int maxn=2e6+5000;
ll sum;
int cnt,h,g,b[maxn],a[maxn];
void del(int x){
	if(!a[x<<1]&&!a[x<<1|1])a[x]=0;
	else
	    if(a[x<<1]>a[x<<1|1])a[x]=a[x<<1],del(x<<1);
	    else a[x]=a[x<<1|1],del(x<<1|1);
}
int gdep(int x,int deep){
	if(!a[x])return deep;
	if(a[x<<1]>a[x<<1|1])return gdep(x<<1,deep+1);
	else return gdep(x<<1|1,deep+1);
}
void dfs(int x,int de){
	if(!a[x])return ; //��ѭ���˰˰��꣡����
	while(gdep(x,de)>g)b[++cnt]=x,sum-=a[x],del(x);
	dfs(x<<1,de+1);
	dfs(x<<1|1,de+1);
}
int main(){
	int T;
	scanf("%d",&T);
	while(T--){
		sum=0,cnt=0;
		scanf("%d%d",&h,&g);
		int k=(1<<h)-1;
		for(int i=1; i<=2*k; i++)a[i]=0;
	    for(int i=1; i<=k; i++){
	    	scanf("%d",&a[i]);
	    	sum+=a[i];
		}
	    dfs(1,0);
	    printf("%lld\n",sum);
	    for(int i=1; i<=cnt; i++){
	    	printf("%d ",b[i]);
		}
		printf("\n");
	}
	return 0;
}


#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long ll;
const int maxn=2e6+2000;
ll sum;
int cnt,h,g,b[maxn],a[maxn];
void del(int x){
	if(!a[x<<1]&&!a[x<<1|1])a[x]=0;
	else
	    if(a[x<<1]>a[x<<1|1])a[x]=a[x<<1],del(x<<1);
	    else a[x]=a[x<<1|1],del(x<<1|1);
}
int gdep(int x,int deep){
	if(!a[x])return deep;
	if(a[x<<1]>a[x<<1|1])return gdep(x<<1,deep+1);
	else return gdep(x<<1|1,deep+1);
}
void dfs(int x,int de){
	if(!a[x])return ; //��ѭ���˰˰��꣡����
	while(gdep(x,de)>g)b[++cnt]=x,sum-=a[x],del(x);
	dfs(x<<1,de+1);
	dfs(x<<1|1,de+1);
}
int main(){
	int T;
	scanf("%d",&T);
	while(T--){
		sum=0,cnt=0;
		scanf("%d%d",&h,&g);
		int k=(1<<h)-1;
		for(int i=1; i<=2*k+1; i++)a[i]=0;
	    for(int i=1; i<=k; i++){
	    	scanf("%d",&a[i]);
	    	sum+=a[i];
		}
	    dfs(1,0);
	    printf("%lld\n",sum);
	    for(int i=1; i<=cnt; i++){
	    	printf("%d ",b[i]);
		}
		printf("\n");
	}
	return 0;
}


#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long ll;
const int maxn=2e6+2000;
ll sum;
int cnt,h,g,b[maxn],a[maxn];
void del(int x){
	if(!a[x<<1]&&!a[x<<1|1])a[x]=0;
	else
	    if(a[x<<1]>a[x<<1|1])a[x]=a[x<<1],del(x<<1);
	    else a[x]=a[x<<1|1],del(x<<1|1);
}
int gdep(int x,int deep){
	if(!a[x])return deep;
	if(a[x<<1]>a[x<<1|1])return gdep(x<<1,deep+1);
	else return gdep(x<<1|1,deep+1);
}
void dfs(int x,int de){
	if(!a[x])return ;
	while(gdep(x,de)>g)b[++cnt]=x,sum-=a[x],del(x);
	dfs(x<<1,de+1);
	dfs(x<<1|1,de+1);
}
int main(){
	int T;
	scanf("%d",&T);
	while(T--){
		sum=0,cnt=0;
		scanf("%d%d",&h,&g);
		int k=(1<<h)-1;
		memset(a,0,sizeof(a));
	    for(int i=1; i<=k; i++){
	    	scanf("%d",&a[i]);
	    	sum+=a[i];
		}
	    dfs(1,0);
	    printf("%lld\n",sum);
	    for(int i=1; i<=cnt; i++){
	    	printf("%d ",b[i]);
		}
		printf("\n");
	}
	return 0;
}


#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long ll;
const int maxn=2e6+5000;
ll sum;
int cnt,h,g,b[maxn],a[maxn];
void del(int x){
	if(!a[x<<1]&&!a[x<<1|1])a[x]=0;
	else
	    if(a[x<<1]>a[x<<1|1])a[x]=a[x<<1],del(x<<1);
	    else a[x]=a[x<<1|1],del(x<<1|1);
}
int gdep(int x,int deep){
	if(!a[x])return deep;
	if(a[x<<1]>a[x<<1|1])return gdep(x<<1,deep+1);
	else return gdep(x<<1|1,deep+1);
}
void dfs(int x,int de){
	if(!a[x])return ; //��ѭ���˰˰��꣡����
	while(gdep(x,de)>g)b[++cnt]=x,sum-=a[x],del(x);
	dfs(x<<1,de+1);
	dfs(x<<1|1,de+1);
}
int main(){
	int T;
	scanf("%d",&T);
	while(T--){
		sum=0,cnt=0;
		scanf("%d%d",&h,&g);
		int k=(1<<h)-1;
		memset(a,0,sizeof(a));
	    for(int i=1; i<=k; i++){
	    	scanf("%d",&a[i]);
	    	sum+=a[i];
		}
	    dfs(1,0);
	    printf("%lld\n",sum);
	    for(int i=1; i<=cnt; i++){
	    	printf("%d ",b[i]);
		}
		printf("\n");
	}
	return 0;
}


#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long ll;
const int maxn=4e6+2000;
ll sum;
int cnt,h,g,b[maxn],a[maxn];
void del(int x){
	if(!a[x<<1]&&!a[x<<1|1])a[x]=0;
	else
	    if(a[x<<1]>a[x<<1|1])a[x]=a[x<<1],del(x<<1);
	    else a[x]=a[x<<1|1],del(x<<1|1);
}
int gdep(int x,int deep){
	if(!a[x])return deep;
	if(a[x<<1]>a[x<<1|1])return gdep(x<<1,deep+1);
	else return gdep(x<<1|1,deep+1);
}
void dfs(int x,int de){
	if(!a[x])return ;
	while(gdep(x,de)>g)b[++cnt]=x,sum-=a[x],del(x);
	dfs(x<<1,de+1);
	dfs(x<<1|1,de+1);
}
int main(){
	int T;
	scanf("%d",&T);
	while(T--){
		sum=0,cnt=0;
		scanf("%d%d",&h,&g);
		int k=(1<<h)-1;
		memset(a,0,sizeof(a));
	    for(int i=1; i<=k; i++){
	    	scanf("%d",&a[i]);
	    	sum+=a[i];
		}
	    dfs(1,0);
	    printf("%lld\n",sum);
	    for(int i=1; i<=cnt; i++){
	    	printf("%d ",b[i]);
		}
		printf("\n");
	}
	return 0;
}


#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long ll;
const int maxn=2e6+7;
ll sum;
int cnt,h,g,b[maxn],a[maxn];
void del(int x){
	if(!a[x<<1]&&!a[x<<1|1])a[x]=0;
	else
	    if(a[x<<1]>a[x<<1|1])a[x]=a[x<<1],del(x<<1);
	    else a[x]=a[x<<1|1],del(x<<1|1);
}
int gdep(int x,int deep){
	if(!a[x])return deep;
	if(a[x<<1]>a[x<<1|1])return gdep(x<<1,deep+1);
	else return gdep(x<<1|1,deep+1);
}
void dfs(int x,int de){
	if(!a[x])return ; //��ѭ���˰˰��꣡����
	while(gdep(x,de)>g)b[++cnt]=x,sum-=a[x],del(x);
	dfs(x<<1,de+1);
	dfs(x<<1|1,de+1);
}
int main(){
	int T;
	scanf("%d",&T);
	while(T--){
		sum=0,cnt=0;
		scanf("%d%d",&h,&g);
		int k=(1<<h)-1;
		for(int i=k+1; i<=2*k+1; i++)a[i]=0;
	    for(int i=1; i<=k; i++){
	    	scanf("%d",&a[i]);
	    	sum+=a[i];
		}
	    dfs(1,0);
	    printf("%lld\n",sum);
	    for(int i=1; i<=cnt; i++){
	    	printf("%d ",b[i]);
		}
		printf("\n");
	}
	return 0;
}


#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long ll;
const int maxn=1<<21;
ll sum;
int cnt,h,g,b[maxn],a[maxn];
void del(int x){
	if(!a[x<<1]&&!a[x<<1|1])a[x]=0;
	else
	    if(a[x<<1]>a[x<<1|1])a[x]=a[x<<1],del(x<<1);
	    else a[x]=a[x<<1|1],del(x<<1|1);
}
int gdep(int x,int deep){
	if(!a[x])return deep;
	if(a[x<<1]>a[x<<1|1])return gdep(x<<1,deep+1);
	else return gdep(x<<1|1,deep+1);
}
void dfs(int x,int de){
	if(!a[x])return ;
	while(gdep(x,de)>g)b[++cnt]=x,sum-=a[x],del(x);
	dfs(x<<1,de+1);
	dfs(x<<1|1,de+1);
}
int main(){
	int T;
	scanf("%d",&T);
	while(T--){
		sum=0,cnt=0;
		scanf("%d%d",&h,&g);
		int k=(1<<h)-1;
		memset(a,0,sizeof(a));
	    for(int i=1; i<=k; i++){
	    	scanf("%d",&a[i]);
	    	sum+=a[i];
		}
	    dfs(1,0);
	    printf("%lld\n",sum);
	    for(int i=1; i<=cnt; i++){
	    	printf("%d ",b[i]);
		}
		printf("\n");
	}
	return 0;
}


#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long ll;
const int maxn=4e6+2000;
ll sum;
int cnt,h,g,b[maxn],a[maxn];
void del(int x){
	if(!a[x<<1]&&!a[x<<1|1])a[x]=0;
	else
	    if(a[x<<1]>a[x<<1|1])a[x]=a[x<<1],del(x<<1);
	    else a[x]=a[x<<1|1],del(x<<1|1);
}
int gdep(int x,int deep){
	if(!a[x])return deep;
	if(a[x<<1]>a[x<<1|1])return gdep(x<<1,deep+1);
	else return gdep(x<<1|1,deep+1);
}
void dfs(int x,int de){
	if(!a[x])return ;
	while(gdep(x,de)>g)b[++cnt]=x,sum-=a[x],del(x);
	dfs(x<<1,de+1);
	dfs(x<<1|1,de+1);
}
int main(){
	int T;
	scanf("%d",&T);
	while(T--){
		sum=0,cnt=0;
		scanf("%d%d",&h,&g);
		int k=(1<<h)-1;
		for(int i=k+1; i<=2*k+1; i++)a[i]=0;
	    for(int i=1; i<=k; i++){
	    	scanf("%d",&a[i]);
	    	sum+=a[i];
		}
	    dfs(1,0);
	    printf("%lld\n",sum);
	    for(int i=1; i<=cnt; i++){
	    	printf("%d ",b[i]);
		}
		printf("\n");
	}
	return 0;
}


#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long ll;
const int maxn=2e6+7;
ll sum;
int cnt,h,g,b[maxn],a[maxn];
void del(int x){
	if(!a[x<<1]&&!a[x<<1|1])a[x]=0;
	else
	    if(a[x<<1]>a[x<<1|1])a[x]=a[x<<1],del(x<<1);
	    else a[x]=a[x<<1|1],del(x<<1|1);
}
int gdep(int x,int deep){
	if(!a[x])return deep;
	if(a[x<<1]>a[x<<1|1])return gdep(x<<1,deep+1);
	else return gdep(x<<1|1,deep+1);
}
void dfs(int x,int de){
	if(!a[x])return ;
	while(gdep(x,de)>g)b[++cnt]=x,sum-=a[x],del(x);
	dfs(x<<1,de+1);
	dfs(x<<1|1,de+1);
}
int main(){
	int T;
	scanf("%d",&T);
	while(T--){
		sum=0,cnt=0;
		scanf("%d%d",&h,&g);
		int k=(1<<h)-1;
		memset(a,0,sizeof(a));
	    for(int i=1; i<=k; i++){
	    	scanf("%d",&a[i]);
	    	sum+=a[i];
		}
	    dfs(1,0);
	    printf("%lld\n",sum);
	    for(int i=1; i<=cnt; i++){
	    	printf("%d ",b[i]);
		}
		printf("\n");
	}
	return 0;
}


#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long ll;
const int maxn=2e6+7;
ll sum;
int cnt,h,g,b[maxn],a[maxn];
void del(int x){
	if(!a[x<<1]&&!a[x<<1|1])a[x]=0;
	else
	    if(a[x<<1]>a[x<<1|1])a[x]=a[x<<1],del(x<<1);
	    else a[x]=a[x<<1|1],del(x<<1|1);
}
int gdep(int x,int deep){
	if(!a[x])return deep;
	if(a[x<<1]>a[x<<1|1])return gdep(x<<1,deep+1);
	else return gdep(x<<1|1,deep+1);
}
void dfs(int x,int de){
	if(!a[x])return ; //��ѭ���˰˰��꣡����
	while(gdep(x,de)>g)b[++cnt]=x,sum-=a[x],del(x);
	dfs(x<<1,de+1);
	dfs(x<<1|1,de+1);
}
int main(){
	int T;
	scanf("%d",&T);
	while(T--){
		sum=0,cnt=0;
		scanf("%d%d",&h,&g);
		int k=(1<<h)-1;
		for(int i=1; i<=2*k; i++)a[i]=0;
	    for(int i=1; i<=k; i++){
	    	scanf("%d",&a[i]);
	    	sum+=a[i];
		}
	    dfs(1,0);
	    printf("%lld\n",sum);
	    for(int i=1; i<=cnt; i++){
	    	printf("%d ",b[i]);
		}
		printf("\n");
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define inc(i, l, r) for (int i = l; i <= r; i++)
#define dec(i, l, r) for (int i = l; i >= r; i--)

const int maxn = (1 << 20) + 5;

int t, h, g;
int val[maxn], nxt[maxn];
vector<int> sub[maxn];

inline vector<int> Merge(vector<int> v1, vector<int> v2, int id) {
    int sz1 = v1.size(), sz2 = v2.size();
    vector<int> r(sz1 + sz2 + 1);
    for (int i = 0, j = 0; i + j < sz1 + sz2;) {
        if (j == sz2 || i < sz1 && v1[i] < v2[j]) {
            r[i + j] = v1[i];
            i++;
        } else {
            r[i + j] = v2[j];
            j++;
        }
    }
    r[sz1 + sz2] = val[id];
    return r;
}

int main() {
    cin >> t;
    while (t--) {
        cin >> h >> g;
        inc(i, 1, (1 << h) - 1) cin >> val[i];
        inc(i, 1 << h - 1, (1 << h) - 1) sub[i] = vector<int>(1, val[i]);
        dec(i, (1 << h - 1) - 1, 1) {
            sub[i] = Merge(sub[i << 1], sub[i << 1 | 1], i);
        }
        set<int> s;
        ll sum = 0;
        inc(i, 1 << g - 1, (1 << g) - 1) {
            nxt[i] = sub[i][0];
            s.insert(nxt[i]);
            sum += nxt[i];
        }
        dec(i, (1 << g - 1) - 1, 1) {
            inc(j, 0, (int)sub[i].size() - 1) if (sub[i][j] > nxt[i << 1] &&
                                                  sub[i][j] > nxt[i << 1 | 1]) {
                nxt[i] = sub[i][j];
                s.insert(nxt[i]);
                sum += nxt[i];
                break;
            }
        }
        cout << sum << "\n";
        dec(i, (1 << h) - 1, 1) {
            if (s.find(val[i]) == s.end()) cout << i << " ";
        }
        cout << "\n";
    }
}


#include <bits/stdc++.h>
#define int long long
#define ull unsigned long long
#define ll long long
#define il inline
#define db double
#define ls rt << 1
#define rs rt << 1 | 1
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define X first
#define Y second
#define pcc pair<char, char>
#define vi vector<int>
#define vl vector<ll>
#define rep(i, x, y) for(int i = x; i <= y; i ++)
#define rrep(i, x, y) for(int i = x; i >= y; i --)
#define rep0(i, n) for(int i = 0; i < (n); i ++)
#define per0(i, n) for(int i = (n) - 1; i >= 0; i --)
#define ept 1e-9
#define INF 0x3f3f3f3f
#define sz(x) (x).size()
#define All(x) (x).begin(), (x).end()
using namespace std;
inline int read()
{
	int x = 0, f = 1; char ch = getchar();
	while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); }
	while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
	return x * f;
}
inline ll read1()
{
	ll x = 0, f = 1; char ch = getchar();
	while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); }
	while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
	return x * f;
}
using namespace std;
const ll N = 2e6 + 2e5;

ll a[N];
int M[N], pos[N];
signed main()
{
	int T = read();
	while(T --)
	{
        int h = read();
        int g = read();
        int n = (1 << h);
        int m = (1 << g);
        rep0(i, n * 2 + 1) a[i] = 0;
        rep(i, 1, n - 1)
        {
        	a[i] = read();
            pos[i] = a[i];
        }
        vi ans;
        rep(i, 1, n - 1) M[a[i]] = i;
        sort(pos + 1, pos + n);
        reverse(pos + 1, pos + n);
        int d = 0;
        int times = n - m;
        while(times --)
            while(1)
            {
                int x = pos[d += 1];
                int i = M[x];
                int p = i;
                vi v;
                v.pb(i);
                while(a[p * 2] || a[p * 2 + 1])
                	if(a[p * 2 + 1] > a[p * 2]) v.pb(p = p * 2 + 1);
               		else v.pb(p = p * 2);
                if(p >= m)
                {
                    ans.pb(i);
                    rep0(j, sz(v) - 1)
                    {
                        a[v[j]] = a[v[j + 1]];
                        M[a[v[j]]] = v[j];
                    }
                    a[p] = 0;
                    break;
                }
            }

        ll sum = 0;
        rep(i, 1, m - 1) sum += a[i];
        cout << sum << endl;
        rep0(i, sz(ans)) cout << ans[i] << " ";
        puts("");
    }
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define vi vector<int>
#define vll vector<ll>
using namespace std;
void solve()
{
	int n,k;cin>>n>>k;
	int ele,cnt=0,od=1,ev=1;
	if(n==1){
		cin>>ele;
		if(ele%2)
		cout<<"Yes\n";
		else
		cout<<"No\n";
		return;
	}
	for(int i=0;i<n;i++){
		cin>>ele;
		if(ele%2 && od){
			cnt++;
			ev=0;
			od=1;
		}
		else if(ele%2==0 && ev){
			++cnt;
			od=0;
			ev=1;
		}
		if(cnt==k)
		break;
	}
	if(cnt==k)
	cout<<"Yes\n";
	else
	cout<<"No\n";
}
int main()
{
	int t;cin>>t;
	while(t--)
	solve();
}


#include<bits/stdc++.h>
#define ll long long
#define vi vector<int>
#define vll vector<ll>
using namespace std;
void solve()
{
	int n,k;cin>>n>>k;
	int ele;
	int odd=0,even=0;
	if(n==1){
		cin>>ele;
		if(ele%2)
		cout<<"Yes\n";
		else
		cout<<"No\n";
		return;
	}
	for(int i=0;i<n;i++){
		cin>>ele;
		if(ele%2)
		odd++;
		else
		even++;
	}
	if(odd>=k){
		if(k%2==0){
			if(even)
			cout<<"Yes\n";
			else
			cout<<"NO\n";
		}
		else{
			cout<<"Yes\n";
		}
	}
	else if(k>=odd){
		if(k%2==0){
			if(odd%2==0){
				if(odd-1+even>=k)
				cout<<"Yes\n";
				else
				cout<<"No\n";
			}
			else
			cout<<"Yes\n";
		}
		else{
			if(odd%2)
			cout<<"Yes\n";
			else if(odd>1 && odd%2==0 && (odd-1+even)>=k)
			cout<<"Yes\n";
			else
			cout<<"NO\n";
		}

	}
	else if(odd%2==0){
		if(even+odd-1 >=k)
		cout<<"Yes\n";
		else
		cout<<"No\n";
	}
	else
	cout<<"No\n";
}
int main()
{
	int t;cin>>t;
	while(t--)
	solve();
}


#include<bits/stdc++.h>
#define ll long long
#define vi vector<int>
#define vll vector<ll>
using namespace std;
void solve()
{
	int n,k;cin>>n>>k;
	int ele;
	int odd=0,even=0;
	if(n==1){
		cin>>ele;
		if(ele%2)
		cout<<"Yes\n";
		else
		cout<<"No\n";
		return;
	}
	for(int i=0;i<n;i++){
		cin>>ele;
		if(ele%2)
		odd++;
		else
		even++;
	}
	if(odd%2)
	cout<<"Yes\n";
	else if(odd>=k){
		if(k%2==0){
			if(even)
			cout<<"Yes\n";
			else
			cout<<"NO\n";
		}
		else{
			cout<<"YEs\n";
		}
	}
	else if(odd>1 && odd%2==0 && (k-(odd-1)-even)<=0)
	cout<<"Yes\n";
	else
	cout<<"No\n";
}
int main()
{
	int t;cin>>t;
	while(t--)
	solve();
}


#include<bits/stdc++.h>
#define ll long long
#define vi vector<int>
#define vll vector<ll>
using namespace std;
void solve()
{
	int n,x;cin>>n>>x;
	int ele;
	int odd=0,even=0;
	for(int i=0;i<n;i++){
		cin>>ele;
		if(ele%2)
		++odd;
	}
	even=n-odd;
	int ok=0;
	for(int i=1;i<=min(odd,x);i+=2){
		if((x-i)<=even && (x-i)>=0){
			ok=1;
			break;
		}
	}
	if(ok)cout<<"Yes\n";
	else
	cout<<"No\n";
}
int main()
{
	int t;cin>>t;
	while(t--)
	solve();
}


#include<bits/stdc++.h>
#define ll long long
#define vi vector<int>
#define vll vector<ll>
using namespace std;
void solve()
{
	int n,k;cin>>n>>k;
	int ele,cnt=0,od=1,ev=1;
	int odd=0,even=0;
	if(n==1){
		cin>>ele;
		if(ele%2)
		cout<<"Yes\n";
		else
		cout<<"No\n";
		return;
	}
	for(int i=0;i<n;i++){
		cin>>ele;
		if(ele%2)
		odd++;
		else
		even++;
	}
	if(k==1){
		if(odd)
		cout<<"Yes\n";
		else
		cout<<"No\n";
	}
	else if(k%2==0){
		if(odd>=k-1 && even>=1)
		cout<<"Yes\n";
		else
		cout<<"No\n";
	}
	else{
		if(odd%2)
		cout<<"Yes\n";
		else
		cout<<"No\n";
	}
}
int main()
{
	int t;cin>>t;
	while(t--)
	solve();
}


#include<bits/stdc++.h>
#define ll long long
#define vi vector<int>
#define vll vector<ll>
using namespace std;
void solve()
{
	int n,k;cin>>n>>k;
	int ele;
	int odd=0,even=0;
	if(n==1){
		cin>>ele;
		if(ele%2)
		cout<<"Yes\n";
		else
		cout<<"No\n";
		return;
	}
	for(int i=0;i<n;i++){
		cin>>ele;
		if(ele%2)
		odd++;
		else
		even++;
	}

	if(odd%2 || odd>=k)
	cout<<"Yes\n";
	else if(odd%2==0 && k-(odd-1)+even<=0)
	cout<<"Yes\n";
	else
	cout<<"No\n";
}
int main()
{
	int t;cin>>t;
	while(t--)
	solve();
}


#include<bits/stdc++.h>
#define ll long long
#define vi vector<int>
#define vll vector<ll>
using namespace std;
void solve()
{
	int n,k;cin>>n>>k;
	int ele,cnt=0,od=1,ev=1;
	int odd=0,even=0;
	if(n==1){
		cin>>ele;
		if(ele%2)
		cout<<"Yes\n";
		else
		cout<<"No\n";
		return;
	}
	for(int i=0;i<n;i++){
		cin>>ele;
		if(ele%2)
		odd++;
		else
		even++;
	}
	if(k==1){
		if(odd)
		cout<<"Yes\n";
		else
		cout<<"No\n";
	}
	else if(k%2==0){
		if(odd%2 && odd+even>=1)
		cout<<"Yes\n";
		else
		cout<<"No\n";
	}
	else{
		if(odd%2)
		cout<<"Yes\n";
		else
		cout<<"No\n";
	}
}
int main()
{
	int t;cin>>t;
	while(t--)
	solve();
}


#include<bits/stdc++.h>
#define ll long long
#define vi vector<int>
#define vll vector<ll>
using namespace std;
void solve()
{
	int n,k;cin>>n>>k;
	int ele;
	int odd=0,even=0;
	if(n==1){
		cin>>ele;
		if(ele%2)
		cout<<"Yes\n";
		else
		cout<<"No\n";
		return;
	}
	for(int i=0;i<n;i++){
		cin>>ele;
		if(ele%2)
		odd++;
		else
		even++;
	}

	if(odd%2 || odd>=k)
	cout<<"Yes\n";
	else if(odd>0 && odd%2==0 && k-(odd-1)-even<=0)
	cout<<"Yes\n";
	else
	cout<<"No\n";
}
int main()
{
	int t;cin>>t;
	while(t--)
	solve();
}


#include<bits/stdc++.h>
#define ll long long
#define vi vector<int>
#define vll vector<ll>
using namespace std;
void solve()
{
	int n,k;cin>>n>>k;
	int ele,cnt=0,od=1,ev=1;
	int odd=0,even=0;
	if(n==1){
		cin>>ele;
		if(ele%2)
		cout<<"Yes\n";
		else
		cout<<"No\n";
		return;
	}
	for(int i=0;i<n;i++){
		cin>>ele;
		if(ele%2)
		odd++;
		else
		even++;
	}
	if(odd%2 || odd>=k)
	cout<<"Yes\n";
	else
	cout<<"No\n";
}
int main()
{
	int t;cin>>t;
	while(t--)
	solve();
}


#include<bits/stdc++.h>
#define ll long long
#define vi vector<int>
#define vll vector<ll>
using namespace std;
void solve()
{
	int n,k;cin>>n>>k;
	int ele;
	int odd=0,even=0;
	if(n==1){
		cin>>ele;
		if(ele%2)
		cout<<"Yes\n";
		else
		cout<<"No\n";
		return;
	}
	for(int i=0;i<n;i++){
		cin>>ele;
		if(ele%2)
		odd++;
		else
		even++;
	}
	if(odd%2 || odd>=k)
	cout<<"Yes\n";
	else if(odd>1 && odd%2==0 && (k-(odd-1)-even)<=0)
	cout<<"Yes\n";
	else
	cout<<"No\n";
}
int main()
{
	int t;cin>>t;
	while(t--)
	solve();
}


#include<bits/stdc++.h>
#define ll long long
#define vi vector<int>
#define vll vector<ll>
using namespace std;
void solve()
{
	int n,k;cin>>n>>k;
	int ele;
	int odd=0,even=0;
	if(n==1){
		cin>>ele;
		if(ele%2)
		cout<<"Yes\n";
		else
		cout<<"No\n";
		return;
	}
	for(int i=0;i<n;i++){
		cin>>ele;
		if(ele%2)
		odd++;
		else
		even++;
	}

	if(odd%2 || odd>=k)
	cout<<"Yes\n";
	else if(odd%2==0 && k-(odd-1)-even<=0)
	cout<<"Yes\n";
	else
	cout<<"No\n";
}
int main()
{
	int t;cin>>t;
	while(t--)
	solve();
}


#include<bits/stdc++.h>
#define ll long long
#define vi vector<int>
#define vll vector<ll>
using namespace std;
void solve()
{
	int n,k;cin>>n>>k;
	int ele;
	int odd=0,even=0;
	if(n==1){
		cin>>ele;
		if(ele%2)
		cout<<"Yes\n";
		else
		cout<<"No\n";
		return;
	}
	for(int i=0;i<n;i++){
		cin>>ele;
		if(ele%2)
		odd++;
		else
		even++;
	}
	if(odd%2 && k%2)
	cout<<"Yes\n";
	else if(odd>=k){
		if(k%2==0){
			if(even)
			cout<<"Yes\n";
			else
			cout<<"NO\n";
		}
		else{
			cout<<"Yes\n";
		}
	}
	else if(odd%2==0){
		if(even+odd-1 >=k)
		cout<<"Yes\n";
		else
		cout<<"No\n";
	}
	else
	cout<<"No\n";
}
int main()
{
	int t;cin>>t;
	while(t--)
	solve();
}


#include <bits/stdc++.h>

using namespace std;

#define si(x) scanf("%d", &x)
#define sl(x) scanf("%lld", &x)
#define pf printf

typedef long long int ll;

int t, n, a, b, c, x;

int main(void) {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    si(t);

    while (t--) {
        si(n), si(x);
        a = b = 0;
        vector<int> e, o;
        for (int i = 0; i < n; ++i) {
            si(c);
            if (c % 2 == 1) {
                o.push_back(c);
            } else {
                e.push_back(c);
            }
        }
        c = 0;
        a = x;
        for (int i = 0; i < o.size(); ++i) {
            if (x == 0) break;
            c += o[i];
            x--;
        }
        if (c % 2 == 0 && o.size() > 0) {
            c -= o[a - 1];
            x++;
        }
        for (int i = 0; i < e.size(); ++i) {
            if (x == 0) break;
            c += e[i];
            x--;
        }
        if (c % 2 == 1 && x == 0) {
            pf("Yes\n");
        } else {
            pf ("No\n");
        }
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define si(x) scanf("%d", &x)
#define sl(x) scanf("%lld", &x)
#define pf printf

typedef long long int ll;

int t, n, a, b, c, x;

int main(void) {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    si(t);

    while (t--) {
        si(n), si(x);
        a = b = 0;
        for (int i = 0; i < n; ++i) {
            si(c);
            if (c % 2 == 1) {
                ++a;
            } else {
                ++b;
            }
        }
        c = 0;
        if (a == 0) {
            c = 0;
        } else {
            if (a % 2 == 1) {
                c = 1;
                if (a >= x) x = 0;
                else x -= a;
            } else {
                c = 1;
                if (a >= x) x = 1;
                else x = (x - (a - 1));
            }
            if (x > 0 && x <= b) {
                x = 0;
            }
        }
        if (c % 2 == 1 && x == 0) {
            pf("Yes\n");
        } else {
            pf ("No\n");
        }
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define si(x) scanf("%d", &x)
#define sl(x) scanf("%lld", &x)
#define pf printf

typedef long long int ll;

int t, n, a, b, c, x;

int main(void) {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    si(t);

    while (t--) {
        si(n), si(x);
        a = b = 0;
        vector<int> e, o;
        for (int i = 0; i < n; ++i) {
            si(c);
            if (c % 2 == 1) {
                o.push_back(c);
            } else {
                e.push_back(c);
            }
        }
        c = 0;
        a = x;
        for (int i = 0; i < o.size(); ++i) {
            if (x == 0) break;
            c += 1;
            x--;
        }
        if (c % 2 == 0 && o.size() > 0) {
            c -= 1;
            x++;
        }
        for (int i = 0; i < e.size(); ++i) {
            if (x == 0) break;
            c += 2;
            x--;
        }
        if (c % 2 == 1 && x == 0) {
            pf("Yes\n");
        } else {
            pf ("No\n");
        }
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define si(x) scanf("%d", &x)
#define sl(x) scanf("%lld", &x)
#define pf printf

typedef long long int ll;

int t, n, a, b, c, x;

int main(void) {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    si(t);

    while (t--) {
        si(n), si(x);
        a = b = 0;
        vector<int> e, o;
        for (int i = 0; i < n; ++i) {
            si(c);
            if (c % 2 == 1) {
                o.push_back(c);
            } else {
                e.push_back(c);
            }
        }
        c = 0;
        for (int i = 0; i < o.size(); ++i) {
            if (x == 0) break;
            c += o[i];
            x--;
        }
        if (c % 2 == 0 && o.size() > 0) {
            c -= o[o.size() - 1];
            x++;
        }
        for (int i = 0; i < e.size(); ++i) {
            if (x == 0) break;
            c += e[i];
            x--;
        }
        if (c % 2 == 1 && x == 0) {
            pf("Yes\n");
        } else {
            pf ("No\n");
        }
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define si(x) scanf("%d", &x)
#define sl(x) scanf("%lld", &x)
#define pf printf

typedef long long int ll;

int t, n, a, b, c, x;

int main(void) {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    si(t);

    while (t--) {
        si(n), si(x);
        a = b = 0;
        for (int i = 0; i < n; ++i) {
            si(c);
            if (c % 2 == 1) {
                ++a;
            } else {
                ++b;
            }
        }
        c = 0;
        if (a == 0) {
            c = 0;
        } else {
            if (a % 2 == 1) {
//                pf("odd");
                c = 1;
                if (x % 2 == 0) {
                    if (x > a) {
                        x -= a;
                    } else {
                        x = 0;
                    }
                } else if (a >= x) x = 0;
                else x -= a;
            } else {
//                pf("even");
                c = 1;
                if (a >= x) x = 1;
                else x = (x - (a - 1));
            }
//            pf("%d %d \n", c, x);
            if (x > 0 && x <= b) {
                x = 0;
            }
        }
        if (c == 1 && x == 0) {
            pf("Yes\n");
        } else {
            pf ("No\n");
        }
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define si(x) scanf("%d", &x)
#define sl(x) scanf("%lld", &x)
#define pf printf

typedef long long int ll;

int t, n, a, b, c, x;
vector<int> o, e;

int main(void) {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    si(t);

    while (t--) {
        si(n), si(x);
        o.clear();
        e.clear();
        for (int i = 0; i < n; ++i) {
            si(c);
            if (c % 2 == 1) {
                o.push_back(c);
            } else {
                e.push_back(c);
            }
        }
        c = 0;
        if (x % 2 == 1) {
            for (auto it: o) {
                if (x <= 0) break;
                else c += it;
                x--;
            }
            if (c % 2 == 0) {
                c = 1;
                x++;
            }
            for (auto it: e) {
                if (x <= 0) break;
                else c += it;
                x--;
            }
        } else {
            for (auto it: e) {
                if (x <= 0) break;
                else c += it;
                x--;
            }
            if (x == 0) {
                x++;
                c = 2;
            }
            for (auto it: o) {
                if (x <= 0) break;
                else c += it;
                x--;
            }
        }
        if (c % 2 == 1 && x == 0) {
            pf("Yes\n");
        } else {
            pf ("No\n");
        }
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define si(x) scanf("%d", &x)
#define sl(x) scanf("%lld", &x)
#define pf printf

typedef long long int ll;

int t, n, a, b, c, x;
vector<int> o, e;

int main(void) {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    si(t);

    while (t--) {
        si(n), si(x);
        o.clear();
        e.clear();
        for (int i = 0; i < n; ++i) {
            si(c);
            if (c % 2 == 1) {
                o.push_back(c);
            } else {
                e.push_back(c);
            }
        }
        c = 0;
        if (x % 2 == 1) {
            c = 0;
            for (auto it: o) {
                if (x <= 0) break;
                else c += it;
                x--;
            }
            if (c % 2 == 0) {
                c -= o[o.size() - 1];
                x++;
            }
            for (auto it: e) {
                if (x <= 0) break;
                else c += it;
                x--;
            }
        } else {
            c = 0;
            a = x;
            for (auto it: e) {
                if (x <= 0) break;
                else c += it;
                x--;
            }
            if (x == 0) {
                x++;
                c -= e[a - 1];
            }
            for (auto it: o) {
                if (x <= 0) break;
                else c += it;
                x--;
            }
        }
        if (c % 2 == 1 && x == 0) {
            pf("Yes\n");
        } else {
            pf ("No\n");
        }
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define si(x) scanf("%d", &x)
#define sl(x) scanf("%lld", &x)
#define pf printf

typedef long long int ll;

int t, n, a, b, c, x;
vector<int> o, e;

int main(void) {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    si(t);

    while (t--) {
        si(n), si(x);
        o.clear();
        e.clear();
        for (int i = 0; i < n; ++i) {
            si(c);
            if (c % 2 == 1) {
                o.push_back(c);
            } else {
                e.push_back(c);
            }
        }
        c = 0;
        if (x % 2 == 1) {
            c = 0;
            for (auto it: o) {
                if (x <= 0) break;
                else c += it;
                x--;
            }
            if (c % 2 == 0) {
                c -= o[o.size() - 1];
                x++;
            }
            for (auto it: e) {
                if (x <= 0) break;
                else c += it;
                x--;
            }
        } else {
            c = 0;
            a = x;
            for (auto it: e) {
                if (x <= 0) break;
                else c += it;
                x--;
            }
            if (x == 0) {
                x++;
                c -= e[a - 1];
            }
            for (auto it: o) {
                if (x <= 0) break;
                else c += it;
                x--;
            }
        }
        if (c % 2 == 1 && x == 0) {
            pf("YES\n");
        } else {
            pf ("NO\n");
        }
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define si(x) scanf("%d", &x)
#define sl(x) scanf("%lld", &x)
#define pf printf

typedef long long int ll;

int t, n, a, b, c, x;

int main(void) {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    si(t);

    while (t--) {
        si(n), si(x);
        a = b = 0;
        for (int i = 0; i < n; ++i) {
            si(c);
            if (c % 2 == 1) {
                ++a;
            } else {
                ++b;
            }
        }
        c = 0;
        if (x % 2 == 1) {
            if (a == 0) {
                c = 0;
            } else if (a >= x) {
                c = 1;
                x = 0;
            }else {
                if (a % 2 == 1) {
                    c = 1;
                    x -= a;
                } else {
                    c = 1;
                    x -= a;
                    x++;
                }
                if (x <= b) {
                    x = 0;
                }
            }
        } else {
            if (a == 0) {
                c = 0;
            } else {
                if (a % 2 == 1) {
                    c = 1;
                    x -= a;
                } else {
                    c = 1;
                    x -= a;
                    x++;
                }
                if (x <= b) {
                    x = 0;
                }
            }
        }
        if (c % 2 == 1 && x <= 0) {
            pf("Yes\n");
        } else {
            pf ("No\n");
        }
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define si(x) scanf("%d", &x)
#define sl(x) scanf("%lld", &x)
#define pf printf

typedef long long int ll;

int t, n, a, b, c, x;
set<int> o, e;

int main(void) {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    si(t);

    while (t--) {
        si(n), si(x);
        o.clear();
        e.clear();
        for (int i = 0; i < n; ++i) {
            si(c);
            if (c % 2 == 1) {
                o.insert(c);
            } else {
                e.insert(c);
            }
        }
        c = 0;
        if (x % 2 == 1) {
            for (auto it: o) {
                if (x <= 0) break;
                else c += it;
                x--;
            }
            if (c % 2 == 0) {
                c = 1;
                x++;
            }
            for (auto it: e) {
                if (x <= 0) break;
                else c += it;
                x--;
            }
        } else {
            for (auto it: e) {
                if (x <= 0) break;
                else c += it;
                x--;
            }
            if (x == 0) {
                x++;
                c = 2;
            }
            for (auto it: o) {
                if (x <= 0) break;
                else c += it;
                x--;
            }
        }
        if (c % 2 == 1 && x == 0) {
            pf("Yes\n");
        } else {
            pf ("No\n");
        }
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define si(x) scanf("%d", &x)
#define sl(x) scanf("%lld", &x)
#define pf printf

typedef long long int ll;

int t, n, a, b, c, x;

int main(void) {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    si(t);

    while (t--) {
        si(n), si(x);
        a = b = 0;
        for (int i = 0; i < n; ++i) {
            si(c);
            if (c % 2 == 1) {
                ++a;
            } else {
                ++b;
            }
        }
        c = 0;
        if (a == 0) {
            c = 0;
        } else {
            if (a % 2 == 1) {
                c = 1;
                x -= a;
            } else {
                c = 1;
                x -= a;
                x++;
            }
            if (x > 0 && x <= b) {
                x = 0;
            }
        }
        if (c % 2 == 1 && x == 0) {
            pf("Yes\n");
        } else {
            pf ("No\n");
        }
    }

    return 0;
}