try:
    for _ in range(int(input())):
        a=int(input())
        b=list(map(int,input().split()))
        flag=True
        for j in range(1,a-1):
            if not flag:
                break
            start=0
            end=a-1
            flag1=True
            while start<end and start<j and flag and end>j:
                temp=b[end]-b[start]
                if b[j]<=temp:
                    flag=False
                    print(start+1,j+1,end+1)
                    break
                else:
                    if flag1:
                        start+=1
                    else:
                        end-=1
                    flag1=not flag1
        if flag:
            print(-1)

except Exception as e:
    print(e)


try:
    for _ in range(int(input())):
        a=int(input())
        b=list(map(int,input().split()))
        flag=True
        i=0
        k=a-1
        while flag and i+1<k:
            temp=b[k]-b[i]
            if temp>b[i]:
                for j in range(i+1,k):
                    if b[j]<temp:
                        print(i+1,j+1,k+1)
                        flag=False
                        break
            k-=1
        if flag:
            print(-1)

except Exception as e:
    print(e)


try:
    for _ in range(int(input())):
        a=int(input())
        b=list(map(int,input().split()))
        flag=True
        i=0
        k=a-1
        while flag and i+1<k:
            temp=b[k]-b[i]
            if temp>b[i]:
                print(i+1,k,k+1)
                flag=False
                break
            else:
                k-=1
        if flag:
            print(-1)

except Exception as e:
    print(e)


try:
    for _ in range(int(input())):
        a=int(input())
        b=list(map(int,input().split()))
        flag=True
        i=0
        k=a-1
        while flag and i+1<k:
            temp=b[k]-b[i]
            if temp>b[i]:
                for j in range(i+1,k):
                    if b[j]<temp:
                        print(i+1,j+1,k+1)
                        flag=False
                        break
            i+=1
        if flag:
            print(-1)

except Exception as e:
    print(e)


try:
    for _ in range(int(input())):
        a=int(input())
        b=list(map(int,input().split()))
        flag=True
        i=0
        k=a-1
        flag1=True
        while flag and i+1<k:
            temp=b[k]-b[i]
            if temp>b[i]:
                for j in range(i+1,k):
                    if b[j]<=temp:
                        print(i+1,j+1,k+1)
                        flag=False
                        break
            if flag1:
                i+=1
            else:
                k-=1
            flag1=not flag1
        if flag:
            print(-1)

except Exception as e:
    print(e)


#include <bits/stdc++.h>

using namespace std;

#ifdef NeverBeRed
#include "debug.h"
#else
#define debug(...) 9715
#endif
typedef long long ll;
typedef long double ld;
typedef complex<ll> point;
#define F first
#define S second

int main()
{
	#ifdef TurnRed
		freopen("a.in", "r", stdin);
		//freopen("a.out", "w", stdout);
	#endif

	ios_base::sync_with_stdio(0), cin.tie(0);

	int t;
	cin >> t;
	while (t--)
	{
		int h, g;
		cin >> h >> g;
		vector<int> a((1<<h)-1);
		for (auto &i : a) cin >> i;
		a.insert(a.begin(), 0);
		a.resize(1<<(h+1));

		auto f = [&](int x, bool doit)
		{
			while (true)
			{
				int l = 2*x;
				int r = l+1;
				if (a[l] == 0 && a[r] == 0)
				{
					if (doit) a[x] = 0;
					return __lg(x);
				}
				else
				{
					if (a[l] > a[r])
					{
						if (doit) a[x] = a[l];
						x = l;
					}
					else
					{
						if (doit) a[x] = a[r];
						x = r;
					}
				}
			}
		};

		vector<int> ans;
		priority_queue<pair<int, int>> pq;
		pq.push({ a[1], 1 });
		for (int it = (1<<h)-(1<<g); it--; )
		{
			int x = pq.top().S;
			pq.pop();

			if (f(x, false) < g)
			{
				pq.push({ a[2*x], 2*x });
				pq.push({ a[2*x+1], 2*x+1 });
				++it;
				continue;
			}

			f(x, true);
			ans.push_back(x);
			pq.push({ a[x], x });
		}

		cout << accumulate(a.begin(), a.end(), 0LL) << "\n";
		for (auto &i : ans) cout << i << " \n"[&i==&ans.back()];
	}

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#ifdef NeverBeRed
#include "debug.h"
#else
#define debug(...) 9715
#endif
typedef long long ll;
typedef long double ld;
typedef complex<ll> point;
#define F first
#define S second

int main()
{
	#ifdef TurnRed
		freopen("a.in", "r", stdin);
		//freopen("a.out", "w", stdout);
	#endif

	ios_base::sync_with_stdio(0), cin.tie(0);

	int t;
	cin >> t;
	while (t--)
	{
		int h, g;
		cin >> h >> g;
		vector<int> a((1<<h)-1);
		for (auto &i : a) cin >> i;
		a.insert(a.begin(), 0);
		a.resize(1<<(h+1));

		auto f = [&](int x, bool doit)
		{
			while (true)
			{
				int l = 2*x;
				int r = l+1;
				if (a[l] == 0 && a[r] == 0)
				{
					if (doit) a[x] = 0;
					return __lg(x);
				}
				else
				{
					if (a[l] > a[r])
					{
						if (doit) a[x] = a[l];
						x = l;
					}
					else
					{
						if (doit) a[x] = a[r];
						x = r;
					}
				}
			}
		};

		vector<int> ans;
		queue<int> q;
		q.push(1);
		for (int it = (1<<h)-(1<<g); it--; )
		{
			int x = q.front();
			q.pop();

			if (f(x, false) < g)
			{
				if (a[2*x]) q.push(2*x);
				if (a[2*x+1]) q.push(2*x+1);
				++it;
				continue;
			}

			f(x, true);
			ans.push_back(x);
			if (a[x]) q.push(x);
		}

		cout << accumulate(a.begin(), a.end(), 0LL) << "\n";
		for (auto &i : ans) cout << i << " \n"[&i==&ans.back()];
	}

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#ifdef NeverBeRed
#include "debug.h"
#else
#define debug(...) 9715
#endif
typedef long long ll;
typedef long double ld;
typedef complex<ll> point;
#define F first
#define S second

int main()
{
	#ifdef TurnRed
		freopen("a.in", "r", stdin);
		//freopen("a.out", "w", stdout);
	#endif

	ios_base::sync_with_stdio(0), cin.tie(0);

	int t;
	cin >> t;
	while (t--)
	{
		int h, g;
		cin >> h >> g;
		vector<int> a((1<<h)-1);
		for (auto &i : a) cin >> i;
		a.insert(a.begin(), 0);
		a.resize(1<<(h+1));

		auto f = [&](int x)
		{
			while (true)
			{
				int l = 2*x;
				int r = 2*x+1;
				if (a[l] == 0 && a[r] == 0)
				{
					a[x] = 0;
					break;
				}
				else
				{
					if (a[l] > a[r])
						a[x] = a[l], x = l;
					else
						a[x] = a[r], x = r;
				}
			}
		};

		auto f2 = [&](int x)
		{
			int lvl = __lg(x);
			while (true)
			{
				int l = 2*x;
				int r = 2*x+1;
				if (a[l] == 0 && a[r] == 0)
				{
					return lvl;
				}
				else
				{
					++lvl;
					if (a[l] > a[r])
						x = l;
					else
						x = r;
				}
			}
			return lvl;
		};

		vector<int> ans;
		priority_queue<pair<int, int>> pq;
		pq.push({ a[1], 1 });
		for (int it = (1<<h)-(1<<g); it--; )
		{
			int x = pq.top().S;
			pq.pop();

			if (f2(x) < g)
			{
				pq.push({ a[2*x], 2*x });
				pq.push({ a[2*x+1], 2*x+1 });
				++it;
				continue;
			}

			f(x);
			ans.push_back(x);
			pq.push({ a[x], x });
		}

		cout << accumulate(a.begin(), a.end(), 0LL) << "\n";
		for (auto &i : ans) cout << i << " \n"[&i==&ans.back()];
	}

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#ifdef NeverBeRed
#include "debug.h"
#else
#define debug(...) 9715
#endif
typedef long long ll;
typedef long double ld;
typedef complex<ll> point;
#define F first
#define S second

int main()
{
	#ifdef TurnRed
		freopen("a.in", "r", stdin);
		//freopen("a.out", "w", stdout);
	#endif

	ios_base::sync_with_stdio(0), cin.tie(0);

	int t;
	cin >> t;
	while (t--)
	{
		int h, g;
		cin >> h >> g;
		vector<int> a((1<<h)-1);
		for (auto &i : a) cin >> i;
		a.insert(a.begin(), 0);
		a.resize(1<<(h+1));

		auto f = [&](int x, bool doit)
		{
			while (true)
			{
				int l = 2*x;
				int r = l+1;
				if (a[l] == 0 && a[r] == 0)
				{
					if (doit) a[x] = 0;
					return __lg(x);
				}
				else
				{
					if (a[l] > a[r])
					{
						if (doit) a[x] = a[l];
						x = l;
					}
					else
					{
						if (doit) a[x] = a[r];
						x = r;
					}
				}
			}
		};

		vector<int> ans;
		queue<int> q;
		q.push(1);
		for (int it = (1<<h)-(1<<g); it--; )
		{
			int x = q.front();
			q.pop();

			if (a[x] == 0 || f(x, false) < g)
			{
				if (f(x, false) < g)
				{
					q.push(2*x);
					q.push(2*x+1);
				}
				++it;
				continue;
			}

			f(x, true);
			ans.push_back(x);
			q.push(x);
		}

		cout << accumulate(a.begin(), a.end(), 0LL) << "\n";
		for (auto &i : ans) cout << i << " \n"[&i==&ans.back()];
	}

	return 0;
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ii pair<int,int>
#define ll long long
#define pll pair<ll,ll>
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define iiordered_set tree<pll, null_type,less<pll>, rb_tree_tag,tree_order_statistics_node_update>
using namespace std;
auto clk=clock();
mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rng(ll lim) {
    uniform_int_distribution<ll> uid(0,lim-1);
    return uid(rang);
}
#define MOD 1000000007
// #define MOD 998244353
ll power(ll a,ll b, ll m=MOD)
{
    ll res=1;
    while(b>0)
    {
        if(b&1)
            res=(res*a)%MOD;
        a=(a*a)%MOD;
        b>>=1;
    }
    return res;
}
ll inverse(ll a,ll m=MOD)
{
    return power(a,m-2,m);
}
int ceil(int a,int b)
{
    return (a+b-1)/b;
}
#define INFl 1e18
#define vi vector<int>
#define vvi vector<vi>
#define vl vector<long long>
#define vvl vector<vl>
#define vll vector<pll>
#define vii vector<ii>
#define vvii vector<vii>
#define F first
#define S second
#define forl(i,n) for(int i=0;i<n;i++)
#define fore(i,n) for(int i=1;i<=n;i++)
#define CASE(t) cout<<"Case #"<<(t)<<": ";
#define INF 1020000000
#define gcd(a,b) __gcd(a,b)
#define all(x)  x.begin(),x.end()
#define mp make_pair
#define msb(x) 31-__builtin_clz(x)
#define pb push_back
#define print(x)    for(auto it=x.begin();it!=x.end();it++) cout<<*it<<' '; cout<<endl;
#define printii(x)  for(auto it=x.begin();it!=x.end();it++) cout<<it->F<<' '<<it->S<<endl;
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)

#define int ll
const int MAX=(1<<21);
int a[MAX],mx[MAX];
int h,g,n;
void rem(int x)
{
    if(!a[(x<<1)] && !a[(x<<1)|1])
    {
        a[x]=0;
        mx[x]=msb(x);
        return;
    }
    int p1=a[(x<<1)];
    int p2=a[(x<<1)|1];
    if(p1>p2)
    {
        a[x]=p1;
        rem((x<<1));
    }
    else
    {
        a[x]=p2;
        rem((x<<1)|1);
    }
    p1=a[(x<<1)];
    p2=a[(x<<1)|1];
    if(p1>p2)
        mx[x]=mx[(x<<1)];
    else
        mx[x]=mx[(x<<1)|1];
}
void solve()
{
    scanf("%lld %lld",&h,&g);
    int n=(1<<h)-1;
    memset(a,0,2*n*sizeof(n));
    ll tot=0;
    fore(i,n)
        scanf("%lld",&a[i]),mx[i]=h,tot+=a[i];
    vi ans;
    fore(i,n)
    {
        while(a[i] && mx[i]>g)
            tot-=a[i],rem(i),ans.pb(i);
    }
    printf("%lld\n", tot);
    for(int i=0;i<ans.size();i++)
    {
        printf("%lld ",ans[i] );
    }
    printf("\n");
}
signed main()
{
    // cout<<msb(1)<<endl;
    signed t=1;
    scanf("%d",&t);
    while(t--)
        solve();

#ifdef loc
        cout<<"~~~~~~~~~~~~~~~~~~~~~~~~"<<endl<<"Time elapsed: "<<(double)(clock()-clk)/CLOCKS_PER_SEC<<endl;
#endif
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ii pair<int,int>
#define ll long long
#define pll pair<ll,ll>
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define iiordered_set tree<pll, null_type,less<pll>, rb_tree_tag,tree_order_statistics_node_update>
using namespace std;
auto clk=clock();
mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rng(ll lim) {
    uniform_int_distribution<ll> uid(0,lim-1);
    return uid(rang);
}
#define MOD 1000000007
// #define MOD 998244353
ll power(ll a,ll b, ll m=MOD)
{
    ll res=1;
    while(b>0)
    {
        if(b&1)
            res=(res*a)%MOD;
        a=(a*a)%MOD;
        b>>=1;
    }
    return res;
}
ll inverse(ll a,ll m=MOD)
{
    return power(a,m-2,m);
}
int ceil(int a,int b)
{
    return (a+b-1)/b;
}
#define INFl 1e18
#define vi vector<int>
#define vvi vector<vi>
#define vl vector<long long>
#define vvl vector<vl>
#define vll vector<pll>
#define vii vector<ii>
#define vvii vector<vii>
#define F first
#define S second
#define forl(i,n) for(int i=0;i<n;i++)
#define fore(i,n) for(int i=1;i<=n;i++)
#define CASE(t) cout<<"Case #"<<(t)<<": ";
#define INF 1020000000
#define gcd(a,b) __gcd(a,b)
#define all(x)  x.begin(),x.end()
#define mp make_pair
#define msb(x) 31-__builtin_clz(x)
#define pb push_back
#define print(x)    for(auto it=x.begin();it!=x.end();it++) cout<<*it<<' '; cout<<endl;
#define printii(x)  for(auto it=x.begin();it!=x.end();it++) cout<<it->F<<' '<<it->S<<endl;
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)

const int MAX=(1<<21);
int a[MAX],mx[MAX];
int h,g,n;
void rem(int x)
{
    if(!a[(x<<1)] && !a[(x<<1)|1])
    {
        a[x]=0;
        mx[x]=msb(x);
        return;
    }
    int p1=a[(x<<1)];
    int p2=a[(x<<1)|1];
    if(p1>p2)
    {
        a[x]=p1;
        rem((x<<1));
    }
    else
    {
        a[x]=p2;
        rem((x<<1)|1);
    }
    p1=a[(x<<1)];
    p2=a[(x<<1)|1];
    if(p1>p2)
        mx[x]=mx[(x<<1)];
    else
        mx[x]=mx[(x<<1)|1];
}
void solve()
{
    scanf("%d %d",&h,&g);
    int n=(1<<h)-1;
    ll tot=0;
    fore(i,n)
        scanf("%d",&a[i]),mx[i]=h,tot+=a[i];
    vi ans;
    fore(i,n)
    {
        while(a[i] && mx[i]>g)
            tot-=a[i],rem(i),ans.pb(i);
    }
    printf("%llu\n", tot);
    for(int i=0;i<ans.size();i++)
    {
        printf("%d ",ans[i] );
    }
    printf("\n");
}
signed main()
{
    // cout<<msb(1)<<endl;
    signed t=1;
    scanf("%d",&t);
    while(t--)
        solve();

#ifdef loc
        cout<<"~~~~~~~~~~~~~~~~~~~~~~~~"<<endl<<"Time elapsed: "<<(double)(clock()-clk)/CLOCKS_PER_SEC<<endl;
#endif
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ii pair<int,int>
#define ll long long
#define pll pair<ll,ll>
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define iiordered_set tree<pll, null_type,less<pll>, rb_tree_tag,tree_order_statistics_node_update>
using namespace std;
auto clk=clock();
mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rng(ll lim) {
    uniform_int_distribution<ll> uid(0,lim-1);
    return uid(rang);
}
#define MOD 1000000007
// #define MOD 998244353
ll power(ll a,ll b, ll m=MOD)
{
    ll res=1;
    while(b>0)
    {
        if(b&1)
            res=(res*a)%MOD;
        a=(a*a)%MOD;
        b>>=1;
    }
    return res;
}
ll inverse(ll a,ll m=MOD)
{
    return power(a,m-2,m);
}
int ceil(int a,int b)
{
    return (a+b-1)/b;
}
#define INFl 1e18
#define vi vector<int>
#define vvi vector<vi>
#define vl vector<long long>
#define vvl vector<vl>
#define vll vector<pll>
#define vii vector<ii>
#define vvii vector<vii>
#define F first
#define S second
#define forl(i,n) for(int i=0;i<n;i++)
#define fore(i,n) for(int i=1;i<=n;i++)
#define CASE(t) cout<<"Case #"<<(t)<<": ";
#define INF 1020000000
#define gcd(a,b) __gcd(a,b)
#define all(x)  x.begin(),x.end()
#define mp make_pair
#define msb(x) 31-__builtin_clz(x)
#define pb push_back
#define print(x)    for(auto it=x.begin();it!=x.end();it++) cout<<*it<<' '; cout<<endl;
#define printii(x)  for(auto it=x.begin();it!=x.end();it++) cout<<it->F<<' '<<it->S<<endl;
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)

#define int ll
const int MAX=(1<<21);
int a[MAX],mx[MAX];
int h,g,n;
void rem(int x)
{
    if(!a[(x<<1)] && !a[(x<<1)|1])
    {
        a[x]=0;
        mx[x]=msb(x);
        return;
    }
    int p1=a[(x<<1)];
    int p2=a[(x<<1)|1];
    if(p1>p2)
    {
        a[x]=p1;
        rem((x<<1));
    }
    else
    {
        a[x]=p2;
        rem((x<<1)|1);
    }
    p1=a[(x<<1)];
    p2=a[(x<<1)|1];
    if(p1>p2)
        mx[x]=mx[(x<<1)];
    else
        mx[x]=mx[(x<<1)|1];
}
void solve()
{
    scanf("%lld %lld",&h,&g);
    int n=(1<<h)-1;
    memset(a,0,(2*n+2)*sizeof(int));
    ll tot=0;
    fore(i,n)
        scanf("%lld",&a[i]),mx[i]=h,tot+=a[i];
    vi ans;
    fore(i,n)
    {
        while(a[i] && mx[i]>g)
            tot-=a[i],rem(i),ans.pb(i);
    }
    printf("%lld\n", tot);
    for(int i=0;i<ans.size();i++)
    {
        printf("%lld ",ans[i] );
    }
    printf("\n");
}
signed main()
{
    // cout<<msb(1)<<endl;
    signed t=1;
    scanf("%d",&t);
    while(t--)
        solve();

#ifdef loc
        cout<<"~~~~~~~~~~~~~~~~~~~~~~~~"<<endl<<"Time elapsed: "<<(double)(clock()-clk)/CLOCKS_PER_SEC<<endl;
#endif
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ii pair<int,int>
#define ll long long
#define pll pair<ll,ll>
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define iiordered_set tree<pll, null_type,less<pll>, rb_tree_tag,tree_order_statistics_node_update>
using namespace std;
auto clk=clock();
mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rng(ll lim) {
    uniform_int_distribution<ll> uid(0,lim-1);
    return uid(rang);
}
#define MOD 1000000007
// #define MOD 998244353
ll power(ll a,ll b, ll m=MOD)
{
    ll res=1;
    while(b>0)
    {
        if(b&1)
            res=(res*a)%MOD;
        a=(a*a)%MOD;
        b>>=1;
    }
    return res;
}
ll inverse(ll a,ll m=MOD)
{
    return power(a,m-2,m);
}
int ceil(int a,int b)
{
    return (a+b-1)/b;
}
#define INFl 1e18
#define vi vector<int>
#define vvi vector<vi>
#define vl vector<long long>
#define vvl vector<vl>
#define vll vector<pll>
#define vii vector<ii>
#define vvii vector<vii>
#define F first
#define S second
#define forl(i,n) for(int i=0;i<n;i++)
#define fore(i,n) for(int i=1;i<=n;i++)
#define CASE(t) cout<<"Case #"<<(t)<<": ";
#define INF 1020000000
#define gcd(a,b) __gcd(a,b)
#define all(x)  x.begin(),x.end()
#define mp make_pair
#define msb(x) 31-__builtin_clz(x)
#define pb push_back
#define print(x)    for(auto it=x.begin();it!=x.end();it++) cout<<*it<<' '; cout<<endl;
#define printii(x)  for(auto it=x.begin();it!=x.end();it++) cout<<it->F<<' '<<it->S<<endl;
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)

#define int ll
const int MAX=(1<<21);
int a[MAX],mx[MAX];
int h,g,n;
void rem(int x)
{
    if(!a[(x<<1)] && !a[(x<<1)|1])
    {
        a[x]=0;
        mx[x]=msb(x);
        return;
    }
    int p1=a[(x<<1)];
    int p2=a[(x<<1)|1];
    if(p1>p2)
    {
        a[x]=p1;
        rem((x<<1));
    }
    else
    {
        a[x]=p2;
        rem((x<<1)|1);
    }
    p1=a[(x<<1)];
    p2=a[(x<<1)|1];
    if(p1>p2)
        mx[x]=mx[(x<<1)];
    else
        mx[x]=mx[(x<<1)|1];
}
void solve()
{
    scanf("%lld %lld",&h,&g);
    int n=(1<<h)-1;
    ll tot=0;
    fore(i,n)
        scanf("%lld",&a[i]),mx[i]=h,tot+=a[i];
    vi ans;
    fore(i,n)
    {
        while(a[i] && mx[i]>g)
            tot-=a[i],rem(i),ans.pb(i);
    }
    printf("%lld\n", tot);
    for(int i=0;i<ans.size();i++)
    {
        printf("%lld ",ans[i] );
    }
    printf("\n");
}
signed main()
{
    // cout<<msb(1)<<endl;
    signed t=1;
    scanf("%d",&t);
    while(t--)
        solve();

#ifdef loc
        cout<<"~~~~~~~~~~~~~~~~~~~~~~~~"<<endl<<"Time elapsed: "<<(double)(clock()-clk)/CLOCKS_PER_SEC<<endl;
#endif
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ii pair<int,int>
#define ll long long
#define pll pair<ll,ll>
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define iiordered_set tree<pll, null_type,less<pll>, rb_tree_tag,tree_order_statistics_node_update>
using namespace std;
auto clk=clock();
mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rng(ll lim) {
    uniform_int_distribution<ll> uid(0,lim-1);
    return uid(rang);
}
#define MOD 1000000007
// #define MOD 998244353
ll power(ll a,ll b, ll m=MOD)
{
    ll res=1;
    while(b>0)
    {
        if(b&1)
            res=(res*a)%MOD;
        a=(a*a)%MOD;
        b>>=1;
    }
    return res;
}
ll inverse(ll a,ll m=MOD)
{
    return power(a,m-2,m);
}
int ceil(int a,int b)
{
    return (a+b-1)/b;
}
#define INFl 1e18
#define vi vector<int>
#define vvi vector<vi>
#define vl vector<long long>
#define vvl vector<vl>
#define vll vector<pll>
#define vii vector<ii>
#define vvii vector<vii>
#define F first
#define S second
#define forl(i,n) for(int i=0;i<n;i++)
#define fore(i,n) for(int i=1;i<=n;i++)
#define CASE(t) cout<<"Case #"<<(t)<<": ";
#define INF 1020000000
#define gcd(a,b) __gcd(a,b)
#define all(x)  x.begin(),x.end()
#define mp make_pair
#define msb(x) 31-__builtin_clz(x)
#define pb push_back
#define print(x)    for(auto it=x.begin();it!=x.end();it++) cout<<*it<<' '; cout<<endl;
#define printii(x)  for(auto it=x.begin();it!=x.end();it++) cout<<it->F<<' '<<it->S<<endl;
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)

const int MAX=(1<<21);
int a[MAX],mx[MAX];
int h,g,n;
void rem(int x)
{
    if(!a[(x<<1)] && !a[(x<<1)|1])
    {
        a[x]=0;
        mx[x]=msb(x);
        return;
    }
    int p1=a[(x<<1)];
    int p2=a[(x<<1)|1];
    if(p1>p2)
    {
        a[x]=p1;
        rem((x<<1));
    }
    else
    {
        a[x]=p2;
        rem((x<<1)|1);
    }
    p1=a[(x<<1)];
    p2=a[(x<<1)|1];
    if(p1>p2)
        mx[x]=mx[(x<<1)];
    else
        mx[x]=mx[(x<<1)|1];
}
void solve()
{
    scanf("%d %d",&h,&g);
    int n=(1<<h)-1;
    ll tot=0;
    fore(i,n)
        scanf("%d",&a[i]),mx[i]=h,tot+=a[i];
    vi ans;
    fore(i,n)
    {
        while(mx[i]>g)
            tot-=a[i],rem(i),ans.pb(i);
    }
    printf("%llu\n", tot);
    for(int i=0;i<ans.size();i++)
    {
        printf("%d ",ans[i] );
    }
    printf("\n");
}
signed main()
{
    // cout<<msb(1)<<endl;
    signed t=1;
    scanf("%d",&t);
    while(t--)
        solve();

#ifdef loc
        cout<<"~~~~~~~~~~~~~~~~~~~~~~~~"<<endl<<"Time elapsed: "<<(double)(clock()-clk)/CLOCKS_PER_SEC<<endl;
#endif
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ii pair<int,int>
#define ll long long
#define pll pair<ll,ll>
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define iiordered_set tree<pll, null_type,less<pll>, rb_tree_tag,tree_order_statistics_node_update>
using namespace std;
auto clk=clock();
mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rng(ll lim) {
    uniform_int_distribution<ll> uid(0,lim-1);
    return uid(rang);
}
#define MOD 1000000007
// #define MOD 998244353
ll power(ll a,ll b, ll m=MOD)
{
    ll res=1;
    while(b>0)
    {
        if(b&1)
            res=(res*a)%MOD;
        a=(a*a)%MOD;
        b>>=1;
    }
    return res;
}
ll inverse(ll a,ll m=MOD)
{
    return power(a,m-2,m);
}
int ceil(int a,int b)
{
    return (a+b-1)/b;
}
#define INFl 1e18
#define vi vector<int>
#define vvi vector<vi>
#define vl vector<long long>
#define vvl vector<vl>
#define vll vector<pll>
#define vii vector<ii>
#define vvii vector<vii>
#define F first
#define S second
#define forl(i,n) for(int i=0;i<n;i++)
#define fore(i,n) for(int i=1;i<=n;i++)
#define CASE(t) cout<<"Case #"<<(t)<<": ";
#define INF 1020000000
#define gcd(a,b) __gcd(a,b)
#define all(x)  x.begin(),x.end()
#define mp make_pair
#define msb(x) 31-__builtin_clz(x)
#define pb push_back
#define print(x)    for(auto it=x.begin();it!=x.end();it++) cout<<*it<<' '; cout<<endl;
#define printii(x)  for(auto it=x.begin();it!=x.end();it++) cout<<it->F<<' '<<it->S<<endl;
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)

const int MAX=(1<<21);
int a[MAX],mx[MAX];
int h,g,n;
void rem(int x)
{
    if(!a[(x<<1)] && !a[(x<<1)|1])
    {
        a[x]=0;
        mx[x]=msb(x);
        return;
    }
    int p1=a[(x<<1)];
    int p2=a[(x<<1)|1];
    if(p1>p2)
    {
        a[x]=p1;
        rem((x<<1));
    }
    else
    {
        a[x]=p2;
        rem((x<<1)|1);
    }
    p1=a[(x<<1)];
    p2=a[(x<<1)|1];
    if(p1>p2)
        mx[x]=mx[(x<<1)];
    else
        mx[x]=mx[(x<<1)|1];
}
void solve()
{
    scanf("%d %d",&h,&g);
    int n=(1<<h)-1;
    ll tot=0;
    fore(i,n)
        scanf("%d",&a[i]),mx[i]=h,tot+=a[i];
    vi ans;
    fore(i,n)
    {
        while(a[i] && mx[i]>g)
            tot-=a[i],rem(i),ans.pb(i);
    }
    printf("%lld\n", tot);
    for(int i=0;i<ans.size();i++)
    {
        printf("%d ",ans[i] );
    }
    printf("\n");
}
signed main()
{
    // cout<<msb(1)<<endl;
    signed t=1;
    scanf("%d",&t);
    while(t--)
        solve();

#ifdef loc
        cout<<"~~~~~~~~~~~~~~~~~~~~~~~~"<<endl<<"Time elapsed: "<<(double)(clock()-clk)/CLOCKS_PER_SEC<<endl;
#endif
}


#include<bits/stdc++.h>
using namespace std;
long long arr[1100010];
pair<long long,long long> rec[1100010];
pair<long long,long long> tmp[1100010];
long long recit,tmpit;
long long visited[1100010];
long long ans[1100010];
long long pw2[30];
void dorec(long long x,long long h){
	if(x>pw2[h]-1) return;
	dorec(2*x,h);
	rec[recit].second=x;
	rec[recit].first=arr[x];
	recit++;
	dorec(2*x+1,h);
}
void docal(long long index,long long lev,long long st,long long ed,long long g){
	//printf("x%lld\n",index);
	if(lev==g){
		//printf("a%lld\n",index);
		sort(rec+st,rec+ed+1);
		ans[index]=rec[st].first;
		visited[rec[st].second]=1;
		return;
	}
	long long mid=(st+ed)/2,i,j,M;
	docal(index*2,lev+1,st,mid-1,g);
	docal(index*2+1,lev+1,mid+1,ed,g);
	tmpit=0;
	i=st;
	j=mid+1;
	//printf("b%lld\n",index);
	while(1){
		if(i==mid && j==ed+1) break;
		if(i<mid && j==ed+1){
			tmp[tmpit]=rec[i];
			i++;
			tmpit++;
		}
		else if(i==mid && j<ed+1){
			tmp[tmpit]=rec[j];
			j++;
			tmpit++;
		}
		else if(rec[i].first<rec[j].first){
			tmp[tmpit]=rec[i];
			i++;
			tmpit++;
		}
		else{
			tmp[tmpit]=rec[j];
			j++;
			tmpit++;
		}
		//printf("c%lld\n",index);
	}
	tmp[tmpit]=rec[mid];
	tmpit++;
	M=max(ans[index*2],ans[index*2+1]);
	for(i=0;i<tmpit;i++) rec[st+i]=tmp[i];
	for(i=0;i<tmpit;i++){
		if(tmp[i].first>M){
			ans[index]=tmp[i].first;
			visited[tmp[i].second]=1;
			break;
		}
	}
}
int main(){
	long long a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,tmp,cnt,ret;
	scanf("%lld",&t);
	pw2[0]=1;
	for(i=1;i<30;i++)pw2[i]=2*pw2[i-1];
	for(z=0;z<t;z++){
		scanf("%lld %lld",&h,&g);
		for(j=1;j<=pw2[h]-1;j++) scanf("%lld",&arr[j]);
		visited[j]=0;
		ans[j]=0;
		recit=1;
		dorec(1,h);
		//for(j=1;j<recit;j++) printf("%lld ",rec[j].first);
		docal(1,1,1,pw2[h]-1,g);
		ret=0;
		//for(i=1;i<=pw2[g]-1;i++) printf("%lld ",ans[i]);
		for(i=1;i<=pw2[g]-1;i++) ret+=ans[i];
		printf("%lld\n",ret);
		for(j=h;j>=1;j--){
			for(k=pw2[j-1];k<pw2[j];k++) if(visited[k]==0) printf("%lld ",k);
		}
		printf("\n");
	}
}


#include<bits/stdc++.h>
using namespace std;
long long arr[1100010];
pair<long long,long long> rec[1100010];
pair<long long,long long> tmp[1100010];
long long recit,tmpit;
long long visited[1100010];
long long ans[1100010];
long long pw2[30];
void dorec(long long x,long long h){
	if(x>pw2[h]-1) return;
	dorec(2*x,h);
	rec[recit].second=x;
	rec[recit].first=arr[x];
	recit++;
	dorec(2*x+1,h);
}
void docal(long long index,long long lev,long long st,long long ed,long long g){
	//printf("x%lld\n",index);
	if(lev==g){
		//printf("a%lld\n",index);
		sort(rec+st,rec+ed+1);
		ans[index]=rec[st].first;
		visited[rec[st].second]=1;
		return;
	}
	long long mid=(st+ed)/2,i,j,M;
	docal(index*2,lev+1,st,mid-1,g);
	docal(index*2+1,lev+1,mid+1,ed,g);
	tmpit=0;
	i=st;
	j=mid+1;
	//printf("b%lld\n",index);
	while(1){
		if(i==mid && j==ed+1) break;
		if(i<mid && j==ed+1){
			tmp[tmpit]=rec[i];
			i++;
			tmpit++;
		}
		else if(i==mid && j<ed+1){
			tmp[tmpit]=rec[j];
			j++;
			tmpit++;
		}
		else if(rec[i].first<rec[j].first){
			tmp[tmpit]=rec[i];
			i++;
			tmpit++;
		}
		else{
			tmp[tmpit]=rec[j];
			j++;
			tmpit++;
		}
		//printf("c%lld\n",index);
	}
	tmp[tmpit]=rec[mid];
	tmpit++;
	M=max(ans[index*2],ans[index*2+1]);
	for(i=0;i<tmpit;i++) rec[st+i]=tmp[i];
	for(i=0;i<tmpit;i++){
		if(tmp[i].first>M){
			ans[index]=tmp[i].first;
			visited[tmp[i].second]=1;
			break;
		}
	}
}
int main(){
	long long a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,tmp,cnt,ret;
	scanf("%lld",&t);
	pw2[0]=1;
	for(i=1;i<30;i++)pw2[i]=2*pw2[i-1];
	for(z=0;z<t;z++){
		scanf("%lld %lld",&h,&g);
		for(j=1;j<=pw2[h]-1;j++){
			scanf("%lld",&arr[j]);
			visited[j]=0;
			ans[j]=0;
		}
		recit=1;
		dorec(1,h);
		//for(j=1;j<recit;j++) printf("%lld ",rec[j].first);
		docal(1,1,1,pw2[h]-1,g);
		ret=0;
		//for(i=1;i<=pw2[g]-1;i++) printf("%lld ",ans[i]);
		for(i=1;i<=pw2[g]-1;i++) ret+=ans[i];
		printf("%lld\n",ret);
		for(j=h;j>=1;j--){
			for(k=pw2[j-1];k<pw2[j];k++) if(visited[k]==0) printf("%lld ",k);
		}
		printf("\n");
	}
}


#include<bits/stdc++.h>
using namespace std;
long long arr[1100010];
pair<long long,long long> rec[1100010];
pair<long long,long long> tmp[1100010];
long long recit,tmpit;
long long visited[1100010];
long long ans[1100010];
long long pw2[30];
void dorec(long long x,long long h){
	if(x>pw2[h]-1) return;
	dorec(2*x,h);
	rec[recit].second=x;
	rec[recit].first=arr[x];
	recit++;
	dorec(2*x+1,h);
}
void docal(long long index,long long lev,long long st,long long ed,long long g){
	//printf("x%lld\n",index);
	if(lev==g){
		//printf("a%lld\n",index);
		sort(rec+st,rec+ed+1);
		ans[index]=rec[st].first;
		visited[rec[st].second]=1;
		return;
	}
	long long mid=(st+ed)/2,i,j,M;
	docal(index*2,lev+1,st,mid-1,g);
	docal(index*2+1,lev+1,mid+1,ed,g);
	tmpit=1;
	tmp[0]=rec[mid];
	i=st;
	j=mid+1;
	//printf("b%lld\n",index);
	while(1){
		if(i==mid && j==ed+1) break;
		if(i<mid && j==ed+1){
			tmp[tmpit]=rec[i];
			i++;
			tmpit++;
		}
		else if(i==mid && j<ed+1){
			tmp[tmpit]=rec[j];
			j++;
			tmpit++;
		}
		else if(rec[i].first<rec[j].first){
			tmp[tmpit]=rec[i];
			i++;
			tmpit++;
		}
		else{
			tmp[tmpit]=rec[j];
			j++;
			tmpit++;
		}
		//printf("c%lld\n",index);
	}
	for(i=0;i<tmpit-1;i++){
		if(tmp[i]>tmp[i+1]) swap(tmp[i],tmp[i+1]);
		else break;
	}
	M=max(ans[index*2],ans[index*2+1]);
	for(i=0;i<tmpit;i++) rec[st+i]=tmp[i];
	for(i=0;i<tmpit;i++){
		if(tmp[i].first>M){
			ans[index]=tmp[i].first;
			visited[tmp[i].second]=1;
			break;
		}
	}
}
int main(){
	long long a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,tmp,cnt,ret;
	scanf("%lld",&t);
	pw2[0]=1;
	for(i=1;i<30;i++)pw2[i]=2*pw2[i-1];
	for(z=0;z<t;z++){
		scanf("%lld %lld",&h,&g);
		for(j=1;j<=pw2[h]-1;j++) scanf("%lld",&arr[j]);
		visited[j]=0;
		ans[j]=0;
		recit=1;
		dorec(1,h);
		//for(j=1;j<recit;j++) printf("%lld ",rec[j].first);
		docal(1,1,1,pw2[h]-1,g);
		ret=0;
		//for(i=1;i<=pw2[g]-1;i++) printf("%lld ",ans[i]);
		for(i=1;i<=pw2[g]-1;i++) ret+=ans[i];
		printf("%lld\n",ret);
		for(j=h;j>=1;j--){
			for(k=pw2[j-1];k<pw2[j];k++) if(visited[k]==0) printf("%lld ",k);
		}
		printf("\n");
	}
}


#include<bits/stdc++.h>
using namespace std;
long long arr[1100010];
pair<long long,long long> rec[1100010];
pair<long long,long long> tmp[1100010];
long long recit,tmpit;
long long visited[1100010];
long long ans[1100010];
long long pw2[30];
void dorec(long long x,long long h){
	if(x>pw2[h]-1) return;
	dorec(2*x,h);
	rec[recit].second=x;
	rec[recit].first=arr[x];
	recit++;
	dorec(2*x+1,h);
}
void docal(long long index,long long lev,long long st,long long ed,long long g){
	//printf("x%lld\n",index);
	if(lev==g){
		//printf("a%lld\n",index);
		sort(rec+st,rec+ed+1);
		ans[index]=rec[st].first;
		visited[rec[st].second]=1;
		return;
	}
	long long mid=(st+ed)/2,i,j,M;
	docal(index*2,lev+1,st,mid-1,g);
	docal(index*2+1,lev+1,mid+1,ed,g);
	tmpit=1;
	tmp[0]=rec[mid];
	i=st;
	j=mid+1;
	//printf("b%lld\n",index);
	while(1){
		if(i==mid && j==ed+1) break;
		if(i<mid && j==ed+1){
			tmp[tmpit]=rec[i];
			i++;
			tmpit++;
		}
		else if(i==mid && j<ed+1){
			tmp[tmpit]=rec[j];
			j++;
			tmpit++;
		}
		else if(rec[i].first<rec[j].first){
			tmp[tmpit]=rec[i];
			i++;
			tmpit++;
		}
		else{
			tmp[tmpit]=rec[j];
			j++;
			tmpit++;
		}
		//printf("c%lld\n",index);
	}
	for(i=0;i<tmpit-1;i++){
		if(tmp[i]>tmp[i+1]) swap(tmp[i],tmp[i+1]);
		else break;
	}
	M=max(ans[index*2],ans[index*2+1]);
	for(i=0;i<tmpit-1;i++) rec[st+i]=tmp[i];
	for(i=0;i<tmpit-1;i++){
		if(tmp[i].first>M){
			ans[index]=tmp[i].first;
			visited[tmp[i].second]=1;
			break;
		}
	}
}
int main(){
	long long a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,tmp,cnt,ret;
	scanf("%lld",&t);
	pw2[0]=1;
	for(i=1;i<30;i++)pw2[i]=2*pw2[i-1];
	for(z=0;z<t;z++){
		scanf("%lld %lld",&h,&g);
		for(j=1;j<=pw2[h]-1;j++) scanf("%lld",&arr[j]);
		visited[j]=0;
		ans[j]=0;
		recit=1;
		dorec(1,h);
		//for(j=1;j<recit;j++) printf("%lld ",rec[j].first);
		docal(1,1,1,pw2[h]-1,g);
		ret=0;
		for(i=1;i<=pw2[g]-1;i++) ret+=ans[i];
		printf("%lld\n",ret);
		for(j=h;j>=1;j--){
			for(k=pw2[j-1];k<pw2[j];k++) if(visited[k]==0) printf("%lld ",k);
		}
		printf("\n");
	}
}


#include<bits/stdc++.h>
using namespace std;
long long arr[1100010];
pair<long long,long long> rec[1100010];
pair<long long,long long> tmp[1100010];
long long recit,tmpit;
long long visited[1100010];
long long ans[1100010];
long long pw2[30];
void dorec(long long x,long long h){
	if(x>pw2[h]-1) return;
	dorec(2*x,h);
	rec[recit].second=x;
	rec[recit].first=arr[x];
	recit++;
	dorec(2*x+1,h);
}
void docal(long long index,long long lev,long long st,long long ed,long long g){
	//printf("x%lld\n",index);
	if(lev==g){
		//printf("a%lld\n",index);
		sort(rec+st,rec+ed+1);
		ans[index]=rec[st].first;
		visited[rec[st].second]=1;
		return;
	}
	long long mid=(st+ed)/2,i,j,M;
	docal(index*2,lev+1,st,mid-1,g);
	docal(index*2+1,lev+1,mid+1,ed,g);
	tmpit=0;
	i=st;
	j=mid+1;
	//printf("b%lld\n",index);
	while(1){
		if(i==mid && j==ed+1) break;
		if(i<mid && j==ed+1){
			tmp[tmpit]=rec[i];
			i++;
			tmpit++;
		}
		else if(i==mid && j<ed+1){
			tmp[tmpit]=rec[j];
			j++;
			tmpit++;
		}
		else if(rec[i].first<rec[j].first){
			tmp[tmpit]=rec[i];
			i++;
			tmpit++;
		}
		else{
			tmp[tmpit]=rec[j];
			j++;
			tmpit++;
		}
		//printf("c%lld\n",index);
	}
	tmp[tmpit]=rec[mid];
	tmpit++;
	M=max(ans[index*2],ans[index*2+1]);
	for(i=0;i<tmpit;i++) rec[st+i]=tmp[i];
	for(i=0;i<tmpit;i++){
		if(tmp[i].first>M){
			ans[index]=tmp[i].first;
			visited[tmp[i].second]=1;
			break;
		}
	}
}
int main(){
	long long a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,tmp,cnt,ret;
	scanf("%lld",&t);
	pw2[0]=1;
	for(i=1;i<30;i++)pw2[i]=2*pw2[i-1];
	for(z=0;z<t;z++){
		scanf("%lld %lld",&h,&g);
		for(j=1;j<=pw2[h]-1;j++){
			scanf("%lld",&arr[j]);
			visited[j]=0;
			ans[j]=0;
		}
		recit=1;
		dorec(1,h);
		for(j=1;j<recit;j++) printf("%lld ",rec[j].first);
		docal(1,1,1,pw2[h]-1,g);
		ret=0;
		//for(i=1;i<=pw2[g]-1;i++) printf("%lld ",ans[i]);
		for(i=1;i<=pw2[g]-1;i++) ret+=ans[i];
		printf("%lld\n",ret);
		for(j=h;j>=1;j--){
			for(k=pw2[j-1];k<pw2[j];k++) if(visited[k]==0) printf("%lld ",k);
		}
		printf("\n");
	}
}


#include <bits/stdc++.h>

#define F first
#define S second
#define EB emplace_back
#define INF 0x3f3f3f3f
#define INFLL 0x3f3f3f3f3f3f3f3fLL
#define all(x) x.begin(),x.end()
#define endl '\n'
#define W(x) cerr << "\033[31m"<<  #x << " = " << x << "\033[0m" << endl;

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

std::mt19937 rng((int) std::chrono::steady_clock::now().time_since_epoch().count());

const int MOD = 1000000007;
const int MAXN = (1<<20) + 10;

int v[MAXN];
void f(int i){
	int l = i<<1;
	int r = l+1;
	if(v[l] == 0 and v[r] == 0){
		v[i] = 0;
	}else if(v[l] > v[r]){
		v[i] = v[l];
		f(l);
	}else{
		v[i] = v[r];
		f(r);
	}
}
int main() {
	ios_base::sync_with_stdio(false); cin.tie(NULL);
	int tests;
	cin >> tests;
	while(tests--){
		int g, h;
		cin >> h >> g;
		int n = (1<<h)-1;
		for(int i=1; i<=n; i++)
			cin >> v[i];
		vector<int> vans;
		for(int i=0; i<((1<<h)-(1<<g)); i+=2){
			int x = (n-i)/2;
			while(x > 1 and v[x] > v[x^1])
				x/=2;
			vans.push_back(x);
			f(x);
			x = (n-i)/2;
			while(x > 1 and v[x] > v[x^1])
				x/=2;
			vans.push_back(x);
			f(x);
		}
		ll ans = 0;
		for(int i=1; i<(1<<g); i++)
			ans += v[i];
		cout << ans << endl;
		for(int x: vans)
			cout << x << " ";
		cout << endl;
	}
	return 0;
}


#include <bits/stdc++.h>

#define F first
#define S second
#define EB emplace_back
#define INF 0x3f3f3f3f
#define INFLL 0x3f3f3f3f3f3f3f3fLL
#define all(x) x.begin(),x.end()
#define endl '\n'
#define W(x) cerr << "\033[31m"<<  #x << " = " << x << "\033[0m" << endl;

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

std::mt19937 rng((int) std::chrono::steady_clock::now().time_since_epoch().count());

const int MOD = 1000000007;
const int MAXN = (1<<21) + 10;

int v[MAXN];
void f(int i){
	int l = i<<1;
	int r = l+1;
	if(v[l] == 0 and v[r] == 0){
		v[i] = 0;
	}else if(v[l] > v[r]){
		v[i] = v[l];
		f(l);
	}else{
		v[i] = v[r];
		f(r);
	}
}

int h1(int i){
	int l = i<<1;
	int r = l+1;
	if(v[l] == 0 and v[r] == 0){
		return 0;
	}else if(v[l] > v[r]){
		return h1(l) + 1;
	}else{
		return h1(r) + 1;
	}
}
vector<int> vans;
int main() {
	ios_base::sync_with_stdio(false); cin.tie(NULL);
	int tests;
	cin >> tests;
	while(tests--){
		vans.clear();
		int g, h;
		cin >> h >> g;
		int n = (1<<h)-1;

		for(int i=1; i<=n; i++){
			cin >> v[i];
			v[2*i] = v[2*i+1] = 0;
		}
		for(int i=1; i<(1<<g); i++){
			int x=i;
			int x2=0;
			while(x > 1){
				x2++;
				x/=2;
			}
			while(h1(i)+x2 >= g){
				f(i);
				vans.push_back(i);
			}
		}

		ll ans = 0;
		for(int i=1; i<(1<<g); i++)
			ans += v[i];
		cout << ans << endl;
		for(int x: vans)
			cout << x << " ";
		cout << endl;
	}
	return 0;
}
/*
1
3 2
7 5 6 4 2 1 3

*/


#include <bits/stdc++.h>

#define F first
#define S second
#define EB emplace_back
#define INF 0x3f3f3f3f
#define INFLL 0x3f3f3f3f3f3f3f3fLL
#define all(x) x.begin(),x.end()
#define endl '\n'
#define W(x) cerr << "\033[31m"<<  #x << " = " << x << "\033[0m" << endl;

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

std::mt19937 rng((int) std::chrono::steady_clock::now().time_since_epoch().count());

const int MOD = 1000000007;
const int MAXN = (1<<21) + 10;

int v[MAXN];
void f(int i){
	int l = i<<1;
	int r = l+1;
	if(v[l] == 0 and v[r] == 0){
		v[i] = 0;
	}else if(v[l] > v[r]){
		v[i] = v[l];
		f(l);
	}else{
		v[i] = v[r];
		f(r);
	}
}

int h1(int i){
	int l = i<<1;
	int r = l+1;
	if(v[l] == 0 and v[r] == 0){
		return 0;
	}else if(v[l] > v[r]){
		return h1(l) + 1;
	}else{
		return h1(r) + 1;
	}
}
vector<int> vans;
int main() {
	ios_base::sync_with_stdio(false); cin.tie(NULL);
	int tests;
	cin >> tests;
	while(tests--){
		vans.clear();
		int g, h;
		cin >> h >> g;
		int n = (1<<h)-1;
		for(int i=1; i<=n; i++)
			cin >> v[i];
		for(int i=1; i<(1<<g); i++){
			int x=i;
			int x2=0;
			while(x > 1){
				x2++;
				x/=2;
			}
			while(h1(i)+x2 >= g){
				f(i);
				vans.push_back(i);
			}
		}

		ll ans = 0;
		for(int i=1; i<(1<<g); i++)
			ans += v[i];
		cout << ans << endl;
		for(int x: vans)
			cout << x << " ";
		cout << endl;
	}
	return 0;
}
/*
1
3 2
7 5 6 4 2 1 3

*/


#include <bits/stdc++.h>

#define F first
#define S second
#define EB emplace_back
#define INF 0x3f3f3f3f
#define INFLL 0x3f3f3f3f3f3f3f3fLL
#define all(x) x.begin(),x.end()
#define endl '\n'
#define W(x) cerr << "\033[31m"<<  #x << " = " << x << "\033[0m" << endl;

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

std::mt19937 rng((int) std::chrono::steady_clock::now().time_since_epoch().count());

const int MOD = 1000000007;
const int MAXN = (1<<20) + 10;

int v[MAXN];
void f(int i){
	int l = i<<1;
	int r = l+1;
	if(v[l] == 0 and v[r] == 0){
		v[i] = 0;
	}else if(v[l] > v[r]){
		v[i] = v[l];
		f(l);
	}else{
		v[i] = v[r];
		f(r);
	}
}

int h1(int i){
	int l = i<<1;
	int r = l+1;
	if(v[l] == 0 and v[r] == 0){
		return 0;
	}else if(v[l] > v[r]){
		return h1(l) + 1;
	}else{
		return h1(r) + 1;
	}
}

int main() {
	ios_base::sync_with_stdio(false); cin.tie(NULL);
	int tests;
	cin >> tests;
	while(tests--){
		int g, h;
		cin >> h >> g;
		int n = (1<<h)-1;
		for(int i=1; i<=n; i++)
			cin >> v[i];
		vector<int> vans;
		for(int i=1; i<(1<<g); i++){
			int x=i;
			int x2=0;
			while(x > 1){
				x2++;
				x/=2;
			}
			while(h1(i)+x2 >= g){
				f(i);
				vans.push_back(i);
			}
		}

		ll ans = 0;
		for(int i=1; i<(1<<g); i++)
			ans += v[i];
		cout << ans << endl;
		for(int x: vans)
			cout << x << " ";
		cout << endl;
	}
	return 0;
}
/*
1
3 2
7 5 6 4 2 1 3

*/


#include <bits/stdc++.h>

#define rep(i,a,n) for (int i=a;i<=n;i++)
#define per(i,a,n) for (int i=n;i>=a;i--)
#define pii pair<int,int>
#define vi vector<int>
#define vii vector<vi >
#define vpii vector<pii >
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define sz(x) ((int)(x).size())
#define mod(x) ((x)%MOD)
#define o(X) (1<<(X))
#define oL(X) (1LL<<(X))
#define LL long long
#define ppt(x) __builtin_popcount(x)
using namespace std;
const int INF=0x3f3f3f3f,N=4e6+5,MOD=1e9+7;
const LL INF_LL=0x3f3f3f3f3f3f3f3fLL;

template<typename T>
inline int getplc(T x,int y) { return (x>>y)&1; }
template<typename T>
T square(T x) {return x*x;}

LL qpow(LL a,LL b=MOD-2,LL _MOD=MOD){
	LL res=1;
	for(;b;b>>=1,a=a*a%_MOD){
		if(b&1)res=res*a%_MOD;
	}
	return res;
}

template <typename T>
T Smax(T x) { return x; }
template<typename T, typename... Args>
T Smax(T a, Args... args) {
	return max(a, Smax(args...));
}
template <typename T>
T Smin(T x) { return x; }
template<typename T, typename... Args>
T Smin(T a, Args... args) {
	return min(a, Smin(args...));
}

template <typename T>
#define errorl(args...) {  \
	string _s = #args;   \
	replace(_s.begin(), _s.end(), ',', ' ');  \
    stringstream _ss(_s);  \
    istream_iterator<string> _it(_ss);  \
    errl(_it, args);  \
}
void errl(istream_iterator<string> it) {}
template<typename T, typename... Args>
void errl(istream_iterator<string> it, T a, Args... args) {
	cerr << *it << " = " << a << endl;
	errl(++it, args...);
}
#define error(args...) {  \
  string _s = #args;  \
  replace(_s.begin(), _s.end(), ',', ' ');  \
  stringstream _ss(_s);  \
  istream_iterator<string> _it(_ss);  \
  err(_it, args);  \
  cerr<<endl; \
}
void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
	cerr << *it << "=" << a << " # ";
	err(++it, args...);
}

int a[N],dep[N];
vi ans;

void del(int x){
  int l=x<<1,r=x<<1|1;
  if(a[l]==0&&a[r]==0){
    a[x]=0;
  }
  else{
    if(a[l]>a[r]){
      a[x]=a[l];
      del(l);
    }
    else{
      a[x]=a[r];
      del(r);
    }
  }
}

int getdep(int x){
  int l=x<<1,r=x<<1|1;
  if(a[l]==0&&a[r]==0){
    return 0;
  }
  else{
    if(a[l]>a[r]){
      return 1+getdep(l);
    }
    else{
      return 1+getdep(r);
    }
  }
}

void Solve(){
  int cas;
  for(cin>>cas;cas--;){
    int n,m;
    cin>>n>>m;
    LL sum=0;
    for(int i=1;i<=o(n)-1;i++){
      cin>>a[i];
      sum+=a[i];
    }

    dep[0]=0;
    for(int i=1;i<=o(n)-1;i++){
      dep[i]=dep[i/2]+1;
      if(a[i]){
        while(dep[i]+getdep(i)>m){
          sum-=a[i];
          ans.pb(i);
          del(i);
        }
      }
    }

    cout<<sum<<'\n';
    for(auto p:ans)cout<<p<<" ";cout<<'\n';

    for(int i=1;i<=o(n)-1;i++)a[i]=0;
    ans.clear();
  }
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.txt","r",stdin);
//  freopen("o1.txt","w",stdout);
#endif
	ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);
	Solve();
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define F(i,a,b) for(int i=a;i<=(b);++i)
#define F2(i,a,b) for(int i=a;i<(b);++i)
#define dF(i,a,b) for(int i=a;i>=(b);--i)
#define debug(...) fprintf(stderr,__VA_ARGS__)
#define Debug debug("Passing [%s] in LINE %d\n",__FUNCTION__,__LINE__)
#define MN 4000005
#define ll long long
#define mod 998244353
#define inf 0x3f3f3f3f
#define infll 0x3f3f3f3f3f3f3f3f
typedef pair<int,int> pii;
#define pb push_back
#define mkp make_pair
#define fi first
#define se second
inline int qpow(int b,ll e,int m=mod){int a=1;for(;e;e>>=1,b=(ll)b*b%m)if(e&1)a=(ll)a*b%m;return a;}
int n,m;ll s;
int a[MN],b[MN];
int stk[MN],tp;
int ch(int i){
	if(!a[i<<1]&&!a[i<<1|1]){
		if(i<1<<m)return b[i]=0,0;
		return a[i]=0,1;
	}
	if(a[i<<1]>a[i<<1|1]){
		int v=a[i<<1];
		if(!ch(i<<1))return b[i]=0,0;
		return a[i]=v,1;
	}else{
		int v=a[i<<1|1];
		if(!ch(i<<1|1))return b[i]=0,0;
		return a[i]=v,1;
	}
}
void dfs(int i){
	if(b[i]){while(b[i])stk[++tp]=i,s-=a[i],ch(i);--tp,s+=a[i];}
	if(a[i<<1])dfs(i<<1);
	if(a[i<<1|1])dfs(i<<1|1);
}
int main(){int tests=1;scanf("%d",&tests);
while(tests--){
	scanf("%d%d",&n,&m),s=tp=0;
	F2(i,1,2<<n)a[i]=0;
	F2(i,1,1<<n)scanf("%d",a+i),b[i]=1,s+=a[i];
	dfs(1);
	printf("%lld\n",s);
	F(i,1,tp)printf("%d ",stk[i]);puts("");
}	return 0;
}


#include <bits/stdc++.h>

#define rep(i,a,n) for (int i=a;i<=n;i++)
#define per(i,a,n) for (int i=n;i>=a;i--)
#define pii pair<int,int>
#define vi vector<int>
#define vii vector<vi >
#define vpii vector<pii >
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define sz(x) ((int)(x).size())
#define mod(x) ((x)%MOD)
#define o(X) (1<<(X))
#define oL(X) (1LL<<(X))
#define LL long long
#define ppt(x) __builtin_popcount(x)
using namespace std;
const int INF=0x3f3f3f3f,N=2e6+5,MOD=1e9+7;
const LL INF_LL=0x3f3f3f3f3f3f3f3fLL;

template<typename T>
inline int getplc(T x,int y) { return (x>>y)&1; }
template<typename T>
T square(T x) {return x*x;}

LL qpow(LL a,LL b=MOD-2,LL _MOD=MOD){
	LL res=1;
	for(;b;b>>=1,a=a*a%_MOD){
		if(b&1)res=res*a%_MOD;
	}
	return res;
}

template <typename T>
T Smax(T x) { return x; }
template<typename T, typename... Args>
T Smax(T a, Args... args) {
	return max(a, Smax(args...));
}
template <typename T>
T Smin(T x) { return x; }
template<typename T, typename... Args>
T Smin(T a, Args... args) {
	return min(a, Smin(args...));
}

template <typename T>
#define errorl(args...) {  \
	string _s = #args;   \
	replace(_s.begin(), _s.end(), ',', ' ');  \
    stringstream _ss(_s);  \
    istream_iterator<string> _it(_ss);  \
    errl(_it, args);  \
}
void errl(istream_iterator<string> it) {}
template<typename T, typename... Args>
void errl(istream_iterator<string> it, T a, Args... args) {
	cerr << *it << " = " << a << endl;
	errl(++it, args...);
}
#define error(args...) {  \
  string _s = #args;  \
  replace(_s.begin(), _s.end(), ',', ' ');  \
  stringstream _ss(_s);  \
  istream_iterator<string> _it(_ss);  \
  err(_it, args);  \
  cerr<<endl; \
}
void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
	cerr << *it << "=" << a << " # ";
	err(++it, args...);
}

int a[N],dep[N];
vi ans;

void del(int x){
  int l=x<<1,r=x<<1|1;
  if(a[l]==0&&a[r]==0){
    a[x]=0;
  }
  else{
    if(a[l]>a[r]){
      a[x]=a[l];
      del(l);
    }
    else{
      a[x]=a[r];
      del(r);
    }
  }
}

int getdep(int x){
  int l=x<<1,r=x<<1|1;
  if(a[l]==0&&a[r]==0){
    return 0;
  }
  else{
    if(a[l]>a[r]){
      return 1+getdep(l);
    }
    else{
      return 1+getdep(r);
    }
  }
}

void Solve(){
  int cas;
  for(cin>>cas;cas--;){
    int n,m;
    cin>>n>>m;
    for(int i=1;i<=o(n)-1;i++){
      cin>>a[i];
    }

    dep[0]=0;
    for(int i=1;i<=o(n)-1;i++){
      dep[i]=dep[i/2]+1;
      if(a[i]){
        while(dep[i]+getdep(i)>m){
          ans.pb(i);
          del(i);
        }
      }
    }

    LL sum=0;
    for(int i=1;i<=o(m)-1;i++)sum+=a[i];
    cout<<sum<<'\n';
    for(auto p:ans)cout<<p<<" ";cout<<'\n';

    ans.clear();
  }
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.txt","r",stdin);
//  freopen("o1.txt","w",stdout);
#endif
	ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);
	Solve();
	return 0;
}


#include <bits/stdc++.h>

#define rep(i,a,n) for (int i=a;i<=n;i++)
#define per(i,a,n) for (int i=n;i>=a;i--)
#define pii pair<int,int>
#define vi vector<int>
#define vii vector<vi >
#define vpii vector<pii >
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define sz(x) ((int)(x).size())
#define mod(x) ((x)%MOD)
#define o(X) (1<<(X))
#define oL(X) (1LL<<(X))
#define LL long long
#define ppt(x) __builtin_popcount(x)
using namespace std;
const int INF=0x3f3f3f3f,N=4e6+5,MOD=1e9+7;
const LL INF_LL=0x3f3f3f3f3f3f3f3fLL;

template<typename T>
inline int getplc(T x,int y) { return (x>>y)&1; }
template<typename T>
T square(T x) {return x*x;}

LL qpow(LL a,LL b=MOD-2,LL _MOD=MOD){
	LL res=1;
	for(;b;b>>=1,a=a*a%_MOD){
		if(b&1)res=res*a%_MOD;
	}
	return res;
}

template <typename T>
T Smax(T x) { return x; }
template<typename T, typename... Args>
T Smax(T a, Args... args) {
	return max(a, Smax(args...));
}
template <typename T>
T Smin(T x) { return x; }
template<typename T, typename... Args>
T Smin(T a, Args... args) {
	return min(a, Smin(args...));
}

template <typename T>
#define errorl(args...) {  \
	string _s = #args;   \
	replace(_s.begin(), _s.end(), ',', ' ');  \
    stringstream _ss(_s);  \
    istream_iterator<string> _it(_ss);  \
    errl(_it, args);  \
}
void errl(istream_iterator<string> it) {}
template<typename T, typename... Args>
void errl(istream_iterator<string> it, T a, Args... args) {
	cerr << *it << " = " << a << endl;
	errl(++it, args...);
}
#define error(args...) {  \
  string _s = #args;  \
  replace(_s.begin(), _s.end(), ',', ' ');  \
  stringstream _ss(_s);  \
  istream_iterator<string> _it(_ss);  \
  err(_it, args);  \
  cerr<<endl; \
}
void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
	cerr << *it << "=" << a << " # ";
	err(++it, args...);
}

int a[N],dep[N];
vi ans;

void del(int x){
  int l=x<<1,r=x<<1|1;
  if(a[l]==0&&a[r]==0){
    a[x]=0;
  }
  else{
    if(a[l]>a[r]){
      a[x]=a[l];
      del(l);
    }
    else{
      a[x]=a[r];
      del(r);
    }
  }
}

int getdep(int x){
  int l=x<<1,r=x<<1|1;
  if(a[l]==0&&a[r]==0){
    return 0;
  }
  else{
    if(a[l]>a[r]){
      return 1+getdep(l);
    }
    else{
      return 1+getdep(r);
    }
  }
}

void Solve(){
  int cas;
  for(cin>>cas;cas--;){
    int n,m;
    cin>>n>>m;
    LL sum=0;
    for(int i=1;i<=o(n)-1;i++){
      cin>>a[i];
      sum+=a[i];
    }

    dep[0]=0;
    for(int i=1;i<=o(n)-1;i++){
      dep[i]=dep[i/2]+1;
      if(a[i]){
        while(dep[i]+getdep(i)>m){
          sum-=a[i];
          ans.pb(i);
          del(i);
        }
      }
    }

    /* for(int i=1;i<=o(m)-1;i++)sum+=a[i]; */
    cout<<sum<<'\n';
    for(auto p:ans)cout<<p<<" ";cout<<'\n';

    ans.clear();
  }
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.txt","r",stdin);
//  freopen("o1.txt","w",stdout);
#endif
	ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);
	Solve();
	return 0;
}


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

int inv[(1 << 20)];
bool flag[(1 << 20)];
vector<int> last;

int main(){
    ios::sync_with_stdio(0);
    cin.tie(NULL);
    cout.tie(NULL);
    int t;
    cin >> t;

    fill(flag, flag + (1 << 20), 0);
    fill(inv, inv + (1 << 20), -1);

    while(t--){
        int h, g;
        long long ans = 0;
        cin >> h >> g;

        pair<int, set<int> > hp [(1 << h)];
        int ma[(1 << h)];
        fill(ma, ma + (1 << h), 0);

        for(int x: last){
            inv[x] = -1;
            flag[x] = 0;
        }

        last.resize(0);

        for(int i = 1; i<(1 << h); i++){
            cin >> hp[i].first;
            inv[hp[i].first] = i;
            last.push_back(hp[i].first);

            if(i >= (1 << (h - 1))){
                hp[i].second.insert(hp[i].first);
            }
        }

        for(int i = (1 << (h - 1)) - 1; i>=1; i--){
            hp[i].second = hp[2*i].second;
            for(int x: hp[2*i + 1].second){
                hp[i].second.insert(x);
            }
            hp[i].second.insert(hp[i].first);
            ma[i] = max(ma[2*i], ma[2*i + 1]);

            if(i < (1 << g)){
                int a = *hp[i].second.upper_bound(ma[i]);
                ans += a;
                flag[a] = 1;
                hp[i].second.erase(a);
                ma[i] = a;
            }
        }

        cout << ans << endl;

        for(int i = (1 << 20) - 1; i>=1; i--){
            if(inv[i] > 0 && flag[i] == 0){
                int pos = inv[i];
                cout << pos << " ";

                while(pos < (1 << (h - 1))){
                    if(hp[2*pos].first == 0 && hp[2*pos + 1].first == 0){
                        hp[pos].first = 0;
                        break;
                    }

                    else if(hp[2*pos].first > hp[2*pos + 1].first){
                        int j = hp[2*pos].first;
                        swap(inv[i], inv[j]);
                        swap(hp[pos].first, hp[2*pos].first);
                        pos = 2*pos;
                    }

                    else if(hp[2*pos].first < hp[2*pos + 1].first){
                        int j = hp[2*pos + 1].first;
                        swap(inv[i], inv[j]);
                        swap(hp[pos].first, hp[2*pos + 1].first);
                        pos = 2*pos + 1;
                    }

                    if(pos >= (1 << (h - 1))) hp[pos].first = 0;
                }

                inv[i] = -1;
            }
        }

        cout << endl;

    }

}


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

int main(){
    ios::sync_with_stdio(0);
    int t;
    cin >> t;

    while(t--){
        int h, g;
        long long ans = 0;
        cin >> h >> g;

        pair<int, set<int> > hp [(1 << h)];
        int ma[(1 << h)];
        fill(ma, ma + (1 << h), 0);

        int inv[(1 << 20)];
        bool flag[(1 << 20)];
        fill(flag, flag + (1 << 20), 0);
        fill(inv, inv + (1 << 20), -1);

        for(int i = 1; i<(1 << h); i++){
            cin >> hp[i].first;
            inv[hp[i].first] = i;

            if(i >= (1 << (h - 1))){
                hp[i].second.insert(hp[i].first);
            }
        }

        for(int i = (1 << (h - 1)) - 1; i>=1; i--){
            hp[i].second = hp[2*i].second;
            for(int x: hp[2*i + 1].second){
                hp[i].second.insert(x);
            }
            hp[i].second.insert(hp[i].first);
            ma[i] = max(ma[2*i], ma[2*i + 1]);

            if(i < (1 << g)){
                int a = *hp[i].second.upper_bound(ma[i]);
                ans += a;
                flag[a] = 1;
                hp[i].second.erase(a);
                ma[i] = a;
            }
        }

        cout << ans << endl;

        for(int i = (1 << 20) - 1; i>=1; i--){
            if(inv[i] > 0 && flag[i] == 0){
                int pos = inv[i];
                cout << pos << " ";

                while(pos < (1 << (h - 1))){
                    if(hp[2*pos].first == 0 && hp[2*pos + 1].first == 0){
                        hp[pos].first = 0;
                        break;
                    }

                    else if(hp[2*pos].first > hp[2*pos + 1].first){
                        int j = hp[2*pos].first;
                        swap(inv[i], inv[j]);
                        swap(hp[pos].first, hp[2*pos].first);
                        pos = 2*pos;
                    }

                    else if(hp[2*pos].first < hp[2*pos + 1].first){
                        int j = hp[2*pos + 1].first;
                        swap(inv[i], inv[j]);
                        swap(hp[pos].first, hp[2*pos + 1].first);
                        pos = 2*pos + 1;
                    }

                    if(pos >= (1 << (h - 1))) hp[pos].first = 0;
                }

                inv[i] = -1;
            }
        }

        cout << endl;

    }

}


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

int main(){
    ios::sync_with_stdio(0);
    int t;
    cin >> t;

    while(t--){
        int h, g;
        long long ans = 0;
        cin >> h >> g;

        pair<int, set<int> > hp [(1 << h)];
        int ma[(1 << h)];
        fill(ma, ma + (1 << h), 0);

        int inv[(1 << 20)];
        bool flag[(1 << 20)];
        fill(flag, flag + (1 << 20), 0);
        fill(inv, inv + (1 << 20), -1);

        for(int i = 1; i<(1 << h); i++){
            cin >> hp[i].first;
            inv[hp[i].first] = i;

            if(i >= (1 << (h - 1))){
                hp[i].second.insert(hp[i].first);
            }
        }

        for(int i = (1 << (h - 1)) - 1; i>=1; i--){
            set_union(begin(hp[2*i].second), end(hp[2*i].second), begin(hp[2*i + 1].second), end(hp[2*i + 1].second), inserter(hp[i].second, begin(hp[i].second)));
            hp[i].second.insert(hp[i].first);
            ma[i] = max(ma[2*i], ma[2*i + 1]);

            if(i < (1 << g)){
                int a = *hp[i].second.upper_bound(ma[i]);
                ans += a;
                flag[a] = 1;
                hp[i].second.erase(a);
                ma[i] = a;
            }
        }

        cout << ans << endl;

        for(int i = (1 << 20) - 1; i>=1; i--){
            if(inv[i] > 0 && flag[i] == 0){
                int pos = inv[i];
                cout << pos << " ";

                while(pos < (1 << (h - 1))){
                    if(hp[2*pos].first == 0 && hp[2*pos + 1].first == 0){
                        hp[pos].first = 0;
                        break;
                    }

                    else if(hp[2*pos].first > hp[2*pos + 1].first){
                        int j = hp[2*pos].first;
                        swap(inv[i], inv[j]);
                        swap(hp[pos].first, hp[2*pos].first);
                        pos = 2*pos;
                    }

                    else if(hp[2*pos].first < hp[2*pos + 1].first){
                        int j = hp[2*pos + 1].first;
                        swap(inv[i], inv[j]);
                        swap(hp[pos].first, hp[2*pos + 1].first);
                        pos = 2*pos + 1;
                    }

                    if(pos >= (1 << (h - 1))) hp[pos].first = 0;
                }

                inv[i] = -1;
            }
        }

        cout << endl;

    }

}


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

int inv[(1 << 20)];
bool flag[(1 << 20)];
vector<int> last;

int main(){
    ios::sync_with_stdio(0);
    cin.tie(NULL);
    cout.tie(NULL);
    int t;
    cin >> t;

    fill(flag, flag + (1 << 20), 0);
    fill(inv, inv + (1 << 20), -1);

    while(t--){
        if(t == 69700) return 1;
        int h, g;
        long long ans = 0;
        cin >> h >> g;

        pair<int, set<int> > hp [(1 << h)];
        int ma[(1 << h)];
        fill(ma, ma + (1 << h), 0);

        for(int x: last){
            inv[x] = -1;
            flag[x] = 0;
        }

        last.resize(0);

        for(int i = 1; i<(1 << h); i++){
            cin >> hp[i].first;
            inv[hp[i].first] = i;
            last.push_back(hp[i].first);

            if(i >= (1 << (h - 1))){
                hp[i].second.insert(hp[i].first);
            }
        }

        for(int i = (1 << (h - 1)) - 1; i>=1; i--){
            hp[i].second = hp[2*i].second;
            for(int x: hp[2*i + 1].second){
                hp[i].second.insert(x);
            }
            hp[i].second.insert(hp[i].first);
            ma[i] = max(ma[2*i], ma[2*i + 1]);

            if(i < (1 << g)){
                int a = *hp[i].second.upper_bound(ma[i]);
                ans += a;
                flag[a] = 1;
                hp[i].second.erase(a);
                ma[i] = a;
            }
        }

        cout << ans << endl;

        for(int i = (1 << 20) - 1; i>=1; i--){
            if(inv[i] > 0 && flag[i] == 0){
                int pos = inv[i];
                cout << pos << " ";

                while(pos < (1 << (h - 1))){
                    if(hp[2*pos].first == 0 && hp[2*pos + 1].first == 0){
                        hp[pos].first = 0;
                        break;
                    }

                    else if(hp[2*pos].first > hp[2*pos + 1].first){
                        int j = hp[2*pos].first;
                        swap(inv[i], inv[j]);
                        swap(hp[pos].first, hp[2*pos].first);
                        pos = 2*pos;
                    }

                    else if(hp[2*pos].first < hp[2*pos + 1].first){
                        int j = hp[2*pos + 1].first;
                        swap(inv[i], inv[j]);
                        swap(hp[pos].first, hp[2*pos + 1].first);
                        pos = 2*pos + 1;
                    }

                    if(pos >= (1 << (h - 1))) hp[pos].first = 0;
                }

                inv[i] = -1;
            }
        }

        cout << endl;

    }

}


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

int inv[(1 << 20)];
bool flag[(1 << 20)];
vector<int> last;

int main(){
    ios::sync_with_stdio(0);
    cin.tie(NULL);
    cout.tie(NULL);
    int t;
    cin >> t;

    fill(flag, flag + (1 << 20), 0);
    fill(inv, inv + (1 << 20), -1);

    while(t--){
        if(t == 60000) return 1;
        int h, g;
        long long ans = 0;
        cin >> h >> g;

        pair<int, set<int> > hp [(1 << h)];
        int ma[(1 << h)];
        fill(ma, ma + (1 << h), 0);

        for(int x: last){
            inv[x] = -1;
            flag[x] = 0;
        }

        last.resize(0);

        for(int i = 1; i<(1 << h); i++){
            cin >> hp[i].first;
            inv[hp[i].first] = i;
            last.push_back(hp[i].first);

            if(i >= (1 << (h - 1))){
                hp[i].second.insert(hp[i].first);
            }
        }

        for(int i = (1 << (h - 1)) - 1; i>=1; i--){
            hp[i].second = hp[2*i].second;
            for(int x: hp[2*i + 1].second){
                hp[i].second.insert(x);
            }
            hp[i].second.insert(hp[i].first);
            ma[i] = max(ma[2*i], ma[2*i + 1]);

            if(i < (1 << g)){
                int a = *hp[i].second.upper_bound(ma[i]);
                ans += a;
                flag[a] = 1;
                hp[i].second.erase(a);
                ma[i] = a;
            }
        }

        cout << ans << endl;

        for(int i = (1 << 20) - 1; i>=1; i--){
            if(inv[i] > 0 && flag[i] == 0){
                int pos = inv[i];
                cout << pos << " ";

                while(pos < (1 << (h - 1))){
                    if(hp[2*pos].first == 0 && hp[2*pos + 1].first == 0){
                        hp[pos].first = 0;
                        break;
                    }

                    else if(hp[2*pos].first > hp[2*pos + 1].first){
                        int j = hp[2*pos].first;
                        swap(inv[i], inv[j]);
                        swap(hp[pos].first, hp[2*pos].first);
                        pos = 2*pos;
                    }

                    else if(hp[2*pos].first < hp[2*pos + 1].first){
                        int j = hp[2*pos + 1].first;
                        swap(inv[i], inv[j]);
                        swap(hp[pos].first, hp[2*pos + 1].first);
                        pos = 2*pos + 1;
                    }

                    if(pos >= (1 << (h - 1))) hp[pos].first = 0;
                }

                inv[i] = -1;
            }
        }

        cout << endl;

    }

}


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

int main(){
    ios::sync_with_stdio(0);
    int t;
    cin >> t;

    while(t--){
        int h, g;
        long long ans = 0;
        cin >> h >> g;

        pair<int, set<int> > hp [(1 << h)];
        int ma[(1 << h)];
        fill(ma, ma + (1 << h), 0);

        int inv[(1 << 15)];
        bool flag[(1 << 15)];
        fill(flag, flag + (1 << 15), 0);
        fill(inv, inv + (1 << 15), -1);

        for(int i = 1; i<(1 << h); i++){
            cin >> hp[i].first;
            inv[hp[i].first] = i;

            if(i >= (1 << (h - 1))){
                hp[i].second.insert(hp[i].first);
            }
        }

        for(int i = (1 << (h - 1)) - 1; i>=1; i--){
            set_union(begin(hp[2*i].second), end(hp[2*i].second), begin(hp[2*i + 1].second), end(hp[2*i + 1].second), inserter(hp[i].second, begin(hp[i].second)));
            hp[i].second.insert(hp[i].first);
            ma[i] = max(ma[2*i], ma[2*i + 1]);

            if(i < (1 << g)){
                int a = *hp[i].second.upper_bound(ma[i]);
                ans += a;
                flag[a] = 1;
                hp[i].second.erase(a);
                ma[i] = a;
            }
        }

        cout << ans << endl;

        for(int i = (1 << 15) - 1; i>=1; i--){
            if(inv[i] > 0 && flag[i] == 0){
                int pos = inv[i];
                cout << pos << " ";

                while(pos < (1 << (h - 1))){
                    if(hp[2*pos].first == 0 && hp[2*pos + 1].first == 0){
                        hp[pos].first = 0;
                        break;
                    }

                    else if(hp[2*pos].first > hp[2*pos + 1].first){
                        int j = hp[2*pos].first;
                        swap(inv[i], inv[j]);
                        swap(hp[pos].first, hp[2*pos].first);
                        pos = 2*pos;
                    }

                    else if(hp[2*pos].first < hp[2*pos + 1].first){
                        int j = hp[2*pos + 1].first;
                        swap(inv[i], inv[j]);
                        swap(hp[pos].first, hp[2*pos + 1].first);
                        pos = 2*pos + 1;
                    }

                    if(pos >= (1 << (h - 1))) hp[pos].first = 0;
                }

                inv[i] = -1;
            }
        }

        cout << endl;

    }

}


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

int inv[(1 << 20)];
bool flag[(1 << 20)];
vector<int> last;

int main(){
    ios::sync_with_stdio(0);
    int t;
    cin >> t;

    fill(flag, flag + (1 << 20), 0);
    fill(inv, inv + (1 << 20), -1);

    while(t--){
        int h, g;
        long long ans = 0;
        cin >> h >> g;

        pair<int, set<int> > hp [(1 << h)];
        int ma[(1 << h)];
        fill(ma, ma + (1 << h), 0);

        for(int x: last){
            inv[x] = -1;
            flag[x] = 0;
        }

       last.resize(0);

        for(int i = 1; i<(1 << h); i++){
            cin >> hp[i].first;
            inv[hp[i].first] = i;
            last.push_back(hp[i].first);

            if(i >= (1 << (h - 1))){
                hp[i].second.insert(hp[i].first);
            }
        }

        for(int i = (1 << (h - 1)) - 1; i>=1; i--){
            hp[i].second = hp[2*i].second;
            for(int x: hp[2*i + 1].second){
                hp[i].second.insert(x);
            }
            hp[i].second.insert(hp[i].first);
            ma[i] = max(ma[2*i], ma[2*i + 1]);

            if(i < (1 << g)){
                int a = *hp[i].second.upper_bound(ma[i]);
                ans += a;
                flag[a] = 1;
                hp[i].second.erase(a);
                ma[i] = a;
            }
        }

        cout << ans << endl;

        for(int i = (1 << 20) - 1; i>=1; i--){
            if(inv[i] > 0 && flag[i] == 0){
                int pos = inv[i];
                cout << pos << " ";

                while(pos < (1 << (h - 1))){
                    if(hp[2*pos].first == 0 && hp[2*pos + 1].first == 0){
                        hp[pos].first = 0;
                        break;
                    }

                    else if(hp[2*pos].first > hp[2*pos + 1].first){
                        int j = hp[2*pos].first;
                        swap(inv[i], inv[j]);
                        swap(hp[pos].first, hp[2*pos].first);
                        pos = 2*pos;
                    }

                    else if(hp[2*pos].first < hp[2*pos + 1].first){
                        int j = hp[2*pos + 1].first;
                        swap(inv[i], inv[j]);
                        swap(hp[pos].first, hp[2*pos + 1].first);
                        pos = 2*pos + 1;
                    }

                    if(pos >= (1 << (h - 1))) hp[pos].first = 0;
                }

                inv[i] = -1;
            }
        }

        cout << endl;

    }

}


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

int inv[(1 << 20)];
bool flag[(1 << 20)];
vector<int> last;

int main(){
    ios::sync_with_stdio(0);
    cin.tie(NULL);
    cout.tie(NULL);
    int t;
    cin >> t;

    fill(flag, flag + (1 << 20), 0);
    fill(inv, inv + (1 << 20), -1);

    while(t--){
        int h, g;
        long long ans = 0;
        cin >> h >> g;

        pair<int, set<int> > hp [(1 << h)];
        int ma[(1 << h)];
        fill(ma, ma + (1 << h), 0);

        for(int x: last){
            inv[x] = -1;
            flag[x] = 0;
        }

        last.resize(0);

        for(int i = 1; i<(1 << h); i++){
            cin >> hp[i].first;
            inv[hp[i].first] = i;
            last.push_back(hp[i].first);

            if(i >= (1 << (h - 1))){
                hp[i].second.insert(hp[i].first);
            }
        }

        for(int i = (1 << (h - 1)) - 1; i>=1; i--){
            hp[i].second = hp[2*i].second;
            for(int x: hp[2*i + 1].second){
                hp[i].second.insert(x);
            }
            hp[i].second.insert(hp[i].first);
            ma[i] = max(ma[2*i], ma[2*i + 1]);

            if(i < (1 << g)){
                int a = *hp[i].second.upper_bound(ma[i]);
                ans += a;
                flag[a] = 1;
                hp[i].second.erase(a);
                ma[i] = a;
            }
        }

        cout << ans << endl;

        sort(last.begin(), last.end());
        reverse(last.begin(), last.end());
        for(auto i: last){
            if(inv[i] > 0 && flag[i] == 0){
                int pos = inv[i];
                cout << pos << " ";

                while(pos < (1 << (h - 1))){
                    if(hp[2*pos].first == 0 && hp[2*pos + 1].first == 0){
                        hp[pos].first = 0;
                        break;
                    }

                    else if(hp[2*pos].first > hp[2*pos + 1].first){
                        int j = hp[2*pos].first;
                        swap(inv[i], inv[j]);
                        swap(hp[pos].first, hp[2*pos].first);
                        pos = 2*pos;
                    }

                    else if(hp[2*pos].first < hp[2*pos + 1].first){
                        int j = hp[2*pos + 1].first;
                        swap(inv[i], inv[j]);
                        swap(hp[pos].first, hp[2*pos + 1].first);
                        pos = 2*pos + 1;
                    }

                    if(pos >= (1 << (h - 1))) hp[pos].first = 0;
                }

                inv[i] = -1;
            }
        }

        cout << endl;

    }

}


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

int inv[(1 << 20)];
bool flag[(1 << 20)];
vector<int> last;

int main(){
    ios::sync_with_stdio(0);
    cin.tie(NULL);
    cout.tie(NULL);
    int t;
    cin >> t;

    fill(flag, flag + (1 << 20), 0);
    fill(inv, inv + (1 << 20), -1);

    while(t--){
        if(t == 69000) return 1;
        int h, g;
        long long ans = 0;
        cin >> h >> g;

        pair<int, set<int> > hp [(1 << h)];
        int ma[(1 << h)];
        fill(ma, ma + (1 << h), 0);

        for(int x: last){
            inv[x] = -1;
            flag[x] = 0;
        }

        last.resize(0);

        for(int i = 1; i<(1 << h); i++){
            cin >> hp[i].first;
            inv[hp[i].first] = i;
            last.push_back(hp[i].first);

            if(i >= (1 << (h - 1))){
                hp[i].second.insert(hp[i].first);
            }
        }

        for(int i = (1 << (h - 1)) - 1; i>=1; i--){
            hp[i].second = hp[2*i].second;
            for(int x: hp[2*i + 1].second){
                hp[i].second.insert(x);
            }
            hp[i].second.insert(hp[i].first);
            ma[i] = max(ma[2*i], ma[2*i + 1]);

            if(i < (1 << g)){
                int a = *hp[i].second.upper_bound(ma[i]);
                ans += a;
                flag[a] = 1;
                hp[i].second.erase(a);
                ma[i] = a;
            }
        }

        cout << ans << endl;

        for(int i = (1 << 20) - 1; i>=1; i--){
            if(inv[i] > 0 && flag[i] == 0){
                int pos = inv[i];
                cout << pos << " ";

                while(pos < (1 << (h - 1))){
                    if(hp[2*pos].first == 0 && hp[2*pos + 1].first == 0){
                        hp[pos].first = 0;
                        break;
                    }

                    else if(hp[2*pos].first > hp[2*pos + 1].first){
                        int j = hp[2*pos].first;
                        swap(inv[i], inv[j]);
                        swap(hp[pos].first, hp[2*pos].first);
                        pos = 2*pos;
                    }

                    else if(hp[2*pos].first < hp[2*pos + 1].first){
                        int j = hp[2*pos + 1].first;
                        swap(inv[i], inv[j]);
                        swap(hp[pos].first, hp[2*pos + 1].first);
                        pos = 2*pos + 1;
                    }

                    if(pos >= (1 << (h - 1))) hp[pos].first = 0;
                }

                inv[i] = -1;
            }
        }

        cout << endl;

    }

}


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

int inv[(1 << 20)];
bool flag[(1 << 20)];
vector<int> last;

int main(){
    ios::sync_with_stdio(0);
    cin.tie(NULL);
    cout.tie(NULL);
    int t;
    cin >> t;

    fill(flag, flag + (1 << 20), 0);
    fill(inv, inv + (1 << 20), -1);

    while(t--){
        if(t == 30700) return 1;
        int h, g;
        long long ans = 0;
        cin >> h >> g;

        pair<int, set<int> > hp [(1 << h)];
        int ma[(1 << h)];
        fill(ma, ma + (1 << h), 0);

        for(int x: last){
            inv[x] = -1;
            flag[x] = 0;
        }

        last.resize(0);

        for(int i = 1; i<(1 << h); i++){
            cin >> hp[i].first;
            inv[hp[i].first] = i;
            last.push_back(hp[i].first);

            if(i >= (1 << (h - 1))){
                hp[i].second.insert(hp[i].first);
            }
        }

        for(int i = (1 << (h - 1)) - 1; i>=1; i--){
            hp[i].second = hp[2*i].second;
            for(int x: hp[2*i + 1].second){
                hp[i].second.insert(x);
            }
            hp[i].second.insert(hp[i].first);
            ma[i] = max(ma[2*i], ma[2*i + 1]);

            if(i < (1 << g)){
                int a = *hp[i].second.upper_bound(ma[i]);
                ans += a;
                flag[a] = 1;
                hp[i].second.erase(a);
                ma[i] = a;
            }
        }

        cout << ans << endl;

        for(int i = (1 << 20) - 1; i>=1; i--){
            if(inv[i] > 0 && flag[i] == 0){
                int pos = inv[i];
                cout << pos << " ";

                while(pos < (1 << (h - 1))){
                    if(hp[2*pos].first == 0 && hp[2*pos + 1].first == 0){
                        hp[pos].first = 0;
                        break;
                    }

                    else if(hp[2*pos].first > hp[2*pos + 1].first){
                        int j = hp[2*pos].first;
                        swap(inv[i], inv[j]);
                        swap(hp[pos].first, hp[2*pos].first);
                        pos = 2*pos;
                    }

                    else if(hp[2*pos].first < hp[2*pos + 1].first){
                        int j = hp[2*pos + 1].first;
                        swap(inv[i], inv[j]);
                        swap(hp[pos].first, hp[2*pos + 1].first);
                        pos = 2*pos + 1;
                    }

                    if(pos >= (1 << (h - 1))) hp[pos].first = 0;
                }

                inv[i] = -1;
            }
        }

        cout << endl;

    }

}


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

int inv[(1 << 20)];
bool flag[(1 << 20)];
vector<int> last;

int main(){
    ios::sync_with_stdio(0);
    cin.tie(NULL);
    cout.tie(NULL);
    int t;
    cin >> t;

    fill(flag, flag + (1 << 20), 0);
    fill(inv, inv + (1 << 20), -1);

    while(t--){
        int h, g;
        long long ans = 0;
        cin >> h >> g;

        pair<int, vector<int> > hp [(1 << h)];
        int ma[(1 << h)];
        fill(ma, ma + (1 << h), 0);

        for(int x: last){
            inv[x] = -1;
            flag[x] = 0;
        }

        last.resize(0);

        for(int i = 1; i<(1 << h); i++){
            cin >> hp[i].first;
            inv[hp[i].first] = i;
            last.push_back(hp[i].first);

            if(i >= (1 << (h - 1))){
                hp[i].second.push_back(hp[i].first);
            }
        }

        for(int i = (1 << (h - 1)) - 1; i>=1; i--){
            hp[i].second = hp[2*i].second;
            for(int x: hp[2*i + 1].second){
                hp[i].second.push_back(x);
            }
            hp[i].second.push_back(hp[i].first);
            ma[i] = max(ma[2*i], ma[2*i + 1]);
            int mi = 1e9 + 7;

            if(i < (1 << g)){
                for(auto x: hp[i].second){
                    if(x > ma[i]) mi = min(mi, x);
                }
                ans += mi;
                flag[mi] = 1;
                ma[i] = mi;
            }
        }

        cout << ans << endl;

        sort(last.begin(), last.end());
        reverse(last.begin(), last.end());
        for(auto i: last){
            if(inv[i] > 0 && flag[i] == 0){
                int pos = inv[i];
                cout << pos << " ";

                while(pos < (1 << (h - 1))){
                    if(hp[2*pos].first == 0 && hp[2*pos + 1].first == 0){
                        hp[pos].first = 0;
                        break;
                    }

                    else if(hp[2*pos].first > hp[2*pos + 1].first){
                        int j = hp[2*pos].first;
                        swap(inv[i], inv[j]);
                        swap(hp[pos].first, hp[2*pos].first);
                        pos = 2*pos;
                    }

                    else if(hp[2*pos].first < hp[2*pos + 1].first){
                        int j = hp[2*pos + 1].first;
                        swap(inv[i], inv[j]);
                        swap(hp[pos].first, hp[2*pos + 1].first);
                        pos = 2*pos + 1;
                    }

                    if(pos >= (1 << (h - 1))) hp[pos].first = 0;
                }

                inv[i] = -1;
            }
        }

        cout << endl;

    }

}


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

int inv[(1 << 20)];
bool flag[(1 << 20)];
vector<int> last;

int main(){
    ios::sync_with_stdio(0);
    cin.tie(NULL);
    cout.tie(NULL);
    int t;
    cin >> t;

    fill(flag, flag + (1 << 20), 0);
    fill(inv, inv + (1 << 20), -1);

    while(t--){
        int h, g;
        long long ans = 0;
        cin >> h >> g;

        pair<int, set<int> > hp [(1 << h)];
        int ma[(1 << h)];
        fill(ma, ma + (1 << h), 0);

        for(int x: last){
            inv[x] = -1;
            flag[x] = 0;
        }

        last.resize(0);

        for(int i = 1; i<(1 << h); i++){
            cin >> hp[i].first;
            inv[hp[i].first] = i;
            last.push_back(hp[i].first);

            if(i >= (1 << (h - 1))){
                hp[i].second.insert(hp[i].first);
            }
        }

        for(int i = (1 << (h - 1)) - 1; i>=1; i--){
            hp[i].second = hp[2*i].second;
            for(int x: hp[2*i + 1].second){
                hp[i].second.insert(x);
            }
            hp[i].second.insert(hp[i].first);
            ma[i] = max(ma[2*i], ma[2*i + 1]);

            if(i < (1 << g)){
                int a = *hp[i].second.upper_bound(ma[i]);
                ans += a;
                flag[a] = 1;
                hp[i].second.erase(a);
                ma[i] = a;
            }
        }

        cout << ans << endl;

        for(auto i: last){
            if(inv[i] > 0 && flag[i] == 0){
                int pos = inv[i];
                cout << pos << " ";

                while(pos < (1 << (h - 1))){
                    if(hp[2*pos].first == 0 && hp[2*pos + 1].first == 0){
                        hp[pos].first = 0;
                        break;
                    }

                    else if(hp[2*pos].first > hp[2*pos + 1].first){
                        int j = hp[2*pos].first;
                        swap(inv[i], inv[j]);
                        swap(hp[pos].first, hp[2*pos].first);
                        pos = 2*pos;
                    }

                    else if(hp[2*pos].first < hp[2*pos + 1].first){
                        int j = hp[2*pos + 1].first;
                        swap(inv[i], inv[j]);
                        swap(hp[pos].first, hp[2*pos + 1].first);
                        pos = 2*pos + 1;
                    }

                    if(pos >= (1 << (h - 1))) hp[pos].first = 0;
                }

                inv[i] = -1;
            }
        }

        cout << endl;

    }

}


#include<cstdio>
using namespace std;
int T,H,G;
int A[1<<21];
int ans[1<<20];
int vis[25];
main()
{
	scanf("%d",&T);
	for(;T--;)
	{
		scanf("%d%d",&H,&G);
		for(int i=1;i<1<<H;i++)scanf("%d",&A[i]);
		int anscnt=0;
		int dep=0;
		for(int i=1;i<1<<H;i++)
		{
			if((i&i-1)==0)dep++;
			while(true)
			{
				int viscnt=0;
				int id=i;
				while(A[id]!=0)
				{
					vis[viscnt++]=id;
					if(A[id*2]>A[id*2+1])id=id*2;
					else id=id*2+1;
				}
				if(viscnt<=G-dep+1)break;
				ans[anscnt++]=i;
				for(int j=0;j<viscnt-1;j++)A[vis[j]]=A[vis[j+1]];
				A[vis[viscnt-1]]=0;
			}
		}
		long long sum=0;
		for(int i=1;i<1<<G;i++)sum+=A[i];
		printf("%lld\n",sum);
		for(int i=0;i<anscnt;i++)printf("%d%c",ans[i],i+1==anscnt?'\n':' ');
	}
}


#include<cstdio>
using namespace std;
int T,H,G;
int A[1<<21];
int ans[1<<20];
int vis[25];
main()
{
	scanf("%d",&T);
	for(;T--;)
	{
		scanf("%d%d",&H,&G);
		for(int i=1;i<1<<H;i++)scanf("%d",&A[i]);
		int anscnt=0;
		int dep=0;
		for(int i=1;i<1<<H;i++)
		{
			if((i&i-1)==0)dep++;
			while(A[i])
			{
				int viscnt=0;
				int id=i;
				while(A[id]!=0)
				{
					vis[viscnt++]=id;
					if(A[id*2]>A[id*2+1])id=id*2;
					else id=id*2+1;
				}
				if(viscnt<=G-dep+1)break;
				ans[anscnt++]=i;
				for(int j=0;j<viscnt-1;j++)A[vis[j]]=A[vis[j+1]];
				if(viscnt>0)A[vis[viscnt-1]]=0;
			}
		}
		long long sum=0;
		for(int i=1;i<1<<G;i++)sum+=A[i];
		printf("%lld\n",sum);
		for(int i=0;i<anscnt;i++)printf("%d%c",ans[i],i+1==anscnt?'\n':' ');
	}
}


#include<cstdio>
using namespace std;
int T,H,G;
int A[1<<21];
int ans[1<<20];
int vis[25];
main()
{
	scanf("%d",&T);
	for(;T--;)
	{
		scanf("%d%d",&H,&G);
		for(int i=1;i<1<<H;i++)scanf("%d",&A[i]);
		int anscnt=0;
		int dep=0;
		for(int i=1;i<1<<H;i++)
		{
			if((i&i-1)==0)dep++;
			while(A[i])
			{
				int viscnt=0;
				int id=i;
				while(id<1<<H&&A[id]!=0)
				{
					vis[viscnt++]=id;
					if(A[id*2]>A[id*2+1])id=id*2;
					else id=id*2+1;
				}
				if(viscnt<=G-dep+1)break;
				ans[anscnt++]=i;
				for(int j=0;j<viscnt-1;j++)A[vis[j]]=A[vis[j+1]];
				A[vis[viscnt-1]]=0;
			}
		}
		long long sum=0;
		for(int i=1;i<1<<G;i++)sum+=A[i];
		printf("%lld\n",sum);
		for(int i=0;i<anscnt;i++)printf("%d%c",ans[i],i+1==anscnt?'\n':' ');
	}
}


#include<cstdio>
using namespace std;
int T,H,G;
int A[1<<21];
int ans[1<<20];
int vis[25];
main()
{
	scanf("%d",&T);
	for(;T--;)
	{
		scanf("%d%d",&H,&G);
		for(int i=1;i<1<<H;i++)scanf("%d",&A[i]);
		int anscnt=0;
		int dep=0;
		for(int i=1;i<1<<H;i++)
		{
			if((i&i-1)==0)dep++;
			while(A[i])
			{
				int viscnt=0;
				int id=i;
				while(A[id]!=0)
				{
					vis[viscnt++]=id;
					if(A[id*2]>A[id*2+1])id=id*2;
					else id=id*2+1;
				}
				if(viscnt<=G-dep+1)break;
				ans[anscnt++]=i;
				for(int j=0;j<viscnt-1;j++)A[vis[j]]=A[vis[j+1]];
				A[vis[viscnt-1]]=0;
			}
		}
		long long sum=0;
		for(int i=1;i<1<<G;i++)sum+=A[i];
		printf("%lld\n",sum);
		for(int i=0;i<anscnt;i++)printf("%d%c",ans[i],i+1==anscnt?'\n':' ');
	}
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 3e6+5;

#define ls (x<<1)
#define rs (x<<1|1)

int T;
int h,g;
int a[N];
int id[N],p[N],res[N];
ll ans;

bool cmp(int x,int y){
	return a[x]>a[y];
}

int main(){
	scanf("%d",&T);
	while(T--){
		scanf("%d%d",&h,&g);
		ans=0;
		for(int i=1;i<(1<<h);++i){
			scanf("%d",&a[i]);
			ans+=a[i];
			id[i]=i;
		}
		sort(id+1,id+(1<<h));
		for(int i=1;i<(1<<h);++i)p[id[i]]=i;
		for(int i=1,cnt=0;cnt<(1<<h)-(1<<g);++i){
			int x=id[i],m;
			static int q[30];
			q[m=1]=x;
			while(a[ls]||a[rs]){
				if(a[ls]>a[rs])x=ls;
				else x=rs;
				q[++m]=x;
			}
			q[m+1]=0;
			if(m<(1<<g))continue;
			res[++cnt]=q[1];
			ans-=a[q[1]];
			for(int j=1;j<=m;++j){
				a[q[j]]=a[q[j+1]];
				p[q[j]]=p[q[j+1]];
				id[p[q[j]]]=q[j];
			}
		}
		for(int i=1;i<(1<<h);++i)a[i]=0;
		printf("%lld\n",ans);
		for(int i=1;i<=(1<<h)-(1<<g);++i)printf("%d ",res[i]);
		printf("\n");
	}
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 3e6+5;

#define ls (x<<1)
#define rs (x<<1|1)

int T;
int h,g;
int a[N];
int id[N],p[N],res[N];
ll ans;

bool cmp(int x,int y){
	return a[x]>a[y];
}

int main(){
	scanf("%d",&T);
	while(T--){
		scanf("%d%d",&h,&g);
		ans=0;
		for(int i=1;i<(1<<h);++i){
			scanf("%d",&a[i]);
			ans+=a[i];
			id[i]=i;
		}
		sort(id+1,id+(1<<h));
		for(int i=1;i<(1<<h);++i)p[id[i]]=i;
		for(int i=1,cnt=0;cnt<(1<<h)-(1<<g);++i){
			int x=id[i],m;
			static int q[30];
			q[m=1]=x;
			while(a[ls]||a[rs]){
				if(a[ls]>a[rs])x=ls;
				else x=rs;
				q[++m]=x;
			}
			q[m+1]=0;
			if(q[m]<(1<<g))continue;
			res[++cnt]=q[1];
			ans-=a[q[1]];
			for(int j=1;j<=m;++j){
				a[q[j]]=a[q[j+1]];
				p[q[j]]=p[q[j+1]];
				id[p[q[j]]]=q[j];
			}
		}
		printf("%lld\n",ans);
		for(int i=1;i<=(1<<h)-(1<<g);++i)printf("%d ",res[i]);
		printf("\n");
	}
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 3e6+5;

#define ls (x<<1)
#define rs (x<<1|1)

int T;
int h,g;
int a[N];
int id[N],p[N],res[N];
ll ans;

bool cmp(int x,int y){
	return a[x]>a[y];
}

int main(){
	scanf("%d",&T);
	while(T--){
		scanf("%d%d",&h,&g);
		ans=0;
		for(int i=1;i<(1<<h);++i){
			scanf("%d",&a[i]);
			ans+=a[i];
			id[i]=i;
		}
		sort(id+1,id+(1<<h),cmp);
		for(int i=1;i<(1<<h);++i)p[id[i]]=i;
		for(int i=1,cnt=0;cnt<(1<<h)-(1<<g);++i){
			int x=id[i],m;
			static int q[30];
			q[m=1]=x;
			while(a[ls]||a[rs]){
				if(a[ls]>a[rs])x=ls;
				else x=rs;
				q[++m]=x;
			}
			q[m+1]=0;
			if(q[m]<(1<<g))continue;
			res[++cnt]=q[1];
			ans-=a[q[1]];
			for(int j=1;j<=m;++j){
				a[q[j]]=a[q[j+1]];
				p[q[j]]=p[q[j+1]];
				id[p[q[j]]]=q[j];
			}
		}
		for(int i=1;i<(1<<h);++i)a[i]=0;
		printf("%lld\n",ans);
		for(int i=1;i<=(1<<h)-(1<<g);++i)printf("%d ",res[i]);
		printf("\n");
	}
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 3e6+5;

#define ls (x<<1)
#define rs (x<<1|1)

int T;
int h,g;
int a[N];
int id[N],p[N],res[N];
ll ans;

bool cmp(int x,int y){
	return a[x]>a[y];
}

int main(){
	scanf("%d",&T);
	while(T--){
		scanf("%d%d",&h,&g);
		ans=0;
		for(int i=1;i<(1<<h);++i){
			scanf("%d",&a[i]);
			ans+=a[i];
			id[i]=i;
		}
		sort(id+1,id+(1<<h));
		for(int i=1;i<(1<<h);++i)p[id[i]]=i;
		for(int i=1,cnt=0;cnt<(1<<h)-(1<<g);++i){
			int x=id[i],m;
			static int q[30];
			q[m=1]=x;
			while(a[ls]||a[rs]){
				if(a[ls]>a[rs])x=ls;
				else x=rs;
				q[++m]=x;
			}
			q[m+1]=0;
			if(q[m]<(1<<g))continue;
			res[++cnt]=q[1];
			ans-=a[q[1]];
			for(int j=1;j<=m;++j){
				a[q[j]]=a[q[j+1]];
				p[q[j]]=p[q[j+1]];
				id[p[q[j]]]=q[j];
			}
		}
		for(int i=1;i<(1<<h);++i)a[i]=0;
		printf("%lld\n",ans);
		for(int i=1;i<=(1<<h)-(1<<g);++i)printf("%d ",res[i]);
		printf("\n");
	}
}


#include<bits/stdc++.h>
using namespace std;
int a[1<<22],sz[1<<22],pop[1<<22];
list<int> cand[1<<22];
struct cmp{
	bool operator()(int i,int j)const{
		return a[i]<a[j];
	}
};
int main(){
	int t,g,h;
	scanf("%d",&t);
	while(t--){
		scanf("%d%d",&h,&g);
		for(int i=1;i<(1<<h);i++){
			scanf("%d",&a[i]);
			if(i<(1<<g)) sz[i]=1;
			else sz[i]=0;
			pop[i]=1;
		}
		for(int i=(1<<h);i<(1<<h+1);i++) sz[i]=0;
		for(int i=(1<<g)-1;i>0;i--){
			sz[i/2]+=sz[i];
		}
		for(int i=(1<<h)-1;i>0;i--){
			list<int> tmp;
			auto it=cand[i*2].begin();
			advance(it,sz[i*2]);
			cand[i].splice(cand[i].end(),cand[i*2],cand[i*2].begin(),it);
			it=cand[i*2+1].begin();
			advance(it,sz[i*2+1]);
			cand[i].splice(cand[i].end(),cand[i*2+1],cand[i*2+1].begin(),it);
			int mx=0;
			for(auto x:cand[i]){
				mx=max(mx,a[x]);
			}
			cand[i*2].merge(cand[i*2+1],cmp());
			while(it!=cand[i*2].end() && a[*it]<mx){
				it=cand[i*2].erase(it);
			}
			cand[i].splice(cand[i].end(),cand[i*2]);
			cand[i].push_back(i);
			//for(auto x:cand[i]) printf("%d ",x);
			//puts("");
		}
		long long ans=0;
		for(int i=1;i<(1<<g);i++){
			pop[cand[1].front()]=0;
			ans+=a[cand[1].front()];
			cand[1].pop_front();
		}
		for(int i=(1<<h)-1;i>=(1<<g);i--) pop[i/2]+=pop[i];
		printf("%lld\n",ans);
		for(int i=(1<<g)-1;i>0;i--){
			while(pop[i]--) printf("%d ",i);
		}
		puts("");
		cand[1].clear();
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int a[1<<22],sz[1<<22],pop[1<<22];
list<int> cand[1<<22];
struct cmp{
	bool operator()(int i,int j)const{
		return a[i]<a[j];
	}
};
int main(){
	int t,g,h;
	scanf("%d",&t);
	while(t--){
		scanf("%d%d",&h,&g);
		for(int i=1;i<(1<<h);i++){
			scanf("%d",&a[i]);
			if(i<(1<<g)) sz[i]=1;
			else sz[i]=0;
			pop[i]=1;
		}
		for(int i=(1<<g)-1;i>0;i--){
			sz[i/2]+=sz[i];
		}
		for(int i=(1<<h-1);i<(1<<h);i++){
			cand[i].push_back(i);
		}
		for(int i=(1<<h-1)-1;i>0;i--){
			list<int> tmp;
			auto it=cand[i*2].begin();
			advance(it,sz[i*2]);
			cand[i].splice(cand[i].end(),cand[i*2],cand[i*2].begin(),it);
			it=cand[i*2+1].begin();
			advance(it,sz[i*2+1]);
			cand[i].splice(cand[i].end(),cand[i*2+1],cand[i*2+1].begin(),it);
			int mx=0;
			for(auto x:cand[i]){
				mx=max(mx,a[x]);
			}
			cand[i*2].merge(cand[i*2+1],cmp());
			while(it!=cand[i*2].end() && a[*it]<mx){
				it=cand[i*2].erase(it);
			}
			cand[i].splice(cand[i].end(),cand[i*2]);
			cand[i].push_back(i);
		}
		long long ans=0;
		for(int i=1;i<(1<<g);i++){
			pop[cand[1].front()]=0;
			ans+=a[cand[1].front()];
			cand[1].pop_front();
		}
		printf("%lld\n",ans);
		for(int i=(1<<h)-1;i>0;i--){
			if(pop[i]) printf("%d ",i);
		}
		puts("");
		cand[1].clear();
		for(int i=1;i<(1<<h);i++) assert(cand[i].empty());
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int a[1<<22],sz[1<<22],pop[1<<22];
list<int> cand[1<<22];
struct cmp{
	bool operator()(int i,int j)const{
		return a[i]<a[j];
	}
};
int main(){
	int t,g,h;
	scanf("%d",&t);
	while(t--){
		scanf("%d%d",&h,&g);
		for(int i=1;i<(1<<h);i++){
			scanf("%d",&a[i]);
			if(i<(1<<g)) sz[i]=1;
			else sz[i]=0;
			pop[i]=1;
		}
		for(int i=(1<<h);i<(1<<h+1);i++) sz[i]=0;
		for(int i=(1<<g)-1;i>0;i--){
			sz[i/2]+=sz[i];
		}
		for(int i=(1<<h)-1;i>0;i--){
			list<int> tmp;
			auto it=cand[i*2].begin();
			advance(it,sz[i*2]);
			cand[i].splice(cand[i].end(),cand[i*2],cand[i*2].begin(),it);
			it=cand[i*2+1].begin();
			advance(it,sz[i*2+1]);
			cand[i].splice(cand[i].end(),cand[i*2+1],cand[i*2+1].begin(),it);
			int mx=0;
			for(auto x:cand[i]){
				mx=max(mx,a[x]);
			}
			cand[i*2].merge(cand[i*2+1],cmp());
			while(it!=cand[i*2].end() && a[*it]<mx){
				it=cand[i*2].erase(it);
			}
			cand[i].splice(cand[i].end(),cand[i*2]);
			cand[i].push_back(i);
			//for(auto x:cand[i]) printf("%d ",x);
			//puts("");
		}
		long long ans=0;
		for(int i=1;i<(1<<g);i++){
			pop[cand[1].front()]=0;
			ans+=a[cand[1].front()];
			cand[1].pop_front();
		}
		printf("%lld\n",ans);
		for(int i=(1<<h)-1;i>0;i--){
			if(pop[i]) printf("%d ",i);
		}
		puts("");
		cand[1].clear();
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int a[1<<22],sz[1<<22],pop[1<<22];
list<int> cand[1<<22];
struct cmp{
	bool operator()(int i,int j)const{
		return a[i]<a[j];
	}
};
int main(){
	int t,g,h;
	scanf("%d",&t);
	while(t--){
		scanf("%d%d",&h,&g);
		for(int i=1;i<(1<<h);i++){
			scanf("%d",&a[i]);
			if(i<(1<<g)) sz[i]=1;
			else sz[i]=0;
			pop[i]=1;
		}
		for(int i=(1<<g)-1;i>1;i--){
			sz[i/2]+=sz[i];
		}
		for(int i=(1<<h-1);i<(1<<h);i++){
			cand[i].push_back(i);
		}
		for(int i=(1<<h-1)-1;i>0;i--){
			list<int> tmp;
			auto it=cand[i*2].begin();
			advance(it,sz[i*2]);
			cand[i].splice(cand[i].end(),cand[i*2],cand[i*2].begin(),it);
			it=cand[i*2+1].begin();
			advance(it,sz[i*2+1]);
			cand[i].splice(cand[i].end(),cand[i*2+1],cand[i*2+1].begin(),it);
			int mx=0;
			for(auto x:cand[i]){
				mx=max(mx,a[x]);
			}
			//printf("mx=%d\n",mx);
			cand[i*2].merge(cand[i*2+1],cmp());
			it=cand[i*2].begin();
			while(it!=cand[i*2].end() && a[*it]<mx){
				it=cand[i*2].erase(it);
			}
			cand[i].splice(cand[i].end(),cand[i*2]);
			cand[i].push_back(i);
			//for(auto x:cand[i]){
			//	printf("%d ",a[x]);
			//}
			//printf(" <--- heap %d\n",i);
		}
		long long ans=0;
		for(int i=1;i<(1<<g);i++){
			pop[cand[1].front()]=0;
			ans+=a[cand[1].front()];
			cand[1].pop_front();
		}
		printf("%lld\n",ans);
		for(int i=(1<<h)-1;i>0;i--){
			if(pop[i]) printf("%d ",i);
		}
		puts("");
		cand[1].clear();
		for(int i=1;i<(1<<h);i++) assert(cand[i].empty());
	}
	return 0;
}


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

const int maxN = (1 << 20) + 1234;
int tc;
int n, m, p[maxN];
void solve()
{
	cin >> n >> m;
	for (int i = 1; i < (1 << n); i++)
		cin >> p[i];
	vector<int> seq;
	priority_queue<pair<int, int>> q;
	q.push({p[1], 1});
	for (int i = 0; i < (1 << n) - (1 << m); i++)
	{
		while (true)
		{
			int x = q.top().second;
			int y = x;
			while (true)
			{
				if (2 * y > (1 << n))
					break;
				if (p[2 * y] == 0 && p[2 * y + 1] == 0)
					break;
				if (p[2 * y] > p[2 * y + 1])
					y = 2 * y;
				else
					y = 2 * y + 1;
			}
			if (y < (1 << m))
			{
				q.pop();
				if (p[2 * x])
					q.push({p[2 * x], 2 * x});
				if (p[2 * x + 1])
					q.push({p[2 * x + 1], 2 * x + 1});
			}
			else
			{
				y = x;
				while (true)
				{
					if (2 * y > (1 << n))
					{
						p[y] = 0;
						break;
					}
					if (p[2 * y] == 0 && p[2 * y + 1] == 0)
					{
						p[y] = 0;
						break;
					}
					if (p[2 * y] > p[2 * y + 1])
						p[y] = p[2 * y], y = 2 * y;
					else
						p[y] = p[2 * y + 1], y = 2 * y + 1;
				}
				seq.push_back(x);
				break;
			}
		}
	}
	i64 s = 0;
	for (int i = 1; i < (1 << m); i++)
		s += p[i];
	cout << s << "\n";
	for (auto x : seq)
		cout
			<< x << " ";
	cout << endl;
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin >> tc;
	for (int i = 0; i < tc; i++)
	{
		solve();
	}
}


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

const int maxN = 456789;
int tc;
int n, m, p[maxN];
void solve()
{
	cin >> n >> m;
	for (int i = 1; i < (1 << n); i++)
		cin >> p[i];
	vector<int> seq;
	priority_queue<pair<int, int>> q;
	q.push({p[1], 1});
	for (int i = 0; i < (1 << n) - (1 << m); i++)
	{
		while (true)
		{
			int x = q.top().second;
			int y = x;
			while (true)
			{
				if (2 * y > (1 << n))
					break;
				if (p[2 * y] == 0 && p[2 * y + 1] == 0)
					break;
				if (p[2 * y] > p[2 * y + 1])
					y = 2 * y;
				else
					y = 2 * y + 1;
			}
			if (y < (1 << m))
			{
				q.pop();
				if (p[2 * x])
					q.push({p[2 * x], 2 * x});
				if (p[2 * x + 1])
					q.push({p[2 * x + 1], 2 * x + 1});
			}
			else
			{
				y = x;
				while (true)
				{
					if (2 * y > (1 << n))
					{
						p[y] = 0;
						break;
					}
					if (p[2 * y] == 0 && p[2 * y + 1] == 0)
					{
						p[y] = 0;
						break;
					}
					if (p[2 * y] > p[2 * y + 1])
						p[y] = p[2 * y], y = 2 * y;
					else
						p[y] = p[2 * y + 1], y = 2 * y + 1;
				}
				seq.push_back(x);
				break;
			}
		}
	}
	i64 s = 0;
	for (int i = 1; i < (1 << m); i++)
		s += p[i];
	cout << s << "\n";
	for (auto x : seq)
		cout
			<< x << " ";
	cout << endl;
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin >> tc;
	for (int i = 0; i < tc; i++)
	{
		solve();
	}
}


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

const int maxN = (1 << 20) + 1234;
int tc;
int n, m, p[maxN];
void solve()
{
	cin >> n >> m;
	for (int i = 1; i < (1 << n); i++)
		cin >> p[i];
	vector<int> seq;
	priority_queue<pair<int, int>> q;
	q.push({p[1], 1});
	for (int i = 0; i < (1 << n) - (1 << m); i++)
	{
		while (true)
		{
			int x = q.top().second;
			int y = x;
			while (true)
			{
				if (2 * y >= (1 << n))
					break;
				if (p[2 * y] == 0 && p[2 * y + 1] == 0)
					break;
				if (p[2 * y] > p[2 * y + 1])
					y = 2 * y;
				else
					y = 2 * y + 1;
			}
			if (y < (1 << m))
			{
				q.pop();
				if (p[2 * x])
					q.push({p[2 * x], 2 * x});
				if (p[2 * x + 1])
					q.push({p[2 * x + 1], 2 * x + 1});
			}
			else
			{
				y = x;
				while (true)
				{
					if (2 * y >= (1 << n))
					{
						p[y] = 0;
						break;
					}
					if (p[2 * y] == 0 && p[2 * y + 1] == 0)
					{
						p[y] = 0;
						break;
					}
					if (p[2 * y] > p[2 * y + 1])
						p[y] = p[2 * y], y = 2 * y;
					else
						p[y] = p[2 * y + 1], y = 2 * y + 1;
				}
				seq.push_back(x);
				break;
			}
		}
	}
	i64 s = 0;
	for (int i = 1; i < (1 << m); i++)
		s += p[i];
	cout << s << "\n";
	for (auto x : seq)
		cout
			<< x << " ";
	cout << endl;
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin >> tc;
	for (int i = 0; i < tc; i++)
	{
		solve();
	}
}