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

typedef long long ll;
typedef pair<int, int> Pi;
typedef pair<ll, ll> Pll;

// #define TT() printf("%.4f sec\n", (double) clock() / CLOCKS_PER_SEC )
#define Fi first
#define Se second
#define rep(i, n) for(int i=0;i<n;i++)
#define repp(i, n) for(int i=1;i<=n;i++)

#define INF 987654321
#define IINF 987654321987654321

int n,k;

vector<int> v,ans;
int chk[1<<21];

vector<int> dfs(int a){
	vector<int> res;
	if(a >= (1<<n)) return res;
	vector<int> v1 = dfs(2*a);
	vector<int> v2 = dfs(2*a+1);
	v1.push_back(INF); v2.push_back(INF);
	int M = max(ans[2*a],ans[2*a+1]);

	int a1 = v1.size(); int a2 = v2.size();

	int i,j = 0;
	for(int i=0,j=0; i < a1 && j < a2; ){
		if(v1[i] < v2[j]){
			if(v1[i] > M) res.push_back(v1[i]);
			i++;
		}
		else{
			if(v2[j] > M) res.push_back(v2[j]);
			j++;
		}
	}
	res.pop_back();
	res.push_back(v[a]);
	if(a >= (1<<k) ) ans[a] = 0;
	else ans[a] = res[0];

	// printf("%d : ",a);
	// for(auto e : res) printf("%d ",e);
	// printf("\n");

	return res;
}

int main(){
	int Tc; scanf("%d",&Tc);
	repp(tc,Tc){
		scanf("%d%d",&n,&k);
		v.clear(); ans.clear();
		v.resize(1<<(n+1)+5);
		ans.resize(1<<(n+1)+5); rep(i,(int)ans.size()) ans[i] = 0;

		repp(i,(1<<n)-1) scanf("%d",&v[i]);

		dfs(1);
		ll minsum = 0;
		// repp(i,(1<<k)-1) printf("%d : %d\n",i,ans[i]);
		repp(i,(1<<k)-1) {
			minsum += ans[i];
			chk[ans[i]] = tc;
		}
		printf("%lld\n",minsum);
		int cnt = 0;
		for(int i = (1<<n)-1; i >= 1; i--) if(chk[v[i]] != tc) printf("%d ",i);
		printf("\n");

	}

	return 0;
}


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

typedef long long ll;
typedef pair<int, int> Pi;
typedef pair<ll, ll> Pll;

// #define TT() printf("%.4f sec\n", (double) clock() / CLOCKS_PER_SEC )
#define Fi first
#define Se second
#define rep(i, n) for(int i=0;i<n;i++)
#define repp(i, n) for(int i=1;i<=n;i++)

#define INF 987654321
#define IINF 987654321987654321

int n,k;

vector<int> v,ans;
int chk[1<<21];

vector<int> dfs(int a){
	vector<int> res;
	if(a >= (1<<n)) return res;
	vector<int> v1 = dfs(2*a);
	vector<int> v2 = dfs(2*a+1);
	v1.push_back(INF); v2.push_back(INF);
	int M = max(ans[2*a],ans[2*a+1]);

	int a1 = v1.size(); int a2 = v2.size();

	int i,j = 0;
	for(int i=0,j=0; i < a1 && j < a2; ){
		if(v1[i] < v2[j]){
			if(v1[i] > M) res.push_back(v1[i]);
			i++;
		}
		else{
			if(v2[j] > M) res.push_back(v2[j]);
			j++;
		}
	}
	res.pop_back();
	res.push_back(v[a]);
	if(a >= (1<<k) ) ans[a] = 0;
	else ans[a] = res[0];

	// printf("%d : ",a);
	// for(auto e : res) printf("%d ",e);
	// printf("\n");

	return res;
}

int main(){
	int Tc; scanf("%d",&Tc);
	repp(tc,Tc){
		scanf("%d%d",&n,&k);
		v.clear(); ans.clear();
		v.resize(1<<(n+1)+5);
		ans.resize(1<<(n+1)+5); rep(i,(int)ans.size()) ans[i] = 0;

		repp(i,(1<<n)-1) scanf("%d",&v[i]);

		dfs(1);
		ll minsum = 0;
		repp(i,(1<<n)) chk[i] = 0;
		// repp(i,(1<<k)-1) printf("%d : %d\n",i,ans[i]);
		repp(i,(1<<k)-1) {
			minsum += ans[i];
			chk[ans[i]] = 1;
		}
		printf("%lld\n",minsum);
		int cnt = 0;
		for(int i = (1<<n)-1; i >= 1; i--) if(chk[v[i]] == 0) {
			// printf("%d ",i);
			cnt++;
		}
		if(cnt != (1<<n) - (1<<k)) exit(0);
		for(int i = (1<<n)-1; i >= 1; i--) if(chk[v[i]] == 0) {
			printf("%d ",i);
		}
		printf("\n");

	}

	return 0;
}


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

typedef long long ll;
typedef pair<int, int> Pi;
typedef pair<ll, ll> Pll;

// #define TT() printf("%.4f sec\n", (double) clock() / CLOCKS_PER_SEC )
#define Fi first
#define Se second
#define rep(i, n) for(int i=0;i<n;i++)
#define repp(i, n) for(int i=1;i<=n;i++)

#define INF 987654321
#define IINF 987654321987654321

int n,k;

vector<int> v,ans;
int chk[1<<21];

vector<int> dfs(int a){
	vector<int> res;
	if(a >= (1<<n)) return res;
	vector<int> v1 = dfs(2*a);
	vector<int> v2 = dfs(2*a+1);
	v1.push_back(INF); v2.push_back(INF);
	int M = max(ans[2*a],ans[2*a+1]);

	int a1 = v1.size(); int a2 = v2.size();

	int i,j = 0;
	for(int i=0,j=0; i < a1 && j < a2; ){
		if(v1[i] < v2[j]){
			if(v1[i] > M) res.push_back(v1[i]);
			i++;
		}
		else{
			if(v2[j] > M) res.push_back(v2[j]);
			j++;
		}
	}
	res.pop_back();
	res.push_back(v[a]);
	if(a >= (1<<k) ) ans[a] = 0;
	else ans[a] = res[0];

	// printf("%d : ",a);
	// for(auto e : res) printf("%d ",e);
	// printf("\n");

	return res;
}

int main(){
	int Tc; scanf("%d",&Tc);
	repp(tc,Tc){
		scanf("%d%d",&n,&k);
		v.clear(); ans.clear();
		v.resize(1<<(n+1)+5);
		ans.resize(1<<(n+1)+5); rep(i,ans.size()) ans[i] = 0;

		repp(i,(1<<n)-1) scanf("%d",&v[i]);

		dfs(1);
		ll minsum = 0;
		repp(i,(1<<n)) chk[i] = 0;
		// repp(i,(1<<k)-1) printf("%d : %d\n",i,ans[i]);
		repp(i,(1<<k)-1) {
			minsum += ans[i];
			chk[ans[i]] = 1;
		}
		printf("%lld\n",minsum);
		for(int i = (1<<n)-1; i >= 1; i--) if(chk[v[i]] == 0) printf("%d ",i);
		printf("\n");

	}

	return 0;
}


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

typedef long long ll;
typedef pair<int, int> Pi;
typedef pair<ll, ll> Pll;

// #define TT() printf("%.4f sec\n", (double) clock() / CLOCKS_PER_SEC )
#define Fi first
#define Se second
#define rep(i, n) for(int i=0;i<n;i++)
#define repp(i, n) for(int i=1;i<=n;i++)

#define INF 987654321
#define IINF 987654321987654321

int n,k;

vector<int> v,ans;
int chk[1<<21];

vector<int> aa[1<<21];

int main(){
	int Tc; scanf("%d",&Tc);
	repp(tc,Tc){
		scanf("%d%d",&n,&k);
		v.clear(); ans.clear();
		v.resize(1<<(n+1)+5);
		ans.resize(1<<(n+1)+5); rep(i,(int)ans.size()) ans[i] = 0;
		repp(i,(1<<n)-1) scanf("%d",&v[i]);
		repp(i,1<<(n+1)) aa[i].clear();

		for(int a = (1<<n)-1; a >= 1; a--){
			aa[2*a].push_back(INF); aa[2*a+1].push_back(INF);
			int M = max(ans[2*a],ans[2*a+1]);
			int a1 = aa[2*a].size(); int a2 = aa[2*a+1].size();
			int i,j = 0;
			for(int i=0,j=0; i < a1 && j < a2; ){
				if(aa[2*a][i] < aa[2*a+1][j]){
					if(aa[2*a][i] > M) aa[a].push_back(aa[2*a][i]);
					i++;
				}
				else{
					if(aa[2*a+1][j] > M) aa[a].push_back(aa[2*a+1][j]);
					j++;
				}
			}
			aa[a].pop_back();
			aa[a].push_back(v[a]);
			if(a >= (1<<k) ) ans[a] = 0;
			else ans[a] = aa[a][0];
		}
		ll minsum = 0;
		// repp(i,(1<<k)-1) printf("%d : %d\n",i,ans[i]);
		repp(i,(1<<k)-1) {
			minsum += ans[i];
			chk[ans[i]] = tc;
		}
		printf("%lld\n",minsum);
		int cnt = 0;
		for(int i = (1<<n)-1; i >= 1; i--) if(chk[v[i]] != tc) printf("%d ",i);
		printf("\n");

	}

	return 0;
}


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

typedef long long ll;
typedef pair<int, int> Pi;
typedef pair<ll, ll> Pll;

// #define TT() printf("%.4f sec\n", (double) clock() / CLOCKS_PER_SEC )
#define Fi first
#define Se second
#define rep(i, n) for(int i=0;i<n;i++)
#define repp(i, n) for(int i=1;i<=n;i++)

#define INF 987654321
#define IINF 987654321987654321

int n,k;

vector<int> v,ans;
int chk[1<<21];

vector<int> aa[1<<20+5];

int main(){
	int Tc; scanf("%d",&Tc);
	repp(tc,Tc){
		scanf("%d%d",&n,&k);
		v.clear(); ans.clear();
		v.resize((1<<n)+5);
		ans.resize(1<<(n+1)+5); rep(i,(int)ans.size()) ans[i] = 0;
		repp(i,(1<<n)-1) scanf("%d",&v[i]);

		for(int a = (1<<(n-1)); a < (1<<n); a++){
			aa[a].push_back(v[a]);
		}

		for(int a = (1<<(n-1))-1; a >= 1; a--){
			aa[2*a].push_back(INF); aa[2*a+1].push_back(INF);
			int M = max(ans[2*a],ans[2*a+1]);
			int a1 = aa[2*a].size(); int a2 = aa[2*a+1].size();
			int i,j = 0;
			for(int i=0,j=0; i < a1 && j < a2; ){
				if(aa[2*a][i] < aa[2*a+1][j]){
					if(aa[2*a][i] > M) aa[a].push_back(aa[2*a][i]);
					i++;
				}
				else{
					if(aa[2*a+1][j] > M) aa[a].push_back(aa[2*a+1][j]);
					j++;
				}
			}
			aa[a].pop_back();
			aa[a].push_back(v[a]);
			if(a >= (1<<k) ) ans[a] = 0;
			else ans[a] = aa[a][0];
		}
		ll minsum = 0;
		// repp(i,(1<<k)-1) printf("%d : %d\n",i,ans[i]);
		repp(i,(1<<k)-1) {
			minsum += ans[i];
			chk[ans[i]] = tc;
		}
		printf("%lld\n",minsum);
		for(int i = (1<<n)-1; i >= 1; i--) if(chk[v[i]] != tc) printf("%d ",i);
		printf("\n");

		repp(i,1<<n) aa[i].clear();

	}

	return 0;
}


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

#define all(x) begin(x), end(x)
#define sz(x) (ll)(x).size()

using ll = long long;
using ld = long double;

ll h, g;
vector<ll> a;
ll inv[(1<<20) + 7];

bool check(ll i){
    ll l = 2*i;
    ll r = 2*i + 1;
    if (a[l] == 0 && a[r] == 0) {
        return i >= (1ll << g);
    } else if (a[l] > a[r]){
        return check(l);
    } else {
        return check(r);
    }
}

void pop(ll i){
    ll l = 2*i;
    ll r = 2*i + 1;
    if (a[l] == 0 && a[r] == 0) {
        a[i] = 0;
    } else if (a[l] > a[r]){
        a[i] = a[l];
        inv[a[i]] = i;
        pop(l);
    } else {
        a[i] = a[r];
        inv[a[i]] = i;
        pop(r);
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    ll t;
    cin >> t;
    for (ll i = 0; i < t; i++) {
        cin >> h >> g;
        a.assign(1ll<<(h + 1), 0);
        ll sum = 0;
        vector<ll> sorted(sz(a) - 1);
        for (ll j = 1; j < (1ll << h); j++) {
            cin >> a[j];
            inv[a[j]] = j;
            sum += a[j];
            sorted[j - 1] = a[j];
        }
        sort(all(sorted), greater<ll>());
        vector<ll> opps;
        for (ll x : sorted) {
            if (check(inv[x])) {
                sum -= x;
                opps.push_back(inv[x]);
                pop(inv[x]);
            }
        }
        cout << sum << "\n";
        for (ll x : opps) {
            cout << x << " ";
        }
        cout << "\n";
    }
}


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

#define all(x) begin(x), end(x)
#define sz(x) (ll)(x).size()

using ll = long long;
using ld = long double;

ll h, g;
vector<ll> a;
map<ll, ll> inv;

bool check(ll i){
    ll l = 2*i;
    ll r = 2*i + 1;
    if (a[l] == 0 && a[r] == 0) {
        return i >= (1ll << g);
    } else if (a[l] > a[r]){
        return check(l);
    } else {
        return check(r);
    }
}

void pop(ll i){
    ll l = 2*i;
    ll r = 2*i + 1;
    if (a[l] == 0 && a[r] == 0) {
        a[i] = 0;
    } else if (a[l] > a[r]){
        a[i] = a[l];
        if (inv.count(a[i]) > 0) inv[a[i]] = i;
        pop(l);
    } else {
        a[i] = a[r];
        if (inv.count(a[i]) > 0) inv[a[i]] = i;
        pop(r);
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    ll t;
    cin >> t;
    for (ll i = 0; i < t; i++) {
        cin >> h >> g;
        a.assign(1ll<<(h + 1), 0);
        inv.clear();
        ll sum = 0;
        for (ll j = 1; j < (1ll << h); j++) {
            cin >> a[j];
            inv[a[j]] = j;
            sum += a[j];
        }
        vector<ll> opps;
        while (!inv.empty()) {
            auto e = *prev(inv.end());
            inv.erase(prev(inv.end()));
            if (check(e.second)) {
                sum -= e.first;
                pop(e.second);
                opps.push_back(e.second);
            }
        }
        cout << sum << endl;
        for (ll x : opps) {
            cout << x << " ";
        }
        cout << endl;
    }
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 5e5+5;
int arr[MAXN];
int ans[MAXN];
int res[MAXN];
int check(int curr,int depth){
    int l = 2*curr;
    int r = 2*curr+1;
    if(arr[l] ==0 && arr[r]==0){
        return depth;
    }
    if(arr[l]>arr[r]){
        return check(2*curr,depth+1);
    }else{
        return check(2*curr+1,depth+1);
    }
}
void f(int curr){
    int l = 2*curr;
    int r = 2*curr+1;
    //cout<<curr<<endl;
    if(arr[l] ==0 && arr[r]==0){
        arr[curr] = 0;
        return;
    }
    if(arr[l]>arr[r]){
        arr[curr] = arr[l];
        f(2*curr);
    }else{
        arr[curr] = arr[r];
        f(2*curr+1);
    }
}
int main(){
    int t;
    cin>>t;
    while(t--){
        int h,g;
        cin>>h>>g;
       int n = (1<<h)-1;
        for(int i=1;i<=4*n;i++){
            arr[i] = 0;
        }
        for(int i=1;i<=n;i++){
            cin>>arr[i];
        }
        int curr = 1;
        int dep = 1;
        int ind = 0;
        while(true){
            if(dep>g){
                break;
            }
            while(check(curr,dep)>g){
                f(curr);
                ans[ind] = curr;
                ind++;

            }
            dep++;
            if(check(2*curr,dep)>g){
                curr = 2*curr;
                //cout<<curr<<" "<<check(curr,dep)<<endl;
            }else if(check(2*curr+1,dep)>g){
                curr=2*curr+1;
            }else{
                break;
            }
        }
        int res = 0;
        for(int i=1;i<=n;i++){
            res+=arr[i];
        }
        cout<<res<<endl;
        for(int i=0;i<ind;i++){
            cout<<ans[i]<<" ";
        }
        cout<<endl;
    }
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 5e5+5;
int arr[MAXN];
int depth[MAXN];
int ans[MAXN];
int res[MAXN];
int check(int curr,int depth){
    int l = 2*curr;
    int r = 2*curr+1;
    if(arr[curr] == 0){
        return depth-1;
    }
    if(arr[l] ==0 && arr[r]==0){
        return depth;
    }
    if(arr[l]>arr[r]){
        return check(2*curr,depth+1);
    }else{
        return check(2*curr+1,depth+1);
    }
}
void f(int curr){
    int l = 2*curr;
    int r = 2*curr+1;
    //cout<<curr<<endl;
    if(arr[l] ==0 && arr[r]==0){
        arr[curr] = 0;
    }else if(arr[l]>arr[r]){
        arr[curr] = arr[l];
        f(2*curr);
    }else{
        arr[curr] = arr[r];
        f(2*curr+1);
    }
}
void dfs(int curr,int d,int mx){
    depth[curr] = d;
    if(2*curr>mx){
        return;
    }
    dfs(2*curr,d+1,mx);
    dfs(2*curr+1,d+1,mx);
}
int main(){
    int t;
    cin>>t;
    while(t--){
        int h,g;
        cin>>h>>g;
       int n = (1<<h)-1;
        for(int i=1;i<=4*n;i++){
            arr[i] = 0;
            depth[i] = 0;
        }
        for(int i=1;i<=n;i++){
            cin>>arr[i];
        }
        dfs(1,1,n);
        //int curr = 1;

        int ind = 0;
        for(int curr=1;curr<=n;curr++){
            int dep = depth[curr];
            if(dep>g){
                continue;
            }
            while(check(curr,dep)>g){
                f(curr);
                ans[ind] = curr;
                ind++;

            }
        }
        int res = 0;
        for(int i=1;i<=(1<<g)-1;i++){
            res+=arr[i];
        }
        cout<<res<<endl;
        for(int i=0;i<ind;i++){
            cout<<ans[i]<<" ";
        }
        cout<<endl;
    }
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 5e6+5;
int arr[MAXN];
int depth[MAXN];
int ans[MAXN];
int res[MAXN];
int check(int curr,int depth){
    int l = 2*curr;
    int r = 2*curr+1;
    if(arr[curr] == 0){
        return depth-1;
    }
    if(arr[l] ==0 && arr[r]==0){
        return depth;
    }
    if(arr[l]>arr[r]){
        return check(2*curr,depth+1);
    }else{
        return check(2*curr+1,depth+1);
    }
}
void f(int curr){
    int l = 2*curr;
    int r = 2*curr+1;
    //cout<<curr<<endl;
    if(arr[l] ==0 && arr[r]==0){
        arr[curr] = 0;
    }else if(arr[l]>arr[r]){
        arr[curr] = arr[l];
        f(2*curr);
    }else{
        arr[curr] = arr[r];
        f(2*curr+1);
    }
}
void dfs(int curr,int d,int mx){
    depth[curr] = d;
    if(2*curr>mx){
        return;
    }
    dfs(2*curr,d+1,mx);
    dfs(2*curr+1,d+1,mx);
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t;
    cin>>t;
    while(t--){
        int h,g;
        cin>>h>>g;
       int n = (1<<h)-1;
        for(int i=1;i<=4*n;i++){
            arr[i] = 0;
            depth[i] = 0;
        }
        for(int i=1;i<=n;i++){
            cin>>arr[i];
        }
        dfs(1,1,n);
        //int curr = 1;

        int ind = 0;
        for(int curr=1;curr<=n;curr++){
            int dep = depth[curr];
            if(dep>g){
                continue;
            }
            while(check(curr,dep)>g){
                f(curr);
                ans[ind] = curr;
                ind++;

            }
        }
        long long res = 0;
        for(int i=1;i<=(1<<g)-1;i++){
            res+=arr[i];
        }
        cout<<res<<endl;
        for(int i=0;i<ind;i++){
            cout<<ans[i]<<" ";
        }
        cout<<endl;
    }
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 5e5+5;
int arr[MAXN];
int ans[MAXN];
int res[MAXN];
int check(int curr,int depth){
    int l = 2*curr;
    int r = 2*curr+1;
    if(arr[l] ==0 && arr[r]==0){
        return depth;
    }
    if(arr[l]>arr[r]){
        return check(2*curr,depth+1);
    }else{
        return check(2*curr+1,depth+1);
    }
}
void f(int curr){
    int l = 2*curr;
    int r = 2*curr+1;
    //cout<<curr<<endl;
    if(arr[l] ==0 && arr[r]==0){
        arr[curr] = 0;
        return;
    }
    if(arr[l]>arr[r]){
        arr[curr] = arr[l];
        f(2*curr);
    }else{
        arr[curr] = arr[r];
        f(2*curr+1);
    }
}
int main(){
    int t;
    cin>>t;
    while(t--){
        int h,g;
        cin>>h>>g;
       int n = (1<<h)-1;
        for(int i=1;i<=4*n;i++){
            arr[i] = 0;
        }
        for(int i=1;i<=n;i++){
            cin>>arr[i];
        }
        int curr = 1;
        int dep = 1;
        int ind = 0;
        while(true){
            if(dep>g){
                break;
            }
            while(check(curr,dep)>g){
                f(curr);
                ans[ind] = curr;
                ind++;

            }
            dep++;
            if(check(2*curr,dep)>g && arr[2*curr]){
                curr = 2*curr;
                //cout<<curr<<" "<<check(curr,dep)<<endl;
            }else if(check(2*curr+1,dep)>g && arr[2*curr+1]){
                curr=2*curr+1;
            }else{
                break;
            }
        }
        int res = 0;
        for(int i=1;i<=n;i++){
            res+=arr[i];
        }
        cout<<res<<endl;
        for(int i=0;i<ind;i++){
            cout<<ans[i]<<" ";
        }
        cout<<endl;
    }
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 5e5+5;
int arr[MAXN];
int ans[MAXN];
int res[MAXN];
int check(int curr,int depth){
    int l = 2*curr;
    int r = 2*curr+1;
    if(arr[curr] == 0){
        return depth-1;
    }
    if(arr[l] ==0 && arr[r]==0){
        return depth;
    }
    if(arr[l]>arr[r]){
        return check(2*curr,depth+1);
    }else{
        return check(2*curr+1,depth+1);
    }
}
void f(int curr){
    int l = 2*curr;
    int r = 2*curr+1;
    //cout<<curr<<endl;
    if(arr[l] ==0 && arr[r]==0){
        arr[curr] = 0;
    }else if(arr[l]>arr[r]){
        arr[curr] = arr[l];
        f(2*curr);
    }else{
        arr[curr] = arr[r];
        f(2*curr+1);
    }
}
int main(){
    int t;
    cin>>t;
    while(t--){
        int h,g;
        cin>>h>>g;
       int n = (1<<h)-1;
        for(int i=1;i<=4*n;i++){
            arr[i] = 0;
        }
        for(int i=1;i<=n;i++){
            cin>>arr[i];
        }
        int curr = 1;
        int dep = 1;
        int ind = 0;
        while(true){
            if(dep>g){
                break;
            }
            while(check(curr,dep)>g){
                f(curr);
                ans[ind] = curr;
                ind++;

            }
            dep++;
            if(check(2*curr,dep)>g && arr[2*curr]){
                curr = 2*curr;
                //cout<<curr<<" "<<check(curr,dep)<<endl;
            }else if(check(2*curr+1,dep)>g && arr[2*curr+1]){
                curr=2*curr+1;
            }else{
                break;
            }
        }
        int res = 0;
        for(int i=1;i<=(1<<g)-1;i++){
            res+=arr[i];
        }
        cout<<res<<endl;
        for(int i=0;i<ind;i++){
            cout<<ans[i]<<" ";
        }
        cout<<endl;
    }
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 5e6+5;
int arr[MAXN];
int depth[MAXN];
int ans[MAXN];
int res[MAXN];
int check(int curr,int depth){
    int l = 2*curr;
    int r = 2*curr+1;
    if(arr[curr] == 0){
        return depth-1;
    }
    if(arr[l] ==0 && arr[r]==0){
        return depth;
    }
    if(arr[l]>arr[r]){
        return check(2*curr,depth+1);
    }else{
        return check(2*curr+1,depth+1);
    }
}
void f(int curr){
    int l = 2*curr;
    int r = 2*curr+1;
    //cout<<curr<<endl;
    if(arr[l] ==0 && arr[r]==0){
        arr[curr] = 0;
    }else if(arr[l]>arr[r]){
        arr[curr] = arr[l];
        f(2*curr);
    }else{
        arr[curr] = arr[r];
        f(2*curr+1);
    }
}
void dfs(int curr,int d,int mx){
    depth[curr] = d;
    if(2*curr>mx){
        return;
    }
    dfs(2*curr,d+1,mx);
    dfs(2*curr+1,d+1,mx);
}
int main(){
    int t;
    cin>>t;
    while(t--){
        int h,g;
        cin>>h>>g;
       int n = (1<<h)-1;
        for(int i=1;i<=4*n;i++){
            arr[i] = 0;
            depth[i] = 0;
        }
        for(int i=1;i<=n;i++){
            cin>>arr[i];
        }
        dfs(1,1,n);
        //int curr = 1;

        int ind = 0;
        for(int curr=1;curr<=n;curr++){
            int dep = depth[curr];
            if(dep>g){
                continue;
            }
            while(check(curr,dep)>g){
                f(curr);
                ans[ind] = curr;
                ind++;

            }
        }
        int res = 0;
        for(int i=1;i<=(1<<g)-1;i++){
            res+=arr[i];
        }
        cout<<res<<endl;
        for(int i=0;i<ind;i++){
            cout<<ans[i]<<" ";
        }
        cout<<endl;
    }
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 5e5+5;
int arr[MAXN];
int ans[MAXN];
int res[MAXN];
int check(int curr,int depth){
    int l = 2*curr;
    int r = 2*curr+1;
    if(arr[l] ==0 && arr[r]==0){
        return depth;
    }
    if(arr[l]>arr[r]){
        return check(2*curr,depth+1);
    }else{
        return check(2*curr+1,depth+1);
    }
}
void f(int curr){
    int l = 2*curr;
    int r = 2*curr+1;
    //cout<<curr<<endl;
    if(arr[l] ==0 && arr[r]==0){
        arr[curr] = 0;
        return;
    }
    if(arr[l]>arr[r]){
        arr[curr] = arr[l];
        f(2*curr);
    }else{
        arr[curr] = arr[r];
        f(2*curr+1);
    }
}
int main(){
    int t;
    cin>>t;
    while(t--){
        int h,g;
        cin>>h>>g;
       int n = (1<<h)-1;
        for(int i=1;i<=4*n;i++){
            arr[i] = 0;
        }
        for(int i=1;i<=n;i++){
            cin>>arr[i];
        }
        int curr = 1;
        int dep = 1;
        int ind = 0;
        while(true){
            if(dep>g){
                break;
            }
            while(check(curr,dep)>g){
                f(curr);
                ans[ind] = curr;
                ind++;

            }
            dep++;
            if(check(2*curr,dep)>g && arr[2*curr]){
                curr = 2*curr;
                //cout<<curr<<" "<<check(curr,dep)<<endl;
            }else if(check(2*curr+1,dep)>g && arr[2*curr+1]){
                curr=2*curr+1;
            }else{
                break;
            }
        }
        int res = 0;
        for(int i=1;i<=(1<<g)-1;i++){
            res+=arr[i];
        }
        cout<<res<<endl;
        for(int i=0;i<ind;i++){
            cout<<ans[i]<<" ";
        }
        cout<<endl;
    }
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 5e6+5;
int arr[MAXN];
int depth[MAXN];
int ans[MAXN];
int res[MAXN];
int check(int curr,int depth){
    int l = 2*curr;
    int r = 2*curr+1;
    if(arr[curr] == 0){
        return depth-1;
    }
    if(arr[l] ==0 && arr[r]==0){
        return depth;
    }
    if(arr[l]>arr[r]){
        return check(2*curr,depth+1);
    }else{
        return check(2*curr+1,depth+1);
    }
}
void f(int curr){
    int l = 2*curr;
    int r = 2*curr+1;
    //cout<<curr<<endl;
    if(arr[l] ==0 && arr[r]==0){
        arr[curr] = 0;
    }else if(arr[l]>arr[r]){
        arr[curr] = arr[l];
        f(2*curr);
    }else{
        arr[curr] = arr[r];
        f(2*curr+1);
    }
}
void dfs(int curr,int d,int mx){
    depth[curr] = d;
    if(2*curr>mx){
        return;
    }
    dfs(2*curr,d+1,mx);
    dfs(2*curr+1,d+1,mx);
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t;
    cin>>t;
    while(t--){
        int h,g;
        cin>>h>>g;
       int n = (1<<h)-1;
        for(int i=1;i<=4*n;i++){
            arr[i] = 0;
            depth[i] = 0;
        }
        for(int i=1;i<=n;i++){
            cin>>arr[i];
        }
        dfs(1,1,n);
        //int curr = 1;

        int ind = 0;
        for(int curr=1;curr<=n;curr++){
            int dep = depth[curr];
            if(dep>g){
                continue;
            }
            while(check(curr,dep)>g){
                f(curr);
                ans[ind] = curr;
                ind++;

            }
        }
        int res = 0;
        for(int i=1;i<=(1<<g)-1;i++){
            res+=arr[i];
        }
        cout<<res<<endl;
        for(int i=0;i<ind;i++){
            cout<<ans[i]<<" ";
        }
        cout<<endl;
    }
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1 << 21;

int t;
int a[maxn];
int ans[maxn];

int f(int x)
{
  if(!a[x << 1] && !a[x << 1 | 1])  return x;
  else if(a[x << 1] > a[x << 1 | 1]) return  f(x << 1);
  else return  f(x << 1 | 1);
}

void dfs(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];
    dfs(x << 1);
  }
  else{
    a[x] = a[x << 1 | 1];
    dfs(x << 1 | 1);
  }
}

int main()
{
  scanf("%d",&t);
  while(t --){
    int h, g;
    int cnt = 0;
    scanf("%d%d",&h,&g);
    for(int i = 1; i < 1 << (h + 1); i ++)  a[i] = 0, ans[i] = 0;
    for(int i = 1; i < 1 << h; i ++)  scanf("%d",&a[i]);
    int up = (1 << g) - 1;
    for(int i = 1; i < 1 << g; i ++){
      while(f(i) > up){
        ans[cnt ++] = i;
        dfs(i);
      }
    }
    ll sum = 0;
    for(int i = 1; i < 1 << g; i ++)
      sum += a[i];
    printf("%lld\n", sum);
    for(int i = 0; i < cnt; i ++)
      printf("%d%c", ans[i], i == cnt - 1 ? '\n' : ' ');
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef map<int,int> mii;
typedef map<ll,ll> mll;
#define pi 3.141592653589793
#define mod 998244353
#define pb push_back
#define all(v) v.begin(),v.end()
#define tc int t;cin>>t;while(t--)
#define pqmax priority_queue<int>
#define pqmin priority_queue<int,vi,greater<int>>
#define fast_io ios_base::sync_with_stdio(0), cin.tie(NULL)
#define tc_g int tt;cin>>tt;for(int ti=1;ti<=tt;ti++)
#define case_g "Case #"<<ti<<": "
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<int, null_type, greater_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_multiset;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
typedef tree<int, int, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_map;

int h, g, n, m;
vi a, b, d;

void heapify(int cur) {
	if (cur >= n/2)
		return;
	int l = cur * 2;
	int r = l + 1;
	if (min(b[l], b[r]) < b[cur]) {
		if (b[l] < b[r]) {
			swap(b[l], b[cur]);
			heapify(l);
		}
		else if (b[r] < b[l]) {
			swap(b[r], b[cur]);
			heapify(r);
		}
	}
}

void f(int cur) {
	if (cur >= n/2)
		a[cur] = 0;
	else {
		int l = cur * 2;
		int r = l + 1;
		if (a[l] > a[r]) {
			a[cur] = a[l];
			f(l);
		}
		else {
			a[cur] = a[r];
			f(r);
		}
	}
}

void dfs(int cur) {
	int l = cur * 2;
	int r = l + 1;
	if (cur < n/2) {
		dfs(l);
		dfs(r);
		if (cur < m/2) {
			if (b[l] < b[r])
				while (b[l] < b[r]) {
					b[l] = INT32_MAX;
					heapify(l);
				}
			else
				while (b[r] < b[l]) {
					b[r] = INT32_MAX;
					heapify(r);
				}
		}
	}
	b[cur] = a[cur];
	heapify(cur);
	if (cur < m) {
		d[cur] = b[cur];
		b[cur] = INT32_MAX;
		heapify(cur);
	}
}

int main() {
	fast_io;
	tc {
		cin >> h >> g;
		n = 1 << h;
		m = 1 << g;
		a.resize(n);
		b.resize(n);
		d.resize(m);
		for (int i = 1;i < n;i++)
			cin >> a[i];
		dfs(1);
		ll sum = 0;
		vi ans;
		for (int i = 1;i < m;i++) {
			sum += d[i];
			while (a[i] != d[i]) {
				f(i);
				ans.pb(i);
			}
		}
		cout << sum << '\n';
		for (auto el : ans)
			cout << el << ' ';
		cout << '\n';
	}
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef map<int,int> mii;
typedef map<ll,ll> mll;
#define pi 3.141592653589793
#define mod 998244353
#define pb push_back
#define all(v) v.begin(),v.end()
#define tc int t;cin>>t;while(t--)
#define pqmax priority_queue<int>
#define pqmin priority_queue<int,vi,greater<int>>
#define fast_io ios_base::sync_with_stdio(0), cin.tie(NULL)
#define tc_g int tt;cin>>tt;for(int ti=1;ti<=tt;ti++)
#define case_g "Case #"<<ti<<": "
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<int, null_type, greater_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_multiset;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
typedef tree<int, int, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_map;

int h, g, n, m;
vi a, b, d;

void heapify(int cur) {
	if (cur >= n/2)
		return;
	int l = cur * 2;
	int r = l + 1;
	if (min(b[l], b[r]) < b[cur]) {
		if (b[l] < b[r]) {
			swap(b[l], b[cur]);
			heapify(l);
		}
		else if (b[r] < b[l]) {
			swap(b[r], b[cur]);
			heapify(r);
		}
	}
}

void f(int cur) {
	if (cur >= n/2)
		a[cur] = 0;
	else {
		int l = cur * 2;
		int r = l + 1;
		if (a[l] > a[r]) {
			a[cur] = a[l];
			f(l);
		}
		else if (a[r] > a[l]) {
			a[cur] = a[r];
			f(r);
		}
	}
}

void dfs(int cur) {
	int l = cur * 2;
	int r = l + 1;
	if (cur < n/2) {
		dfs(l);
		dfs(r);
		if (cur < m/2) {
			if (b[l] < b[r])
				while (b[l] < b[r]) {
					b[l] = INT32_MAX;
					heapify(l);
				}
			else
				while (b[r] < b[l]) {
					b[r] = INT32_MAX;
					heapify(r);
				}
		}
	}
	b[cur] = a[cur];
	heapify(cur);
	if (cur < m) {
		d[cur] = b[cur];
		b[cur] = INT32_MAX;
		heapify(cur);
	}
}

int main() {
	fast_io;
	tc {
		cin >> h >> g;
		n = 1 << h;
		m = 1 << g;
		a.resize(n);
		b.resize(n);
		d.resize(m);
		for (int i = 1;i < n;i++)
			cin >> a[i];
		dfs(1);
		ll sum = 0;
		vi ans;
		for (int i = 1;i < m;i++) {
			sum += d[i];
			while (a[i] != d[i]) {
				f(i);
				ans.pb(i);
			}
		}
		cout << sum << '\n';
		for (auto el : ans)
			cout << el << ' ';
		cout << '\n';
	}
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef map<int,int> mii;
typedef map<ll,ll> mll;
#define pi 3.141592653589793
#define mod 998244353
#define pb push_back
#define all(v) v.begin(),v.end()
#define tc int t;cin>>t;while(t--)
#define pqmax priority_queue<int>
#define pqmin priority_queue<int,vi,greater<int>>
#define fast_io ios_base::sync_with_stdio(0), cin.tie(NULL)
#define tc_g int tt;cin>>tt;for(int ti=1;ti<=tt;ti++)
#define case_g "Case #"<<ti<<": "
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<int, null_type, greater_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_multiset;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
typedef tree<int, int, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_map;

int h, g, n, m;
vi a, b, d;

void heapify(int cur) {
	if (cur >= n/2)
		return;
	int l = cur * 2;
	int r = l + 1;
	if (min(b[l], b[r]) < b[cur]) {
		if (b[l] < b[r]) {
			swap(b[l], b[cur]);
			heapify(l);
		}
		else if (b[r] < b[l]) {
			swap(b[r], b[cur]);
			heapify(r);
		}
	}
}

void f(int cur) {
	if (cur >= n/2)
		a[cur] = 0;
	else {
		int l = cur * 2;
		int r = l + 1;
		if (a[l] > a[r]) {
			a[cur] = a[l];
			f(l);
		}
		else {
			a[cur] = a[r];
			f(r);
		}
	}
}

void dfs(int cur) {
	int l = cur * 2;
	int r = l + 1;
	if (cur < n/2) {
		dfs(l);
		dfs(r);
		if (cur < m/2) {
			int mx = max(d[l], d[r]);
			while (b[l] < mx) {
				b[l] = INT32_MAX;
				heapify(l);
			}
			while (b[r] < mx) {
				b[r] = INT32_MAX;
				heapify(r);
			}
			b[cur] = a[cur];
			heapify(cur);
			d[cur] = b[cur];
			b[cur] = INT32_MAX;
			heapify(cur);
		}
		else if (cur < m) {
			b[cur] = a[cur];
			heapify(cur);
			d[cur] = b[cur];
			b[cur] = INT32_MAX;
			heapify(cur);
		}
	}
	else {
		b[cur] = a[cur];
		heapify(cur);
	}
}

int main() {
	fast_io;
	tc {
		cin >> h >> g;
		n = 1 << h;
		m = 1 << g;
		a.resize(n);
		b.resize(n);
		d.resize(m);
		for (int i = 1;i < n;i++)
			cin >> a[i];
		dfs(1);
		ll sum = 0;
		vi ans;
		for (int i = 1;i < m;i++) {
			sum += d[i];
			while (a[i] != d[i]) {
				f(i);
				ans.pb(i);
			}
		}
		cout << sum << '\n';
		for (auto el : ans)
			cout << el << ' ';
		cout << '\n';
	}
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef map<int,int> mii;
typedef map<ll,ll> mll;
#define pi 3.141592653589793
#define mod 998244353
#define pb push_back
#define all(v) v.begin(),v.end()
#define tc int t;cin>>t;while(t--)
#define pqmax priority_queue<int>
#define pqmin priority_queue<int,vi,greater<int>>
#define fast_io ios_base::sync_with_stdio(0), cin.tie(NULL)
#define tc_g int tt;cin>>tt;for(int ti=1;ti<=tt;ti++)
#define case_g "Case #"<<ti<<": "
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<int, null_type, greater_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_multiset;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
typedef tree<int, int, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_map;

int h, g, n, m;
vi a, b, d;

void heapify(int cur) {
	if (cur >= n/2)
		return;
	int l = cur * 2;
	int r = l + 1;
	if (min(b[l], b[r]) < b[cur]) {
		if (b[l] < b[r]) {
			swap(b[l], b[cur]);
			heapify(l);
		}
		else if (b[r] < b[l]) {
			swap(b[r], b[cur]);
			heapify(r);
		}
	}
}

void f(int cur) {
	if (cur >= n/2)
		a[cur] = 0;
	else {
		int l = cur * 2;
		int r = l + 1;
		if (a[l] > a[r]) {
			a[cur] = a[l];
			f(l);
		}
		else {
			a[cur] = a[r];
			f(r);
		}
	}
}

void dfs(int cur) {
	int l = cur * 2;
	int r = l + 1;
	if (cur < n/2) {
		dfs(l);
		dfs(r);
	}
	if (cur < m/2) {
		int mx = max(d[l], d[r]);
		while (b[l] < mx) {
			b[l] = INT32_MAX;
			heapify(l);
		}
		while (b[r] < mx) {
			b[r] = INT32_MAX;
			heapify(r);
		}
		b[cur] = a[cur];
		heapify(cur);
		d[cur] = b[cur];
		b[cur] = INT32_MAX;
		heapify(cur);
	}
	else if (cur < m) {
		b[cur] = a[cur];
		heapify(cur);
		d[cur] = b[cur];
		b[cur] = INT32_MAX;
		heapify(cur);
	}
	else {
		b[cur] = a[cur];
		heapify(cur);
	}
}

int main() {
	fast_io;
	tc {
		cin >> h >> g;
		n = 1 << h;
		m = 1 << g;
		a.resize(n);
		b.resize(n);
		d.resize(m);
		for (int i = 1;i < n;i++)
			cin >> a[i];
		dfs(1);
		ll sum = 0;
		vi ans;
		for (int i = 1;i < m;i++) {
			sum += d[i];
			while (a[i] != d[i]) {
				f(i);
				ans.pb(i);
			}
		}
		cout << sum << '\n';
		for (auto el : ans)
			cout << el << ' ';
		cout << '\n';
	}
}


#include<bits/stdc++.h>
#define mp make_pair
#define pii pair<int,int>
#pragma GCC optimize ("O3")
#define pdd pair<double,double>
#define pll pair<ll,ll>
#define mst(a,b) memset(a,b,sizeof(a))
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<ll> vec;
typedef vector<vec> mat;
const double eps = 1e-7;
const int maxn = (1<<22)+9;
const double pi=acos(-1.0);
const int inf = 1e7;
const ll mod = 1e6;
ll op[maxn];
int pq[maxn],a,b,num;
int test(int p){
    for(int i=20;i>=0;--i){
        if(p>>i&1) return i;
    }
}
ll hj(int pos){
    if(pos>=(1<<a)) return 0;
    ll ans=0;
    if(op[pos]&&test(pos)+1>b) ++ans;
    ans+=hj(pos<<1)+hj(pos<<1|1);
    return ans;
}
void update(int pos){
    if(pos>=(1<<a)) return;
    int l=pos<<1,r=pos<<1|1;
    if(l>=(1<<a)){
        op[pos]=0;
        return;
    }
    if(!op[l]&&!op[r]){
        op[pos]=0;
    }
    else if(op[l]>op[r]){
        op[pos]=op[l];
        update(l);
    }
    else{
        op[pos]=op[r];
        update(r);
    }
}
void sol(int pos){
    ll p1=hj(pos<<1),p2=hj(pos<<1|1);
    int l=pos<<1,r=pos<<1|1;
    while(p1&&p2){
        pq[num++]=pos;
        if(op[l]>op[r]) --p1;
        else --p2;
        update(pos);
    }
    if(!p1){
        while(p2&&op[l]<op[r]){
            pq[num++]=pos;
            --p2;
            update(pos);
        }
        if(p2) sol(r);
    }
    else{
        while(p1&&op[l]>op[r]){
            pq[num++]=pos;
            --p1;
            update(pos);
        }
        if(p1) sol(l);
    }
}
int main()
{
	ios::sync_with_stdio(false);
    int t;
    cin>>t;
    while(t--){
        cin>>a>>b;
        for(int i=1;i<(1<<a);++i) cin>>op[i];
        for(int i=(1<<a);i<(1<<a+1);++i) op[i]=0;
        ll ans=0;
        num=0;
        sol(1);
        for(int i=1;i<(1<<b);++i) ans+=op[i];
        cout<<ans<<"\n";
        for(int i=0;i<num;++i) cout<<pq[i]<<" ";
        cout<<"\n";
    }
	return 0;
}


#include<bits/stdc++.h>
#define mp make_pair
#define pii pair<int,int>
#pragma GCC optimize ("O3")
#define pdd pair<double,double>
#define pll pair<ll,ll>
#define mst(a,b) memset(a,b,sizeof(a))
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<ll> vec;
typedef vector<vec> mat;
const double eps = 1e-7;
const int maxn = (1<<22)+9;
const double pi=acos(-1.0);
const int inf = 1e7;
const ll mod = 1e6;
ll op[maxn];
int pq[maxn],a,b,num;
int test(int p){
    for(int i=20;i>=0;--i){
        if(p>>i&1) return i;
    }
}
ll hj(int pos){
    if(pos>=(1<<a)) return 0;
    ll ans=0;
    if(op[pos]&&test(pos)+1>b) ++ans;
    ans+=hj(pos<<1)+hj(pos<<1|1);
    return ans;
}
void update(int pos){
    if(pos>=(1<<a)) return;
    int l=pos<<1,r=pos<<1|1;
    if(l>=(1<<a)){
        op[pos]=0;
        return;
    }
    if(!op[l]&&!op[r]){
        op[pos]=0;
    }
    else if(op[l]>op[r]){
        op[pos]=op[l];
        update(l);
    }
    else{
        op[pos]=op[r];
        update(r);
    }
}
void sol(int pos){
    ll p1=hj(pos<<1),p2=hj(pos<<1|1);
    int l=pos<<1,r=pos<<1|1;
    while(p1&&p2){
        pq[num++]=pos;
        if(op[l]>op[r]) --p1;
        else --p2;
        update(pos);
    }
    if(!p1){
        while(p2&&op[l]<op[r]){
            pq[num++]=pos;
            --p2;
            update(pos);
        }
        if(p2) sol(r);
    }
    else{
        while(p1&&op[l]>op[r]){
            pq[num++]=pos;
            --p1;
            update(pos);
        }
        if(p1) sol(l);
    }
}
int main()
{
	ios::sync_with_stdio(false);
    int t;
    cin>>t;
    while(t--){
        cin>>a>>b;
        for(int i=1;i<(1<<a);++i) cin>>op[i];
        ll ans=0;
        num=0;
        sol(1);
        for(int i=1;i<(1<<b);++i) ans+=op[i];
        cout<<ans<<"\n";
        for(int i=0;i<num;++i) cout<<pq[i]<<" ";
        cout<<"\n";
    }
	return 0;
}


#include<bits/stdc++.h>
#define mp make_pair
#define pii pair<int,int>
#pragma GCC optimize ("O3")
#define pdd pair<double,double>
#define pll pair<ll,ll>
#define mst(a,b) memset(a,b,sizeof(a))
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<ll> vec;
typedef vector<vec> mat;
const double eps = 1e-7;
const int maxn = (1<<22)+9;
const double pi=acos(-1.0);
const int inf = 1e7;
const ll mod = 1e6;
ll op[maxn];
int pq[maxn],a,b,num;
int test(int p){
    for(int i=20;i>=0;--i){
        if(p>>i&1) return i;
    }
}
ll hj(int pos){
    if(pos>=(1<<a)) return 0;
    ll ans=0;
    if(op[pos]&&test(pos)+1>b) ++ans;
    ans+=hj(pos<<1)+hj(pos<<1|1);
    return ans;
}
void update(int pos){
    if(pos>=(1<<a)) return;
    int l=pos<<1,r=pos<<1|1;
    if(!op[l]&&!op[r]){
        op[pos]=0;
    }
    else if(op[l]>op[r]){
        op[pos]=op[l];
        update(l);
    }
    else{
        op[pos]=op[r];
        update(r);
    }
}
void sol(int pos){
    ll p1=hj(pos<<1),p2=hj(pos<<1|1);
    int l=pos<<1,r=pos<<1|1;
    while(p1&&p2){
        pq[num++]=pos;
        if(op[l]>op[r]) --p1;
        else --p2;
        update(pos);
    }
    if(!p1){
        while(p2&&op[l]<op[r]){
            pq[num++]=pos;
            --p2;
            update(pos);
        }
        if(p2) sol(r);
    }
    else{
        while(p1&&op[l]>op[r]){
            pq[num++]=pos;
            --p1;
            update(pos);
        }
        if(p1) sol(l);
    }
}
int main()
{
	ios::sync_with_stdio(false);
    int t;
    cin>>t;
    while(t--){
        cin>>a>>b;
        for(int i=1;i<(1<<a);++i) cin>>op[i];
        ll ans=0;
        num=0;
        sol(1);
        for(int i=1;i<(1<<b);++i) ans+=op[i];
        cout<<ans<<"\n";
        for(int i=0;i<num;++i) cout<<pq[i]<<" ";
        cout<<"\n";
    }
	return 0;
}


#include<bits/stdc++.h>
#define mp make_pair
#define pii pair<int,int>
#pragma GCC optimize ("O3")
#define pdd pair<double,double>
#define pll pair<ll,ll>
#define mst(a,b) memset(a,b,sizeof(a))
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<ll> vec;
typedef vector<vec> mat;
const double eps = 1e-7;
const int maxn = (1<<22)+9;
const double pi=acos(-1.0);
const int inf = 1e7;
const ll mod = 1e6;
ll op[maxn];
int pq[maxn],a,b,num;
void update(int pos){
    if(pos>=(1<<a)) return;
    int l=pos<<1,r=pos<<1|1;
    if(l>=(1<<a)){
        op[pos]=0;
        return;
    }
    if(!op[l]&&!op[r]){
        op[pos]=0;
    }
    else if(op[l]>op[r]){
        op[pos]=op[l];
        update(l);
    }
    else{
        op[pos]=op[r];
        update(r);
    }
}
bool test(int pos){
    int l=pos<<1,r=pos<<1|1;
    if(!op[l]&&!op[r]) return pos>=(1<<b);
    else if(op[l]>op[r]) return test(l);
    else return test(r);
}
void sol(int pos){
    if(!op[pos]) return;
    int l=pos<<1,r=pos<<1|1;
    while(test(pos)&&num<((1<<a)-(1<<b))){
        update(pos);
        pq[num++]=pos;
    }
    if(num<((1<<a)-(1<<b))){
        sol(l);
        sol(r);
    }
}
int main()
{
	ios::sync_with_stdio(false);
    int t;
    cin>>t;
    while(t--){
        cin>>a>>b;
        for(int i=1;i<(1<<a);++i) cin>>op[i];
        for(int i=(1<<a);i<=(1<<a+1);++i) op[i]=0;
        ll ans=0;
        num=0;
        sol(1);
        for(int i=1;i<(1<<b);++i) ans+=op[i];
        cout<<ans<<"\n";
        for(int i=0;i<num;++i) cout<<pq[i]<<" ";
        cout<<"\n";
    }
	return 0;
}


#include <cstdio>
#include <algorithm>
#include <map>
#include <iostream>
#include <vector>
#include <queue>
#include <set>
#include <cstring>
#include <unordered_map>
#include <iomanip>
#include <functional>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int maxn = 2500050;
const int inf = 0x3f3f3f3f;
const int mod = 998244353;

ll power(ll x, ll y)
{
    ll ans = 1;
    while (y)
    {
        if (y & 1)
            ans = ans * x % mod;
        x = x * x % mod;
        y >>= 1;
    }
    return ans;
}

ll gcd(ll x, ll y)
{
    if (x % y == 0)
        return y;
    return gcd(y, x % y);
}

int n, m, a[maxn], b[maxn], s[maxn], d[maxn];
vector<int> ans;
bool pa(int i)
{
    int l = i * 2, r = i * 2 + 1;
    if (a[l] == a[r] && a[l] == 0)
    {
        //printf("...%d %d %d\n", i, d[i], m);
        if (d[i] > m)
            return true;
        if (d[i] <= m)
        {
            //printf("..%d %d\n", d[i], m);
            return false;
        }
    }
    if (a[l] > a[r])
        return pa(l);
    else
        return pa(r);
}

void pb(int i)
{
    int l = i * 2, r = i * 2 + 1;
    if (a[l] == a[r] && a[l] == 0)
    {
        a[i] = 0;
        return;
    }
    int x = a[l] > a[r] ? l : r;
    a[i] = a[x];
    b[a[i]] = i;
    pb(x);
}

int main()
{
    int i, j, k, t;
    scanf("%d", &t);
    while (t--)
    {
        scanf("%d %d", &n, &m);
        for (i = 1; i < (1 << n); i++)
        {
            scanf("%d", &a[i]);
            b[a[i]] = i;
            s[i] = a[i];
            int x = 0, y = i;
            while (y)
            {
                y /= 2;
                x++;
            }
            d[i] = x;
        }
        sort(s + 1, s + (1 << n), greater<int>());
        for (i = (1 << n); i < (1 << (n + 1)); i++)
            a[i] = 0;
        ans.clear();
        ll sum = 0;
        for (i = 1; i < (1 << n); i++)
        {
            int x = s[i], y = b[x];
            //printf("%d %d %d\n", x, y, pa(y));
            if (pa(y))
            {
                pb(y);
                ans.push_back(y);
            }
            else
                sum += x;
        }
        printf("%I64d\n", sum);
        for (i = 0; i < ans.size(); i++)
            printf("%d%c", ans[i], i == ans.size() - 1 ? '\n' : ' ');
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
mt19937 rng(1234);
//mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

signed main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);
#endif
    ios_base::sync_with_stdio(0); cin.tie(0);

    int t;
    cin >> t;
    vector<char> pick((1 << 20) + 1);
    while (t--) {
        int h, g;
        cin >> h >> g;
        int n = (1 << h) - 1;
        vector<int> a(n);
        for (auto &e : a) cin >> e;

        int bot = 1 << (g - 1);
        vector<priority_queue<int, vector<int>, greater<>>> que(bot);
        vector<int> heap_idx(n);
        for (int i = bot - 1, j = 0; i < n; ++i, ++j) {
            heap_idx[i] = (j < bot ? j : heap_idx[(i - 1) / 2]);
            que[heap_idx[i]].push(a[i]);
        }

        for (int i = 0; i < bot; ++i) {
            pick[que[i].top()] = true;
        }

        for (int level = g - 2; level >= 0; --level) {
            int cnt = 1 << level;
            vector<priority_queue<int, vector<int>, greater<>>> nxt_que(cnt);
            for (int i = 0; i < cnt; ++i) {
                int l = 2 * i;
                int r = 2 * i + 1;
                int mi = max(que[l].top(), que[r].top());
                while (!que[l].empty()) {
                    int t = que[l].top();
                    que[l].pop();
                    if (t > mi) nxt_que[i].push(t);
                }
                while (!que[r].empty()) {
                    int t = que[r].top();
                    que[r].pop();
                    if (t > mi) nxt_que[i].push(t);
                }
                nxt_que[i].push(a[cnt - 1 + i]);
                pick[nxt_que[i].top()] = true;
            }
            swap(que, nxt_que);
        }

        vector<int> b = a;
        ll sum = 0;
        vector<int> moves;
        for (int i = 0; i < n; ) {
            if (pick[b[i]] || !b[i]) {
                pick[b[i]] = false;
                sum += b[i];
                ++i;
            }
            else {
                moves.push_back(i);
                int idx = i;
                while (2 * idx + 2 < n) {
                    if (b[2 * idx + 1] > b[2 * idx + 2]) {
                        swap(b[idx], b[2 * idx + 1]);
                        idx = 2 * idx + 1;
                    }
                    else {
                        swap(b[idx], b[2 * idx + 2]);
                        idx = 2 * idx + 2;
                    }
                }
                b[idx]  = 0;
            }
        }

        cout << sum << "\n";
        for (auto e : moves) cout << e + 1 << " ";
        cout << "\n";
    }
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
mt19937 rng(1234);
//mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

signed main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);
#endif
    ios_base::sync_with_stdio(0); cin.tie(0);

    int t;
    cin >> t;
    vector<char> pick((1 << 20) + 1);
    while (t--) {
        int h, g;
        cin >> h >> g;
        int n = (1 << h) - 1;
        vector<int> a(n);
        for (auto &e : a) cin >> e;

        auto up = [](int value, vector<int> &heap) {
            heap.push_back(value);
            int i = heap.size() - 1;
            while (i && heap[i] < heap[(i - 1) / 2]) {
                swap(heap[i], heap[(i - 1) / 2]);
                i = (i - 1) / 2;
            }
        };

        auto down = [](int idx, vector<int> &heap) {
            while (2 * idx + 2 < heap.size()) {
                if (heap[2 * idx + 1] > heap[2 * idx + 2]) {
                    swap(heap[idx], heap[2 * idx + 1]);
                    idx = 2 * idx + 1;
                } else {
                    swap(heap[idx], heap[2 * idx + 2]);
                    idx = 2 * idx + 2;
                }
            }
            heap[idx] = 0;
        };

        int bot = 1 << (g - 1);
        vector<vector<int>> que(bot);
        vector<int> heap_idx(n);
        for (int i = bot - 1, j = 0; i < n; ++i, ++j) {
            heap_idx[i] = (j < bot ? j : heap_idx[(i - 1) / 2]);
            up(a[i], que[heap_idx[i]]);
        }

        for (int i = 0; i < bot; ++i) {
            pick[que[i].front()] = true;
        }

        for (int level = g - 2; level >= 0; --level) {
            int cnt = 1 << level;
            vector<vector<int>> nxt_que(cnt);
            for (int i = 0; i < cnt; ++i) {
                int l = 2 * i;
                int r = 2 * i + 1;
                int mi = max(que[l].front(), que[r].front());
                for (auto &v : que[l]) {
                    if (v > mi) up(v, nxt_que[i]);
                }
                for (auto &v : que[r]) {
                    if (v > mi) up(v, nxt_que[i]);
                }
                up(a[cnt - 1 + i], nxt_que[i]);
                pick[nxt_que[i].front()] = true;
            }
            swap(que, nxt_que);
        }

        vector<int> b = a;
        ll sum = 0;
        vector<int> moves;
        for (int i = 0; i < n; ) {
            if (pick[b[i]] || !b[i]) {
                pick[b[i]] = false;
                sum += b[i];
                ++i;
            }
            else {
                moves.push_back(i);
                down(i, b);
            }
        }

        cout << sum << "\n";
        for (auto e : moves) cout << e + 1 << " ";
        cout << "\n";
    }
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
mt19937 rng(1234);
//mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

signed main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);
#endif
    ios_base::sync_with_stdio(0); cin.tie(0);

    int t;
    cin >> t;
    vector<char> pick((1 << 22) + 1);
    while (t--) {
        int h, g;
        cin >> h >> g;
        int n = (1 << h) - 1;
        vector<int> a(n);
        for (auto &e : a) cin >> e;

        int bot = 1 << (g - 1);
        vector<priority_queue<int, vector<int>, greater<>>> que(bot);
        vector<int> heap_idx(n);
        for (int i = bot - 1, j = 0; i < n; ++i, ++j) {
            heap_idx[i] = (j < bot ? j : heap_idx[(i - 1) / 2]);
            que[heap_idx[i]].push(a[i]);
        }

        for (int i = 0; i < bot; ++i) {
            if (que[i].empty()) {
                cout << "error";
                exit(0);
            }
            pick[que[i].top()] = true;
        }

        for (int level = g - 2; level >= 0; --level) {
            int cnt = 1 << level;
            vector<priority_queue<int, vector<int>, greater<>>> nxt_que(cnt);
            for (int i = 0; i < cnt; ++i) {
                int l = 2 * i;
                int r = 2 * i + 1;
                if (que[l].empty() || que[r].empty()) {
                    cout << "error1";
                    exit(0);
                }
                int mi = max(que[l].top(), que[r].top());
                while (!que[l].empty()) {
                    int t = que[l].top();
                    que[l].pop();
                    if (t > mi) nxt_que[i].push(t);
                }
                while (!que[r].empty()) {
                    int t = que[r].top();
                    que[r].pop();
                    if (t > mi) nxt_que[i].push(t);
                }

                pick[nxt_que[i].top()] = true;
            }
            que = nxt_que;
        }

        vector<int> b = a;
        ll sum = 0;
        vector<int> moves;
        for (int i = 0; i < n; ) {
            if (pick[b[i]] || !b[i]) {
                pick[b[i]] = false;
                sum += b[i];
                ++i;
            }
            else {
                moves.push_back(i);
                int idx = i;
                while (2 * idx + 2 < n) {
                    if (b[2 * idx + 1] > b[2 * idx + 2]) {
                        swap(b[idx], b[2 * idx + 1]);
                        idx = 2 * idx + 1;
                    }
                    else {
                        swap(b[idx], b[2 * idx + 2]);
                        idx = 2 * idx + 2;
                    }
                }
                b[idx]  = 0;
            }
        }

        cout << sum << "\n";
        for (auto e : moves) cout << e + 1 << " ";
        cout << "\n";
    }
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
mt19937 rng(1234);
//mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

signed main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);
#endif
    ios_base::sync_with_stdio(0); cin.tie(0);

    int t;
    cin >> t;
    vector<char> pick((1 << 20) + 1);
    while (t--) {
        int h, g;
        cin >> h >> g;
        int n = (1 << h) - 1;
        vector<int> a(n);
        for (auto &e : a) cin >> e;

        int bot = 1 << (g - 1);
        vector<priority_queue<int, vector<int>, greater<>>> que(bot);
        vector<int> heap_idx(n);
        for (int i = bot - 1, j = 0; i < n; ++i, ++j) {
            heap_idx[i] = (j < bot ? j : heap_idx[(i - 1) / 2]);
            que[heap_idx[i]].push(a[i]);
        }

        for (int i = 0; i < bot; ++i) {
            if (que[i].empty()) {
                cout << "error";
                exit(0);
            }
            pick[que[i].top()] = true;
        }

        for (int level = g - 2; level >= 0; --level) {
            int cnt = 1 << level;
            vector<priority_queue<int, vector<int>, greater<>>> nxt_que(cnt);
            for (int i = 0; i < cnt; ++i) {
                int l = 2 * i;
                int r = 2 * i + 1;
                if (que[l].empty() || que[r].empty()) {
                    cout << "error1";
                    exit(0);
                }
                int mi = max(que[l].top(), que[r].top());
                while (!que[l].empty()) {
                    int t = que[l].top();
                    que[l].pop();
                    if (t > mi) nxt_que[i].push(t);
                }
                while (!que[r].empty()) {
                    int t = que[r].top();
                    que[r].pop();
                    if (t > mi) nxt_que[i].push(t);
                }

                pick[nxt_que[i].top()] = true;
            }
            que = nxt_que;
        }

        vector<int> b = a;
        ll sum = 0;
        vector<int> moves;
        for (int i = 0; i < n; ) {
            if (pick[b[i]] || !b[i]) {
                pick[b[i]] = false;
                sum += b[i];
                ++i;
            }
            else {
                moves.push_back(i);
                int idx = i;
                while (2 * idx + 2 < n) {
                    if (b[2 * idx + 1] > b[2 * idx + 2]) {
                        swap(b[idx], b[2 * idx + 1]);
                        idx = 2 * idx + 1;
                    }
                    else {
                        swap(b[idx], b[2 * idx + 2]);
                        idx = 2 * idx + 2;
                    }
                }
                b[idx]  = 0;
            }
        }

        cout << sum << "\n";
        for (auto e : moves) cout << e + 1 << " ";
        cout << "\n";
    }
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
mt19937 rng(1234);
//mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

signed main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);
#endif
    ios_base::sync_with_stdio(0); cin.tie(0);

    int t;
    cin >> t;
    vector<char> pick((1 << 22) + 1);
    while (t--) {
        int h, g;
        cin >> h >> g;
        int n = (1 << h) - 1;
        vector<int> a(n);
        for (auto &e : a) cin >> e;

        int bot = 1 << (g - 1);
        vector<priority_queue<int, vector<int>, greater<>>> que(bot);
        vector<int> heap_idx(n);
        for (int i = bot - 1, j = 0; i < n; ++i, ++j) {
            heap_idx[i] = (j < bot ? j : heap_idx[(i - 1) / 2]);
            que[heap_idx[i]].push(a[i]);
        }

        for (int i = 0; i < bot; ++i) {
            if (que[i].empty()) {
                cout << "error";
                exit(0);
            }
            pick[que[i].top()] = true;
        }

        for (int level = g - 2; level >= 0; --level) {
            int cnt = 1 << level;
            vector<priority_queue<int, vector<int>, greater<>>> nxt_que(cnt);
            for (int i = 0; i < cnt; ++i) {
                int l = 2 * i;
                int r = 2 * i + 1;
                if (que[l].empty() || que[r].empty()) {
                    cout << "error1";
                    exit(0);
                }
                int mi = max(que[l].top(), que[r].top());
                while (!que[l].empty()) {
                    int t = que[l].top();
                    que[l].pop();
                    if (t > mi) nxt_que[i].push(t);
                }
                while (!que[r].empty()) {
                    int t = que[r].top();
                    que[r].pop();
                    if (t > mi) nxt_que[i].push(t);
                }

                if (nxt_que[i].empty()) {
                    cout << "error2";
                    exit(0);
                }
                pick[nxt_que[i].top()] = true;
            }
            que = nxt_que;
        }

        vector<int> b = a;
        ll sum = 0;
        vector<int> moves;
        for (int i = 0; i < n; ) {
            if (pick[b[i]] || !b[i]) {
                pick[b[i]] = false;
                sum += b[i];
                ++i;
            }
            else {
                moves.push_back(i);
                int idx = i;
                while (2 * idx + 2 < n) {
                    if (b[2 * idx + 1] > b[2 * idx + 2]) {
                        swap(b[idx], b[2 * idx + 1]);
                        idx = 2 * idx + 1;
                    }
                    else {
                        swap(b[idx], b[2 * idx + 2]);
                        idx = 2 * idx + 2;
                    }
                }
                b[idx]  = 0;
            }
        }

        cout << sum << "\n";
        for (auto e : moves) cout << e + 1 << " ";
        cout << "\n";
    }
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
mt19937 rng(1234);
//mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

signed main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);
#endif
    ios_base::sync_with_stdio(0); cin.tie(0);

    int t;
    cin >> t;
    vector<char> pick((1 << 20) + 1);
    while (t--) {
        int h, g;
        cin >> h >> g;
        int n = (1 << h) - 1;
        vector<int> a(n);
        for (auto &e : a) cin >> e;

        int bot = 1 << (g - 1);
        vector<priority_queue<int, vector<int>, greater<>>> que(bot);
        vector<int> heap_idx(n);
        for (int i = bot - 1, j = 0; i < n; ++i, ++j) {
            heap_idx[i] = (j < bot ? j : heap_idx[(i - 1) / 2]);
            que[heap_idx[i]].push(a[i]);
        }

        for (int i = 0; i < bot; ++i) {
            if (que[i].empty()) {
                cout << "error";
                exit(0);
            }
            pick[que[i].top()] = true;
        }

        for (int level = g - 2; level >= 0; --level) {
            int cnt = 1 << level;
            vector<priority_queue<int, vector<int>, greater<>>> nxt_que(cnt);
            for (int i = 0; i < cnt; ++i) {
                int l = 2 * i;
                int r = 2 * i + 1;
                int mi = max(que[l].top(), que[r].top());
                while (!que[l].empty()) {
                    int t = que[l].top();
                    que[l].pop();
                    if (t > mi) nxt_que[i].push(t);
                }
                while (!que[r].empty()) {
                    int t = que[r].top();
                    que[r].pop();
                    if (t > mi) nxt_que[i].push(t);
                }

                pick[nxt_que[i].top()] = true;
            }
            que = nxt_que;
        }

        vector<int> b = a;
        ll sum = 0;
        vector<int> moves;
        for (int i = 0; i < n; ) {
            if (pick[b[i]] || !b[i]) {
                pick[b[i]] = false;
                sum += b[i];
                ++i;
            }
            else {
                moves.push_back(i);
                int idx = i;
                while (2 * idx + 2 < n) {
                    if (b[2 * idx + 1] > b[2 * idx + 2]) {
                        swap(b[idx], b[2 * idx + 1]);
                        idx = 2 * idx + 1;
                    }
                    else {
                        swap(b[idx], b[2 * idx + 2]);
                        idx = 2 * idx + 2;
                    }
                }
                b[idx]  = 0;
            }
        }

        cout << sum << "\n";
        for (auto e : moves) cout << e + 1 << " ";
        cout << "\n";
    }
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
mt19937 rng(1234);
//mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

signed main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);
#endif
    ios_base::sync_with_stdio(0); cin.tie(0);

    int t;
    cin >> t;
    while (t--) {
        int h, g;
        cin >> h >> g;
        int n = (1 << h) - 1;
        vector<int> a(n);
        for (auto &e : a) cin >> e;

        int bot = 1 << (g - 1);
        vector<priority_queue<int, vector<int>, greater<>>> que(bot);
        vector<int> heap_idx(n);
        for (int i = bot - 1, j = 0; i < n; ++i, ++j) {
            heap_idx[i] = (j < bot ? j : heap_idx[(i - 1) / 2]);
            que[heap_idx[i]].push(a[i]);
        }

        vector<char> pick((1 << 20) + 1);
        for (int i = 0; i < bot; ++i) {
            pick[que[i].top()] = true;
        }

        for (int level = g - 2; level >= 0; --level) {
            int cnt = 1 << level;
            vector<priority_queue<int, vector<int>, greater<>>> nxt_que(cnt);
            for (int i = 0; i < cnt; ++i) {
                int l = 2 * i;
                int r = 2 * i + 1;
                int mi = max(que[l].top(), que[r].top());
                while (!que[l].empty()) {
                    int t = que[l].top();
                    que[l].pop();
                    if (t > mi) nxt_que[i].push(t);
                }
                while (!que[r].empty()) {
                    int t = que[r].top();
                    que[r].pop();
                    if (t > mi) nxt_que[i].push(t);
                }

                pick[nxt_que[i].top()] = true;
            }
            que = nxt_que;
        }

        vector<int> b = a;
        ll sum = 0;
        vector<int> moves;
        for (int i = 0; i < n; ) {
            if (pick[b[i]] || !b[i]) {
                sum += b[i];
                ++i;
            }
            else {
                moves.push_back(i);
                int idx = i;
                while (2 * idx + 2 < n) {
                    if (b[2 * idx + 1] > b[2 * idx + 2]) {
                        swap(b[idx], b[2 * idx + 1]);
                        idx = 2 * idx + 1;
                    }
                    else {
                        swap(b[idx], b[2 * idx + 2]);
                        idx = 2 * idx + 2;
                    }
                }
                b[idx] = 0;
            }
        }

        cout << sum << "\n";
        for (auto e : moves) cout << e + 1 << " ";
        cout << "\n";
    }
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
mt19937 rng(1234);
//mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

signed main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);
#endif
    ios_base::sync_with_stdio(0); cin.tie(0);

    int t;
    cin >> t;
    vector<char> pick((1 << 22) + 1);
    while (t--) {
        int h, g;
        cin >> h >> g;
        int n = (1 << h) - 1;
        vector<int> a(n);
        for (auto &e : a) cin >> e;

        int bot = 1 << (g - 1);
        vector<priority_queue<int, vector<int>, greater<>>> que(bot);
        vector<int> heap_idx(n);
        for (int i = bot - 1, j = 0; i < n; ++i, ++j) {
            heap_idx[i] = (j < bot ? j : heap_idx[(i - 1) / 2]);
            que[heap_idx[i]].push(a[i]);
        }

        for (int i = 0; i < bot; ++i) {
            if (que[i].empty()) {
                cout << "error";
                exit(0);
            }
            pick[que[i].top()] = true;
        }

        for (int level = g - 2; level >= 0; --level) {
            int cnt = 1 << level;
            vector<priority_queue<int, vector<int>, greater<>>> nxt_que(cnt);
            for (int i = 0; i < cnt; ++i) {
                int l = 2 * i;
                int r = 2 * i + 1;
                if (que[l].empty() || que[r].empty()) {
                    cout << "error1";
                    exit(0);
                }
                int mi = max(que[l].top(), que[r].top());
                while (!que[l].empty()) {
                    int t = que[l].top();
                    que[l].pop();
                    if (t > mi) nxt_que[i].push(t);
                }
                while (!que[r].empty()) {
                    int t = que[r].top();
                    que[r].pop();
                    if (t > mi) nxt_que[i].push(t);
                }
                nxt_que[i].push(a[cnt - 1 + i]);

                if (nxt_que[i].empty()) {
                    cout << "error2";
                    exit(0);
                }
                pick[nxt_que[i].top()] = true;
            }
            que = nxt_que;
        }

        vector<int> b = a;
        ll sum = 0;
        vector<int> moves;
        for (int i = 0; i < n; ) {
            if (pick[b[i]] || !b[i]) {
                pick[b[i]] = false;
                sum += b[i];
                ++i;
            }
            else {
                moves.push_back(i);
                int idx = i;
                while (2 * idx + 2 < n) {
                    if (b[2 * idx + 1] > b[2 * idx + 2]) {
                        swap(b[idx], b[2 * idx + 1]);
                        idx = 2 * idx + 1;
                    }
                    else {
                        swap(b[idx], b[2 * idx + 2]);
                        idx = 2 * idx + 2;
                    }
                }
                b[idx]  = 0;
            }
        }

        cout << sum << "\n";
        for (auto e : moves) cout << e + 1 << " ";
        cout << "\n";
    }
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
mt19937 rng(1234);
//mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

signed main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);
#endif
    ios_base::sync_with_stdio(0); cin.tie(0);

    int t;
    cin >> t;
    vector<char> pick((1 << 20) + 1);
    while (t--) {
        int h, g;
        cin >> h >> g;
        int n = (1 << h) - 1;
        vector<int> a(n);
        for (auto &e : a) cin >> e;

        int bot = 1 << (g - 1);
        vector<priority_queue<int, vector<int>, greater<>>> que(bot);
        vector<int> heap_idx(n);
        for (int i = bot - 1, j = 0; i < n; ++i, ++j) {
            heap_idx[i] = (j < bot ? j : heap_idx[(i - 1) / 2]);
            que[heap_idx[i]].push(a[i]);
        }

        for (int i = 0; i < bot; ++i) {
            pick[que[i].top()] = true;
        }

        for (int level = g - 2; level >= 0; --level) {
            int cnt = 1 << level;
            vector<priority_queue<int, vector<int>, greater<>>> nxt_que(cnt);
            for (int i = 0; i < cnt; ++i) {
                int l = 2 * i;
                int r = 2 * i + 1;
                int mi = max(que[l].top(), que[r].top());
                while (!que[l].empty()) {
                    int t = que[l].top();
                    que[l].pop();
                    if (t > mi) nxt_que[i].push(t);
                }
                while (!que[r].empty()) {
                    int t = que[r].top();
                    que[r].pop();
                    if (t > mi) nxt_que[i].push(t);
                }

                pick[nxt_que[i].top()] = true;
            }
            que = nxt_que;
        }

        vector<int> b = a;
        ll sum = 0;
        vector<int> moves;
        for (int i = 0; i < n; ) {
            if (pick[b[i]] || !b[i]) {
                pick[b[i]] = false;
                sum += b[i];
                ++i;
            }
            else {
                moves.push_back(i);
                int idx = i;
                while (2 * idx + 2 < n) {
                    if (b[2 * idx + 1] > b[2 * idx + 2]) {
                        swap(b[idx], b[2 * idx + 1]);
                        idx = 2 * idx + 1;
                    }
                    else {
                        swap(b[idx], b[2 * idx + 2]);
                        idx = 2 * idx + 2;
                    }
                }
                b[idx] = 0;
            }
        }

        cout << sum << "\n";
        for (auto e : moves) cout << e + 1 << " ";
        cout << "\n";
    }
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
mt19937 rng(1234);
//mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

signed main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);
#endif
    ios_base::sync_with_stdio(0); cin.tie(0);

    int t;
    cin >> t;
    vector<char> pick((1 << 20) + 1);
    while (t--) {
        int h, g;
        cin >> h >> g;
        int n = (1 << h) - 1;
        vector<int> a(n);
        for (auto &e : a) cin >> e;

        int bot = 1 << (g - 1);
        vector<priority_queue<int, vector<int>, greater<>>> que(bot);
        vector<int> heap_idx(n);
        for (int i = bot - 1, j = 0; i < n; ++i, ++j) {
            heap_idx[i] = (j < bot ? j : heap_idx[(i - 1) / 2]);
            que[heap_idx[i]].push(a[i]);
        }

        for (int i = 0; i < bot; ++i) {
            pick[que[i].top()] = true;
        }

        for (int level = g - 2; level >= 0; --level) {
            int cnt = 1 << level;
            vector<priority_queue<int, vector<int>, greater<>>> nxt_que(cnt);
            for (int i = 0; i < cnt; ++i) {
                int l = 2 * i;
                int r = 2 * i + 1;
                int mi = max(que[l].top(), que[r].top());
                while (!que[l].empty()) {
                    int t = que[l].top();
                    que[l].pop();
                    if (t > mi) nxt_que[i].push(t);
                }
                while (!que[r].empty()) {
                    int t = que[r].top();
                    que[r].pop();
                    if (t > mi) nxt_que[i].push(t);
                }
                nxt_que[i].push(a[cnt - 1 + i]);
                pick[nxt_que[i].top()] = true;
            }
            que = nxt_que;
        }

        vector<int> b = a;
        ll sum = 0;
        vector<int> moves;
        for (int i = 0; i < n; ) {
            if (pick[b[i]] || !b[i]) {
                pick[b[i]] = false;
                sum += b[i];
                ++i;
            }
            else {
                moves.push_back(i);
                int idx = i;
                while (2 * idx + 2 < n) {
                    if (b[2 * idx + 1] > b[2 * idx + 2]) {
                        swap(b[idx], b[2 * idx + 1]);
                        idx = 2 * idx + 1;
                    }
                    else {
                        swap(b[idx], b[2 * idx + 2]);
                        idx = 2 * idx + 2;
                    }
                }
                b[idx]  = 0;
            }
        }

        cout << sum << "\n";
        for (auto e : moves) cout << e + 1 << " ";
        cout << "\n";
    }
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
mt19937 rng(1234);
//mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

signed main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);
#endif
    ios_base::sync_with_stdio(0); cin.tie(0);

    int t;
    cin >> t;
    while (t--) {
        int h, g;
        cin >> h >> g;
        int n = (1 << h) - 1;
        vector<int> a(n);
        for (auto &e : a) cin >> e;

        int bot = 1 << (g - 1);
        vector<priority_queue<int, vector<int>, greater<>>> que(bot);
        vector<int> heap_idx(n);
        for (int i = bot - 1, j = 0; i < n; ++i, ++j) {
            heap_idx[i] = (j < bot ? j : heap_idx[(i - 1) / 2]);
            que[heap_idx[i]].push(a[i]);
        }

        vector<char> pick(n + 1);
        for (int i = 0; i < bot; ++i) {
            pick[que[i].top()] = true;
        }

        for (int level = g - 2; level >= 0; --level) {
            int cnt = 1 << level;
            vector<priority_queue<int, vector<int>, greater<>>> nxt_que(cnt);
            for (int i = 0; i < cnt; ++i) {
                int l = 2 * i;
                int r = 2 * i + 1;
                int mi = max(que[l].top(), que[r].top());
                while (!que[l].empty()) {
                    int t = que[l].top();
                    que[l].pop();
                    if (t > mi) nxt_que[i].push(t);
                }
                while (!que[r].empty()) {
                    int t = que[r].top();
                    que[r].pop();
                    if (t > mi) nxt_que[i].push(t);
                }

                pick[nxt_que[i].top()] = true;
            }
            que = nxt_que;
        }

        vector<int> b = a;
        ll sum = 0;
        vector<int> moves;
        for (int i = 0; i < n; ) {
            if (pick[b[i]] || !b[i]) {
                sum += b[i];
                ++i;
            }
            else {
                moves.push_back(i);
                int idx = i;
                while (2 * idx + 2 < n) {
                    if (b[2 * idx + 1] > b[2 * idx + 2]) {
                        swap(b[idx], b[2 * idx + 1]);
                        idx = 2 * idx + 1;
                    }
                    else {
                        swap(b[idx], b[2 * idx + 2]);
                        idx = 2 * idx + 2;
                    }
                }
                b[idx] = 0;
            }
        }

        cout << sum << "\n";
        for (auto e : moves) cout << e + 1 << " ";
        cout << "\n";
    }
}


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

#define pb push_back
#define mp make_pair

#define forn(i,n) for(int i = 0;i<int(n);i++)
#define dforn(i,n) for(int i = int(n)-1;i>=0;i--)
#define forsn(i,s,n) for(int i = int(s);i<int(n);i++)
#define fore(i,s,n) for(int i = int(s);i<int(n);i++)
#define dforsn(i,s,n) for(int i = int(n)-1;i>=int(s);i--)

#define DBG(x) do{cout<<#x<<" = "<<x<<endl;}while(false)
#define get(mask,i) (mask&(1<<i))
#define set(mask,i) (mask|=(1<<i))
template<class T> ostream & operator<<(ostream & out, vector<T> & v){
	out<<"[";
	for(auto k : v) out<<k<<" ";
	out<<"]"<<"\n";
	return out;
}
typedef long long intl;
priority_queue<pair<int,int> > PQ;
void F(int i, vector<int> & h, vector<bool> & leg, int n2){
    if(leg[i]==false)return;
    int l = i*2;
    int r = i*2+1;
    if(h[l]==0 && h[r]==0){
        if(i<n2){
            leg[i]=false;
        }
        else h[i] = 0;
    }
    else if(h[l]>h[r]){
        int v = h[l];//h[i]=h[l];
        F(l,h,leg,n2);
        if(leg[l]){
            h[i] = v;
        }else leg[i]=false;
    }else{
        //h[i] = h[r];
        int v = h[r];
        F(r,h,leg,n2);
        if(leg[r]){
            h[i]=v;
        }else leg[i]= false;
    }
    if(leg[i] && h[i]!=0)PQ.push({h[i],i});
}
int main()
{
    cin.tie(0);
    cin.sync_with_stdio(0);
    /*vector<int> a = {0,7,6,5,4,3,2,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
    int n1 = 7, n2 = 3;
    vector<bool> leg(15,true);
    F(2,a,leg,n2);
    F(1,a,leg,n2);
    F(3,a,leg,n2);
    F(1,a,leg,n2);
    cout<<a<<" / "<<leg<<endl;*/
	int t; cin>>t;
	while(t--){
        PQ = priority_queue<pair<int,int> >();
        int h,g;
        cin>>h>>g;
        int n1 = 1<<h, n2 = 1<<g, d = n1-n2;
        vector<int> a(n1*4);
        vector<bool> leg(n1*4,true);
        intl tot = 0;
        forsn(i,1,n1){
            cin>>a[i];
            tot+=a[i];
        //    cout<<i<<" , "<<a[i]<<endl;
            PQ.push({a[i],i});
        }
        vector<intl> res;
        forn(i,d){
        //    cout<<a<<endl;
            bool seguir = true;
            int v,p;
            do{
                auto q = PQ.top();
                PQ.pop();
                v = q.first, p = q.second;
          //      cout<<v<<" ::: "<<p<<endl;
                if(leg[p] && a[p]==v) seguir = false;
            }while(seguir);
        //    cout<<"Llamo en "<<p<<" con "<<leg[p]<<endl;
            F(p,a,leg,n2);
            if(leg[p]==false)d++;
            else {
                    tot-=v;
                res.push_back(p);
            }
        }
        cout<<tot<<"\n";
        forn(i,res.size())cout<<res[i]<<((i==int(res.size()-1))?"\n":" ");
        //cout<<a<<endl;
       // cout<<tot<<" : "<<a<<endl<<leg<<endl;
	}
	return 0;
}


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

#define pb push_back
#define mp make_pair

#define forn(i,n) for(int i = 0;i<int(n);i++)
#define dforn(i,n) for(int i = int(n)-1;i>=0;i--)
#define forsn(i,s,n) for(int i = int(s);i<int(n);i++)
#define fore(i,s,n) for(int i = int(s);i<int(n);i++)
#define dforsn(i,s,n) for(int i = int(n)-1;i>=int(s);i--)

#define DBG(x) do{cout<<#x<<" = "<<x<<endl;}while(false)
#define get(mask,i) (mask&(1<<i))
#define set(mask,i) (mask|=(1<<i))
template<class T> ostream & operator<<(ostream & out, vector<T> & v){
	out<<"[";
	for(auto k : v) out<<k<<" ";
	out<<"]"<<"\n";
	return out;
}
typedef long long intl;
priority_queue<pair<int,int> > PQ;
void F(int i, vector<int> & h, vector<bool> & leg, int n2){
    if(leg[i]==false)return;
    int l = i*2;
    int r = i*2+1;
    if(h[l]==0 && h[r]==0){
        if(i<n2){
            leg[i]=false;
        }
        else h[i] = 0;
    }
    else if(h[l]>h[r]){
        int v = h[l];//h[i]=h[l];
        F(l,h,leg,n2);
        if(leg[l]){
            h[i] = v;
        }else leg[i]=false;
    }else{
        //h[i] = h[r];
        int v = h[r];
        F(r,h,leg,n2);
        if(leg[r]){
            h[i]=v;
        }else leg[i]= false;
    }
    PQ.push({h[i],i});
}
int main()
{
    cin.tie(0);
    cin.sync_with_stdio(0);
    /*vector<int> a = {0,7,6,5,4,3,2,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
    int n1 = 7, n2 = 3;
    vector<bool> leg(15,true);
    F(2,a,leg,n2);
    F(1,a,leg,n2);
    F(3,a,leg,n2);
    F(1,a,leg,n2);
    cout<<a<<" / "<<leg<<endl;*/
	int t; cin>>t;
	while(t--){
        PQ = priority_queue<pair<int,int> >();
        int h,g;
        cin>>h>>g;
        int n1 = 1<<h, n2 = 1<<g, d = n1-n2;
        vector<int> a(n1*4);
        vector<bool> leg(n1*4,true);
        intl tot = 0;
        forsn(i,1,n1){
            cin>>a[i];
            tot+=a[i];
        //    cout<<i<<" , "<<a[i]<<endl;
            PQ.push({a[i],i});
        }
        vector<intl> res;
        forn(i,d){
        //    cout<<a<<endl;
            bool seguir = true;
            int v,p;
            do{
                auto q = PQ.top();
                PQ.pop();
                v = q.first, p = q.second;
          //      cout<<v<<" ::: "<<p<<endl;
                if(leg[p]&& a[p]==v) seguir = false;
            }while(seguir);
        //    cout<<"Llamo en "<<p<<" con "<<leg[p]<<endl;
            F(p,a,leg,n2);
            if(leg[p]==false)d++;
            else {
                    tot-=v;
                res.push_back(p);
            }
        }
        cout<<tot<<"\n";
        forn(i,res.size())cout<<res[i]<<((i==int(res.size()-1))?"\n":" ");
        //cout<<a<<endl;
       // cout<<tot<<" : "<<a<<endl<<leg<<endl;
	}
	return 0;
}


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

#define pb push_back
#define mp make_pair

#define forn(i,n) for(int i = 0;i<int(n);i++)
#define dforn(i,n) for(int i = int(n)-1;i>=0;i--)
#define forsn(i,s,n) for(int i = int(s);i<int(n);i++)
#define fore(i,s,n) for(int i = int(s);i<int(n);i++)
#define dforsn(i,s,n) for(int i = int(n)-1;i>=int(s);i--)

#define DBG(x) do{cout<<#x<<" = "<<x<<endl;}while(false)
#define get(mask,i) (mask&(1<<i))
#define set(mask,i) (mask|=(1<<i))
template<class T> ostream & operator<<(ostream & out, vector<T> & v){
	out<<"[";
	for(auto k : v) out<<k<<" ";
	out<<"]"<<"\n";
	return out;
}
typedef long long intl;
priority_queue<pair<int,int> > PQ;
void F(int i, vector<int> & h, vector<bool> & leg, int n2){
    if(leg[i]==false)return;
    int l = i*2;
    int r = i*2+1;
    if(h[l]==0 && h[r]==0){
        if(i<n2){
            leg[i]=false;
        }
        else h[i] = 0;
    }
    else if(h[l]>h[r]){
        int v = h[l];//h[i]=h[l];
        F(l,h,leg,n2);
        if(leg[l]){
            h[i] = v;
        }else leg[i]=false;
    }else{
        //h[i] = h[r];
        int v = h[r];
        F(r,h,leg,n2);
        if(leg[r]){
            h[i]=v;
        }else leg[i]= false;
    }
    //if(leg[i] && h[i]!=0)PQ.push({h[i],i});
}
int main()
{
    cin.tie(0);
    cin.sync_with_stdio(0);
    /*vector<int> a = {0,7,6,5,4,3,2,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
    int n1 = 7, n2 = 3;
    vector<bool> leg(15,true);
    F(2,a,leg,n2);
    F(1,a,leg,n2);
    F(3,a,leg,n2);
    F(1,a,leg,n2);
    cout<<a<<" / "<<leg<<endl;*/
	int t; cin>>t;
	while(t--){
        PQ = priority_queue<pair<int,int> >();
        int h,g;
        cin>>h>>g;
        int n1 = 1<<h, n2 = 1<<g, d = n1-n2;
        vector<int> a(n1*4);
        vector<bool> leg(n1*4,true);
        intl tot = 0;
        forsn(i,1,n1){
            cin>>a[i];
            tot+=a[i];
        //    cout<<i<<" , "<<a[i]<<endl;
        //    PQ.push({a[i],i});
        }
        vector<intl> res;
        PQ.push({a[1],1});
        forn(i,d){
        //    cout<<a<<endl;
            bool seguir = true;
            int v,p;
            do{
                auto q = PQ.top();
                PQ.pop();
                v = q.first, p = q.second;
          //      cout<<v<<" ::: "<<p<<endl;
                if(leg[p] && a[p]==v) seguir = false;
            }while(seguir);
        //    cout<<"Llamo en "<<p<<" con "<<leg[p]<<endl;
            F(p,a,leg,n2);
            if(leg[p]==false){
                d++;
                PQ.push({a[p*2],p*2});
                PQ.push({a[p*2+1],p*2+1});
            }
            else {
                PQ.push({a[p],p});
                tot-=v;
                res.push_back(p);
            }
        }
        cout<<tot<<"\n";
        forn(i,res.size())cout<<res[i]<<((i==int(res.size()-1))?"\n":" ");
        //cout<<a<<endl;
       // cout<<tot<<" : "<<a<<endl<<leg<<endl;
	}
	return 0;
}


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

#define pb push_back
#define mp make_pair

#define forn(i,n) for(int i = 0;i<int(n);i++)
#define dforn(i,n) for(int i = int(n)-1;i>=0;i--)
#define forsn(i,s,n) for(int i = int(s);i<int(n);i++)
#define fore(i,s,n) for(int i = int(s);i<int(n);i++)
#define dforsn(i,s,n) for(int i = int(n)-1;i>=int(s);i--)

#define DBG(x) do{cout<<#x<<" = "<<x<<endl;}while(false)
#define get(mask,i) (mask&(1<<i))
#define set(mask,i) (mask|=(1<<i))
template<class T> ostream & operator<<(ostream & out, vector<T> & v){
	out<<"[";
	for(auto k : v) out<<k<<" ";
	out<<"]"<<"\n";
	return out;
}
typedef long long intl;
priority_queue<pair<int,int> > PQ;
void F(int i, vector<int> & h, vector<bool> & leg, int n2){
    if(leg[i]==false)return;
    int l = i*2;
    int r = i*2+1;
    if(h[l]==0 && h[r]==0){
        if(i<n2){
            leg[i]=false;
        }
        else h[i] = 0;
    }
    else if(h[l]>h[r]){
        int v = h[l];//h[i]=h[l];
        F(l,h,leg,n2);
        if(leg[l]){
            h[i] = v;
        }else leg[i]=false;
    }else{
        //h[i] = h[r];
        int v = h[r];
        F(r,h,leg,n2);
        if(leg[r]){
            h[i]=v;
        }else leg[i]= false;
    }
    PQ.push({h[i],i});
}
int main()
{
    cin.tie(0);
    cin.sync_with_stdio(0);
    /*vector<int> a = {0,7,6,5,4,3,2,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
    int n1 = 7, n2 = 3;
    vector<bool> leg(15,true);
    F(2,a,leg,n2);
    F(1,a,leg,n2);
    F(3,a,leg,n2);
    F(1,a,leg,n2);
    cout<<a<<" / "<<leg<<endl;*/
	int t; cin>>t;
	while(t--){
        PQ = priority_queue<pair<int,int> >();
        int h,g;
        cin>>h>>g;
        int n1 = 1<<h, n2 = 1<<g, d = n1-n2;
        vector<int> a(n1*4);
        vector<bool> leg(n1*4,true);
        intl tot = 0;
        forsn(i,1,n1){
            cin>>a[i];
            tot+=a[i];
        //    cout<<i<<" , "<<a[i]<<endl;
            PQ.push({a[i],i});
        }
        vector<int> res;
        forn(i,d){
            bool seguir = true;
            int v,p;
            do{
                auto q = PQ.top();
                PQ.pop();
                v = q.first, p = q.second;
          //      cout<<v<<" ::: "<<p<<endl;
                if(leg[p]&& a[p]==v) seguir = false;
            }while(seguir);
        //    cout<<"Llamo en "<<p<<" con "<<leg[p]<<endl;
            tot-=v;
            F(p,a,leg,n2);
            if(leg[p]==false)d++;
            else res.push_back(p);
        }
        cout<<tot<<"\n";
        forn(i,res.size())cout<<res[i]<<((i==int(res.size()-1))?"\n":" ");
       // cout<<tot<<" : "<<a<<endl<<leg<<endl;
	}
	return 0;
}


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

#define pb push_back
#define mp make_pair

#define forn(i,n) for(int i = 0;i<int(n);i++)
#define dforn(i,n) for(int i = int(n)-1;i>=0;i--)
#define forsn(i,s,n) for(int i = int(s);i<int(n);i++)
#define fore(i,s,n) for(int i = int(s);i<int(n);i++)
#define dforsn(i,s,n) for(int i = int(n)-1;i>=int(s);i--)

#define DBG(x) do{cout<<#x<<" = "<<x<<endl;}while(false)
#define get(mask,i) (mask&(1<<i))
#define set(mask,i) (mask|=(1<<i))
template<class T> ostream & operator<<(ostream & out, vector<T> & v){
	out<<"[";
	for(auto k : v) out<<k<<" ";
	out<<"]"<<"\n";
	return out;
}
typedef long long intl;
priority_queue<pair<int,int> > PQ;
void F(int i, vector<int> & h, vector<bool> & leg, int n2){
    if(leg[i]==false)return;
    int l = i*2;
    int r = i*2+1;
    if(h[l]==0 && h[r]==0){
        if(i<n2){
            leg[i]=false;
        }
        else h[i] = 0;
    }
    else if(h[l]>h[r]){
        int v = h[l];//h[i]=h[l];
        F(l,h,leg,n2);
        if(leg[l]){
            h[i] = v;
        }else leg[i]=false;
    }else{
        //h[i] = h[r];
        int v = h[r];
        F(r,h,leg,n2);
        if(leg[r]){
            h[i]=v;
        }else leg[i]= false;
    }
    //if(leg[i] && h[i]!=0)PQ.push({h[i],i});
}
void DFS(int n, vector<int> & h, vector<bool> & leg, int n2){
    if(n>=n2) return;
    if(leg[n]==false){
        DFS(n*2,h,leg,n2);
        DFS(n*2+1,h,leg,n2);
    }else PQ.push({h[n],n});
}
int main()
{
    cin.tie(0);
    cin.sync_with_stdio(0);
    /*vector<int> a = {0,7,6,5,4,3,2,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
    int n1 = 7, n2 = 3;
    vector<bool> leg(15,true);
    F(2,a,leg,n2);
    F(1,a,leg,n2);
    F(3,a,leg,n2);
    F(1,a,leg,n2);
    cout<<a<<" / "<<leg<<endl;*/
	int t; cin>>t;
	while(t--){
        PQ = priority_queue<pair<int,int> >();
        int h,g;
        cin>>h>>g;
        int n1 = 1<<h, n2 = 1<<g, d = n1-n2;
        vector<int> a(n1*4);
        vector<bool> leg(n1*4,true);
        intl tot = 0;
        forsn(i,1,n1){
            cin>>a[i];
            tot+=a[i];
        //    cout<<i<<" , "<<a[i]<<endl;
        //    PQ.push({a[i],i});
        }
        vector<intl> res;
        PQ.push({a[1],1});
        forn(i,d){
        //    cout<<a<<endl;
            bool seguir = true;
            int v,p;
            do{
                auto q = PQ.top();
                PQ.pop();
                v = q.first, p = q.second;
          //      cout<<v<<" ::: "<<p<<endl;
                if(leg[p] && a[p]==v) seguir = false;
            }while(seguir);
        //    cout<<"Llamo en "<<p<<" con "<<leg[p]<<endl;
            F(p,a,leg,n2);
            if(leg[p]==false){
                d++;
                DFS(p,a,leg,n2);
            }
            else {
                PQ.push({a[p],p});
                tot-=v;
                res.push_back(p);
            }
        }
        cout<<tot<<"\n";
        forn(i,res.size())cout<<res[i]<<((i==int(res.size()-1))?"\n":" ");
        //cout<<a<<endl;
       // cout<<tot<<" : "<<a<<endl<<leg<<endl;
	}
	return 0;
}


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

#define pb push_back
#define mp make_pair

#define forn(i,n) for(int i = 0;i<int(n);i++)
#define dforn(i,n) for(int i = int(n)-1;i>=0;i--)
#define forsn(i,s,n) for(int i = int(s);i<int(n);i++)
#define fore(i,s,n) for(int i = int(s);i<int(n);i++)
#define dforsn(i,s,n) for(int i = int(n)-1;i>=int(s);i--)

#define DBG(x) do{cout<<#x<<" = "<<x<<endl;}while(false)
#define get(mask,i) (mask&(1<<i))
#define set(mask,i) (mask|=(1<<i))
template<class T> ostream & operator<<(ostream & out, vector<T> & v){
	out<<"[";
	for(auto k : v) out<<k<<" ";
	out<<"]"<<"\n";
	return out;
}
typedef long long intl;
priority_queue<pair<int,int> > PQ;
void F(int i, vector<intl> & h, vector<bool> & leg, int n2){
    if(leg[i]==false)return;
    int l = i*2;
    int r = i*2+1;
    if(h[l]==0 && h[r]==0){
        if(i<n2){
            leg[i]=false;
        }
        else h[i] = 0;
    }
    else if(h[l]>h[r]){
        int v = h[l];//h[i]=h[l];
        F(l,h,leg,n2);
        if(leg[l]){
            h[i] = v;
        }else leg[i]=false;
    }else{
        //h[i] = h[r];
        int v = h[r];
        F(r,h,leg,n2);
        if(leg[r]){
            h[i]=v;
        }else leg[i]= false;
    }
    PQ.push({h[i],i});
}
int main()
{
    cin.tie(0);
    cin.sync_with_stdio(0);
    /*vector<int> a = {0,7,6,5,4,3,2,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
    int n1 = 7, n2 = 3;
    vector<bool> leg(15,true);
    F(2,a,leg,n2);
    F(1,a,leg,n2);
    F(3,a,leg,n2);
    F(1,a,leg,n2);
    cout<<a<<" / "<<leg<<endl;*/
	int t; cin>>t;
	while(t--){
        PQ = priority_queue<pair<int,int> >();
        intl h,g;
        cin>>h>>g;
        intl n1 = 1<<h, n2 = 1<<g, d = n1-n2;
        vector<intl> a(n1*4);
        vector<bool> leg(n1*4,true);
        intl tot = 0;
        forsn(i,1,n1){
            cin>>a[i];
            tot+=a[i];
        //    cout<<i<<" , "<<a[i]<<endl;
            PQ.push({a[i],i});
        }
        vector<intl> res;
        forn(i,d){
        //    cout<<a<<endl;
            bool seguir = true;
            int v,p;
            do{
                auto q = PQ.top();
                PQ.pop();
                v = q.first, p = q.second;
          //      cout<<v<<" ::: "<<p<<endl;
                if(leg[p]&& a[p]==v) seguir = false;
            }while(seguir);
        //    cout<<"Llamo en "<<p<<" con "<<leg[p]<<endl;
            F(p,a,leg,n2);
            if(leg[p]==false)d++;
            else {
                    tot-=v;
                res.push_back(p);
            }
        }
        cout<<tot<<"\n";
        forn(i,res.size())cout<<res[i]<<((i==int(res.size()-1))?"\n":" ");
        //cout<<a<<endl;
       // cout<<tot<<" : "<<a<<endl<<leg<<endl;
	}
	return 0;
}


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

#define pb push_back
#define mp make_pair

#define forn(i,n) for(int i = 0;i<int(n);i++)
#define dforn(i,n) for(int i = int(n)-1;i>=0;i--)
#define forsn(i,s,n) for(int i = int(s);i<int(n);i++)
#define fore(i,s,n) for(int i = int(s);i<int(n);i++)
#define dforsn(i,s,n) for(int i = int(n)-1;i>=int(s);i--)

#define DBG(x) do{cout<<#x<<" = "<<x<<endl;}while(false)
#define get(mask,i) (mask&(1<<i))
#define set(mask,i) (mask|=(1<<i))
template<class T> ostream & operator<<(ostream & out, vector<T> & v){
	out<<"[";
	for(auto k : v) out<<k<<" ";
	out<<"]"<<"\n";
	return out;
}
typedef long long intl;
priority_queue<pair<int,int> > PQ;
void F(int i, vector<int> & h, vector<bool> & leg, int n2){
    if(leg[i]==false)return;
    int l = i*2;
    int r = i*2+1;
    if(h[l]==0 && h[r]==0){
        if(i<n2){
            leg[i]=false;
        }
        else h[i] = 0;
    }
    else if(h[l]>h[r]){
        int v = h[l];//h[i]=h[l];
        F(l,h,leg,n2);
        if(leg[l]){
            h[i] = v;
        }else leg[i]=false;
    }else{
        //h[i] = h[r];
        int v = h[r];
        F(r,h,leg,n2);
        if(leg[r]){
            h[i]=v;
        }else leg[i]= false;
    }
    //if(leg[i] && h[i]!=0)PQ.push({h[i],i});
}
int main()
{
    cin.tie(0);
    cin.sync_with_stdio(0);
    /*vector<int> a = {0,7,6,5,4,3,2,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
    int n1 = 7, n2 = 3;
    vector<bool> leg(15,true);
    F(2,a,leg,n2);
    F(1,a,leg,n2);
    F(3,a,leg,n2);
    F(1,a,leg,n2);
    cout<<a<<" / "<<leg<<endl;*/
	int t; cin>>t;
	while(t--){
        PQ = priority_queue<pair<int,int> >();
        int h,g;
        cin>>h>>g;
        int n1 = 1<<h, n2 = 1<<g, d = n1-n2;
        vector<int> a(n1*4);
        vector<bool> leg(n1*4,true);
        intl tot = 0;
        forsn(i,1,n1){
            cin>>a[i];
            tot+=a[i];
        //    cout<<i<<" , "<<a[i]<<endl;
        //    PQ.push({a[i],i});
        }
        vector<intl> res;
        PQ.push({a[1],1});
        forn(i,d){
        //    cout<<a<<endl;
            bool seguir = true;
            int v,p;
            do{
                auto q = PQ.top();
                PQ.pop();
                v = q.first, p = q.second;
          //      cout<<v<<" ::: "<<p<<endl;
                if(leg[p] && a[p]==v) seguir = false;
            }while(seguir);
        //    cout<<"Llamo en "<<p<<" con "<<leg[p]<<endl;
            F(p,a,leg,n2);
            if(leg[p]==false){
                d++;
                PQ.push({h[p*2],p*2});
                PQ.push({h[p*2+1],p*2+1});
            }
            else {
                PQ.push({h[p],p});
                tot-=v;
                res.push_back(p);
            }
        }
        cout<<tot<<"\n";
        forn(i,res.size())cout<<res[i]<<((i==int(res.size()-1))?"\n":" ");
        //cout<<a<<endl;
       // cout<<tot<<" : "<<a<<endl<<leg<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

const int MAXN = 1<<21 + 50;

bool good[MAXN];
bool taken[MAXN];

int main(){
	ios::sync_with_stdio(false);
// 	freopen("in.txt", "r", stdin);
	int t;
	cin >> t;
//	t--;
	while(t--){
		int h, g;
		vector< pair<int, int> > vp;
		cin >> h >> g;
		for(int i=1; i<1<<h; i++){
			int num;
			cin >> num;
			vp.push_back({num, i});
			taken[i] = false;
		}
		for(int i=0; i<1<<h; i++) good[i] = false;
		sort(vp.begin(), vp.end());
		int limit = (1<<g) - 1;
		long long ans = 0;
		vector<int> op;
		for(int i=0; i<vp.size(); i++){
			int pos = vp[i].second;
			while(pos > limit || good[pos]) pos /= 2;
			if(pos == 0) continue;
//			cout << i << " " << vp[i].first << " " << vp[i].second << " ";
			if(good[2 * pos] == good[2 * pos + 1]){
				good[pos] = true;
				ans += vp[i].first;
				taken[vp[i].second] = true;
			}
//			cout << taken[1] << " " << taken[2] << endl;
		}
		cout << ans << endl;
		for(int i=1; i<1<<h; i++){
//			cout << i << " " << taken[i] << endl;
			if(!taken[i]) cout << i << " ";
		}
		cout << '\n';
	}
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

const int MAXN = (1<<21) + 50;

bool good[MAXN];
bool taken[MAXN];

int main(){
	ios::sync_with_stdio(false);
//	freopen("in.txt", "r", stdin);
	int t;
	cin >> t;
	while(t--){
		int h, g;
		vector< pair<int, int> > vp;
		cin >> h >> g;
		for(int i=1; i<1<<h; i++){
			int num;
			cin >> num;
			vp.push_back({num, i});
			taken[i] = false;
		}
		for(int i=0; i<1<<h; i++) good[i] = false;
		sort(vp.begin(), vp.end());
		int limit = (1<<g) - 1;
		long long ans = 0;
		vector<int> op;
		for(int i=0; i<vp.size(); i++){
			int pos = vp[i].second;
			while(pos > limit || good[pos]) pos /= 2;
			if(pos == 0) continue;
			if(good[2 * pos] == good[2 * pos + 1]){
				good[pos] = true;
				ans += vp[i].first;
				taken[vp[i].second] = true;
			}
		}
		cout << ans << endl;
		for(int i=(1<<h)-1; i>0; i--){
			if(!taken[i]) cout << i << " ";
		}
		cout << '\n';
	}
	return 0;
}


#include<iostream>
#include<algorithm>
#include<cmath>
#include<string>
using namespace std;
bool m[256],f;
char l[]={'A','H','I','M','O','T','U','W','V','X','Y'};
int main()
{
    string t;
    int i;
    for(i=0;i<11;i++)
    m[l[i]]=1;
    cin>>t;
    for(i=0;i<t.length()/2+1;i++)
        if(!m[t[i]] || t[i]!=t[t.length()-i-1])
        {f=1;break;}
    if(f)
        cout<<"NO";
    else
        cout<<"YES";
    return 0;
}


#include<iostream>
#include<algorithm>
#include<cmath>
#include<string>
using namespace std;
bool m[256],f;
char l[]={'A','H','I','M','O','T','W','X','Y'};
int main()
{
    string t;
    int i;
    for(i=0;i<10;i++)
    m[l[i]]=1;
    cin>>t;
    for(i=0;i<t.length()/2+1;i++)
        if(!m[t[i]] || t[i]!=t[t.length()-i-1])
        {f=1;break;}
    if(f)
        cout<<"NO";
    else
        cout<<"YES";
    return 0;
}


#include<iostream>
#include<algorithm>
#include<cmath>
#include<string>
using namespace std;
bool m[256],f;
char l[]={'A','H','I','M','O','T','W','X','Y'};
int main()
{
    string t;
    int i;
    for(i=0;i<9;i++)
    m[l[i]]=1;
    cin>>t;
    for(i=0;i<t.length()/2+1;i++)
        if(!m[t[i]] || t[i]!=t[t.length()-i-1])
        {f=1;break;}
    if(f)
        cout<<"NO";
    else
        cout<<"YES";
    return 0;
}


#include<iostream>
#include<algorithm>
#include<cmath>
#include<string>
using namespace std;
bool m[256],f;
char l[]={'A','H','I','M','O','T','W','V','X','Y'};
int main()
{
    string t;
    int i;
    for(i=0;i<10;i++)
    m[l[i]]=1;
    cin>>t;
    for(i=0;i<t.length()/2+1;i++)
        if(!m[t[i]] || t[i]!=t[t.length()-i-1])
        {f=1;break;}
    if(f)
        cout<<"NO";
    else
        cout<<"YES";
    return 0;
}