#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <queue>
using namespace std;
struct A
{
    long long l,r;
    long long here;
    long long con;
    long long deg;
}Node[4000005];
long long what[4000005],N,M;
vector < long long > how;
priority_queue < long long , vector < long long > , less < long long > > all;
void build(long long here,long long deg)
{
    Node[here].deg=deg;
    if(Node[here].here==0)
    {
        Node[here].con=0;
        return;
    }
    build(Node[here].l,deg+1);
    build(Node[here].r,deg+1);
    Node[here].con=Node[Node[here].l].con+Node[Node[here].r].con+1;
}
bool can(long long here)
{
    //printf("%d %d %d\n",here,(Node[here].con-1),(1<<(M-deg)));
    if((Node[here].con-1)>=(1<<(M-Node[here].deg))-1)
    {
        if(Node[here].deg==M-1) return 1;
        else
        {
            if(Node[Node[here].l].here>Node[Node[here].r].here) return can(Node[here].l);
            else return can(Node[here].r);
        }
    }
    //printf("aa %d\n",here);
    return 0;
}
void Delete(long long here)
{
    Node[here].con--;
    if(Node[here].here==0) return ;
    Node[here].here=max(Node[Node[here].l].here,Node[Node[here].r].here);
    what[Node[here].here]=here;
    if(Node[Node[here].l].here>=Node[Node[here].r].here) Delete(Node[here].l);
    else Delete(Node[here].r);
}
int main()
{
	long long T,t,x,i,ans=0;
	scanf("%lld",&T);
	while(T--)
    {
    	ans=0;
    	how.clear();
    	while(!all.empty()) all.pop();
        scanf("%lld %lld",&N,&M);
        for(i=1;i<(1<<N);i++)
        {
            scanf("%lld",&Node[i].here);
            //Node[i].here=(1<<N)-i;
            ans+=Node[i].here;
            Node[i].l=2*i;
            Node[i].r=2*i+1;
            Node[Node[i].l].here=0;
            Node[Node[i].r].here=0;
            what[Node[i].here]=i;
            all.push(Node[i].here);

        }
        build(1,0);
        t=(1<<N)-(1<<M);
        while(t--&&!all.empty())
        {

            x=all.top();
            //printf("aa %lld\n",x);
            if(can(what[x]))
            {
                //printf("%lld\n",x);
                how.push_back(what[x]);
                ans-=x;
                Delete(what[x]);
            }
            else t++;
            all.pop();
        }
        printf("%lld\n",ans);
        for(auto i:how) printf("%lld ",i);
        printf("\n");

    }
	return 0;
}


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <queue>
using namespace std;
struct A
{
    long long l,r;
    long long here;
    long long con;
    long long deg;
}Node[4000005];
long long what[4000005],N,M;
vector < long long > how;
priority_queue < long long , vector < long long > , less < long long > > all;
void build(long long here,long long deg)
{
    Node[here].deg=deg;
    if(deg==N-1)
    {
        Node[here].con=1;
        return ;
    }
    build(Node[here].l,deg+1);
    build(Node[here].r,deg+1);
    Node[here].con=Node[Node[here].l].con+Node[Node[here].r].con+1;
}
int main()
{
	long long T,t,x,i,ans=0,here;
	scanf("%lld",&T);
	while(T--)
    {
    	ans=0;
    	how.clear();
    	while(!all.empty()) all.pop();
        scanf("%lld %lld",&N,&M);
        for(i=1;i<(1<<N);i++)
        {
            scanf("%lld",&Node[i].here);
            ans+=Node[i].here;
            Node[i].l=2*i;
            Node[i].r=2*i+1;
            Node[Node[i].l].here=0;
            Node[Node[i].r].here=0;
            what[Node[i].here]=i;
            all.push(Node[i].here);
        }
        build(1,0);
        if(N!=20)
        {
            t=(1<<N)-(1<<M);
            while(t--)
            {
                x=all.top();
                here=what[x];
                while(1)
                {
                    if((Node[here].con-1)>=(1<<(M-Node[here].deg))-1)
                    {
                        if(Node[here].deg==M-1)
                        {
                            here=what[x];
                            how.push_back(what[x]);
                            ans-=x;
                            while(Node[here].here)
                            {
                                Node[here].con--;
                                Node[here].here=max(Node[Node[here].l].here,Node[Node[here].r].here);
                                what[Node[here].here]=here;
                                if(Node[Node[here].l].here>=Node[Node[here].r].here) here=Node[here].l;
                                else here=Node[here].r;
                            }
                            break;
                        }
                        else
                        {
                            if(Node[Node[here].l].here>Node[Node[here].r].here) here=Node[here].l;
                            else here=Node[here].r;
                        }
                    }
                    else
                    {
                        t++;
                        break;
                    }
                }
                all.pop();
            }
            printf("%lld\n",ans);
            for(auto i:how) printf("%lld ",i);
            printf("\n");
        }

    }
	return 0;
}


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <queue>
using namespace std;
struct A
{
    long long l,r;
    long long here;
    long long con;
    long long deg;
}Node[4000005];
long long what[4000005],N,M;
vector < long long > how;
priority_queue < long long , vector < long long > , less < long long > > all;
void build(long long here,long long deg)
{
    Node[here].deg=deg;
    if(deg==N-1)
    {
        Node[here].con=1;
        return ;
    }
    build(Node[here].l,deg+1);
    build(Node[here].r,deg+1);
    Node[here].con=Node[Node[here].l].con+Node[Node[here].r].con+1;
}
int main()
{
	long long T,t,x,i,ans=0,here;
	scanf("%lld",&T);
	while(T--)
    {
    	ans=0;
    	how.clear();
    	while(!all.empty()) all.pop();
        scanf("%lld %lld",&N,&M);
        for(i=1;i<(1<<N);i++)
        {
            scanf("%lld",&Node[i].here);
            ans+=Node[i].here;
            Node[i].l=2*i;
            Node[i].r=2*i+1;
            Node[Node[i].l].here=0;
            Node[Node[i].r].here=0;
            what[Node[i].here]=i;
            all.push(Node[i].here);
        }
        build(1,0);
        t=(1<<N)-(1<<M);
        while(t--)
        {
            x=all.top();
            here=what[x];
            while(1)
            {
                if((Node[here].con-1)>=(1<<(M-Node[here].deg))-1)
                {
                    if(Node[here].deg==M-1)
                    {
                        here=what[x];
                        how.push_back(what[x]);
                        ans-=x;
                        while(Node[here].here)
                        {
                            Node[here].con--;
                            Node[here].here=max(Node[Node[here].l].here,Node[Node[here].r].here);
                            what[Node[here].here]=here;
                            if(Node[Node[here].l].here>=Node[Node[here].r].here) here=Node[here].l;
                            else here=Node[here].r;
                        }
                        break;
                    }
                    else
                    {
                        if(Node[Node[here].l].here>Node[Node[here].r].here) here=Node[here].l;
                        else here=Node[here].r;
                    }
                }
                else
                {
                    t++;
                    break;
                }
            }
            all.pop();
        }

        printf("%lld\n",ans);
        for(auto i:how) printf("%lld ",i);
        printf("\n");

    }
	return 0;
}


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <queue>
using namespace std;
struct A
{
    long long l,r;
    long long here;
    long long con;
    long long deg;
}Node[400005];
long long what[400005],N,M;
vector < long long > how;
priority_queue < long long , vector < long long > , less < long long > > all;
void build(long long here,long long deg)
{
    Node[here].deg=deg;
    if(Node[here].here==0)
    {
        Node[here].con=0;
        return;
    }
    build(Node[here].l,deg+1);
    build(Node[here].r,deg+1);
    Node[here].con=Node[Node[here].l].con+Node[Node[here].r].con+1;
    //printf("%d %d\n",here,Node[here].con);
}
bool can(long long here)
{
    //printf("%d %d %d\n",here,(Node[here].con-1),(1<<(M-deg)));
    if((Node[here].con-1)>=(1<<(M-Node[here].deg))-1)
    {
        if(Node[here].deg==M-1) return 1;
        else
        {
            if(Node[Node[here].l].here>Node[Node[here].r].here) return can(Node[here].l);
            else return can(Node[here].r);
        }
    }
    //printf("aa %d\n",here);
    return 0;
}
void Delete(long long here)
{
    Node[here].con--;
    if(Node[here].here==0) return ;
    Node[here].here=max(Node[Node[here].l].here,Node[Node[here].r].here);
    what[Node[here].here]=here;
    if(Node[Node[here].l].here>=Node[Node[here].r].here) Delete(Node[here].l);
    else Delete(Node[here].r);
}
int main()
{
	long long T,t,x,i,ans=0;
	scanf("%lld",&T);
	while(T--)
    {
    	ans=0;
    	how.clear();
    	while(!all.empty()) all.pop();
        scanf("%lld %lld",&N,&M);
        for(i=1;i<(1<<N);i++)
        {
            scanf("%lld",&Node[i].here);
            ans+=Node[i].here;
            Node[i].l=2*i;
            Node[i].r=2*i+1;
            Node[Node[i].l].here=0;
            Node[Node[i].r].here=0;
            what[Node[i].here]=i;
            all.push(Node[i].here);

        }
        build(1,0);
        t=(1<<N)-(1<<M);
        while(t--&&!all.empty())
        {

            x=all.top();
            //printf("aa %lld\n",x);
            if(can(what[x]))
            {
                //printf("%lld\n",x);
                how.push_back(what[x]);
                ans-=x;
                Delete(what[x]);
            }
            else t++;
            all.pop();
        }
        printf("%lld\n",ans);
        for(auto i:how) printf("%lld ",i);
        printf("\n");

    }
	return 0;
}


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <queue>
using namespace std;
struct A
{
    int l,r;
    int here;
    int con;
    int deg;
}Node[400005];
int what[400005],N,M;
vector < int > how;
priority_queue < int , vector < int > , less < int > > all;
void build(int here,int deg)
{
    Node[here].deg=deg;
    if(Node[here].here==0)
    {
        Node[here].con=0;
        return;
    }
    build(Node[here].l,deg+1);
    build(Node[here].r,deg+1);
    Node[here].con=Node[Node[here].l].con+Node[Node[here].r].con+1;
    //printf("%d %d\n",here,Node[here].con);
}
bool can(int here)
{
    //printf("%d %d %d\n",here,(Node[here].con-1),(1<<(M-deg)));
    if((Node[here].con-1)>=(1<<(M-Node[here].deg))-1)
    {
        if(Node[here].deg==M-1) return 1;
        else
        {
            if(Node[Node[here].l].here>Node[Node[here].r].here) return can(Node[here].l);
            else return can(Node[here].r);
        }
    }
    //printf("aa %d\n",here);
    return 0;
}
void Delete(int here)
{
    Node[here].con--;
    if(Node[here].here==0) return ;
    Node[here].here=max(Node[Node[here].l].here,Node[Node[here].r].here);
    what[Node[here].here]=here;
    if(Node[Node[here].l].here>=Node[Node[here].r].here) Delete(Node[here].l);
    else Delete(Node[here].r);
}
int main()
{
	int T,t,x,i,ans=0;
	scanf("%d",&T);
	while(T--)
    {
    	ans=0;
    	how.clear();
    	while(!all.empty()) all.pop();
        scanf("%d %d",&N,&M);
        for(i=1;i<(1<<N);i++)
        {

            scanf("%d",&Node[i].here);
            ans+=Node[i].here;
            Node[i].l=2*i;
            Node[i].r=2*i+1;
            Node[Node[i].l].here=0;
            Node[Node[i].r].here=0;
            what[Node[i].here]=i;
            all.push(Node[i].here);

        }
        build(1,0);
        t=(1<<N)-(1<<M);

        while(t--&&!all.empty())
        {

            x=all.top();
            //printf("aa %d\n",x);
            if(can(what[x]))
            {
                //printf("%d\n",x);
                how.push_back(what[x]);
                ans-=x;
                Delete(what[x]);
            }
            else t++;
            all.pop();
        }
        printf("%d\n",ans);
        for(auto i:how) printf("%d ",i);
        printf("\n");

    }
	return 0;
}


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <queue>
#include <algorithm>
using namespace std;
struct A
{
    long long l,r;
    long long here;
    long long con;
    long long deg;
}Node[4000005];
long long what[4000005],N,M;
vector < long long > how;
queue < long long > all;
long long temp[400005];
void build(long long here,long long deg)
{
    Node[here].deg=deg;
    if(Node[here].here==0)
    {
        Node[here].con=0;
        return;
    }
    build(Node[here].l,deg+1);
    build(Node[here].r,deg+1);
    Node[here].con=Node[Node[here].l].con+Node[Node[here].r].con+1;
}
bool can(long long here)
{
    //printf("%d %d %d\n",here,(Node[here].con-1),(1<<(M-deg)));
    if((Node[here].con-1)>=(1<<(M-Node[here].deg))-1)
    {
        if(Node[here].deg==M-1) return 1;
        else
        {
            if(Node[Node[here].l].here>Node[Node[here].r].here) return can(Node[here].l);
            else return can(Node[here].r);
        }
    }
    //printf("aa %d\n",here);
    return 0;
}
void Delete(long long here)
{
    Node[here].con--;
    if(Node[here].here==0) return ;
    Node[here].here=max(Node[Node[here].l].here,Node[Node[here].r].here);
    what[Node[here].here]=here;
    if(Node[Node[here].l].here>=Node[Node[here].r].here) Delete(Node[here].l);
    else Delete(Node[here].r);
}
bool F(long long a,long long b)
{
    return a<b;
}
int main()
{
	long long T,t,x,i,ans=0;
	scanf("%lld",&T);
	while(T--)
    {
    	ans=0;
    	how.clear();
    	while(!all.empty()) all.pop();
        scanf("%lld %lld",&N,&M);
        for(i=1;i<(1<<N);i++)
        {
            scanf("%lld",&Node[i].here);
            temp[i]=Node[i].here;
            ans+=Node[i].here;
            Node[i].l=2*i;
            Node[i].r=2*i+1;
            Node[Node[i].l].here=0;
            Node[Node[i].r].here=0;
            what[Node[i].here]=i;
            all.push(Node[i].here);
        }
        sort(temp+1,temp+(1<<N),F);
        for(i=1;i<(1<<N);i++) all.push(temp[i]);
        build(1,0);
        t=(1<<N)-(1<<M);
        while(t--)
        {
            x=all.front();
            if(can(what[x]))
            {
                how.push_back(what[x]);
                ans-=x;
                Delete(what[x]);
            }
            else t++;
            all.pop();
        }
        printf("%lld\n",ans);
        for(auto i:how) printf("%lld ",i);
        printf("\n");

    }
	return 0;
}


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <queue>
using namespace std;
struct A
{
    long long l,r;
    long long here;
    long long con;
    long long deg;
}Node[4000005];
long long what[4000005],N,M;
vector < long long > how;
priority_queue < long long , vector < long long > , less < long long > > all;
void build(long long here,long long deg)
{
    Node[here].deg=deg;
    if(Node[here].here==0)
    {
        Node[here].con=0;
        return;
    }
    build(Node[here].l,deg+1);
    build(Node[here].r,deg+1);
    Node[here].con=Node[Node[here].l].con+Node[Node[here].r].con+1;
}
bool can(long long here)
{
    if((Node[here].con-1)>=(1<<(M-Node[here].deg))-1)
    {
        if(Node[here].deg==M-1) return 1;
        else
        {
            if(Node[Node[here].l].here>Node[Node[here].r].here) return can(Node[here].l);
            else return can(Node[here].r);
        }
    }
    //printf("aa %d\n",here);
    return 0;
}
void Delete(long long here)
{
    while(Node[here].here)
    {
        Node[here].con--;
        Node[here].here=max(Node[Node[here].l].here,Node[Node[here].r].here);
        what[Node[here].here]=here;
        if(Node[Node[here].l].here>=Node[Node[here].r].here) here=Node[here].l;
        else here=Node[here].r;
    }

}
int main()
{
	long long T,t,x,i,ans=0;
	scanf("%lld",&T);
	while(T--)
    {
    	ans=0;
    	how.clear();
    	while(!all.empty()) all.pop();
        scanf("%lld %lld",&N,&M);
        for(i=1;i<(1<<N);i++)
        {
            scanf("%lld",&Node[i].here);
            ans+=Node[i].here;
            Node[i].l=2*i;
            Node[i].r=2*i+1;
            Node[Node[i].l].here=0;
            Node[Node[i].r].here=0;
            what[Node[i].here]=i;
            all.push(Node[i].here);
        }
        build(1,0);
        t=(1<<N)-(1<<M);
        while(t--)
        {
            x=all.top();
            if(can(what[x]))
            {
                how.push_back(what[x]);
                ans-=x;
                Delete(what[x]);
            }
            else t++;
            all.pop();
        }
        printf("%lld\n",ans);
        for(auto i:how) printf("%lld ",i);
        printf("\n");

    }
	return 0;
}


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <queue>
using namespace std;
struct A
{
    int l,r;
    int here;
    int con;
    int deg;
}Node[400005];
int what[400005],N,M;
vector < int > how;
priority_queue < int , vector < int > , less < int > > all;
void build(int here,int deg)
{
    Node[here].deg=deg;
    if(Node[here].here==0)
    {
        Node[here].con=0;
        return;
    }
    build(Node[here].l,deg+1);
    build(Node[here].r,deg+1);
    Node[here].con=Node[Node[here].l].con+Node[Node[here].r].con+1;
    //printf("%d %d\n",here,Node[here].con);
}
bool can(int here)
{
    //printf("%d %d %d\n",here,(Node[here].con-1),(1<<(M-deg)));
    if((Node[here].con-1)>=(1<<(M-Node[here].deg))-1)
    {
        if(Node[here].deg==M-1) return 1;
        else
        {
            if(Node[Node[here].l].here>Node[Node[here].r].here) return can(Node[here].l);
            else return can(Node[here].r);
        }
    }
    //printf("aa %d\n",here);
    return 0;
}
void Delete(int here)
{
    Node[here].con--;
    if(Node[here].here==0) return ;
    Node[here].here=max(Node[Node[here].l].here,Node[Node[here].r].here);
    what[Node[here].here]=here;
    if(Node[Node[here].l].here>=Node[Node[here].r].here) Delete(Node[here].l);
    else Delete(Node[here].r);
}
int main()
{
	int T,t,x,i,ans=0;
	scanf("%d",&T);
	while(T--)
    {
    	ans=0;
    	how.clear();
        scanf("%d %d",&N,&M);
        for(i=1;i<(1<<N);i++)
        {

            scanf("%d",&Node[i].here);
            ans+=Node[i].here;
            Node[i].l=2*i;
            Node[i].r=2*i+1;
            Node[Node[i].l].here=0;
            Node[Node[i].r].here=0;
            what[Node[i].here]=i;
            all.push(Node[i].here);

        }
        build(1,0);
        t=(1<<N)-(1<<M);
        while(t--&&!all.empty())
        {

            x=all.top();
            //printf("aa %d\n",x);
            if(can(what[x]))
            {
                //printf("%d\n",x);
                how.push_back(what[x]);
                ans-=x;
                Delete(what[x]);
            }
            else t++;
            all.pop();
        }
        printf("%d\n",ans);
        for(auto i:how) printf("%d ",i);
        printf("\n");

    }
	return 0;
}


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <queue>
using namespace std;
struct A
{
    long long l,r;
    long long here;
    long long con;
    long long deg;
}Node[4000005];
long long what[4000005],N,M;
vector < long long > how;
priority_queue < long long , vector < long long > , less < long long > > all;
void build(long long here,long long deg)
{
    Node[here].deg=deg;
    if(deg==N-1)
    {
        Node[here].con=1;
        return ;
    }
    build(Node[here].l,deg+1);
    build(Node[here].r,deg+1);
    Node[here].con=Node[Node[here].l].con+Node[Node[here].r].con+1;
}
int main()
{
	long long T,t,x,i,ans=0,here;
	scanf("%lld",&T);
	while(T--)
    {
    	ans=0;
    	how.clear();
    	while(!all.empty()) all.pop();
        scanf("%lld %lld",&N,&M);
        for(i=1;i<(1<<N);i++)
        {
            scanf("%lld",&Node[i].here);
            ans+=Node[i].here;
            Node[i].l=2*i;
            Node[i].r=2*i+1;
            Node[Node[i].l].here=0;
            Node[Node[i].r].here=0;
            what[Node[i].here]=i;
            all.push(Node[i].here);
        }
        build(1,0);
        t=(1<<N)-(1<<M);
        while(t--)
        {
            x=all.top();
            here=what[x];
            while(1)
            {
                if((Node[here].con-1)>=(1<<(M-Node[here].deg))-1)
                {
                    if(Node[here].deg==M-1)
                    {
                        here=what[x];
                        how.push_back(what[x]);
                        while(Node[here].here)
                        {
                            Node[here].con--;
                            Node[here].here=max(Node[Node[here].l].here,Node[Node[here].r].here);
                            what[Node[here].here]=here;
                            if(Node[Node[here].l].here>=Node[Node[here].r].here) here=Node[here].l;
                            else here=Node[here].r;
                        }
                        ans-=x;
                        break;
                    }
                    else
                    {
                        if(Node[Node[here].l].here>Node[Node[here].r].here) here=Node[here].l;
                        else here=Node[here].r;
                    }
                }
                else
                {
                    t++;
                    break;
                }
            }
            all.pop();
        }
        printf("%lld\n",ans);
        for(auto i:how) printf("%lld ",i);
        printf("\n");

    }
	return 0;
}


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <queue>
using namespace std;
struct A
{
    long long l,r;
    long long here;
    long long con;
    long long deg;
}Node[8000005];
long long what[8000005],N,M;
vector < long long > how;
priority_queue < long long , vector < long long > , less < long long > > all;
void build(long long here,long long deg)
{
    Node[here].deg=deg;
    if(Node[here].here==0)
    {
        Node[here].con=0;
        return;
    }
    build(Node[here].l,deg+1);
    build(Node[here].r,deg+1);
    Node[here].con=Node[Node[here].l].con+Node[Node[here].r].con+1;
}
bool can(long long here)
{
    //printf("%d %d %d\n",here,(Node[here].con-1),(1<<(M-deg)));
    if((Node[here].con-1)>=(1<<(M-Node[here].deg))-1)
    {
        if(Node[here].deg==M-1) return 1;
        else
        {
            if(Node[Node[here].l].here>Node[Node[here].r].here) return can(Node[here].l);
            else return can(Node[here].r);
        }
    }
    //printf("aa %d\n",here);
    return 0;
}
void Delete(long long here)
{
    Node[here].con--;
    if(Node[here].here==0) return ;
    Node[here].here=max(Node[Node[here].l].here,Node[Node[here].r].here);
    what[Node[here].here]=here;
    if(Node[Node[here].l].here>=Node[Node[here].r].here) Delete(Node[here].l);
    else Delete(Node[here].r);
}
int main()
{
	long long T,t,x,i,ans=0;
	scanf("%lld",&T);
	while(T--)
    {
    	ans=0;
    	how.clear();
    	while(!all.empty()) all.pop();
        scanf("%lld %lld",&N,&M);
        for(i=1;i<(1<<N);i++)
        {
            scanf("%lld",&Node[i].here);
            //Node[i].here=(1<<N)-i;
            ans+=Node[i].here;
            Node[i].l=2*i;
            Node[i].r=2*i+1;
            Node[Node[i].l].here=0;
            Node[Node[i].r].here=0;
            what[Node[i].here]=i;
            all.push(Node[i].here);

        }
        build(1,0);
        t=(1<<N)-(1<<M);
        while(t--&&!all.empty())
        {

            x=all.top();
            //printf("aa %lld\n",x);
            if(can(what[x]))
            {
                //printf("%lld\n",x);
                how.push_back(what[x]);
                ans-=x;
                Delete(what[x]);
            }
            else t++;
            all.pop();
        }
        printf("%lld\n",ans);
        for(auto i:how) printf("%lld ",i);
        printf("\n");

    }
	return 0;
}


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <queue>
using namespace std;
struct A
{
    long long l,r;
    long long here;
    long long con;
    long long deg;
}Node[4000005];
long long what[4000005],N,M;
vector < long long > how;
priority_queue < long long , vector < long long > , less < long long > > all;
void build(long long here,long long deg)
{
    Node[here].deg=deg;
    if(deg==N-1)
    {
        Node[here].con=1;
        return ;
    }
    build(Node[here].l,deg+1);
    build(Node[here].r,deg+1);
    Node[here].con=Node[Node[here].l].con+Node[Node[here].r].con+1;
}
bool can(long long here)
{
    while(1)
    {
        if((Node[here].con-1)>=(1<<(M-Node[here].deg))-1)
        {
            if(Node[here].deg==M-1) return 1;
            else
            {
                if(Node[Node[here].l].here>Node[Node[here].r].here) here=Node[here].l;
                else here=Node[here].r;
            }
        }
        else return 0;
    }

}
void Delete(long long here)
{
    while(Node[here].here)
    {
        Node[here].con--;
        Node[here].here=max(Node[Node[here].l].here,Node[Node[here].r].here);
        what[Node[here].here]=here;
        if(Node[Node[here].l].here>=Node[Node[here].r].here) here=Node[here].l;
        else here=Node[here].r;
    }

}
int main()
{
	long long T,t,x,i,ans=0;
	scanf("%lld",&T);
	while(T--)
    {
    	ans=0;
    	how.clear();
    	while(!all.empty()) all.pop();
        scanf("%lld %lld",&N,&M);
        for(i=1;i<(1<<N);i++)
        {
            scanf("%lld",&Node[i].here);
            ans+=Node[i].here;
            Node[i].l=2*i;
            Node[i].r=2*i+1;
            Node[Node[i].l].here=0;
            Node[Node[i].r].here=0;
            what[Node[i].here]=i;
            all.push(Node[i].here);
        }
        build(1,0);
        t=(1<<N)-(1<<M);
        while(t--)
        {
            x=all.top();
            if(can(what[x]))
            {
                how.push_back(what[x]);
                ans-=x;
                Delete(what[x]);
            }
            else t++;
            all.pop();
        }
        printf("%lld\n",ans);
        for(auto i:how) printf("%lld ",i);
        printf("\n");

    }
	return 0;
}


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <queue>
using namespace std;
struct A
{
    long long l,r;
    long long here;
    long long con;
    long long deg;
}Node[4000005];
long long what[4000005],N,M;
vector < long long > how;
priority_queue < long long , vector < long long > , less < long long > > all;
void build(long long here,long long deg)
{
    Node[here].deg=deg;
    if(Node[here].here==0)
    {
        Node[here].con=0;
        return;
    }
    build(Node[here].l,deg+1);
    build(Node[here].r,deg+1);
    Node[here].con=Node[Node[here].l].con+Node[Node[here].r].con+1;
}
bool can(long long here)
{
    while(1)
    {
        if((Node[here].con-1)>=(1<<(M-Node[here].deg))-1)
        {
            if(Node[here].deg==M-1) return 1;
            else
            {
                if(Node[Node[here].l].here>Node[Node[here].r].here) here=Node[here].l;
                else here=Node[here].r;
            }
        }
        else return 0;
    }

}
void Delete(long long here)
{
    while(Node[here].here)
    {
        Node[here].con--;
        Node[here].here=max(Node[Node[here].l].here,Node[Node[here].r].here);
        what[Node[here].here]=here;
        if(Node[Node[here].l].here>=Node[Node[here].r].here) here=Node[here].l;
        else here=Node[here].r;
    }

}
int main()
{
	long long T,t,x,i,ans=0;
	scanf("%lld",&T);
	while(T--)
    {
    	ans=0;
    	how.clear();
    	while(!all.empty()) all.pop();
        scanf("%lld %lld",&N,&M);
        for(i=1;i<(1<<N);i++)
        {
            scanf("%lld",&Node[i].here);
            ans+=Node[i].here;
            Node[i].l=2*i;
            Node[i].r=2*i+1;
            Node[Node[i].l].here=0;
            Node[Node[i].r].here=0;
            what[Node[i].here]=i;
            all.push(Node[i].here);
        }
        build(1,0);
        t=(1<<N)-(1<<M);
        while(t--)
        {
            x=all.top();
            if(can(what[x]))
            {
                how.push_back(what[x]);
                ans-=x;
                Delete(what[x]);
            }
            else t++;
            all.pop();
        }
        printf("%lld\n",ans);
        for(auto i:how) printf("%lld ",i);
        printf("\n");

    }
	return 0;
}


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <queue>
using namespace std;
struct A
{
    long long here;
    long long con;
    long long deg;
}Node[4000005];
long long what[4000005],N,M;
vector < long long > how;
priority_queue < long long , vector < long long > , less < long long > > all;
void build(long long here,long long deg)
{
    Node[here].deg=deg;
    if(deg==N-1)
    {
        Node[here].con=1;
        return ;
    }
    build(2*here,deg+1);
    build(2*here+1,deg+1);
    Node[here].con=Node[2*here].con+Node[2*here].con+1;
}
int main()
{
	long long T,t,x,i,ans=0,here;
	scanf("%lld",&T);
	while(T--)
    {
    	ans=0;
    	how.clear();
    	while(!all.empty()) all.pop();
        scanf("%lld %lld",&N,&M);
        for(i=1;i<(1<<N);i++)
        {
            scanf("%lld",&Node[i].here);
            ans+=Node[i].here;
            Node[2*i].here=0;
            Node[2*i+1].here=0;
            what[Node[i].here]=i;
            all.push(Node[i].here);
        }
        build(1,0);
        t=(1<<N)-(1<<M);
        while(t--)
        {
            x=all.top();
            all.pop();
            here=what[x];
            while(1)
            {
                if((Node[here].con-1)>=(1<<(M-Node[here].deg))-1)
                {
                    if(Node[here].deg==M-1)
                    {
                        here=what[x];
                        how.push_back(what[x]);
                        ans-=x;
                        while(Node[here].here)
                        {
                            Node[here].con--;
                            Node[here].here=max(Node[2*here].here,Node[2*here+1].here);
                            what[Node[here].here]=here;
                            if(Node[2*here].here>=Node[2*here+1].here) here=2*here;
                            else here=2*here+1;
                        }
                        break;
                    }
                    else
                    {
                        if(Node[2*here].here>Node[2*here+1].here) here=2*here;
                        else here=2*here+1;
                    }
                }
                else
                {
                    t++;
                    break;
                }
            }
        }
        printf("%lld\n",ans);
        for(auto i:how) printf("%lld ",i);
        printf("\n");

    }
	return 0;
}


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <queue>
using namespace std;
struct A
{
    long long l,r;
    long long here;
    long long con;
    long long deg;
}Node[4000005];
long long what[4000005],N,M;
vector < long long > how;
priority_queue < long long , vector < long long > , less < long long > > all;
void build(long long here,long long deg)
{
    Node[here].deg=deg;
    if(Node[here].here==0)
    {
        Node[here].con=0;
        return;
    }
    build(Node[here].l,deg+1);
    build(Node[here].r,deg+1);
    Node[here].con=Node[Node[here].l].con+Node[Node[here].r].con+1;
}
bool can(long long here)
{
    //printf("%d %d %d\n",here,(Node[here].con-1),(1<<(M-deg)));
    if((Node[here].con-1)>=(1<<(M-Node[here].deg))-1)
    {
        if(Node[here].deg==M-1) return 1;
        else
        {
            if(Node[Node[here].l].here>Node[Node[here].r].here) return can(Node[here].l);
            else return can(Node[here].r);
        }
    }
    //printf("aa %d\n",here);
    return 0;
}
void Delete(long long here)
{
    Node[here].con--;
    if(Node[here].here==0) return ;
    Node[here].here=max(Node[Node[here].l].here,Node[Node[here].r].here);
    what[Node[here].here]=here;
    if(Node[Node[here].l].here>=Node[Node[here].r].here) Delete(Node[here].l);
    else Delete(Node[here].r);
}
int main()
{
	long long T,t,x,i,ans=0;
	scanf("%lld",&T);
	while(T--)
    {
    	ans=0;
    	how.clear();
    	while(!all.empty()) all.pop();
        scanf("%lld %lld",&N,&M);
        for(i=1;i<(1<<N);i++)
        {
            scanf("%lld",&Node[i].here);
            ans+=Node[i].here;
            Node[i].l=2*i;
            Node[i].r=2*i+1;
            Node[Node[i].l].here=0;
            Node[Node[i].r].here=0;
            what[Node[i].here]=i;
            all.push(Node[i].here);
        }
        build(1,0);
        t=(1<<N)-(1<<M);
        while(t--)
        {
            x=all.top();
            if(can(what[x]))
            {
                how.push_back(what[x]);
                ans-=x;
                Delete(what[x]);
            }
            else t++;
            all.pop();
        }
        printf("%lld\n",ans);
        for(auto i:how) printf("%lld ",i);
        printf("\n");

    }
	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 ll          long long
#define pb          push_back
#define vi          vector<ll int>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define rs(v,n)     v.resize(n)
#define hell        1000000007
#define peak        9123372036854775807
#define pii         acos(-1)
#define clr(a,x)    memset(a,x,sizeof(a))
#define ios         ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
template<typename T>
using ordered_set = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
template<class x,class y> x sum(x a,y b){return a+b;}
template<class x,class y> x mul(x a,y b){return a*b;}
template<class x,class y> x sub(x a,y b){return a-b;}
template<class x,class y> x divi(x a,y b){return a/b;}

template<class x,class y>
istream &operator>>(istream &in,pair<x,y> &p)
{
    in>>p.F>>p.S;
    return in;
}
template<class x>
istream &operator>>(istream &in,vector<x> &v)
{
    for(auto& i:v)
        in>>i;
    return in;
}
template<class x,class y>
ostream &operator<<(ostream &out,pair<x,y> &p)
{
    out<<"("<<p.F<<","<<p.S<<") ";
    return out;
}
template<class x>
ostream &operator<<(ostream &out,vector<x> &v)
{
    out<<v.size()<<endl;
    for(auto i:v)
        out<<i<<" ";
    out<<endl;
    return out;
}
#define N  4000005

vi v[N],rem(N,0),pos(N,0),he(N,0);
ll n,m;
ll check(ll x)
{
	// cout<<n<<" "<<x<<" "<<rem[x]<<endl;
	if(x>n)
		return 1;
	if(rem[x]==0)
		return 0;
	if(he[2*x]>he[2*x+1])
	{
		// cout<<1111<<endl;
		return check(2*x);
	}
	if(he[2*x+1]>he[2*x])
		return check(2*x+1);
	return 1;
}
ll remove(ll x)
{
	rem[x]--;
	if(he[2*x+1]==0&&he[2*x]==0)
	{
		he[x]=0;
		return 0;
	}
	if(he[2*x]>he[2*x+1])
	{
		he[x]=he[2*x];
		pos[he[2*x]]=x;
		remove(2*x);
		return 0;
	}
	if(he[2*x+1]>he[2*x])
	{
		he[x]=he[2*x+1];
		pos[he[2*x+1]]=x;
		remove(2*x+1);
	}
	return 0;
}

int main()
{
    ios;
    ll tt=1;
    cin>>tt;
    while(tt--)
    {
    	ll i,j,k,l;
    	ll h,g;
    	cin>>h>>g;
    	n=(1ll<<h)-1;
    	for(i=1;i<=2*n+3;i++)
    	{
    		v[i].clear();
    		rem[i]=0;
    		pos[i]=0;
    		he[i]=0;
    	}
    	ll sum=0;
    	// cout<<1<<n<<endl;
    	for(i=1;i<=n;i++)
    	{
    		cin>>l;
    		// l=n-i+1;
    		sum+=l;
    		pos[l]=i;
    		he[i]=l;
    	}
    	vi ans;
    	m=(1<<g)-1;
    	ll y=n-m;
    	for(i=1;i<=n;i++)
    	{
    		ll de=log2(i)+1;
    		ll yy=(1ll<<(de-1));
    		rem[i]=y/yy;
    	}
    	ll xx=0;
    	for(i=n;i>=1;i--)
    	{
    		if(check(pos[i]))
    		{
    			xx++;
    			sum-=i;
    			ans.pb(pos[i]);
    			remove(pos[i]);
    		}
    		// cout<<rem;
    	}
    	// cout<<n-xx<<endl;
    	cout<<sum<<endl;
    	for(auto i:ans)
    		cout<<i<<" ";
    	cout<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse2")

#define ll          int
#define pb          push_back
#define vi          vector<int>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define rs(v,n)     v.resize(n)
#define hell        1000000007
#define peak        9123372036854775807
#define pii         acos(-1)
#define clr(a,x)    memset(a,x,sizeof(a))
#define ios         ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
template<typename T>
using ordered_set = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
template<class x,class y> x sum(x a,y b){return a+b;}
template<class x,class y> x mul(x a,y b){return a*b;}
template<class x,class y> x sub(x a,y b){return a-b;}
template<class x,class y> x divi(x a,y b){return a/b;}

template<class x,class y>
istream &operator>>(istream &in,pair<x,y> &p)
{
    in>>p.F>>p.S;
    return in;
}
template<class x>
istream &operator>>(istream &in,vector<x> &v)
{
    for(auto& i:v)
        in>>i;
    return in;
}
template<class x,class y>
ostream &operator<<(ostream &out,pair<x,y> &p)
{
    out<<"("<<p.F<<","<<p.S<<") ";
    return out;
}
template<class x>
ostream &operator<<(ostream &out,vector<x> &v)
{
    out<<v.size()<<endl;
    for(auto i:v)
        out<<i<<" ";
    out<<endl;
    return out;
}
#define N  4000005

vi rem,he;
vi pos;
ll n,m,counti=0;
ll check(ll x)
{
	// cout<<n<<" "<<x<<" "<<rem[x]<<endl;
	if(2*x>n)
		return 1;
	if(rem[x]==0)
		return 0;
	if(he[2*x]>he[2*x+1])
	{
		// cout<<1111<<endl;
		return check(2*x);
	}
	if(he[2*x+1]>he[2*x])
		return check(2*x+1);
	return 1;
}
ll remove(ll x)
{
	rem[x]--;
	if(2*x>n||(he[2*x+1]==0&&he[2*x]==0))
	{
		he[x]=0;
		return 0;
	}
	if(he[2*x]>he[2*x+1])
	{
		he[x]=he[2*x];
		pos[he[2*x]]=x;
		remove(2*x);
		return 0;
	}
	if(he[2*x+1]>he[2*x])
	{
		he[x]=he[2*x+1];
		pos[he[2*x+1]]=x;
		remove(2*x+1);
	}
	return 0;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    ll tt=1;
    cin>>tt;
    while(tt--)
    {
    	ll i,j,k,l;
    	ll h,g;
    	cin>>h>>g;
    	n=(1ll<<h)-1;
    	vector<pair<ll,ll>> vv;
    	vv.pb({0,0});
    	rem.clear();
    	he.clear();
    	he.pb(0);
    	ll sum=0;
    	// cout<<1<<n<<endl;
    	// map<ll,ll> pos;
    	for(i=1;i<=n;i++)
    	{
    		cin>>l;
    		// l=n+5-i;
    		vv.pb({l,i});
    		// l=n-i+1;
    		sum+=l;
    		he.pb(l);
    		// ma[l]=1;
    	}
    	sort(all(vv));
    	pos.clear();
    	pos.pb(0);
    	for(i=1;i<=n;i++)
    	{
    		pos.pb(vv[i].S);
    		he[vv[i].S]=vv[i].F;
    	}
    	vi ans;
    	m=(1ll<<g)-1;
    	ll y=n-m;
    	rem.pb(0);
    	for(i=1;i<=n;i++)
    	{
    		ll de=log2(i)+1;
    		ll yy=(1ll<<(de-1));
    		rem.pb(y/yy);
    	}
    	// cout<<rem;
    	ll xx=0;
    	// remove(1);
    	for(i=n;i>=1;i--)
    	{
    		ll x=vv[i].F;
    		ll y=pos[i];
    		// cout<<x<<endl;
    		if(check(y))
    		{
    			xx++;
    			sum-=x;
    			ans.pb(y);
    			remove(y);
    		}
    		// cout<<rem;
    	}
    	// cout<<n-xx<<endl;
    	cout<<sum<<endl;
    	for(auto i:ans)
    		cout<<i<<" ";
    	cout<<endl;
    	// cout<<counti;a
    }
    return 0;
}


#include<bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse2")

#define ll          int
#define pb          push_back
#define vi          vector<int>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define rs(v,n)     v.resize(n)
#define hell        1000000007
#define peak        9123372036854775807
#define pii         acos(-1)
#define clr(a,x)    memset(a,x,sizeof(a))
#define ios         ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
template<typename T>
using ordered_set = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
template<class x,class y> x sum(x a,y b){return a+b;}
template<class x,class y> x mul(x a,y b){return a*b;}
template<class x,class y> x sub(x a,y b){return a-b;}
template<class x,class y> x divi(x a,y b){return a/b;}

template<class x,class y>
istream &operator>>(istream &in,pair<x,y> &p)
{
    in>>p.F>>p.S;
    return in;
}
template<class x>
istream &operator>>(istream &in,vector<x> &v)
{
    for(auto& i:v)
        in>>i;
    return in;
}
template<class x,class y>
ostream &operator<<(ostream &out,pair<x,y> &p)
{
    out<<"("<<p.F<<","<<p.S<<") ";
    return out;
}
template<class x>
ostream &operator<<(ostream &out,vector<x> &v)
{
    out<<v.size()<<endl;
    for(auto i:v)
        out<<i<<" ";
    out<<endl;
    return out;
}
#define N  4000005

vi rem,he;
vi pos;
ll n,m,counti=0;
ll check(ll x)
{
	// cout<<n<<" "<<x<<" "<<rem[x]<<endl;
	if(2*x>n)
		return 1;
	if(rem[x]==0)
		return 0;
	if(he[2*x]>he[2*x+1])
	{
		// cout<<1111<<endl;
		return check(2*x);
	}
	if(he[2*x+1]>he[2*x])
		return check(2*x+1);
	return 1;
}
ll remove(ll x)
{
	rem[x]--;
	if(2*x>n||(he[2*x+1]==0&&he[2*x]==0))
	{
		he[x]=0;
		return 0;
	}
	if(he[2*x]>he[2*x+1])
	{
		he[x]=he[2*x];
		pos[he[2*x]]=x;
		remove(2*x);
		return 0;
	}
	if(he[2*x+1]>he[2*x])
	{
		he[x]=he[2*x+1];
		pos[he[2*x+1]]=x;
		remove(2*x+1);
	}
	return 0;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    ll tt=1;
    cin>>tt;
    while(tt--)
    {
    	ll i,j,k,l;
    	ll h,g;
    	cin>>h>>g;
    	n=(1ll<<h)-1;
    	vector<pair<ll,ll>> vv;
    	vv.pb({0,0});
    	rem.clear();
    	he.clear();
    	he.pb(0);
    	ll sum=0;
    	// cout<<1<<n<<endl;
    	// map<ll,ll> pos;
    	for(i=1;i<=n;i++)
    	{
    		cin>>l;
    		// l=n+5-i;
    		vv.pb({l,i});
    		// l=n-i+1;
    		sum+=l;
    		he.pb(l);
    		// ma[l]=1;
    	}
    	sort(all(vv));
    	pos.clear();
    	pos.pb(0);
    	for(i=1;i<=n;i++)
    	{
    		pos.pb(vv[i].S);
    		he[vv[i].S]=i;
    	}
    	vi ans;
    	m=(1ll<<g)-1;
    	ll y=n-m;
    	rem.pb(0);
    	for(i=1;i<=n;i++)
    	{
    		ll de=log2(i)+1;
    		ll yy=(1ll<<(de-1));
    		rem.pb(y/yy);
    	}
    	// cout<<rem;
    	ll xx=0;
    	// remove(1);
    	for(i=n;i>=1;i--)
    	{
    		if(xx>n-m)
    			break;
    		ll x=vv[i].F;
    		ll y=pos[i];
    		// cout<<x<<endl;
    		if(check(y))
    		{
    			xx++;
    			sum-=x;
    			ans.pb(y);
    			remove(y);
    		}
    		// cout<<rem;
    	}
    	// cout<<n-xx<<endl;
    	cout<<sum<<endl;
    	for(auto i:ans)
    		cout<<i<<" ";
    	cout<<endl;
    	// cout<<counti;a
    }
    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 ll          long long
#define pb          push_back
#define vi          vector<ll int>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define rs(v,n)     v.resize(n)
#define hell        1000000007
#define peak        9123372036854775807
#define pii         acos(-1)
#define clr(a,x)    memset(a,x,sizeof(a))
#define ios         ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
template<typename T>
using ordered_set = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
template<class x,class y> x sum(x a,y b){return a+b;}
template<class x,class y> x mul(x a,y b){return a*b;}
template<class x,class y> x sub(x a,y b){return a-b;}
template<class x,class y> x divi(x a,y b){return a/b;}

template<class x,class y>
istream &operator>>(istream &in,pair<x,y> &p)
{
    in>>p.F>>p.S;
    return in;
}
template<class x>
istream &operator>>(istream &in,vector<x> &v)
{
    for(auto& i:v)
        in>>i;
    return in;
}
template<class x,class y>
ostream &operator<<(ostream &out,pair<x,y> &p)
{
    out<<"("<<p.F<<","<<p.S<<") ";
    return out;
}
template<class x>
ostream &operator<<(ostream &out,vector<x> &v)
{
    out<<v.size()<<endl;
    for(auto i:v)
        out<<i<<" ";
    out<<endl;
    return out;
}
#define N  4000005

vi rem(N,0),he(N,0);
map<ll,ll> pos;
ll n,m;
ll check(ll x)
{
	// cout<<n<<" "<<x<<" "<<rem[x]<<endl;
	if(x>n)
		return 1;
	if(rem[x]==0)
		return 0;
	if(he[2*x]>he[2*x+1])
	{
		// cout<<1111<<endl;
		return check(2*x);
	}
	if(he[2*x+1]>he[2*x])
		return check(2*x+1);
	return 1;
}
ll remove(ll x)
{
	rem[x]--;
	if(he[2*x+1]==0&&he[2*x]==0)
	{
		he[x]=0;
		return 0;
	}
	if(he[2*x]>he[2*x+1])
	{
		he[x]=he[2*x];
		pos[he[2*x]]=x;
		remove(2*x);
		return 0;
	}
	if(he[2*x+1]>he[2*x])
	{
		he[x]=he[2*x+1];
		pos[he[2*x+1]]=x;
		remove(2*x+1);
	}
	return 0;
}

int main()
{
    ios;
    ll tt=1;
    cin>>tt;
    while(tt--)
    {
    	ll i,j,k,l;
    	ll h,g;
    	cin>>h>>g;
    	n=(1ll<<h)-1;
    	vi vv;
    	for(i=1;i<=2*n+3;i++)
    	{
    		rem[i]=0;
    		// pos[i]=0;
    		he[i]=0;
    	}
    	ll sum=0;
    	// cout<<1<<n<<endl;
    	// map<ll,ll> pos;
    	pos.clear();
    	for(i=1;i<=n;i++)
    	{
    		cin>>l;
    		vv.pb(l);
    		// l=n-i+1;
    		sum+=l;
    		pos[l]=i;
    		he[i]=l;
    		// ma[l]=1;
    	}
    	sort(all(vv));
    	vi ans;
    	m=(1ll<<g)-1;
    	ll y=n-m;
    	for(i=1;i<=n;i++)
    	{
    		ll de=log2(i)+1;
    		ll yy=(1ll<<(de-1));
    		rem[i]=y/yy;
    	}
    	// cout<<rem;
    	ll xx=0;
    	for(i=n-1;i>=0;i--)
    	{
    		ll x=vv[i];
    		// cout<<x<<endl;
    		if(check(pos[x]))
    		{
    			xx++;
    			sum-=x;
    			ans.pb(pos[x]);
    			remove(pos[x]);
    		}
    		// cout<<rem;
    	}
    	// cout<<n-xx<<endl;
    	cout<<sum<<endl;
    	for(auto i:ans)
    		cout<<i<<" ";
    	cout<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse2")

#define ll          int
#define pb          push_back
#define vi          vector<int>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define rs(v,n)     v.resize(n)
#define hell        1000000007
#define peak        9123372036854775807
#define pii         acos(-1)
#define clr(a,x)    memset(a,x,sizeof(a))
#define ios         ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
template<typename T>
using ordered_set = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
template<class x,class y> x sum(x a,y b){return a+b;}
template<class x,class y> x mul(x a,y b){return a*b;}
template<class x,class y> x sub(x a,y b){return a-b;}
template<class x,class y> x divi(x a,y b){return a/b;}

template<class x,class y>
istream &operator>>(istream &in,pair<x,y> &p)
{
    in>>p.F>>p.S;
    return in;
}
template<class x>
istream &operator>>(istream &in,vector<x> &v)
{
    for(auto& i:v)
        in>>i;
    return in;
}
template<class x,class y>
ostream &operator<<(ostream &out,pair<x,y> &p)
{
    out<<"("<<p.F<<","<<p.S<<") ";
    return out;
}
template<class x>
ostream &operator<<(ostream &out,vector<x> &v)
{
    out<<v.size()<<endl;
    for(auto i:v)
        out<<i<<" ";
    out<<endl;
    return out;
}
#define N  4000005

vi rem,he;
unordered_map<ll,ll> pos;
ll n,m,counti=0;
ll check(ll x)
{
	// cout<<n<<" "<<x<<" "<<rem[x]<<endl;
	if(2*x>n)
		return 1;
	if(rem[x]==0)
		return 0;
	if(he[2*x]>he[2*x+1])
	{
		// cout<<1111<<endl;
		return check(2*x);
	}
	if(he[2*x+1]>he[2*x])
		return check(2*x+1);
	return 1;
}
ll remove(ll x)
{
	rem[x]--;
	if(2*x>n||(he[2*x+1]==0&&he[2*x]==0))
	{
		he[x]=0;
		return 0;
	}
	if(he[2*x]>he[2*x+1])
	{
		he[x]=he[2*x];
		pos[he[2*x]]=x;
		remove(2*x);
		return 0;
	}
	if(he[2*x+1]>he[2*x])
	{
		he[x]=he[2*x+1];
		pos[he[2*x+1]]=x;
		remove(2*x+1);
	}
	return 0;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    ll tt=1;
    cin>>tt;
    while(tt--)
    {
    	ll i,j,k,l;
    	ll h,g;
    	cin>>h>>g;
    	n=(1ll<<h)-1;
    	vi vv;
    	rem.clear();
    	he.clear();
    	he.pb(0);
    	ll sum=0;
    	// cout<<1<<n<<endl;
    	// map<ll,ll> pos;
    	pos.clear();
    	for(i=1;i<=n;i++)
    	{
    		cin>>l;
    		// l=n+5-i;
    		vv.pb(l);
    		// l=n-i+1;
    		sum+=l;
    		pos[l]=i;
    		he.pb(l);
    		// ma[l]=1;
    	}
    	sort(all(vv));
    	vi ans;
    	m=(1ll<<g)-1;
    	ll y=n-m;
    	rem.pb(0);
    	for(i=1;i<=n;i++)
    	{
    		ll de=log2(i)+1;
    		ll yy=(1ll<<(de-1));
    		rem.pb(y/yy);
    	}
    	// cout<<rem;
    	ll xx=0;
    	// remove(1);
    	for(i=n-1;i>=0;i--)
    	{
    		ll x=vv[i];
    		ll y=pos[x];
    		// cout<<x<<endl;
    		if(check(y))
    		{
    			xx++;
    			sum-=x;
    			ans.pb(y);
    			remove(y);
    		}
    		// cout<<rem;
    	}
    	// cout<<n-xx<<endl;
    	cout<<sum<<endl;
    	for(auto i:ans)
    		cout<<i<<" ";
    	cout<<endl;
    	// cout<<counti;a
    }
    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 ll          long long
#define pb          push_back
#define vi          vector<ll int>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define rs(v,n)     v.resize(n)
#define hell        1000000007
#define peak        9123372036854775807
#define pii         acos(-1)
#define clr(a,x)    memset(a,x,sizeof(a))
#define ios         ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
template<typename T>
using ordered_set = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
template<class x,class y> x sum(x a,y b){return a+b;}
template<class x,class y> x mul(x a,y b){return a*b;}
template<class x,class y> x sub(x a,y b){return a-b;}
template<class x,class y> x divi(x a,y b){return a/b;}

template<class x,class y>
istream &operator>>(istream &in,pair<x,y> &p)
{
    in>>p.F>>p.S;
    return in;
}
template<class x>
istream &operator>>(istream &in,vector<x> &v)
{
    for(auto& i:v)
        in>>i;
    return in;
}
template<class x,class y>
ostream &operator<<(ostream &out,pair<x,y> &p)
{
    out<<"("<<p.F<<","<<p.S<<") ";
    return out;
}
template<class x>
ostream &operator<<(ostream &out,vector<x> &v)
{
    out<<v.size()<<endl;
    for(auto i:v)
        out<<i<<" ";
    out<<endl;
    return out;
}
#define N  4000005

vi v[N],rem(N,0),pos(N,0),he(N,0);
ll n,m;
ll check(ll x)
{
	// cout<<n<<" "<<x<<" "<<rem[x]<<endl;
	if(x>n)
		return 1;
	if(rem[x]==0)
		return 0;
	if(he[2*x]>he[2*x+1])
	{
		// cout<<1111<<endl;
		return check(2*x);
	}
	if(he[2*x+1]>he[2*x])
		return check(2*x+1);
	return 1;
}
ll remove(ll x)
{
	rem[x]--;
	if(he[2*x+1]==0&&he[2*x]==0)
	{
		he[x]=0;
		return 0;
	}
	if(he[2*x]>he[2*x+1])
	{
		he[x]=he[2*x];
		pos[he[2*x]]=x;
		remove(2*x);
		return 0;
	}
	if(he[2*x+1]>he[2*x])
	{
		he[x]=he[2*x+1];
		pos[he[2*x+1]]=x;
		remove(2*x+1);
	}
	return 0;
}

int main()
{
    ios;
    ll tt=1;
    cin>>tt;
    while(tt--)
    {
    	ll i,j,k,l;
    	ll h,g;
    	cin>>h>>g;
    	n=(1ll<<h)-1;
    	for(i=1;i<=n;i++)
    	{
    		v[i].clear();
    		rem[i]=0;
    	}
    	ll sum=0;
    	// cout<<1<<n<<endl;
    	for(i=1;i<=n;i++)
    	{
    		cin>>l;
    		// l=n-i+1;
    		sum+=l;
    		pos[l]=i;
    		he[i]=l;
    	}
    	vi ans;
    	m=(1<<g)-1;
    	ll y=n-m;
    	for(i=1;i<=n;i++)
    	{
    		ll de=log2(i)+1;
    		ll yy=(1ll<<(de-1));
    		rem[i]=y/yy;
    	}
    	ll xx=0;
    	for(i=n;i>=1;i--)
    	{
    		if(check(pos[i]))
    		{
    			xx++;
    			sum-=i;
    			ans.pb(pos[i]);
    			remove(pos[i]);
    		}
    		// cout<<rem;
    	}
    	// cout<<n-xx<<endl;
    	cout<<sum<<endl;
    	for(auto i:ans)
    		cout<<i<<" ";
    	cout<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse2")

#define ll          long long
#define pb          push_back
#define vi          vector<ll int>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define rs(v,n)     v.resize(n)
#define hell        1000000007
#define peak        9123372036854775807
#define pii         acos(-1)
#define clr(a,x)    memset(a,x,sizeof(a))
#define ios         ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
template<typename T>
using ordered_set = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
template<class x,class y> x sum(x a,y b){return a+b;}
template<class x,class y> x mul(x a,y b){return a*b;}
template<class x,class y> x sub(x a,y b){return a-b;}
template<class x,class y> x divi(x a,y b){return a/b;}

template<class x,class y>
istream &operator>>(istream &in,pair<x,y> &p)
{
    in>>p.F>>p.S;
    return in;
}
template<class x>
istream &operator>>(istream &in,vector<x> &v)
{
    for(auto& i:v)
        in>>i;
    return in;
}
template<class x,class y>
ostream &operator<<(ostream &out,pair<x,y> &p)
{
    out<<"("<<p.F<<","<<p.S<<") ";
    return out;
}
template<class x>
ostream &operator<<(ostream &out,vector<x> &v)
{
    out<<v.size()<<endl;
    for(auto i:v)
        out<<i<<" ";
    out<<endl;
    return out;
}
#define N  4000005

vi rem,he;
vi pos;
ll n,m,counti=0;
ll check(ll x)
{
	// cout<<n<<" "<<x<<" "<<rem[x]<<endl;
	if(2*x>n)
		return 1;
	if(rem[x]==0)
		return 0;
	if(he[2*x]>he[2*x+1])
	{
		// cout<<1111<<endl;
		return check(2*x);
	}
	if(he[2*x+1]>he[2*x])
		return check(2*x+1);
	return 1;
}
ll remove(ll x)
{
	rem[x]--;
	if(2*x>n||(he[2*x+1]==0&&he[2*x]==0))
	{
		he[x]=0;
		return 0;
	}
	if(he[2*x]>he[2*x+1])
	{
		he[x]=he[2*x];
		pos[he[2*x]]=x;
		remove(2*x);
		return 0;
	}
	if(he[2*x+1]>he[2*x])
	{
		he[x]=he[2*x+1];
		pos[he[2*x+1]]=x;
		remove(2*x+1);
	}
	return 0;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    ll tt=1;
    cin>>tt;
    while(tt--)
    {
    	ll i,j,k,l;
    	ll h,g;
    	cin>>h>>g;
    	n=(1ll<<h)-1;
    	vector<pair<ll,ll>> vv;
    	vv.pb({0,0});
    	rem.clear();
    	he.clear();
    	he.pb(0);
    	long long sum=0;
    	// cout<<1<<n<<endl;
    	// map<ll,ll> pos;
    	for(i=1;i<=n;i++)
    	{
    		cin>>l;
    		// l=n-i+1;
    		vv.pb({l,i});
    		// l=n-i+1;
    		sum+=l;
    		he.pb(l);
    		// ma[l]=1;
    	}
    	sort(all(vv));
    	pos.clear();
    	pos.pb(0);
    	for(i=1;i<=n;i++)
    	{
    		pos.pb(vv[i].S);
    		he[vv[i].S]=i;
    	}
    	vi ans;
    	m=(1ll<<g)-1;
    	ll y=n-m;
    	rem.pb(0);
    	for(i=1;i<=n;i++)
    	{
    		ll de=log2(i)+1;
    		ll yy=(1ll<<(de-1));
    		rem.pb(y/yy);
    	}
    	// cout<<rem;
    	ll xx=0;
    	// remove(1);
    	for(i=n;i>=1;i--)
    	{
    		if(xx>n-m)
    			break;
    		ll x=vv[i].F;
    		ll y=pos[i];
    		// cout<<x<<endl;
    		if(check(y))
    		{
    			xx++;
    			sum-=x;
    			ans.pb(y);
    			remove(y);
    		}
    		// cout<<rem;
    	}
    	// cout<<n-xx<<endl;
    	cout<<sum<<endl;
    	for(auto i:ans)
    		cout<<i<<" ";
    	cout<<endl;
    	// cout<<sum<<endl;
    	// cout<<counti;a
    }
    return 0;
}


#include<bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse2")

#define ll          int
#define pb          push_back
#define vi          vector<int>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define rs(v,n)     v.resize(n)
#define hell        1000000007
#define peak        9123372036854775807
#define pii         acos(-1)
#define clr(a,x)    memset(a,x,sizeof(a))
#define ios         ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
template<typename T>
using ordered_set = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
template<class x,class y> x sum(x a,y b){return a+b;}
template<class x,class y> x mul(x a,y b){return a*b;}
template<class x,class y> x sub(x a,y b){return a-b;}
template<class x,class y> x divi(x a,y b){return a/b;}

template<class x,class y>
istream &operator>>(istream &in,pair<x,y> &p)
{
    in>>p.F>>p.S;
    return in;
}
template<class x>
istream &operator>>(istream &in,vector<x> &v)
{
    for(auto& i:v)
        in>>i;
    return in;
}
template<class x,class y>
ostream &operator<<(ostream &out,pair<x,y> &p)
{
    out<<"("<<p.F<<","<<p.S<<") ";
    return out;
}
template<class x>
ostream &operator<<(ostream &out,vector<x> &v)
{
    out<<v.size()<<endl;
    for(auto i:v)
        out<<i<<" ";
    out<<endl;
    return out;
}
#define N  4000005

vi rem,he;
vi pos;
ll n,m,counti=0;
ll check(ll x)
{
	// cout<<n<<" "<<x<<" "<<rem[x]<<endl;
	if(2*x>n)
		return 1;
	if(rem[x]==0)
		return 0;
	if(he[2*x]>he[2*x+1])
	{
		// cout<<1111<<endl;
		return check(2*x);
	}
	if(he[2*x+1]>he[2*x])
		return check(2*x+1);
	return 1;
}
ll remove(ll x)
{
	rem[x]--;
	if(2*x>n||(he[2*x+1]==0&&he[2*x]==0))
	{
		he[x]=0;
		return 0;
	}
	if(he[2*x]>he[2*x+1])
	{
		he[x]=he[2*x];
		pos[he[2*x]]=x;
		remove(2*x);
		return 0;
	}
	if(he[2*x+1]>he[2*x])
	{
		he[x]=he[2*x+1];
		pos[he[2*x+1]]=x;
		remove(2*x+1);
	}
	return 0;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    ll tt=1;
    cin>>tt;
    while(tt--)
    {
    	ll i,j,k,l;
    	ll h,g;
    	cin>>h>>g;
    	n=(1ll<<h)-1;
    	vector<pair<ll,ll>> vv;
    	vv.pb({0,0});
    	rem.clear();
    	he.clear();
    	he.pb(0);
    	ll sum=0;
    	// cout<<1<<n<<endl;
    	// map<ll,ll> pos;
    	for(i=1;i<=n;i++)
    	{
    		cin>>l;
    		// l=n+5-i;
    		vv.pb({l,i});
    		// l=n-i+1;
    		sum+=l;
    		he.pb(l);
    		// ma[l]=1;
    	}
    	sort(all(vv));
    	pos.clear();
    	pos.pb(0);
    	for(i=1;i<=n;i++)
    	{
    		pos.pb(vv[i].S);
    		he[vv[i].S]=i;
    	}
    	vi ans;
    	m=(1ll<<g)-1;
    	ll y=n-m;
    	rem.pb(0);
    	for(i=1;i<=n;i++)
    	{
    		ll de=log2(i)+1;
    		ll yy=(1ll<<(de-1));
    		rem.pb(y/yy);
    	}
    	// cout<<rem;
    	ll xx=0;
    	// remove(1);
    	for(i=n;i>=1;i--)
    	{
    		ll x=vv[i].F;
    		ll y=pos[i];
    		// cout<<x<<endl;
    		if(check(y))
    		{
    			xx++;
    			sum-=x;
    			ans.pb(y);
    			remove(y);
    		}
    		// cout<<rem;
    	}
    	// cout<<n-xx<<endl;
    	cout<<sum<<endl;
    	for(auto i:ans)
    		cout<<i<<" ";
    	cout<<endl;
    	// cout<<counti;a
    }
    return 0;
}


#include<bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse2")

#define ll          int
#define pb          push_back
#define vi          vector<int>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define rs(v,n)     v.resize(n)
#define hell        1000000007
#define peak        9123372036854775807
#define pii         acos(-1)
#define clr(a,x)    memset(a,x,sizeof(a))
#define ios         ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
template<typename T>
using ordered_set = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
template<class x,class y> x sum(x a,y b){return a+b;}
template<class x,class y> x mul(x a,y b){return a*b;}
template<class x,class y> x sub(x a,y b){return a-b;}
template<class x,class y> x divi(x a,y b){return a/b;}

template<class x,class y>
istream &operator>>(istream &in,pair<x,y> &p)
{
    in>>p.F>>p.S;
    return in;
}
template<class x>
istream &operator>>(istream &in,vector<x> &v)
{
    for(auto& i:v)
        in>>i;
    return in;
}
template<class x,class y>
ostream &operator<<(ostream &out,pair<x,y> &p)
{
    out<<"("<<p.F<<","<<p.S<<") ";
    return out;
}
template<class x>
ostream &operator<<(ostream &out,vector<x> &v)
{
    out<<v.size()<<endl;
    for(auto i:v)
        out<<i<<" ";
    out<<endl;
    return out;
}
#define N  4000005

vi rem,he;
unordered_map<ll,ll> pos;
ll n,m,counti=0;
ll check(ll x)
{
	// cout<<n<<" "<<x<<" "<<rem[x]<<endl;
	if(2*x>n)
		return 1;
	if(rem[x]==0)
		return 0;
	if(he[2*x]>he[2*x+1])
	{
		// cout<<1111<<endl;
		return check(2*x);
	}
	if(he[2*x+1]>he[2*x])
		return check(2*x+1);
	return 1;
}
ll remove(ll x)
{
	rem[x]--;
	if(2*x>n||(he[2*x+1]==0&&he[2*x]==0))
	{
		he[x]=0;
		return 0;
	}
	if(he[2*x]>he[2*x+1])
	{
		he[x]=he[2*x];
		pos[he[2*x]]=x;
		remove(2*x);
		return 0;
	}
	if(he[2*x+1]>he[2*x])
	{
		he[x]=he[2*x+1];
		pos[he[2*x+1]]=x;
		remove(2*x+1);
	}
	return 0;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    ll tt=1;
    cin>>tt;
    while(tt--)
    {
    	ll i,j,k,l;
    	ll h,g;
    	cin>>h>>g;
    	n=(1ll<<h)-1;
    	vi vv;
    	rem.clear();
    	he.clear();
    	he.pb(0);
    	ll sum=0;
    	// cout<<1<<n<<endl;
    	// map<ll,ll> pos;
    	pos.clear();
    	for(i=1;i<=n;i++)
    	{
    		cin>>l;
    		// l=n+5-i;
    		vv.pb(l);
    		// l=n-i+1;
    		sum+=l;
    		pos[l]=i;
    		he.pb(l);
    		// ma[l]=1;
    	}
    	sort(all(vv));
    	vi ans;
    	m=(1ll<<g)-1;
    	ll y=n-m;
    	rem.pb(0);
    	for(i=1;i<=n;i++)
    	{
    		ll de=log2(i)+1;
    		ll yy=(1ll<<(de-1));
    		rem.pb(y/yy);
    	}
    	// cout<<rem;
    	ll xx=0;
    	// remove(1);
    	for(i=n-1;i>=0;i--)
    	{
    		ll x=vv[i];
    		// cout<<x<<endl;
    		if(check(pos[x]))
    		{
    			xx++;
    			sum-=x;
    			ans.pb(pos[x]);
    			remove(pos[x]);
    		}
    		// cout<<rem;
    	}
    	// cout<<n-xx<<endl;
    	cout<<sum<<endl;
    	for(auto i:ans)
    		cout<<i<<" ";
    	cout<<endl;
    	// cout<<counti;a
    }
    return 0;
}


#include<bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse2")

#define ll          int
#define pb          push_back
#define vi          vector<int>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define rs(v,n)     v.resize(n)
#define hell        1000000007
#define peak        9123372036854775807
#define pii         acos(-1)
#define clr(a,x)    memset(a,x,sizeof(a))
#define ios         ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
template<typename T>
using ordered_set = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
template<class x,class y> x sum(x a,y b){return a+b;}
template<class x,class y> x mul(x a,y b){return a*b;}
template<class x,class y> x sub(x a,y b){return a-b;}
template<class x,class y> x divi(x a,y b){return a/b;}

template<class x,class y>
istream &operator>>(istream &in,pair<x,y> &p)
{
    in>>p.F>>p.S;
    return in;
}
template<class x>
istream &operator>>(istream &in,vector<x> &v)
{
    for(auto& i:v)
        in>>i;
    return in;
}
template<class x,class y>
ostream &operator<<(ostream &out,pair<x,y> &p)
{
    out<<"("<<p.F<<","<<p.S<<") ";
    return out;
}
template<class x>
ostream &operator<<(ostream &out,vector<x> &v)
{
    out<<v.size()<<endl;
    for(auto i:v)
        out<<i<<" ";
    out<<endl;
    return out;
}
#define N  4000005

vi rem,he;
map<ll,ll> pos;
ll n,m,counti=0;
ll check(ll x)
{
	// cout<<n<<" "<<x<<" "<<rem[x]<<endl;
	if(2*x>n)
		return 1;
	if(rem[x]==0)
		return 0;
	if(he[2*x]>he[2*x+1])
	{
		// cout<<1111<<endl;
		return check(2*x);
	}
	if(he[2*x+1]>he[2*x])
		return check(2*x+1);
	return 1;
}
ll remove(ll x)
{
	rem[x]--;
	if(2*x>n||(he[2*x+1]==0&&he[2*x]==0))
	{
		he[x]=0;
		return 0;
	}
	if(he[2*x]>he[2*x+1])
	{
		he[x]=he[2*x];
		pos[he[2*x]]=x;
		remove(2*x);
		return 0;
	}
	if(he[2*x+1]>he[2*x])
	{
		he[x]=he[2*x+1];
		pos[he[2*x+1]]=x;
		remove(2*x+1);
	}
	return 0;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    ll tt=1;
    cin>>tt;
    while(tt--)
    {
    	ll i,j,k,l;
    	ll h,g;
    	cin>>h>>g;
    	n=(1ll<<h)-1;
    	vi vv;
    	rem.clear();
    	he.clear();
    	he.pb(0);
    	ll sum=0;
    	// cout<<1<<n<<endl;
    	// map<ll,ll> pos;
    	pos.clear();
    	for(i=1;i<=n;i++)
    	{
    		cin>>l;
    		// l=n+5-i;
    		vv.pb(l);
    		// l=n-i+1;
    		sum+=l;
    		pos[l]=i;
    		he.pb(l);
    		// ma[l]=1;
    	}
    	sort(all(vv));
    	vi ans;
    	m=(1ll<<g)-1;
    	ll y=n-m;
    	rem.pb(0);
    	for(i=1;i<=n;i++)
    	{
    		ll de=log2(i)+1;
    		ll yy=(1ll<<(de-1));
    		rem.pb(y/yy);
    	}
    	// cout<<rem;
    	ll xx=0;
    	// remove(1);
    	for(i=n-1;i>=0;i--)
    	{
    		ll x=vv[i];
    		// cout<<x<<endl;
    		if(check(pos[x]))
    		{
    			xx++;
    			sum-=x;
    			ans.pb(pos[x]);
    			remove(pos[x]);
    		}
    		// cout<<rem;
    	}
    	// cout<<n-xx<<endl;
    	cout<<sum<<endl;
    	for(auto i:ans)
    		cout<<i<<" ";
    	cout<<endl;
    	// cout<<counti;a
    }
    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 ll          long long
#define pb          push_back
#define vi          vector<ll int>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define rs(v,n)     v.resize(n)
#define hell        1000000007
#define peak        9123372036854775807
#define pii         acos(-1)
#define clr(a,x)    memset(a,x,sizeof(a))
#define ios         ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
template<typename T>
using ordered_set = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
template<class x,class y> x sum(x a,y b){return a+b;}
template<class x,class y> x mul(x a,y b){return a*b;}
template<class x,class y> x sub(x a,y b){return a-b;}
template<class x,class y> x divi(x a,y b){return a/b;}

template<class x,class y>
istream &operator>>(istream &in,pair<x,y> &p)
{
    in>>p.F>>p.S;
    return in;
}
template<class x>
istream &operator>>(istream &in,vector<x> &v)
{
    for(auto& i:v)
        in>>i;
    return in;
}
template<class x,class y>
ostream &operator<<(ostream &out,pair<x,y> &p)
{
    out<<"("<<p.F<<","<<p.S<<") ";
    return out;
}
template<class x>
ostream &operator<<(ostream &out,vector<x> &v)
{
    out<<v.size()<<endl;
    for(auto i:v)
        out<<i<<" ";
    out<<endl;
    return out;
}
#define N  4000005

vi rem,he;
map<ll,ll> pos;
ll n,m;
ll check(ll x)
{
	// cout<<n<<" "<<x<<" "<<rem[x]<<endl;
	if(2*x>n)
		return 1;
	if(rem[x]==0)
		return 0;
	if(he[2*x]>he[2*x+1])
	{
		// cout<<1111<<endl;
		return check(2*x);
	}
	if(he[2*x+1]>he[2*x])
		return check(2*x+1);
	return 1;
}
ll remove(ll x)
{
	rem[x]--;
	if(2*x>n||(he[2*x+1]==0&&he[2*x]==0))
	{
		he[x]=0;
		return 0;
	}
	if(he[2*x]>he[2*x+1])
	{
		he[x]=he[2*x];
		pos[he[2*x]]=x;
		remove(2*x);
		return 0;
	}
	if(he[2*x+1]>he[2*x])
	{
		he[x]=he[2*x+1];
		pos[he[2*x+1]]=x;
		remove(2*x+1);
	}
	return 0;
}

int main()
{
    ios;
    ll tt=1;
    cin>>tt;
    while(tt--)
    {
    	ll i,j,k,l;
    	ll h,g;
    	cin>>h>>g;
    	n=(1ll<<h)-1;
    	vi vv;
    	rem.clear();
    	he.clear();
    	he.pb(0);
    	ll sum=0;
    	// cout<<1<<n<<endl;
    	// map<ll,ll> pos;
    	pos.clear();
    	for(i=1;i<=n;i++)
    	{
    		cin>>l;
    		vv.pb(l);
    		// l=n-i+1;
    		sum+=l;
    		pos[l]=i;
    		he.pb(l);
    		// ma[l]=1;
    	}
    	sort(all(vv));
    	vi ans;
    	m=(1ll<<g)-1;
    	ll y=n-m;
    	rem.pb(0);
    	for(i=1;i<=n;i++)
    	{
    		ll de=log2(i)+1;
    		ll yy=(1ll<<(de-1));
    		rem.pb(y/yy);
    	}
    	// cout<<rem;
    	ll xx=0;
    	for(i=n-1;i>=0;i--)
    	{
    		ll x=vv[i];
    		// cout<<x<<endl;
    		if(check(pos[x]))
    		{
    			xx++;
    			sum-=x;
    			ans.pb(pos[x]);
    			remove(pos[x]);
    		}
    		// cout<<rem;
    	}
    	// cout<<n-xx<<endl;
    	cout<<sum<<endl;
    	for(auto i:ans)
    		cout<<i<<" ";
    	cout<<endl;
    }
    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 ll          long long
#define pb          push_back
#define vi          vector<ll int>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define rs(v,n)     v.resize(n)
#define hell        1000000007
#define peak        9123372036854775807
#define pii         acos(-1)
#define clr(a,x)    memset(a,x,sizeof(a))
#define ios         ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
template<typename T>
using ordered_set = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
template<class x,class y> x sum(x a,y b){return a+b;}
template<class x,class y> x mul(x a,y b){return a*b;}
template<class x,class y> x sub(x a,y b){return a-b;}
template<class x,class y> x divi(x a,y b){return a/b;}

template<class x,class y>
istream &operator>>(istream &in,pair<x,y> &p)
{
    in>>p.F>>p.S;
    return in;
}
template<class x>
istream &operator>>(istream &in,vector<x> &v)
{
    for(auto& i:v)
        in>>i;
    return in;
}
template<class x,class y>
ostream &operator<<(ostream &out,pair<x,y> &p)
{
    out<<"("<<p.F<<","<<p.S<<") ";
    return out;
}
template<class x>
ostream &operator<<(ostream &out,vector<x> &v)
{
    out<<v.size()<<endl;
    for(auto i:v)
        out<<i<<" ";
    out<<endl;
    return out;
}
#define N  4000005

vi v[N],rem(N,0),pos(N,0),he(N,0);
ll n,m;
ll check(ll x)
{
	// cout<<n<<" "<<x<<" "<<rem[x]<<endl;
	if(x>n)
		return 1;
	if(rem[x]==0)
		return 0;
	if(he[2*x]>he[2*x+1])
	{
		// cout<<1111<<endl;
		return check(2*x);
	}
	if(he[2*x+1]>he[2*x])
		return check(2*x+1);
	return 1;
}
ll remove(ll x)
{
	rem[x]--;
	if(he[2*x+1]==0&&he[2*x]==0)
	{
		he[x]=0;
		return 0;
	}
	if(he[2*x]>he[2*x+1])
	{
		he[x]=he[2*x];
		pos[he[2*x]]=x;
		remove(2*x);
		return 0;
	}
	if(he[2*x+1]>he[2*x])
	{
		he[x]=he[2*x+1];
		pos[he[2*x+1]]=x;
		remove(2*x+1);
	}
	return 0;
}

int main()
{
    ios;
    ll tt=1;
    cin>>tt;
    while(tt--)
    {
    	ll i,j,k,l;
    	ll h,g;
    	cin>>h>>g;
    	n=(1ll<<h)-1;
    	for(i=1;i<=n;i++)
    	{
    		v[i].clear();
    		rem[i]=0;
    	}
    	ll sum=0;
    	// cout<<1<<n<<endl;
    	for(i=1;i<=n;i++)
    	{
    		cin>>l;
    		sum+=l;
    		pos[l]=i;
    		if(i==1)
    			continue;
    		he[i]=l;
    		v[l].pb(he[i/2]);
    		v[he[i/2]].pb(l);
    	}
    	vi ans;
    	m=(1<<g)-1;
    	ll y=n-m;
    	for(i=1;i<=n;i++)
    	{
    		ll de=log2(i)+1;
    		ll yy=(1ll<<(de-1));
    		rem[i]=y/yy;
    	}
    	for(i=n;i>=1;i--)
    	{
    		if(check(pos[i]))
    		{
    			sum-=i;
    			ans.pb(pos[i]);
    			remove(pos[i]);
    		}
    		// cout<<rem;
    	}
    	cout<<sum<<endl;
    	for(auto i:ans)
    		cout<<i<<" ";
    	cout<<endl;
    }
    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 ll          long long
#define pb          push_back
#define vi          vector<ll int>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define rs(v,n)     v.resize(n)
#define hell        1000000007
#define peak        9123372036854775807
#define pii         acos(-1)
#define clr(a,x)    memset(a,x,sizeof(a))
#define ios         ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
template<typename T>
using ordered_set = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
template<class x,class y> x sum(x a,y b){return a+b;}
template<class x,class y> x mul(x a,y b){return a*b;}
template<class x,class y> x sub(x a,y b){return a-b;}
template<class x,class y> x divi(x a,y b){return a/b;}

template<class x,class y>
istream &operator>>(istream &in,pair<x,y> &p)
{
    in>>p.F>>p.S;
    return in;
}
template<class x>
istream &operator>>(istream &in,vector<x> &v)
{
    for(auto& i:v)
        in>>i;
    return in;
}
template<class x,class y>
ostream &operator<<(ostream &out,pair<x,y> &p)
{
    out<<"("<<p.F<<","<<p.S<<") ";
    return out;
}
template<class x>
ostream &operator<<(ostream &out,vector<x> &v)
{
    out<<v.size()<<endl;
    for(auto i:v)
        out<<i<<" ";
    out<<endl;
    return out;
}
#define N  4000005

vi rem(N,0),he(N,0);
map<ll,ll> pos;
ll n,m;
ll check(ll x)
{
	// cout<<n<<" "<<x<<" "<<rem[x]<<endl;
	if(2*x>n)
		return 1;
	if(rem[x]==0)
		return 0;
	if(he[2*x]>he[2*x+1])
	{
		// cout<<1111<<endl;
		return check(2*x);
	}
	if(he[2*x+1]>he[2*x])
		return check(2*x+1);
	return 1;
}
ll remove(ll x)
{
	rem[x]--;
	if(2*x>n||(he[2*x+1]==0&&he[2*x]==0))
	{
		he[x]=0;
		return 0;
	}
	if(he[2*x]>he[2*x+1])
	{
		he[x]=he[2*x];
		pos[he[2*x]]=x;
		remove(2*x);
		return 0;
	}
	if(he[2*x+1]>he[2*x])
	{
		he[x]=he[2*x+1];
		pos[he[2*x+1]]=x;
		remove(2*x+1);
	}
	return 0;
}

int main()
{
    ios;
    ll tt=1;
    cin>>tt;
    while(tt--)
    {
    	ll i,j,k,l;
    	ll h,g;
    	cin>>h>>g;
    	n=(1ll<<h)-1;
    	vi vv;
    	for(i=1;i<=n;i++)
    	{
    		rem[i]=0;
    		// pos[i]=0;
    		he[i]=0;
    	}
    	ll sum=0;
    	// cout<<1<<n<<endl;
    	// map<ll,ll> pos;
    	pos.clear();
    	for(i=1;i<=n;i++)
    	{
    		cin>>l;
    		vv.pb(l);
    		// l=n-i+1;
    		sum+=l;
    		pos[l]=i;
    		he[i]=l;
    		// ma[l]=1;
    	}
    	sort(all(vv));
    	vi ans;
    	m=(1ll<<g)-1;
    	ll y=n-m;
    	for(i=1;i<=n;i++)
    	{
    		ll de=log2(i)+1;
    		ll yy=(1ll<<(de-1));
    		rem[i]=y/yy;
    	}
    	// cout<<rem;
    	ll xx=0;
    	for(i=n-1;i>=0;i--)
    	{
    		ll x=vv[i];
    		// cout<<x<<endl;
    		if(check(pos[x]))
    		{
    			xx++;
    			sum-=x;
    			ans.pb(pos[x]);
    			remove(pos[x]);
    		}
    		// cout<<rem;
    	}
    	// cout<<n-xx<<endl;
    	cout<<sum<<endl;
    	for(auto i:ans)
    		cout<<i<<" ";
    	cout<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse2")

#define ll          int
#define pb          push_back
#define vi          vector<int>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define rs(v,n)     v.resize(n)
#define hell        1000000007
#define peak        9123372036854775807
#define pii         acos(-1)
#define clr(a,x)    memset(a,x,sizeof(a))
#define ios         ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
template<typename T>
using ordered_set = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
template<class x,class y> x sum(x a,y b){return a+b;}
template<class x,class y> x mul(x a,y b){return a*b;}
template<class x,class y> x sub(x a,y b){return a-b;}
template<class x,class y> x divi(x a,y b){return a/b;}

template<class x,class y>
istream &operator>>(istream &in,pair<x,y> &p)
{
    in>>p.F>>p.S;
    return in;
}
template<class x>
istream &operator>>(istream &in,vector<x> &v)
{
    for(auto& i:v)
        in>>i;
    return in;
}
template<class x,class y>
ostream &operator<<(ostream &out,pair<x,y> &p)
{
    out<<"("<<p.F<<","<<p.S<<") ";
    return out;
}
template<class x>
ostream &operator<<(ostream &out,vector<x> &v)
{
    out<<v.size()<<endl;
    for(auto i:v)
        out<<i<<" ";
    out<<endl;
    return out;
}
#define N  4000005

vi rem,he;
map<ll,ll> pos;
ll n,m,counti=0;
ll check(ll x)
{
	// cout<<n<<" "<<x<<" "<<rem[x]<<endl;
	if(2*x>n)
		return 1;
	if(rem[x]==0)
		return 0;
	if(he[2*x]>he[2*x+1])
	{
		// cout<<1111<<endl;
		return check(2*x);
	}
	if(he[2*x+1]>he[2*x])
		return check(2*x+1);
	return 1;
}
ll remove(ll x)
{
	rem[x]--;
	if(2*x>n||(he[2*x+1]==0&&he[2*x]==0))
	{
		he[x]=0;
		return 0;
	}
	if(he[2*x]>he[2*x+1])
	{
		he[x]=he[2*x];
		pos[he[2*x]]=x;
		remove(2*x);
		return 0;
	}
	if(he[2*x+1]>he[2*x])
	{
		he[x]=he[2*x+1];
		pos[he[2*x+1]]=x;
		remove(2*x+1);
	}
	return 0;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    ll tt=1;
    cin>>tt;
    while(tt--)
    {
    	ll i,j,k,l;
    	ll h,g;
    	cin>>h>>g;
    	n=(1ll<<h)-1;
    	vi vv;
    	rem.clear();
    	he.clear();
    	he.pb(0);
    	ll sum=0;
    	// cout<<1<<n<<endl;
    	// map<ll,ll> pos;
    	pos.clear();
    	for(i=1;i<=n;i++)
    	{
    		// cin>>l;
    		l=n+5-i;
    		vv.pb(l);
    		// l=n-i+1;
    		sum+=l;
    		pos[l]=i;
    		he.pb(l);
    		// ma[l]=1;
    	}
    	sort(all(vv));
    	vi ans;
    	m=(1ll<<g)-1;
    	ll y=n-m;
    	rem.pb(0);
    	for(i=1;i<=n;i++)
    	{
    		ll de=log2(i)+1;
    		ll yy=(1ll<<(de-1));
    		rem.pb(y/yy);
    	}
    	// cout<<rem;
    	ll xx=0;
    	// remove(1);
    	for(i=n-1;i>=0;i--)
    	{
    		ll x=vv[i];
    		// cout<<x<<endl;
    		if(check(pos[x]))
    		{
    			xx++;
    			sum-=x;
    			ans.pb(pos[x]);
    			remove(pos[x]);
    		}
    		// cout<<rem;
    	}
    	// cout<<n-xx<<endl;
    	cout<<sum<<endl;
    	for(auto i:ans)
    		cout<<i<<" ";
    	cout<<endl;
    	// cout<<counti;a
    }
    return 0;
}


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

#define X first
#define Y second
#define pb push_back
#define mp make_pair
#define all(a) a.begin(), a.end()
#define rep(i, st, n) for (int i = (st); i < (n); ++i)
#define repinv(i, st, n) for (int i = ((n)-1); i >= (st); --i)
#define MEM(a, b) memset(a, b, sizeof(a));
#ifdef LOCAL
#define debug(x) std::cerr << #x << ": " << x << endl
#else
#define debug(x) 860111
#endif
#define fastIO() ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define fileIO(in, out) freopen(in, "r", stdin); freopen(out, "w", stdout)

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef long double ld;

//mt19937 mrand(random_device{}());
//mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll mod=1e9+7;
//int rnd(int x) { return mrand() % x;}
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
pii operator+(const pii&x, const pii&y) { return mp(x.X+y.X, x.Y+y.Y);}
pii operator-(const pii&x, const pii&y) { return mp(x.X-y.X, x.Y-y.Y);}
//INT_MAX, ULLONG_MAX, LLONG_MAX or numeric_limits<int>::min()

inline ll read(){
	char ch=getchar();ll x=0,f=0;
	while(ch<'0' || ch>'9') f|=ch=='-',ch=getchar();
	while(ch>='0' && ch<='9') x=x*10+ch-'0',ch=getchar();
	return f?-x:x;
}

//------------------------------------------------------------------------//
int T;
const int maxN = 4e6+7;
int n, m;
int N, M;
int a[maxN];
vector<int> ans;

#define pr (x>>1)
#define lc (x<<1)
#define rc (x<<1|1)
#define mc a[lc]>a[rc]?lc:rc

bool ok(int x)
{
  if(a[x] == 0) return 0;
  while(a[mc]) x = mc;
  return x >= M;
}

void pull(int x)
{
  while(a[mc]) x = mc, a[pr] = a[x];
  a[x] = 0;
}

//check T
void solve()
{
  ans.clear();
	cin >> n >> m;
  N = 1<<n;
  M = 1<<m;
  rep(i, 1, N) cin >> a[i];
  rep(i, N, N<<1) a[i] = 0;
  ll sum = 0;
  rep(i, 1, M)
  {
    while(ok(i)) {ans.pb(i); pull(i);}
    sum += a[i];
  }
  cout << sum << endl;
  rep(i, 0, ans.size()) cout << ans[i] << " \n"[i==ans.size()-1];
  return;
}

signed main()
{
  fastIO();
  T = 1;
  cin >> T; //this
  while(T--) solve();
  return 0;
}


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

#define X first
#define Y second
#define pb push_back
#define mp make_pair
#define all(a) a.begin(), a.end()
#define rep(i, st, n) for (int i = (st); i < (n); ++i)
#define repinv(i, st, n) for (int i = ((n)-1); i >= (st); --i)
#define MEM(a, b) memset(a, b, sizeof(a));
#ifdef LOCAL
#define debug(x) std::cerr << #x << ": " << x << endl
#else
#define debug(x) 860111
#endif
#define fastIO() ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define fileIO(in, out) freopen(in, "r", stdin); freopen(out, "w", stdout)

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef long double ld;

//mt19937 mrand(random_device{}());
//mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll mod=1e9+7;
//int rnd(int x) { return mrand() % x;}
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
pii operator+(const pii&x, const pii&y) { return mp(x.X+y.X, x.Y+y.Y);}
pii operator-(const pii&x, const pii&y) { return mp(x.X-y.X, x.Y-y.Y);}
//INT_MAX, ULLONG_MAX, LLONG_MAX or numeric_limits<int>::min()

inline ll read(){
	char ch=getchar();ll x=0,f=0;
	while(ch<'0' || ch>'9') f|=ch=='-',ch=getchar();
	while(ch>='0' && ch<='9') x=x*10+ch-'0',ch=getchar();
	return f?-x:x;
}

//------------------------------------------------------------------------//
int T;
const int maxN = 2e6+7;
int n, m;
int N, M;
int a[maxN];
vector<int> ans;

bool ok(int x)
{
  if(a[x] == 0) return 0;
  while(a[x<<1]||a[x<<1|1]) x = a[x<<1] > a[x<<1|1] ? x<<1 : x<<1|1;
  //debug(x);
  return x >= M;
}

void pull(int x)
{
  while(a[x<<1]||a[x<<1|1])
  {
    x = a[x<<1] > a[x<<1|1] ? x<<1 : x<<1|1;
    a[x>>1] = a[x];
  }
  a[x] = 0;
}

//check T
void solve()
{
  ans.clear();
	cin >> n >> m;
  N = 1<<n;
  M = 1<<m;
  //debug(N); debug(M);
  rep(i, 1, N) cin >> a[i];
  //rep(i, 1, N) debug(a[i]);
  debug("test cerr");
  ll sum = 0;
  rep(i, 1, M)
  {
    while(ok(i)) {ans.pb(i); pull(i);}
    sum += a[i];
  }
  cout << sum << endl;
  rep(i, 0, ans.size()) cout << ans[i] << " \n"[i==ans.size()-1];
  return;
}

signed main()
{
  fastIO();
  T = 1;
  cin >> T; //this
  while(T--) solve();
  return 0;
}


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

#define X first
#define Y second
#define pb push_back
#define mp make_pair
#define all(a) a.begin(), a.end()
#define rep(i, st, n) for (int i = (st); i < (n); ++i)
#define repinv(i, st, n) for (int i = ((n)-1); i >= (st); --i)
#define MEM(a, b) memset(a, b, sizeof(a));
#ifdef LOCAL
#define debug(x) std::cerr << #x << ": " << x << endl
#else
#define debug(x) 860111
#endif
#define fastIO() ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define fileIO(in, out) freopen(in, "r", stdin); freopen(out, "w", stdout)

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef long double ld;

//mt19937 mrand(random_device{}());
//mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll mod=1e9+7;
//int rnd(int x) { return mrand() % x;}
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
pii operator+(const pii&x, const pii&y) { return mp(x.X+y.X, x.Y+y.Y);}
pii operator-(const pii&x, const pii&y) { return mp(x.X-y.X, x.Y-y.Y);}
//INT_MAX, ULLONG_MAX, LLONG_MAX or numeric_limits<int>::min()

inline ll read(){
	char ch=getchar();ll x=0,f=0;
	while(ch<'0' || ch>'9') f|=ch=='-',ch=getchar();
	while(ch>='0' && ch<='9') x=x*10+ch-'0',ch=getchar();
	return f?-x:x;
}

//------------------------------------------------------------------------//
int T;
const int maxN = 4e6+7;
int n, m;
int N, M;
int a[maxN];
vector<int> ans;

#define pr (x>>1)
#define lc (x<<1)
#define rc (x<<1|1)
#define mc a[lc]>a[rc]?lc:rc

bool ok(int x)
{
  if(a[x] == 0) return 0;
  while(a[mc]) x = mc;
  return x >= M;
}

void pull(int x)
{
  while(a[mc]) x = mc, a[pr] = a[x];
  a[x] = 0;
}

//check T
void solve()
{
  ans.clear();
	cin >> n >> m;
  N = 1<<n;
  M = 1<<m;
  rep(i, 1, N) cin >> a[i];
  ll sum = 0;
  rep(i, 1, M)
  {
    while(ok(i)) {ans.pb(i); pull(i);}
    sum += a[i];
  }
  cout << sum << endl;
  rep(i, 0, ans.size()) cout << ans[i] << " \n"[i==ans.size()-1];
  return;
}

signed main()
{
  fastIO();
  T = 1;
  cin >> T; //this
  while(T--) solve();
  return 0;
}


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

#define X first
#define Y second
#define pb push_back
#define mp make_pair
#define all(a) a.begin(), a.end()
#define rep(i, st, n) for (int i = (st); i < (n); ++i)
#define repinv(i, st, n) for (int i = ((n)-1); i >= (st); --i)
#define MEM(a, b) memset(a, b, sizeof(a));
#ifdef LOCAL
#define debug(x) std::cerr << #x << ": " << x << endl
#else
#define debug(x) 860111
#endif
#define fastIO() ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define fileIO(in, out) freopen(in, "r", stdin); freopen(out, "w", stdout)

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef long double ld;

//mt19937 mrand(random_device{}());
//mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll mod=1e9+7;
//int rnd(int x) { return mrand() % x;}
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
pii operator+(const pii&x, const pii&y) { return mp(x.X+y.X, x.Y+y.Y);}
pii operator-(const pii&x, const pii&y) { return mp(x.X-y.X, x.Y-y.Y);}
//INT_MAX, ULLONG_MAX, LLONG_MAX or numeric_limits<int>::min()

inline ll read(){
	char ch=getchar();ll x=0,f=0;
	while(ch<'0' || ch>'9') f|=ch=='-',ch=getchar();
	while(ch>='0' && ch<='9') x=x*10+ch-'0',ch=getchar();
	return f?-x:x;
}

//------------------------------------------------------------------------//
int T;
const int maxN = 4e6+7;
int n, m;
int N, M;
int a[maxN];
vector<int> ans;

bool ok(int x)
{
  if(a[x] == 0) return 0;
  while(a[x<<1]||a[x<<1|1]) x = a[x<<1] > a[x<<1|1] ? x<<1 : x<<1|1;
  //debug(x);
  return x >= M;
}

void pull(int x)
{
  while(a[x<<1]||a[x<<1|1])
  {
    x = a[x<<1] > a[x<<1|1] ? x<<1 : x<<1|1;
    a[x>>1] = a[x];
  }
  a[x] = 0;
}

//check T
void solve()
{
  ans.clear();
	cin >> n >> m;
  N = 1<<n;
  M = 1<<m;
  //debug(N); debug(M);
  rep(i, 1, N) cin >> a[i];
  //rep(i, 1, N) debug(a[i]);
  debug("test cerr");
  ll sum = 0;
  rep(i, 1, M)
  {
    while(ok(i)) {ans.pb(i); pull(i);}
    sum += a[i];
  }
  cout << sum << endl;
  rep(i, 0, ans.size()) cout << ans[i] << " \n"[i==ans.size()-1];
  return;
}

signed main()
{
  fastIO();
  T = 1;
  cin >> T; //this
  while(T--) solve();
  return 0;
}


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

#define X first
#define Y second
#define pb push_back
#define mp make_pair
#define all(a) a.begin(), a.end()
#define rep(i, st, n) for (int i = (st); i < (n); ++i)
#define repinv(i, st, n) for (int i = ((n)-1); i >= (st); --i)
#define MEM(a, b) memset(a, b, sizeof(a));
#ifdef LOCAL
#define debug(x) std::cerr << #x << ": " << x << endl
#else
#define debug(x) 860111
#endif
#define fastIO() ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define fileIO(in, out) freopen(in, "r", stdin); freopen(out, "w", stdout)

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef long double ld;

//mt19937 mrand(random_device{}());
//mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll mod=1e9+7;
//int rnd(int x) { return mrand() % x;}
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
pii operator+(const pii&x, const pii&y) { return mp(x.X+y.X, x.Y+y.Y);}
pii operator-(const pii&x, const pii&y) { return mp(x.X-y.X, x.Y-y.Y);}
//INT_MAX, ULLONG_MAX, LLONG_MAX or numeric_limits<int>::min()

inline ll read(){
	char ch=getchar();ll x=0,f=0;
	while(ch<'0' || ch>'9') f|=ch=='-',ch=getchar();
	while(ch>='0' && ch<='9') x=x*10+ch-'0',ch=getchar();
	return f?-x:x;
}

//------------------------------------------------------------------------//
int T;
const int maxN = 2e6+7;
int n, m;
int N, M;
int a[maxN];
vector<int> ans;

bool ok(int x)
{
  if(a[x] == 0) return 0;
  while(a[x<<1]||a[x<<1|1]) x = a[x<<1] > a[x<<1|1] ? x<<1 : x<<1|1;
  //debug(x);
  return x >= M;
}

void pull(int x)
{
  while(a[x<<1]||a[x<<1|1])
  {
    x = a[x<<1] > a[x<<1|1] ? x<<1 : x<<1|1;
    a[x>>1] = a[x];
  }
  a[x] = 0;
}

//check T
void solve()
{
  ans.clear();
	cin >> n >> m;
  N = 1<<n;
  M = 1<<m;
  //debug(N); debug(M);
  rep(i, 1, N) cin >> a[i];
  //rep(i, 1, N) debug(a[i]);
//debug("test cerr");
  rep(i, 1, M)
  {
    while(ok(i)) {ans.pb(i); pull(i);}
  }
  rep(i, 0, ans.size()) cout << ans[i] << " \n"[i==ans.size()-1];
  return;
}

signed main()
{
  fastIO();
  T = 1;
  cin >> T; //this
  while(T--) solve();
  return 0;
}


// #define _GLIBCXX_DEBUG // for STL debug (optional)
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>
using namespace std;
using ll = long long int;
using int64 = long long int;

template<typename T> void chmax(T &a, T b) {a = max(a, b);}
template<typename T> void chmin(T &a, T b) {a = min(a, b);}
template<typename T> void chadd(T &a, T b) {a = a + b;}

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const int INF = 1LL << 29;
const ll LONGINF = 1LL << 60;
const ll MOD = 1000000007LL;

void solve() {
    int h, g; scanf("%d%d", &h, &g);
    int N = (1 << h) - 1;
    int M = (1 << g) - 1;
    vector<int> A(N);
    for(int i=0; i<N; i++) scanf("%d", &A[i]);

    vector<int> V(N);
    {
        vector<int> B = A;
        sort(B.begin(), B.end());
        B.erase(unique(B.begin(), B.end()), B.end());

        for(int i=0; i<N; i++) {
            int k = lower_bound(B.begin(), B.end(), A[i]) - B.begin();
            V[k] = A[i];
            A[i] = k;
        }
    }

    vector<int> P(N);
    using Elem = int;
    stack<int> que;
    for(int i=0; i<N; i++) {
        que.emplace(-i);
        P[ A[i] ] = i;
    }

    auto f = [&](auto&& self, int cur) -> void {
        int c1 = cur * 2 + 1, c2 = cur * 2 + 2;
        if((c1 >= N or A[c1] == -1)
           and (c2 >= N or A[c2] == -1)) {
            A[cur] = -1;
        }
        else if(c2 >= N or A[c1] > A[c2]) {
            A[cur] = A[c1];
            P[ A[c1] ] = cur;
            self(self, c1);
        }
        else {
            A[cur] = A[c2];
            P[ A[c2] ] = cur;
            self(self, c2);
        }
    };
    auto disappear_idx = [&](auto &&self, int cur) -> int {
        int c1 = cur * 2 + 1, c2 = cur * 2 + 2;
        if((c1 >= N or A[c1] == -1)
           and (c2 >= N or A[c2] == -1)) return cur;
        else if(c2 >= N or A[c1] > A[c2]) return self(self, c1);
        else return self(self, c2);
    };

    ll sum = 0;
    for(int i=0; i<N; i++) sum += V[i];

    vector<int> ans;

    for(int z=0; z<N-M; z++) {
        while(true) {
            int v = que.top(); que.pop();
            v *= -1;
            int k = P[v];
            int d = disappear_idx(disappear_idx, k);
            if(d < M) continue;
            sum -= V[v];
            ans.emplace_back(k);
            f(f, k);

            // for(int i=0; i<N; i++) fprintf(stderr, "%d%c", A[i], " \n"[i + 1 == N]);
            break;
        }
    }

    printf("%lld\n", sum);
    for(size_t i=0; i<ans.size(); i++) printf("%d%c", ans[i] + 1, " \n"[i + 1 == ans.size()]);
}

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


#pragma GCC optimize("O3")
#include <bits/stdc++.h>

using namespace std;

//defines
typedef long long ll;

using namespace std;

int h,g;
int xx=0,d,n,nn;

bool check(int cur, vector<int> &arr, int lev, vector<int> &ans);

bool func(int cur, vector<int> &arr, int lev, vector<int> &ans){
    //cout<<"CFunction called for "<<cur<<"   ";
    int l = 2*cur, r = 2*cur+1;
    int lv = arr[l], rv = arr[r];
    if(lv == 0 && rv == 0){
        if( lev <= g){
            if(cur == 1) return 0;
            if( cur%2 == 0 ){ while( check( cur+1, arr, lev, ans) ); }
            else { while( check( cur-1, arr, lev, ans) ); }
            return 0;
        }
        else{ arr[cur]=0; return 1; }
    }
    else{
        if( lv < rv ){
            if( func(r, arr, lev+1, ans) ){
                arr[cur] = rv; return 1;
            }
            else{
                if(cur == 1) return 0;
                if( cur%2 == 0 ){ while( check( cur+1, arr, lev, ans) ); }
                else { while( check( cur-1, arr, lev, ans) ); }
                return 0;
            }
        }
        else{
            if( func(l, arr, lev+1, ans) ){
                arr[cur] = lv; return 1;
            }
            else{
                if(cur == 1) return 0;
                if( cur%2 == 0 ){ while( check( cur+1, arr, lev, ans) ); }
                else { while( check( cur-1, arr, lev, ans) ); }
                return 0;
            }
        }
    }
}

bool check(int cur, vector<int> &arr, int lev, vector<int> &ans){
    //cout<<"Checking for "<<cur<<"   ";
    int l = 2*cur, r = 2*cur+1;
    if(arr[l] == 0 && arr[r] == 0){
        if( lev <= g){ return 0; }
        else{ arr[cur]=0; ans.push_back(cur); return 1; }
    }
    else{
        if( func(cur, arr, lev, ans) ){
            ans.push_back(cur);
            //cout<<"In function checking "<<cur<<" is inserted"<<endl;
            return 1;
        }
        else return 0;
    }
}

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

    int t; cin>>t;
    while(t--){
        cin>>h>>g;
        n = pow(2,h); nn = pow(2,g);
        vector<int> arr(2*n, 0);
        for(int i=1; i<n; i++) cin>>arr[i];

        d = n - nn; xx=0;

        vector<int> ans;

        while( check( 1, arr, 1, ans) );

        ll sum=0;
        for(int i=1; i<nn; i++) sum+=arr[i];
        //for(int i=1; i<nn; i++) cout<<arr[i]<<" "; cout<<endl;
        cout<<sum<<endl;
        for(int i=0; i<d; i++) cout<<ans[i]<<" "; cout<<endl;
    }
}


#pragma GCC optimize("O3")
#include <bits/stdc++.h>

using namespace std;

//defines
typedef long long ll;

using namespace std;

void func(int par, vector<int> &arr, int n){
    if(arr[par] == 0 || n <= 2*par ){ arr[par]=0; return ; }
    int l = 2*par, r = 2*par+1;

    if(arr[l] < arr[r]){
        arr[par] = arr[r]; func(r, arr, n);
    }
    else{ arr[par] = arr[l]; func(l, arr, n); }
}

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

    int t; cin>>t;
    while(t--){
        ll h,g; cin>>h>>g;
        int n = pow(2,h) , nn = pow(2,g);
        vector<int> arr(n);
        for(int i=1; i<n; i++) cin>>arr[i];

        int d = n - nn;

        vector<int> ans(d); int xx=0;
        int cur,ind;
        for(int i=h-1; i>=g; i--){
            cur = pow(2, i-1)-1;
            for(int j=pow(2,i)-1; j>cur; j--){
                func(j, arr, n); ans[xx++]=j;   //cout<<j<<" ";
            }
            ind=pow(2,i)-1;
            while(ind!=1){
                if( arr[ind-1] < arr[ind] ){
                    func(ind-1, arr, n);  ans[xx++]=ind-1;  //cout<<ind-1<<" ";
                }
                else{
                    func(ind, arr, n);  ans[xx++]=ind;  //cout<<ind<<" ";
                }
                ind-=2;
            }
            ans[xx++]=1; func(1, arr, n); //cout<<1<<" ";
        }

        ll sum=0;
        for(int i=1; i<nn; i++) sum+=arr[i];
        //for(int i=1; i<nn; i++) cout<<arr[i]<<" "; cout<<endl;
        cout<<sum<<endl;
        for(int i=0; i<d; i++) cout<<ans[i]<<" "; cout<<endl;
    }
}


#pragma GCC optimize("O3")
#include <bits/stdc++.h>

using namespace std;

//defines
typedef long long ll;

using namespace std;

int h,g;
int xx=0,d,n,nn;

bool check(int cur, vector<int> &arr, int lev, vector<int> &ans, vector<bool> &cc);

bool func(int cur, vector<int> &arr, int lev, vector<int> &ans, vector<bool> &cc){
    //cout<<"CFunction called for "<<cur<<"   ";
    if(cc[cur]){ return 0; }
    int l = 2*cur, r = 2*cur+1;
    int lv = arr[l], rv = arr[r];
    if(lv == 0 && rv == 0){
        cc[cur]=1;
        if( lev <= g){
            if(cur == 1){ return 0; }
            if( cur%2 == 0 ){  while( check( cur+1, arr, lev, ans, cc) ); }
            else { while( check( cur-1, arr, lev, ans, cc) ); }
            return 0;
        }
        else{ arr[cur]=0; return 1; }
    }
    else{
        if( lv < rv ){
            if( func(r, arr, lev+1, ans, cc) ){
                arr[cur] = rv; return 1;
            }
            else{
                cc[cur]=1;
                if(cur == 1) return 0;
                if( cur%2 == 0 ){ while( check( cur+1, arr, lev, ans, cc) ); }
                else { while( check( cur-1, arr, lev, ans, cc) ); }
                return 0;
            }
        }
        else{
            if( func(l, arr, lev+1, ans, cc) ){
                arr[cur] = lv; return 1;
            }
            else{
                cc[cur]=1;
                if(cur == 1) return 0;
                if( cur%2 == 0 ){ while( check( cur+1, arr, lev, ans, cc) ); }
                else { while( check( cur-1, arr, lev, ans, cc) ); }
                return 0;
            }
        }
    }
}

bool check(int cur, vector<int> &arr, int lev, vector<int> &ans, vector<bool> &cc){
    //cout<<"Checking for "<<cur<<"   ";
    if(cc[cur]) return 0;
    int l = 2*cur, r = 2*cur+1;
    if(arr[l] == 0 && arr[r] == 0){
        if( lev <= g){ return 0; }
        else{ arr[cur]=0; ans.push_back(cur); return 1; }
    }
    else{
        if( func(cur, arr, lev, ans, cc) ){
            ans.push_back(cur);
            //cout<<"In function checking "<<cur<<" is inserted"<<endl;
            return 1;
        }
        else return 0;
    }
}

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

    int t; cin>>t;
    while(t--){
        cin>>h>>g;
        n = pow(2,h); nn = pow(2,g);
        vector<int> arr(2*n, 0);
        for(int i=1; i<n; i++) cin>>arr[i];

        d = n - nn; xx=0;

        vector<int> ans;
        vector<bool> cc(2*n);

        while( check( 1, arr, 1, ans, cc) );

        ll sum=0;
        for(int i=1; i<nn; i++) sum+=arr[i];
        //for(int i=1; i<nn; i++) cout<<arr[i]<<" "; cout<<endl;
        cout<<sum<<endl;
        for(int i=0; i<d; i++) cout<<ans[i]<<" "; cout<<endl;
    }
}