import java.io.*;
import java.util.*;

public class CF1330E extends PrintWriter {
	CF1330E() { super(System.out); }
	static class Scanner {
		Scanner(InputStream in) { this.in = in; } InputStream in;
		int k, l; byte[] bb = new byte[1 << 15];
		byte getc() {
			if (k >= l) {
				k = 0;
				try { l = in.read(bb); } catch (IOException e) { l = 0; }
				if (l <= 0) return -1;
			}
			return bb[k++];
		}
		int nextInt() {
			byte c = 0; while (c <= 32) c = getc();
			int a = 0;
			while (c > 32) { a = a * 10 + c - '0'; c = getc(); }
			return a;
		}
	}
	Scanner sc = new Scanner(System.in);
	public static void main(String[] $) {
		CF1330E o = new CF1330E(); o.main(); o.flush();
	}

	int[] aa, cc; int cnt;
	void fill(int i, int d) {
		cc[cnt++] = aa[i];
		if (d > 0) {
			d--;
			fill(i << 1, d);
			fill(i << 1 | 1, d);
		}
	}
	void merge(int[] bl, int kl, int[] br, int kr) {
		int i = 1, il = 0, ir = 0;
		while (il < kl && ir < kr)
			if (bl[il] < br[ir])
				cc[i++] = bl[il++];
			else
				cc[i++] = br[ir++];
		while (il < kl)
			cc[i++] = bl[il++];
		while (ir < kr)
			cc[i++] = br[ir++];
	}
	void main() {
		int t = sc.nextInt();
		while (t-- > 0) {
			int h = sc.nextInt();
			int g = sc.nextInt();
			int m = 1 << h;
			int n = 1 << g;
			aa = new int[m];
			long sum = 0;
			for (int i = 1; i < m; i++) {
				int a = sc.nextInt();
				sum += a;
				aa[i] = -a;
			}
			int[] kk = new int[n];
			int[][] bb = new int[n][];
			for (int i = n - 1; i >= n / 2; i--) {
				int k = (1 << h - g + 1) - 1;
				kk[i] = k;
				bb[i] = cc = new int[k];
				cnt = 0;
				fill(i, h - g);
				Arrays.sort(cc);
			}
			int[] ff = new int[n];
			for (int i = n / 2 - 1; i >= 1; i--) {
				int l = i << 1, r = l | 1;
				int bl = bb[l][kk[l] - 1], br = bb[r][kk[r] - 1];
				int b = Math.min(bl, br), j = bl < br ? r : l;
				int k = 0;
				while (bb[j][k] < b)
					k++;
				for (int x = k; x < kk[j] - 1; x++)
					sum += bb[j][x];
				kk[j] -= ff[j] = kk[j] - 1 - k;
				kk[i] = k = 1 + (kk[l] - 1) + (kk[r] - 1);
				bb[i] = cc = new int[k];
				cc[0] = aa[i];
				merge(bb[l], kk[l] - 1, bb[r], kk[r] - 1);
			}
			ff[1] = kk[1] - 1;
			for (int x = 0; x < kk[1] - 1; x++)
				sum += bb[1][x];
			println(sum);
			for (int i = 1; i < n; i++)
				while (ff[i]-- > 0)
					print(i + " ");
			println();
		}
	}
}


import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map.Entry;

import java.util.Random;
import java.util.TreeSet;

public final class CF_631_D2_E
{

	static boolean verb=true;
	static void log(Object X){if (verb) System.err.println(X);}
	static void log(Object[] X){if (verb) {for (Object U:X) System.err.print(U+" ");System.err.println("");}}
	static void log(int[] X){if (verb) {for (int U:X) System.err.print(U+" ");System.err.println("");}}
	static void log(int[] X,int L){if (verb) {for (int i=0;i<L;i++) System.err.print(X[i]+" ");System.err.println("");}}
	static void log(long[] X){if (verb) {for (long U:X) System.err.print(U+" ");System.err.println("");}}

	static void logWln(Object X){if (verb) System.err.print(X);}
	static void info(Object o){	System.out.println(o);}
	static void output(Object o){outputWln(""+o+"\n");	}
	static void outputFlush(Object o){try {out.write(""+ o+"\n");out.flush();} catch (Exception e) {}}
	static void outputWln(Object o){try {out.write(""+ o);} catch (Exception e) {}}

	// Global vars
	static BufferedWriter out;
	static InputReader reader;

	static int pgcd(int a,int b) {
		if (a<b)
			return pgcd(b,a);
		while (b>0) {
			int c=b;
			b=a%b;
			a=c;
		}
		return a;

	}

	static long pgcd(long a,long b) {
		if (a<b)
			return pgcd(b,a);
		while (b>0) {
			long c=b;
			b=a%b;
			a=c;
		}
		return a;

	}

	static void test() {
		log("testing");

		Random r=new Random();
		int NTESTS=100000;
		int NMAX=100;
		for (int t=0;t<NTESTS;t++) {

		}

		log("done");

	}

	static int st;

	static boolean canDo(int n,int m,int[] a,int node) {
		boolean ans=true;
		if (a[node]==0) {
			ans=false;
		} else {
			int left=node<<1;
			int right=left+1;
			if (right<=n) {
				if (node>m) {
					ans=true;
				} else
					if (node==m) {
						ans=a[left]!=0 || a[right]!=0;
					} else {
						// now node<m
						if (a[left]>a[right])
							ans=canDo(n,m,a,left);
						else
							ans=canDo(n,m,a,right);
					}
			} else {
				// always ok, as we are supposed to have n<m, so can always remove last rank
			}
		}
		//cache[node]=ans;
		return ans;
	}

	static void execute(int n,int[] a,int node) {
		int left=node<<1;
		int right=left+1;
		if (right>n) {
			//log("node:"+node+" case 1 // right:"+right+" n:"+n);
			a[node]=0;
			return;
		}
		if (a[left]>a[right]) {
			//log("node:"+node+" case 2left");
			a[node]=a[left];
			execute(n,a,left);
		}
		else {
			//log("node:"+node+" case 2right");
			a[node]=a[right];
			execute(n,a,right);
		}
	}

	static int[] moves;

	static void solveRec(int n,int m,int[] a,int node) {
		while (canDo(n,m,a,node)) {
			execute(n,a,node);
			//log(a);
			moves[st++]=node;
		}
		if (2*node+1<=n) {
			solveRec(n,m,a,2*node);
			solveRec(n,m,a,2*node+1);
		}
	}

	static void solve(int n,int m,int[] a) {
		moves=new int[n-m];
		st=0;
		solveRec(n,m,a,1);
		long sum=0;
		for (int i=1;i<=m;i++) {
			sum+=a[i];
		}
		output(sum);
		for (int x:moves)
			outputWln(x+" ");
		output("");
		//log(a);
	}

	static void process() throws Exception {

		//log("hello");

		out = new BufferedWriter(new OutputStreamWriter(System.out));
		reader = new InputReader(System.in);

		//testAll(20);

		int T=reader.readInt();
		for (int t=0;t<T;t++) {
			//log("--------------");
			int h=reader.readInt();
			int g=reader.readInt();
			int n=(1<<h)-1;
			int m=(1<<g)-1;

			int[] a=new int[n+1];
			for (int i=1;i<=n;i++)
				a[i]=reader.readInt();
			solve(n,m,a);

		}

		try {
			out.close();
		} catch (Exception Ex) {
		}

	}

	public static void main(String[] args) throws Exception {
		process();

	}

	static final class InputReader {
		private final InputStream stream;
		private final byte[] buf = new byte[1024];
		private int curChar;
		private int numChars;

		public InputReader(InputStream stream) {
			this.stream = stream;
		}

		private int read() throws IOException {
			if (curChar >= numChars) {
				curChar = 0;
				numChars = stream.read(buf);
				if (numChars <= 0) {
					return -1;
				}
			}
			return buf[curChar++];
		}

		public final String readString() throws IOException {
			int c = read();
			while (isSpaceChar(c)) {
				c = read();
			}
			StringBuilder res = new StringBuilder();
			do {
				res.append((char) c);
				c = read();
			} while (!isSpaceChar(c));
			return res.toString();
		}

		public final String readString(int L) throws IOException {
			int c = read();
			while (isSpaceChar(c)) {
				c = read();
			}
			StringBuilder res = new StringBuilder(L);
			do {
				res.append((char) c);
				c = read();
			} while (!isSpaceChar(c));
			return res.toString();
		}

		public final int readInt() throws IOException {
			int c = read();
			boolean neg = false;
			while (isSpaceChar(c)) {
				c = read();
			}
			char d = (char) c;
			// log("d:"+d);
			if (d == '-') {
				neg = true;
				c = read();
			}
			int res = 0;
			do {
				res *= 10;
				res += c - '0';
				c = read();
			} while (!isSpaceChar(c));
			// log("res:"+res);
			if (neg)
				return -res;
			return res;

		}

		public final long readLong() throws IOException {
			int c = read();
			boolean neg = false;
			while (isSpaceChar(c)) {
				c = read();
			}
			char d = (char) c;
			// log("d:"+d);
			if (d == '-') {
				neg = true;
				c = read();
			}
			long res = 0;
			do {
				res *= 10;
				res += c - '0';
				c = read();
			} while (!isSpaceChar(c));
			// log("res:"+res);
			if (neg)
				return -res;
			return res;

		}

		private boolean isSpaceChar(int c) {
			return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
		}
	}

}
/*

5
1 4 5 2 3

 */


import java.util.*;
import java.io.*;

public class E {

    public static void main(String args[]) throws IOException {
        FastScanner in = new FastScanner(System.in);
        BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
        int t = in.nextInt();
        while (t-- > 0) {
            solve(in, out);
        }
        out.flush();
    }

    public static void solve(FastScanner in, BufferedWriter out) throws IOException {
        int h = in.nextInt();
        int g = in.nextInt();
        int n = (1 << h) - 1;
        int o = (1 << g) - 1;
        Pair[] inp = new Pair[n + 1];
        Pair[] oup = new Pair[o + 1];
        for (int i = 1; i <= n; i++) {
            inp[i] = new Pair(in.nextInt(), i);
        }
        int sum = 0;
        Set<Integer> r = new TreeSet<>();
        Stack<Integer> s = new Stack<>();
        s.push(1);
        while (!s.isEmpty()) {
            int pos = s.peek();
            int lp = pos * 2;
            int rp = pos * 2 + 1;
            if (lp > o) ;
            else if (oup[lp] == null || oup[rp] == null) {
                s.push(lp);
                s.push(rp);
                continue;
            }
            s.pop();
            Pair pv = new Pair(Integer.MAX_VALUE, 0);
            int below = 0;
            if (lp < o + 1) below = Math.max(oup[lp].a, oup[rp].a);
            Queue<Integer> q = new LinkedList<>();
            q.add(pos);
            while (!q.isEmpty()) {
                int a = q.poll();
                if (inp[a].a > below && pv.a > inp[a].a) {
                    pv = inp[a];
                }
                if (a * 2 <= n) {
                    q.add(2 * a);
                    q.add(2 * a + 1);
                }
            }
            oup[pos] = pv;
            oup[pos].removed = false;
            sum += pv.a;
        }
        out.write(sum + "\n");
        boolean b = false;
        for (int i = n; i > 0; i--) {
            if (inp[i].removed) {
                out.write(i + (i == 1 ? "" : " "));
                int lp = 2 * i;
                int rp = 2 * i + 1;
                if (lp <= n) {
                    if (inp[lp].a > inp[rp].a) {
                        inp[i] = inp[lp];
                    } else {
                        inp[i] = inp[rp];
                    }
                } else {
                    inp[i].a = 0;
                }
            }
        }
        out.write("\n");
    }

    static class FastScanner {
        private BufferedReader reader;

        private StringTokenizer tokenizer;

        public FastScanner(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream));
            tokenizer = null;
        }

        public FastScanner(InputStreamReader stream) {
            reader = new BufferedReader(stream);
            tokenizer = null;
        }

        public String nextLine() {
            try {
                return reader.readLine();
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public double nextDouble() {
            return Double.parseDouble(next());
        }
    }

    public static class Pair implements Comparator<Pair> {

        public int a;
        public int b;
        public boolean removed = true;

        public Pair(int a, int b) {
            this.a = a;
            this.b = b;
        }

        @Override
        public int compare(Pair pair, Pair t1) {
            return pair.a - t1.a;
        }
    }
}


import java.util.*;
import java.io.*;

public class E {

    public static void main(String args[]) throws IOException {
        FastScanner in = new FastScanner(System.in);
        BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
        int t = in.nextInt();
        while (t-- > 0) {
            solve(in, out);
        }
        out.flush();
    }

    public static void solve(FastScanner in, BufferedWriter out) throws IOException {
        int h = in.nextInt();
        int g = in.nextInt();
        int n = (1 << h) - 1;
        int o = (1 << g) - 1;
        int[] inp = new int[n + 1];
        int[] oup = new int[o + 1];
        for (int i = 1; i <= n; i++) {
            inp[i] = in.nextInt();
        }
        Set<Integer> r = new TreeSet<>();
        Stack<Integer> s = new Stack<>();
        s.push(1);
        while (!s.isEmpty()) {
            int pos = s.peek();
            int lp = pos * 2;
            int rp = pos * 2 + 1;
            if (lp > o) ;
            else if (oup[lp] == 0 || oup[rp] == 0) {
                s.push(lp);
                s.push(rp);
                continue;
            }
            s.pop();
            int inPos = Integer.MAX_VALUE;
            int below = 0;
            if (lp < o + 1) below = Math.max(oup[lp], oup[rp]);
            Queue<Integer> q = new LinkedList<>();
            q.add(pos);
            while (!q.isEmpty()) {
                int a = q.poll();
                if (inp[a] > below) inPos = Math.min(inPos, inp[a]);
                if (a * 2 <= n) {
                    q.add(2 * a);
                    q.add(2 * a + 1);
                }
            }
            oup[pos] = inPos;
            r.add(inPos);
        }
        int sum = 0;
        for (Integer i: r) sum += i;
        out.write(sum + "\n");
        boolean b = false;
        for (int i = n; i > 0; i--) {
            if (!r.contains(inp[i])) {
                out.write(i + (i == 1 ? "" : " "));
                int lp = 2 * i;
                int rp = 2 * i + 1;
                if (lp <= n) {
                    inp[i] = Math.max(inp[lp], inp[rp]);
                } else {
                    inp[i] = 0;
                }
            }
        }
        out.write("\n");
    }

    static class FastScanner {
        private BufferedReader reader;

        private StringTokenizer tokenizer;

        public FastScanner(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream));
            tokenizer = null;
        }

        public FastScanner(InputStreamReader stream) {
            reader = new BufferedReader(stream);
            tokenizer = null;
        }

        public String nextLine() {
            try {
                return reader.readLine();
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public double nextDouble() {
            return Double.parseDouble(next());
        }
    }
}


import java.util.*;
import java.io.*;

public class E {

    public static void main(String args[]) throws IOException {
        FastScanner in = new FastScanner(System.in);
        BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
        int t = in.nextInt();
        while (t-- > 0) {
            solve(in, out);
        }
        out.flush();
    }

    public static void solve(FastScanner in, BufferedWriter out) throws IOException {
        int h = in.nextInt();
        int g = in.nextInt();
        int n = (1 << h) - 1;
        int o = (1 << g) - 1;
        int[] inp = new int[n + 1];
        int[] oup = new int[o + 1];
        for (int i = 1; i <= n; i++) {
            inp[i] = in.nextInt();
        }
        Set<Integer> r = new HashSet<>();
        Stack<Integer> s = new Stack<>();
        s.push(1);
        while (!s.isEmpty()) {
            int pos = s.peek();
            int lp = pos * 2;
            int rp = pos * 2 + 1;
            if (lp > o) ;
            else if (oup[lp] == 0 || oup[rp] == 0) {
                s.push(lp);
                s.push(rp);
                continue;
            }
            s.pop();
            int inPos = Integer.MAX_VALUE;
            int below = 0;
            if (lp < o + 1) below = Math.max(oup[lp], oup[rp]);
            Queue<Integer> q = new LinkedList<>();
            q.add(pos);
            while (!q.isEmpty()) {
                int a = q.poll();
                if (inp[a] > below) inPos = Math.min(inPos, inp[a]);
                if (a * 2 <= n) {
                    q.add(2 * a);
                    q.add(2 * a + 1);
                }
            }
            oup[pos] = inPos;
            r.add(inPos);
        }
        int sum = 0;
        for (Integer i: r) sum += i;
        out.write(sum + "\n");
        boolean b = false;
        for (int i = n; i > 0; i--) {
            if (!r.contains(inp[i])) {
                out.write(i + (i == 1 ? "" : " "));
                int lp = 2 * i;
                int rp = 2 * i + 1;
                if (lp <= n) {
                    inp[i] = Math.max(inp[lp], inp[rp]);
                } else {
                    inp[i] = 0;
                }
            }
        }
        out.write("\n");
    }

    static class FastScanner {
        private BufferedReader reader;

        private StringTokenizer tokenizer;

        public FastScanner(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream));
            tokenizer = null;
        }

        public FastScanner(InputStreamReader stream) {
            reader = new BufferedReader(stream);
            tokenizer = null;
        }

        public String nextLine() {
            try {
                return reader.readLine();
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public double nextDouble() {
            return Double.parseDouble(next());
        }
    }
}


import java.util.*;
import java.io.*;

public class E {

    public static void main(String args[]) throws IOException {
        FastScanner in = new FastScanner(System.in);
        BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
        int t = in.nextInt();
        while (t-- > 0) {
            solve(in, out);
        }
        out.flush();
    }

    public static void solve(FastScanner in, BufferedWriter out) throws IOException {
        int h = in.nextInt();
        int g = in.nextInt();
        int n = (1 << h) - 1;
        int o = (1 << g) - 1;
        Pair[] inp = new Pair[n + 1];
        Pair[] oup = new Pair[o + 1];
        for (int i = 1; i <= n; i++) {
            inp[i] = new Pair(in.nextInt(), i);
        }
        long sum = 0;
        Set<Integer> r = new TreeSet<>();
        Stack<Integer> s = new Stack<>();
        s.push(1);
        while (!s.isEmpty()) {
            int pos = s.peek();
            int lp = pos * 2;
            int rp = pos * 2 + 1;
            if (lp > o) ;
            else if (oup[lp] == null || oup[rp] == null) {
                s.push(lp);
                s.push(rp);
                continue;
            }
            s.pop();
            Pair pv = new Pair(Integer.MAX_VALUE, 0);
            int below = 0;
            if (lp < o + 1) below = Math.max(oup[lp].a, oup[rp].a);
            Queue<Integer> q = new LinkedList<>();
            q.add(pos);
            while (!q.isEmpty()) {
                int a = q.poll();
                if (inp[a].a > below && pv.a > inp[a].a) {
                    pv = inp[a];
                }
                if (a * 2 <= n) {
                    q.add(2 * a);
                    q.add(2 * a + 1);
                }
            }
            oup[pos] = pv;
            oup[pos].removed = false;
            sum += pv.a;
        }
        out.write(sum + "\n");
        boolean b = false;
        for (int i = n; i > 0; i--) {
            if (inp[i].removed) {
                out.write(i + (i == 1 ? "" : " "));
                int lp = 2 * i;
                int rp = 2 * i + 1;
                if (lp <= n) {
                    if (inp[lp].a > inp[rp].a) {
                        inp[i] = inp[lp];
                    } else {
                        inp[i] = inp[rp];
                    }
                } else {
                    inp[i].a = 0;
                }
            }
        }
        out.write("\n");
    }

    static class FastScanner {
        private BufferedReader reader;

        private StringTokenizer tokenizer;

        public FastScanner(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream));
            tokenizer = null;
        }

        public FastScanner(InputStreamReader stream) {
            reader = new BufferedReader(stream);
            tokenizer = null;
        }

        public String nextLine() {
            try {
                return reader.readLine();
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public double nextDouble() {
            return Double.parseDouble(next());
        }
    }

    public static class Pair implements Comparator<Pair> {

        public int a;
        public int b;
        public boolean removed = true;

        public Pair(int a, int b) {
            this.a = a;
            this.b = b;
        }

        @Override
        public int compare(Pair pair, Pair t1) {
            return pair.a - t1.a;
        }
    }
}


#include "bits/stdc++.h"
#pragma GCC optimize "03"
using namespace std;

#define int long long int
#define ld long double
#define pi pair<int, int>
#define pb push_back
#define fi first
#define se second
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#ifndef LOCAL
#define endl '\n'
#endif

const int N = 3e6 + 5;
const int mod = 1e9 + 7;
const int inf = 1e9 + 9;

int a[N];
int h, g, sum;
vector<int> t, res;

void dfs2(int x){
	t.push_back(x);
	int l = x << 1, r = x << 1 | 1;
	if(a[l] == 0 && a[r] == 0)
		return;
	if(a[l] > a[r])
		dfs2(l);
	else
		dfs2(r);
}

void dfs(int u, int hh){
	while(true){
		t.clear();
		dfs2(u);
		if(hh+(int)t.size()-1 <= g)
			break;
		sum -= a[u];
		res.push_back(u);
		for(int i = 0; i < (int)t.size() - 1; i++)
			a[t[i]] = a[t[i+1]];
		a[t[(int)t.size()-1]] = 0;
	}
	int l = u << 1, r = u << 1 | 1;
	if(a[l] != 0)
		dfs(l, hh+1);
	if(a[r] != 0)
		dfs(r, hh+1);
}

signed main() {
	IOS;
	int t;	cin >> t;
	while(t--){
		cin >> h >> g;
		sum = 0;
		for(int i = 1; i < (1 << h); i++)
			cin >> a[i], sum += a[i];
		for(int i = (1<<h); i <= (1<<(h+1)); i++)
			a[i] = 0;
		dfs(1, 1);
		cout << sum << endl;
		for(auto i: res)
			cout << i << " ";
		cout << endl;
		res.clear();
	}
	return 0;
}


#include "bits/stdc++.h"
#pragma GCC optimize "03"
using namespace std;

#define ll long long int
#define ld long double
#define pi pair<int, int>
#define pb push_back
#define fi first
#define se second
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#ifndef LOCAL
#define endl '\n'
#endif

const int N = 3e6 + 5;
const int mod = 1e9 + 7;
const int inf = 1e9 + 9;

int a[N];
int h, g, sum;
vector<int> t, res;

void dfs2(int x){
	t.push_back(x);
	int l = x << 1, r = x << 1 | 1;
	if(a[l] == 0 && a[r] == 0)
		return;
	if(a[l] > a[r])
		dfs2(l);
	else
		dfs2(r);
}

void dfs(int u, int hh){
	while(true){
		t.clear();
		dfs2(u);
		if(hh+(int)t.size()-1 <= g)
			break;
		sum -= a[u];
		res.push_back(u);
		for(int i = 0; i < (int)t.size() - 1; i++)
			a[t[i]] = a[t[i+1]];
		a[t[(int)t.size()-1]] = 0;
	}
	int l = u << 1, r = u << 1 | 1;
	if(a[l] != 0)
		dfs(l, hh+1);
	if(a[r] != 0)
		dfs(r, hh+1);
}

signed main() {
	IOS;
	int t;	cin >> t;
	while(t--){
		cin >> h >> g;
		sum = 0;
		for(int i = 1; i < (1 << h); i++)
			cin >> a[i], sum += a[i];
		for(int i = (1<<h); i <= (1<<(h+1)); i++)
			a[i] = 0;
		dfs(1, 1);
		cout << sum << endl;
		for(auto i: res)
			cout << i << " ";
		cout << endl;
		res.clear();
	}
	return 0;
}


#include<bits/stdc++.h>
#define pb push_back
#define fast ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define f first
#define s second
#define int long long
#define sz(x) (ll)(x.size())
using namespace std;

const int mod = 1e9+7;

int expo_pow(int x,int y){
  y=y%(mod-1);
  if(y==0) y=mod-1;
  int res=1;
  while(y){
    if(y&1) res=(res*x)%mod;
    x=(x*x)%mod;
    y>>=1;
  }
  return res;
}

vector<int> heap;
int g,h;

int func(int idx){
  int tmp=heap[idx];
  int left=2*idx;
  int right=2*idx+1;
  heap[idx]=0;
  vector<int> qq;
  qq.pb(idx);
  while(idx<(1<<h) and heap[idx]<max(heap[left],heap[right])){
    if(heap[left]>heap[right]){
      swap(heap[left],heap[idx]);
      idx=left;
    }
    else{
      swap(heap[right],heap[idx]);
      idx=right;
    }
    left=2*idx;
    right=2*idx+1;
    qq.pb(idx);

  }
  if(idx<(1<<g)){
    for(int i=qq.size()-1;i>0;i--){
      heap[qq[i]]=heap[qq[i-1]];
    }
    heap[qq[0]]=tmp;
    return 0;
  }
  return tmp;
}

signed main(){
  fast;
  int test;
  cin>>test;
  while(test--){
    cin>>h>>g;
    heap.clear();
    heap.assign(2*(1<<h)+2,0);
    int sum=0;
    for(int i=1;i<(1<<h);i++){
      cin>>heap[i];
      sum+=heap[i];
    }
    vector<int> ans;
    for(int i=1;i<(1<<g);i++){
      while(1){
        int tmp=func(i);
        if(tmp){
          sum-=tmp;
          ans.pb(i);
        }
        else break;
      }
    }
    cout<<sum<<"\n";
    for(auto  u:ans) cout<<u<<" ";
    cout<<"\n";
  }
}


#include<iostream>
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define mod 1000000007
#define inarr(arr,n); for(int i=0;i<n;i++) cin >> arr[i];
#define outarr(arr,n); for(int i=0;i<n;i++) cout<<arr[i]<<' ';
#define ios ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define ve vector
#define fi first
#define se second
#define pa pair
#define fastio  ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define lld long long double
#define fr(i, a, b); for (int i = a; i < b; i++)
#define frn(i, a, b); for (int i = a; i > b; i--)
#define mk make_pair
#define setvector(vname,value,size); for(int i=0;i<size;i++)vname[i]=value;
const ll inf = 1e18;
int t,n,g,h,siz1,siz2,curr,sum,tes;
ve<int> arr(3e6+1,0),crr,ans;
set<pa<int,int>> ma;
int main(void)
{
    fastio;
    cin>>tes;
    while(tes--)
    {
        ma.clear();
        ans.clear();
        sum=0;
        cin>>h>>g;
        siz1=pow(2,h),siz2=pow(2,g);
        fr(i,1,siz1)
        {
            cin>>arr[i];
            ma.insert(mk(arr[i],i));
        }
        fr(i,1,siz1-siz2+1)
        {
            crr.clear();
            auto it=ma.end();
            it--;
            curr=it->se;
            t=curr;
            crr.pb(t);
            n=log2(curr)+1;
            while(2*t<siz1 && 2*t+1<siz1 && (arr[2*t]!=0 || arr[2*t+1]!=0))
            {
                n++;
                t=((arr[2*t]>=arr[2*t+1])?2*t:2*t+1);
                crr.pb(t);
            }
            if(n<=g)
            {
                fr(j,0,crr.size())
                    ma.erase(mk(arr[crr[j]],crr[j]));
                i--;
            }
            else
            {
                n=arr[crr[crr.size()-1]];
                ma.erase(mk(arr[crr[crr.size()-1]],crr[crr.size()-1]));
                ma.insert(mk(0,crr[crr.size()-1]));
                arr[crr[crr.size()-1]]=0;
                frn(j,crr.size()-2,-1)
                {
                    t=arr[crr[j]];
                    ma.erase(mk(arr[crr[j]],crr[j]));
                    ma.insert(mk(n,crr[j]));
                    arr[crr[j]]=n;
                    n=t;
                }
                ans.pb(curr);
            }
        }
        fr(i,1,siz2)
            sum+=arr[i];
        cout<<sum<<endl;
        fr(i,0,ans.size())
            cout<<ans[i]<<" ";
        cout<<endl;
    }
}


#include<iostream>
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define mod 1000000007
#define inarr(arr,n); for(int i=0;i<n;i++) cin >> arr[i];
#define outarr(arr,n); for(int i=0;i<n;i++) cout<<arr[i]<<' ';
#define ios ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define ve vector
#define fi first
#define se second
#define pa pair
#define fastio  ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define lld long long double
#define fr(i, a, b); for (int i = a; i < b; i++)
#define frn(i, a, b); for (int i = a; i > b; i--)
#define mk make_pair
#define setvector(vname,value,size); for(int i=0;i<size;i++)vname[i]=value;
const ll inf = 1e18;
int t,n,g,h,siz1,siz2,curr,sum,tes,siz;
ve<int> arr(3e6+1,0),crr(24,0),ans;
ve<bool> cor(3e6+1,false);
int main(void)
{
    fastio;
    cin>>tes;
    while(tes--)
    {
        ans.clear();
        sum=0;
        cin>>h>>g;
        siz1=pow(2,h),siz2=pow(2,g);
        fr(i,0,siz1)
            cor[i]=true;
        fr(i,1,siz1)
            cin>>arr[i];
        curr=1;
        fr(i,1,siz1-siz2+1)
        {
            siz=0;
            t=curr;
            crr[siz]=t;
            siz++;
            n=log2(curr)+1;
            while(2*t<siz1 && 2*t+1<siz1 && (arr[2*t]!=0 || arr[2*t+1]!=0))
            {
                n++;
                t=((arr[2*t]>=arr[2*t+1])?2*t:2*t+1);
                crr[siz]=t;
                siz++;
            }
            if(n<=g)
            {
                fr(j,0,siz)
                    cor[crr[j]]=false;
                i--;
                while(cor[curr]!=true || arr[curr]==0)
                    curr++;
            }
            else
            {
                n=arr[crr[siz-1]];
                arr[crr[siz-1]]=0;
                frn(j,siz-2,-1)
                {
                    t=arr[crr[j]];
                    arr[crr[j]]=n;
                    n=t;
                }
                ans.pb(curr);
            }
        }
        fr(i,1,siz2)
            sum+=arr[i];
        cout<<sum<<endl;
        fr(i,0,ans.size())
            cout<<ans[i]<<" ";
        cout<<endl;
    }
}


#include<iostream>
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define mod 1000000007
#define inarr(arr,n); for(int i=0;i<n;i++) cin >> arr[i];
#define outarr(arr,n); for(int i=0;i<n;i++) cout<<arr[i]<<' ';
#define ios ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define ve vector
#define fi first
#define se second
#define pa pair
#define fastio  ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define lld long long double
#define fr(i, a, b); for (int i = a; i < b; i++)
#define frn(i, a, b); for (int i = a; i > b; i--)
#define mk make_pair
#define setvector(vname,value,size); for(int i=0;i<size;i++)vname[i]=value;
const ll inf = 1e18;
ll t,n,g,h,siz1,siz2,curr,sum,tes;
ve<ll> arr,crr,ans;
set<pa<ll,ll>> ma;
int main(void)
{
    fastio;
    cin>>tes;
    while(tes--)
    {
        ma.clear();
        ans.clear();
        sum=0;
        arr.clear();
        cin>>h>>g;
        siz1=pow(2,h),siz2=pow(2,g);
        arr.resize(2*siz1,0);
        fr(i,1,siz1)
        {
            cin>>arr[i];
            ma.insert(mk(arr[i],i));
        }
        fr(i,1,siz1-siz2+1)
        {
            crr.clear();
            auto it=ma.end();
            it--;
            curr=it->se;
            t=curr;
            crr.pb(t);
            n=log2(curr)+1;
            while(2*t<siz1 && 2*t+1<siz1 && (arr[2*t]!=0 || arr[2*t+1]!=0))
            {
                n++;
                t=((arr[2*t]>=arr[2*t+1])?2*t:2*t+1);
                crr.pb(t);
            }
            if(n<=g)
            {
                ma.erase(mk(arr[curr],curr));
                i--;
            }
            else
            {
                n=arr[crr[crr.size()-1]];
                ma.erase(mk(arr[crr[crr.size()-1]],crr[crr.size()-1]));
                ma.insert(mk(0,crr[crr.size()-1]));
                arr[crr[crr.size()-1]]=0;
                frn(j,crr.size()-2,-1)
                {
                    t=arr[crr[j]];
                    ma.erase(mk(arr[crr[j]],crr[j]));
                    ma.insert(mk(n,crr[j]));
                    arr[crr[j]]=n;
                    n=t;
                }
                ans.pb(curr);
            }
        }
        fr(i,1,siz2)
            sum+=arr[i];
        cout<<sum<<endl;
        fr(i,0,ans.size())
            cout<<ans[i]<<" ";
        cout<<endl;
    }
}


#include<iostream>
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define mod 1000000007
#define inarr(arr,n); for(int i=0;i<n;i++) cin >> arr[i];
#define outarr(arr,n); for(int i=0;i<n;i++) cout<<arr[i]<<' ';
#define ios ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define ve vector
#define fi first
#define se second
#define pa pair
#define fastio  ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define lld long long double
#define fr(i, a, b); for (int i = a; i < b; i++)
#define frn(i, a, b); for (int i = a; i > b; i--)
#define mk make_pair
#define setvector(vname,value,size); for(int i=0;i<size;i++)vname[i]=value;
const ll inf = 1e18;
int t,n,g,h,siz1,siz2,curr,sum,tes;
ve<int> arr,crr,ans;
set<pa<int,int>> ma;
int main(void)
{
    fastio;
    cin>>tes;
    while(tes--)
    {
        ma.clear();
        ans.clear();
        sum=0;
        arr.clear();
        cin>>h>>g;
        siz1=pow(2,h),siz2=pow(2,g);
        arr.resize(2*siz1,0);
        fr(i,1,siz1)
        {
            cin>>arr[i];
            ma.insert(mk(arr[i],i));
        }
        fr(i,1,siz1-siz2+1)
        {
            crr.clear();
            auto it=ma.end();
            it--;
            curr=it->se;
            t=curr;
            crr.pb(t);
            n=log2(curr)+1;
            while(2*t<siz1 && 2*t+1<siz1 && (arr[2*t]!=0 || arr[2*t+1]!=0))
            {
                n++;
                t=((arr[2*t]>=arr[2*t+1])?2*t:2*t+1);
                crr.pb(t);
            }
            if(n<=g)
            {
                ma.erase(mk(arr[curr],curr));
                i--;
            }
            else
            {
                n=arr[crr[crr.size()-1]];
                ma.erase(mk(arr[crr[crr.size()-1]],crr[crr.size()-1]));
                ma.insert(mk(0,crr[crr.size()-1]));
                arr[crr[crr.size()-1]]=0;
                frn(j,crr.size()-2,-1)
                {
                    t=arr[crr[j]];
                    ma.erase(mk(arr[crr[j]],crr[j]));
                    ma.insert(mk(n,crr[j]));
                    arr[crr[j]]=n;
                    n=t;
                }
                ans.pb(curr);
            }
        }
        fr(i,1,siz2)
            sum+=arr[i];
        cout<<sum<<endl;
        fr(i,0,ans.size())
            cout<<ans[i]<<" ";
        cout<<endl;
    }
}


#include<iostream>
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define mod 1000000007
#define inarr(arr,n); for(int i=0;i<n;i++) cin >> arr[i];
#define outarr(arr,n); for(int i=0;i<n;i++) cout<<arr[i]<<' ';
#define ios ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define ve vector
#define fi first
#define se second
#define pa pair
#define fastio  ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define lld long long double
#define fr(i, a, b); for (int i = a; i < b; i++)
#define frn(i, a, b); for (int i = a; i > b; i--)
#define mk make_pair
#define setvector(vname,value,size); for(int i=0;i<size;i++)vname[i]=value;
const ll inf = 1e18;
ll t,n,g,h,siz1,siz2,curr,sum,tes,siz;
ve<ll> arr(3e6+1,0),crr(24,0),ans;
ve<bool> cor(3e6+1,false);
int main(void)
{
    fastio;
    cin>>tes;
    while(tes--)
    {
        ans.clear();
        sum=0;
        cin>>h>>g;
        siz1=pow(2,h),siz2=pow(2,g);
        fr(i,0,siz1)
            cor[i]=true;
        fr(i,1,siz1)
            cin>>arr[i];
        curr=1;
        fr(i,1,siz1-siz2+1)
        {
            siz=0;
            t=curr;
            crr[siz]=t;
            siz++;
            n=log2(curr)+1;
            while(2*t<siz1 && 2*t+1<siz1 && (arr[2*t]!=0 || arr[2*t+1]!=0))
            {
                n++;
                t=((arr[2*t]>=arr[2*t+1])?2*t:2*t+1);
                crr[siz]=t;
                siz++;
            }
            if(n<=g)
            {
                fr(j,0,siz)
                    cor[crr[j]]=false;
                i--;
                while(cor[curr]!=true || arr[curr]==0)
                    curr++;
            }
            else
            {
                n=arr[crr[siz-1]];
                arr[crr[siz-1]]=0;
                frn(j,siz-2,-1)
                {
                    t=arr[crr[j]];
                    arr[crr[j]]=n;
                    n=t;
                }
                ans.pb(curr);
            }
        }
        fr(i,1,siz2)
            sum+=arr[i];
        cout<<sum<<endl;
        fr(i,0,ans.size())
            cout<<ans[i]<<" ";
        cout<<endl;
    }
}


#define _USE_MATH_DEFINES
#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define reps(i, n) for (int i = 1; i <= (n); ++i)
#define repd(i, n) for (int i = (n - 1); i >= 0; --i)
#define repds(i, n) for (int i = (n); i > 0; --i)
#define loop(i, x, n) for (int i = (x); i < (n); ++i)
#define loops(i, x, n) for (int i = (x); i <= (n); ++i)
#define loopd(i, x, n) for (int i = (x); i > (n); --i)
#define loopds(i, x, n) for (int i = (x); i >= (n); --i)
#define itrep(i, a) for (auto i = begin(a); i != end(a); ++i)
#define itrepd(i, a) for (auto i = rbegin(a); i != rend(a); ++i)
#define each(i, x) for (auto &i : x)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
template <class T>
using priority_queuel = priority_queue<T, vector<T>, greater<T>>;
constexpr ll inf = static_cast<ll>(1e18);
constexpr int iinf = static_cast<int>(1e9);
constexpr double dinf = 1e10;
constexpr ld ldinf = 1e18;
constexpr double eps = 1e-9;
ll mod = static_cast<ll>(1e9 + 7);
template <class T>
constexpr int sz(const T &a) { return (int)a.size(); }
template <class T>
constexpr T pow2(const T a) { return a * a; }
template <class T, class... Args>
constexpr void mins(T &a, Args... args) { a = min<T>({a, args...}); }
template <class T, class... Args>
constexpr void maxs(T &a, Args... args) { a = max<T>({a, args...}); }
template <class A, int N, class T>
void fillArray(A (&array)[N], const T &val) { std::fill((T *)array, (T *)(array + N), val); }

template <class T, class U>
struct FirstPair {
    T first;
    U second;
    FirstPair(T fi, U se) : first(fi), second(se) {}
    bool operator<(const FirstPair &x) const {
        return first < x.first;
    }
    bool operator>(const FirstPair &x) const {
        return first > x.first;
    }
};

int h, g;
vector<ll> a;
bool f(int v) {
    if (!a[2 * v] && !a[2 * v + 1]) {
        if (v < (1 << g)) return false;
        a[v] = 0;
    } else {
        auto nv = (a[2 * v] > a[2 * v + 1] ? 2 * v : 2 * v + 1);
        auto tmp = a[nv];
        if (!f(nv)) return false;
        a[v] = tmp;
    }
    return true;
}
void solve() {
    int t;
    cin >> t;
    rep(_, t) {
        cin >> h >> g;
        auto u = 1 << h;
        a.resize(2 * u + 2, 0);
        reps(i, u - 1) cin >> a[i];

        vector<int> ans;
        auto t = 1 << g;
        reps(i, t - 1) {
            while (a[i] && f(i))
                ans.emplace_back(i);
        }

        ll sum = 0;
        reps(i, u - 1) sum += a[i];
        cout << sum << '\n';
        rep(i, sz(ans)) cout << ans[i] << (i < sz(ans) - 1 ? ' ' : '\n');
    }
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
    solve();
    cout.flush();
    return 0;
}


#define _USE_MATH_DEFINES
#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define reps(i, n) for (int i = 1; i <= (n); ++i)
#define repd(i, n) for (int i = (n)-1; i >= 0; --i)
#define repds(i, n) for (int i = (n); i > 0; --i)
#define loop(i, x, n) for (int i = (x); i < (n); ++i)
#define loops(i, x, n) for (int i = (x); i <= (n); ++i)
#define loopd(i, x, n) for (int i = (x); i > (n); --i)
#define loopds(i, x, n) for (int i = (x); i >= (n); --i)
#define itrep(i, a) for (auto i = begin(a); i != end(a); ++i)
#define itrepd(i, a) for (auto i = rbegin(a); i != rend(a); ++i)
#define each(i, x) for (auto &i : x)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
template <class T>
using priority_queuel = priority_queue<T, vector<T>, greater<T>>;
constexpr ll inf = static_cast<ll>(1e18);
constexpr int iinf = static_cast<int>(1e9);
constexpr double dinf = 1e10;
constexpr ld ldinf = 1e18;
constexpr double eps = 1e-9;
ll mod = static_cast<ll>(1e9 + 7);
template <class T>
constexpr int sz(const T &a) { return (int)a.size(); }
template <class T>
constexpr T pow2(const T a) { return a * a; }
template <class T, class... Args>
constexpr void mins(T &a, Args... args) { a = min<T>({a, args...}); }
template <class T, class... Args>
constexpr void maxs(T &a, Args... args) { a = max<T>({a, args...}); }
template <class A, int N, class T>
void fillArray(A (&array)[N], const T &val) { std::fill((T *)array, (T *)(array + N), val); }

template <class T, class U>
struct FirstPair {
    T first;
    U second;
    FirstPair() : first(), second() {}
    FirstPair(T fi, U se) : first(fi), second(se) {}
    bool operator<(const FirstPair &x) const {
        return first < x.first;
    }
    bool operator>(const FirstPair &x) const {
        return first > x.first;
    }
};

int h, g;
vector<FirstPair<int, int>> a;
void pull(int id) {
    while (a[id] > min(a[id << 1], a[(id << 1) | 1])) {
        if (a[id << 1] < a[(id << 1) | 1]) {
            swap(a[id], a[id << 1]);
            id <<= 1;
        } else {
            swap(a[id], a[(id << 1) | 1]);
            id = (id << 1) | 1;
        }
        if (id >= (1 << h)) return;
    }
}
void solve() {
    int t;
    cin >> t;
    rep(_, t) {
        cin >> h >> g;

        a.resize(1 << h);
        reps(i, (1 << h) - 1) {
            cin >> a[i].first;
            a[i].second = i;
        }

        vector<bool> used(1 << h, false);
        vector<int> fv(1 << h, 0);
        ll sum = 0;
        repd(i, h - 1) {
            auto ll = 1 << i;
            auto rr = 1 << (i + 1);
            loop(v, ll, rr) {
                auto tmp = a[v];
                auto bot = v << (h - 1 - i);
                a[v] = a[bot];
                a[bot] = {iinf, 0};
                pull(v);
                if (i < g) {
                    auto mi = max(fv[v << 1], fv[(v << 1) | 1]);
                    while (a[v].first < mi) {
                        a[v] = {iinf, 0};
                        pull(v);
                    }
                    sum += (fv[v] = a[v].first);
                    used[a[v].second] = true;
                    a[v] = tmp;
                    pull(v);
                } else
                    a[bot] = tmp;
            }
        }

        cout << sum << '\n';
        repds(i, (1 << h) - 1) {
            if (!used[i]) cout << i << ' ';
        }
        cout << '\n';
    }
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
    solve();
    cout.flush();
    return 0;
}


#define _USE_MATH_DEFINES
#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define reps(i, n) for (int i = 1; i <= (n); ++i)
#define repd(i, n) for (int i = (n - 1); i >= 0; --i)
#define repds(i, n) for (int i = (n); i > 0; --i)
#define loop(i, x, n) for (int i = (x); i < (n); ++i)
#define loops(i, x, n) for (int i = (x); i <= (n); ++i)
#define loopd(i, x, n) for (int i = (x); i > (n); --i)
#define loopds(i, x, n) for (int i = (x); i >= (n); --i)
#define itrep(i, a) for (auto i = begin(a); i != end(a); ++i)
#define itrepd(i, a) for (auto i = rbegin(a); i != rend(a); ++i)
#define each(i, x) for (auto &i : x)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
template <class T>
using priority_queuel = priority_queue<T, vector<T>, greater<T>>;
constexpr ll inf = static_cast<ll>(1e18);
constexpr int iinf = static_cast<int>(1e9);
constexpr double dinf = 1e10;
constexpr ld ldinf = 1e18;
constexpr double eps = 1e-9;
ll mod = static_cast<ll>(1e9 + 7);
template <class T>
constexpr int sz(const T &a) { return (int)a.size(); }
template <class T>
constexpr T pow2(const T a) { return a * a; }
template <class T, class... Args>
constexpr void mins(T &a, Args... args) { a = min<T>({a, args...}); }
template <class T, class... Args>
constexpr void maxs(T &a, Args... args) { a = max<T>({a, args...}); }
template <class A, int N, class T>
void fillArray(A (&array)[N], const T &val) { std::fill((T *)array, (T *)(array + N), val); }

template <class T, class U>
struct FirstPair {
    T first;
    U second;
    FirstPair(T fi, U se) : first(fi), second(se) {}
    bool operator<(const FirstPair &x) const {
        return first < x.first;
    }
    bool operator>(const FirstPair &x) const {
        return first > x.first;
    }
};

int h, g;
vector<ll> a;
vector<int> d;
vector<int> ans;
void f(int v) {
    if (!a[2 * v] && !a[2 * v + 1]) {
        a[v] = 0;
        d[v] = 0;
    } else {
        auto nv = (a[2 * v] > a[2 * v + 1] ? 2 * v : 2 * v + 1);
        a[v] = a[nv];
        f(nv);
        nv = (a[2 * v] > a[2 * v + 1] ? 2 * v : 2 * v + 1);
        d[v] = d[nv] + 1;
    }
}
void solve() {
    int t;
    cin >> t;
    rep(_, t) {
        cin >> h >> g;
        auto u = 1 << h;
        a.resize(2 * u + 2, 0);
        reps(i, u - 1) cin >> a[i];
        d.resize(2 * u + 2, 0);
        repds(i, u - 1) {
            if (!a[2 * i] && !a[2 * i + 1])
                d[i] = 1;
            else {
                auto nv = (a[2 * i] > a[2 * i + 1] ? 2 * i : 2 * i + 1);
                d[i] = d[nv] + 1;
            }
        }

        ans.clear();
        priority_queue<FirstPair<ll, int>> que;
        que.emplace(a[1], 1);
        while (!que.empty()) {
            auto v = que.top().second;
            que.pop();
            if (d[v] + 32 - __builtin_clz(v) - 1 > g) {
                ans.emplace_back(v);
                f(v);
                if (a[v])
                    que.emplace(a[v], v);
            } else {
                if (a[2 * v])
                    que.emplace(a[2 * v], 2 * v);
                if (a[2 * v + 1])
                    que.emplace(a[2 * v + 1], 2 * v + 1);
            }
        }

        ll sum = 0;
        reps(i, u - 1) sum += a[i];
        cout << sum << '\n';
        each(v, ans) cout << v << ' ';
        cout << '\n';
    }
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
    solve();
    cout.flush();
    return 0;
}


#define _USE_MATH_DEFINES
#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define reps(i, n) for (int i = 1; i <= (n); ++i)
#define repd(i, n) for (int i = (n - 1); i >= 0; --i)
#define repds(i, n) for (int i = (n); i > 0; --i)
#define loop(i, x, n) for (int i = (x); i < (n); ++i)
#define loops(i, x, n) for (int i = (x); i <= (n); ++i)
#define loopd(i, x, n) for (int i = (x); i > (n); --i)
#define loopds(i, x, n) for (int i = (x); i >= (n); --i)
#define itrep(i, a) for (auto i = begin(a); i != end(a); ++i)
#define itrepd(i, a) for (auto i = rbegin(a); i != rend(a); ++i)
#define each(i, x) for (auto &i : x)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
template <class T>
using priority_queuel = priority_queue<T, vector<T>, greater<T>>;
constexpr ll inf = static_cast<ll>(1e18);
constexpr int iinf = static_cast<int>(1e9);
constexpr double dinf = 1e10;
constexpr ld ldinf = 1e18;
constexpr double eps = 1e-9;
ll mod = static_cast<ll>(1e9 + 7);
template <class T>
constexpr int sz(const T &a) { return (int)a.size(); }
template <class T>
constexpr T pow2(const T a) { return a * a; }
template <class T, class... Args>
constexpr void mins(T &a, Args... args) { a = min<T>({a, args...}); }
template <class T, class... Args>
constexpr void maxs(T &a, Args... args) { a = max<T>({a, args...}); }
template <class A, int N, class T>
void fillArray(A (&array)[N], const T &val) { std::fill((T *)array, (T *)(array + N), val); }

int h, g, u, s;
vector<ll> a;
int f(int v) {
    auto tmp = a[v];
    vector<int> pre;
    a[v] = 0;
    int id = v;
    pre.emplace_back(id);
    while (2 * id < u && a[id] < max(a[2 * id], a[2 * id + 1])) {
        if (a[2 * id] > a[2 * id + 1]) {
            swap(a[id], a[2 * id]);
            id = 2 * id;
        } else {
            swap(a[id], a[2 * id + 1]);
            id = 2 * id + 1;
        }
        pre.emplace_back(id);
    }

    if (id < s) {
        reps(i, sz(pre) - 1) a[pre[i]] = a[pre[i] - 1];
        a[pre[0]] = tmp;
        return 0;
    }

    return tmp;
}
void solve() {
    int t;
    cin >> t;
    rep(_, t) {
        cin >> h >> g;
        u = 1 << h;
        s = 1 << g;
        a.resize(2 * u + 2, 0);
        reps(i, u - 1) cin >> a[i];

        ll sum = 0;
        reps(i, u - 1) sum += a[i];
        vector<int> ans;
        reps(i, s - 1) {
            int v;
            while ((v = f(i))) {
                sum -= v;
                ans.emplace_back(i);
            }
        }

        cout << sum << '\n';
        rep(i, sz(ans)) cout << ans[i] << (i < sz(ans) - 1 ? ' ' : '\n');
    }
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
    solve();
    cout.flush();
    return 0;
}


#define _USE_MATH_DEFINES
#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define reps(i, n) for (int i = 1; i <= (n); ++i)
#define repd(i, n) for (int i = (n)-1; i >= 0; --i)
#define repds(i, n) for (int i = (n); i > 0; --i)
#define loop(i, x, n) for (int i = (x); i < (n); ++i)
#define loops(i, x, n) for (int i = (x); i <= (n); ++i)
#define loopd(i, x, n) for (int i = (x); i > (n); --i)
#define loopds(i, x, n) for (int i = (x); i >= (n); --i)
#define itrep(i, a) for (auto i = begin(a); i != end(a); ++i)
#define itrepd(i, a) for (auto i = rbegin(a); i != rend(a); ++i)
#define each(i, x) for (auto &i : x)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
template <class T>
using priority_queuel = priority_queue<T, vector<T>, greater<T>>;
constexpr ll inf = static_cast<ll>(1e18);
constexpr int iinf = static_cast<int>(1e9);
constexpr double dinf = 1e10;
constexpr ld ldinf = 1e18;
constexpr double eps = 1e-9;
ll mod = static_cast<ll>(1e9 + 7);
template <class T>
constexpr int sz(const T &a) { return (int)a.size(); }
template <class T>
constexpr T pow2(const T a) { return a * a; }
template <class T, class... Args>
constexpr void mins(T &a, Args... args) { a = min<T>({a, args...}); }
template <class T, class... Args>
constexpr void maxs(T &a, Args... args) { a = max<T>({a, args...}); }
template <class A, int N, class T>
void fillArray(A (&array)[N], const T &val) { std::fill((T *)array, (T *)(array + N), val); }

template <class T, class U>
struct FirstPair {
    T first;
    U second;
    FirstPair() : first(), second() {}
    FirstPair(T fi, U se) : first(fi), second(se) {}
    bool operator<(const FirstPair &x) const {
        return first < x.first;
    }
    bool operator>(const FirstPair &x) const {
        return first > x.first;
    }
};

int h, g;
FirstPair<int, int> a[(1 << 21) + 5];
bool used[(1 << 21) + 5];
int fv[(1 << 21) + 5];
void pull(int id) {
    while (a[id] > min(a[id << 1], a[(id << 1) | 1])) {
        if (a[id << 1] < a[(id << 1) | 1]) {
            swap(a[id], a[id << 1]);
            id <<= 1;
        } else {
            swap(a[id], a[(id << 1) | 1]);
            id = (id << 1) | 1;
        }
        if (id >= (1 << h)) return;
    }
}
void solve() {
    int t;
    cin >> t;
    rep(_, t) {
        cin >> h >> g;

        reps(i, (1 << h) - 1) {
            cin >> a[i].first;
            a[i].second = i;
            used[i] = false;
            fv[i] = 0;
        }

        ll sum = 0;
        repd(i, h - 1) {
            auto ll = 1 << i;
            auto rr = 1 << (i + 1);
            loop(v, ll, rr) {
                auto tmp = a[v];
                auto bot = v << (h - 1 - i);
                a[v] = a[bot];
                a[bot] = {iinf, 0};
                pull(v);
                if (i < g) {
                    auto mi = max(fv[v << 1], fv[(v << 1) | 1]);
                    while (a[v].first < mi) {
                        a[v] = {iinf, 0};
                        pull(v);
                    }
                    sum += (fv[v] = a[v].first);
                    used[a[v].second] = true;
                    a[v] = tmp;
                    pull(v);
                } else
                    a[bot] = tmp;
            }
        }

        cout << sum << '\n';
        repds(i, (1 << h) - 1) {
            if (!used[i]) cout << i << ' ';
        }
        cout << '\n';
    }
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
    solve();
    cout.flush();
    return 0;
}


#define _USE_MATH_DEFINES
#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define reps(i, n) for (int i = 1; i <= (n); ++i)
#define repd(i, n) for (int i = (n - 1); i >= 0; --i)
#define repds(i, n) for (int i = (n); i > 0; --i)
#define loop(i, x, n) for (int i = (x); i < (n); ++i)
#define loops(i, x, n) for (int i = (x); i <= (n); ++i)
#define loopd(i, x, n) for (int i = (x); i > (n); --i)
#define loopds(i, x, n) for (int i = (x); i >= (n); --i)
#define itrep(i, a) for (auto i = begin(a); i != end(a); ++i)
#define itrepd(i, a) for (auto i = rbegin(a); i != rend(a); ++i)
#define each(i, x) for (auto &i : x)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
template <class T>
using priority_queuel = priority_queue<T, vector<T>, greater<T>>;
constexpr ll inf = static_cast<ll>(1e18);
constexpr int iinf = static_cast<int>(1e9);
constexpr double dinf = 1e10;
constexpr ld ldinf = 1e18;
constexpr double eps = 1e-9;
ll mod = static_cast<ll>(1e9 + 7);
template <class T>
constexpr int sz(const T &a) { return (int)a.size(); }
template <class T>
constexpr T pow2(const T a) { return a * a; }
template <class T, class... Args>
constexpr void mins(T &a, Args... args) { a = min<T>({a, args...}); }
template <class T, class... Args>
constexpr void maxs(T &a, Args... args) { a = max<T>({a, args...}); }
template <class A, int N, class T>
void fillArray(A (&array)[N], const T &val) { std::fill((T *)array, (T *)(array + N), val); }

int h, g, u, s;
vector<ll> a;
bool f(int v) {
    auto tmp = a[v];
    vector<int> pre;
    a[v] = 0;
    pre.emplace_back(v);
    while (2 * v < u && a[v] < max(a[2 * v], a[2 * v + 1])) {
        if (a[2 * v] > a[2 * v + 1]) {
            swap(a[v], a[2 * v]);
            v = 2 * v;
        } else {
            swap(a[v], a[2 * v + 1]);
            v = 2 * v + 1;
        }
        pre.emplace_back(v);
    }

    if (v < s) {
        repds(i, sz(pre) - 1) a[pre[i]] = a[pre[i] - 1];
        a[pre[0]] = tmp;
        return false;
    }

    return tmp;
}
void solve() {
    int t;
    cin >> t;
    rep(_, t) {
        cin >> h >> g;
        u = 1 << h;
        s = 1 << g;
        a.resize(2 * u + 2, 0);
        reps(i, u - 1) cin >> a[i];

        vector<int> ans;
        reps(i, s - 1) {
            while (f(i))
                ans.emplace_back(i);
        }

        ll sum = 0;
        reps(i, u - 1) sum += a[i];

        cout << sum << '\n';
        rep(i, sz(ans)) cout << ans[i] << (i < sz(ans) - 1 ? ' ' : '\n');
    }
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
    solve();
    cout.flush();
    return 0;
}


#define _USE_MATH_DEFINES
#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define reps(i, n) for (int i = 1; i <= (n); ++i)
#define repd(i, n) for (int i = (n - 1); i >= 0; --i)
#define repds(i, n) for (int i = (n); i > 0; --i)
#define loop(i, x, n) for (int i = (x); i < (n); ++i)
#define loops(i, x, n) for (int i = (x); i <= (n); ++i)
#define loopd(i, x, n) for (int i = (x); i > (n); --i)
#define loopds(i, x, n) for (int i = (x); i >= (n); --i)
#define itrep(i, a) for (auto i = begin(a); i != end(a); ++i)
#define itrepd(i, a) for (auto i = rbegin(a); i != rend(a); ++i)
#define each(i, x) for (auto &i : x)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
template <class T>
using priority_queuel = priority_queue<T, vector<T>, greater<T>>;
constexpr ll inf = static_cast<ll>(1e18);
constexpr int iinf = static_cast<int>(1e9);
constexpr double dinf = 1e10;
constexpr ld ldinf = 1e18;
constexpr double eps = 1e-9;
ll mod = static_cast<ll>(1e9 + 7);
template <class T>
constexpr int sz(const T &a) { return (int)a.size(); }
template <class T>
constexpr T pow2(const T a) { return a * a; }
template <class T, class... Args>
constexpr void mins(T &a, Args... args) { a = min<T>({a, args...}); }
template <class T, class... Args>
constexpr void maxs(T &a, Args... args) { a = max<T>({a, args...}); }
template <class A, int N, class T>
void fillArray(A (&array)[N], const T &val) { std::fill((T *)array, (T *)(array + N), val); }

template <class T, class U>
struct FirstPair {
    T first;
    U second;
    FirstPair(T fi, U se) : first(fi), second(se) {}
    bool operator<(const FirstPair &x) const {
        return first < x.first;
    }
    bool operator>(const FirstPair &x) const {
        return first > x.first;
    }
};

int h, g;
vector<ll> a;
bool f(int v) {
    if (!a[2 * v] && !a[2 * v + 1]) {
        if (v < (1 << g)) return false;
        a[v] = 0;
    } else {
        auto nv = (a[2 * v] > a[2 * v + 1] ? 2 * v : 2 * v + 1);
        auto tmp = a[nv];
        if (!f(nv)) return false;
        a[v] = tmp;
    }
    return true;
}
void solve() {
    int t;
    cin >> t;
    rep(_, t) {
        cin >> h >> g;
        auto u = 1 << h;
        a.resize(2 * u + 2, 0);
        reps(i, u - 1) cin >> a[i];

        vector<int> ans;
        reps(i, u - 1) {
            while (a[i] && f(i))
                ans.emplace_back(i);
        }

        ll sum = 0;
        reps(i, u - 1) sum += a[i];
        cout << sum << '\n';
        rep(i, sz(ans)) cout << ans[i] << (i < sz(ans) - 1 ? ' ' : '\n');
    }
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
    solve();
    cout.flush();
    return 0;
}


#define _USE_MATH_DEFINES
#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define reps(i, n) for (int i = 1; i <= (n); ++i)
#define repd(i, n) for (int i = (n - 1); i >= 0; --i)
#define repds(i, n) for (int i = (n); i > 0; --i)
#define loop(i, x, n) for (int i = (x); i < (n); ++i)
#define loops(i, x, n) for (int i = (x); i <= (n); ++i)
#define loopd(i, x, n) for (int i = (x); i > (n); --i)
#define loopds(i, x, n) for (int i = (x); i >= (n); --i)
#define itrep(i, a) for (auto i = begin(a); i != end(a); ++i)
#define itrepd(i, a) for (auto i = rbegin(a); i != rend(a); ++i)
#define each(i, x) for (auto &i : x)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
template <class T>
using priority_queuel = priority_queue<T, vector<T>, greater<T>>;
constexpr ll inf = static_cast<ll>(1e18);
constexpr int iinf = static_cast<int>(1e9);
constexpr double dinf = 1e10;
constexpr ld ldinf = 1e18;
constexpr double eps = 1e-9;
ll mod = static_cast<ll>(1e9 + 7);
template <class T>
constexpr int sz(const T &a) { return (int)a.size(); }
template <class T>
constexpr T pow2(const T a) { return a * a; }
template <class T, class... Args>
constexpr void mins(T &a, Args... args) { a = min<T>({a, args...}); }
template <class T, class... Args>
constexpr void maxs(T &a, Args... args) { a = max<T>({a, args...}); }
template <class A, int N, class T>
void fillArray(A (&array)[N], const T &val) { std::fill((T *)array, (T *)(array + N), val); }

int h, g, u, s;
vector<ll> a;
bool f(int v) {
    auto tmp = a[v];
    vector<int> pre;
    a[v] = 0;
    int id = v;
    pre.emplace_back(id);
    while (2 * id < u && a[id] < max(a[2 * id], a[2 * id + 1])) {
        if (a[2 * id] > a[2 * id + 1]) {
            swap(a[id], a[2 * id]);
            id = 2 * id;
        } else {
            swap(a[id], a[2 * id + 1]);
            id = 2 * id + 1;
        }
        pre.emplace_back(id);
    }

    if (id < s) {
        rep(i, sz(pre) - 1) a[pre[i] + 1] = a[pre[i]];
        a[v] = tmp;
        return false;
    }

    return tmp;
}
void solve() {
    int t;
    cin >> t;
    rep(_, t) {
        cin >> h >> g;
        u = 1 << h;
        s = 1 << g;
        a.resize(2 * u + 2, 0);
        reps(i, u - 1) cin >> a[i];

        vector<int> ans;
        ll sum = 0;
        reps(i, s - 1) {
            while (f(i))
                ans.emplace_back(i);
            sum += a[i];
        }

        cout << sum << '\n';
        rep(i, sz(ans)) cout << ans[i] << (i < sz(ans) - 1 ? ' ' : '\n');
    }
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
    solve();
    cout.flush();
    return 0;
}


#define _USE_MATH_DEFINES
#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define reps(i, n) for (int i = 1; i <= (n); ++i)
#define repd(i, n) for (int i = (n - 1); i >= 0; --i)
#define repds(i, n) for (int i = (n); i > 0; --i)
#define loop(i, x, n) for (int i = (x); i < (n); ++i)
#define loops(i, x, n) for (int i = (x); i <= (n); ++i)
#define loopd(i, x, n) for (int i = (x); i > (n); --i)
#define loopds(i, x, n) for (int i = (x); i >= (n); --i)
#define itrep(i, a) for (auto i = begin(a); i != end(a); ++i)
#define itrepd(i, a) for (auto i = rbegin(a); i != rend(a); ++i)
#define each(i, x) for (auto &i : x)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
template <class T>
using priority_queuel = priority_queue<T, vector<T>, greater<T>>;
constexpr ll inf = static_cast<ll>(1e18);
constexpr int iinf = static_cast<int>(1e9);
constexpr double dinf = 1e10;
constexpr ld ldinf = 1e18;
constexpr double eps = 1e-9;
ll mod = static_cast<ll>(1e9 + 7);
template <class T>
constexpr int sz(const T &a) { return (int)a.size(); }
template <class T>
constexpr T pow2(const T a) { return a * a; }
template <class T, class... Args>
constexpr void mins(T &a, Args... args) { a = min<T>({a, args...}); }
template <class T, class... Args>
constexpr void maxs(T &a, Args... args) { a = max<T>({a, args...}); }
template <class A, int N, class T>
void fillArray(A (&array)[N], const T &val) { std::fill((T *)array, (T *)(array + N), val); }

int h, g;
vector<ll> a;
bool f(int v) {
    auto nv = (a[2 * v] > a[2 * v + 1] ? 2 * v : 2 * v + 1);
    auto tmp = a[nv];
    if (tmp && !f(nv)) return false;
    if (!tmp && v < (1 << g)) return false;

    a[v] = tmp;
    return true;
}
void solve() {
    int t;
    cin >> t;
    rep(_, t) {
        cin >> h >> g;
        auto u = 1 << h;
        a.resize(2 * u + 2, 0);
        reps(i, u - 1) cin >> a[i];

        vector<int> ans;
        auto t = 1 << g;
        reps(i, t - 1) {
            while (f(i))
                ans.emplace_back(i);
        }

        ll sum = 0;
        reps(i, u - 1) sum += a[i];
        cout << sum << '\n';
        rep(i, sz(ans)) cout << ans[i] << (i < sz(ans) - 1 ? ' ' : '\n');
    }
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
    solve();
    cout.flush();
    return 0;
}


#define _USE_MATH_DEFINES
#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define reps(i, n) for (int i = 1; i <= (n); ++i)
#define repd(i, n) for (int i = (n)-1; i >= 0; --i)
#define repds(i, n) for (int i = (n); i > 0; --i)
#define loop(i, x, n) for (int i = (x); i < (n); ++i)
#define loops(i, x, n) for (int i = (x); i <= (n); ++i)
#define loopd(i, x, n) for (int i = (x); i > (n); --i)
#define loopds(i, x, n) for (int i = (x); i >= (n); --i)
#define itrep(i, a) for (auto i = begin(a); i != end(a); ++i)
#define itrepd(i, a) for (auto i = rbegin(a); i != rend(a); ++i)
#define each(i, x) for (auto &i : x)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
template <class T>
using priority_queuel = priority_queue<T, vector<T>, greater<T>>;
constexpr ll inf = static_cast<ll>(1e18);
constexpr int iinf = static_cast<int>(1e9);
constexpr double dinf = 1e10;
constexpr ld ldinf = 1e18;
constexpr double eps = 1e-9;
ll mod = static_cast<ll>(1e9 + 7);
template <class T>
constexpr int sz(const T &a) { return (int)a.size(); }
template <class T>
constexpr T pow2(const T a) { return a * a; }
template <class T, class... Args>
constexpr void mins(T &a, Args... args) { a = min<T>({a, args...}); }
template <class T, class... Args>
constexpr void maxs(T &a, Args... args) { a = max<T>({a, args...}); }
template <class A, int N, class T>
void fillArray(A (&array)[N], const T &val) { std::fill((T *)array, (T *)(array + N), val); }

template <class T, class U>
struct FirstPair {
    T first;
    U second;
    FirstPair() : first(), second() {}
    FirstPair(T fi, U se) : first(fi), second(se) {}
    bool operator<(const FirstPair &x) const {
        return first < x.first;
    }
    bool operator>(const FirstPair &x) const {
        return first > x.first;
    }
};

int h, g;
vector<FirstPair<int, int>> a;
void pull(int id) {
    while (a[id] > min(a[id << 1], a[(id << 1) | 1])) {
        if (a[id << 1] < a[(id << 1) | 1]) {
            swap(a[id], a[id << 1]);
            id <<= 1;
        } else {
            swap(a[id], a[(id << 1) | 1]);
            id = (id << 1) | 1;
        }
        if (id >= (1 << (h - 1))) return;
    }
}
void solve() {
    int t;
    cin >> t;
    rep(_, t) {
        cin >> h >> g;

        a.resize(1 << h);
        reps(i, (1 << h) - 1) {
            cin >> a[i].first;
            a[i].second = i;
        }

        vector<bool> used(1 << h, false);
        vector<int> fv(1 << h, 0);
        ll sum = 0;
        repd(i, h - 1) {
            auto ll = 1 << i;
            auto rr = 1 << (i + 1);
            loop(v, ll, rr) {
                auto tmp = a[v];
                auto bot = v << (h - 1 - i);
                a[v] = a[bot];
                a[bot] = {iinf, 0};
                pull(v);
                if (i < g) {
                    auto mi = max(fv[v << 1], fv[(v << 1) | 1]);
                    while (a[v].first < mi) {
                        a[v] = {iinf, 0};
                        pull(v);
                    }
                    sum += (fv[v] = a[v].first);
                    used[a[v].second] = true;
                    a[v] = tmp;
                    pull(v);
                } else
                    a[bot] = tmp;
            }
        }

        cout << sum << '\n';
        repds(i, (1 << h) - 1) {
            if (!used[i]) cout << i << ' ';
        }
        cout << '\n';
    }
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
    solve();
    cout.flush();
    return 0;
}


#include <bits/stdc++.h>

typedef long long LL;

int T, lim, mod;

int main() {
    scanf("%d", &T);
    for (int cs = 1; cs <= T; cs++) {
        scanf("%d%d", &lim, &mod);
        int ans = 1;
        for (int i = 1; (1 << (i - 1)) <= lim; i++) {
            ans = (LL)ans * (std::min((1 << i) - 1, lim) - (1 << (i - 1)) + 2) % mod;
        }
        printf("%d\n", (ans - 1 + mod) % mod);
    }
    return 0;
}


#include <bits/stdc++.h>

#define lson (u << 1)
#define rson (u << 1 | 1)

const int N = 1 << 22;

int T, n, m, a[N + 5];

int calc(int u) {
	if (!a[lson] && !a[rson]) {
		return u;
	}
	return a[lson] > a[rson] ? calc(lson) : calc(rson);
}
void erase(int u) {
	if (!a[lson] && !a[rson]) {
		a[u] = 0;
		return;
	}
	if (a[lson] > a[rson]) {
		a[u] = a[lson];
		erase(lson);
	} else {
		a[u] = a[rson];
		erase(rson);
	}
}
int main() {
	scanf("%d", &T);
	for (int cs = 1; cs <= T; cs++) {
		scanf("%d%d", &n, &m);
		for (int i = 1; i < (1 << n); i++) {
			scanf("%d", &a[i]);
		}
		std::vector<int> opt;
		for (int i = 1; i < (1 << m); i++) {
			while (calc(i) >= (1 << m)) {
				erase(i);
				opt.push_back(i);
			}
		}
		printf("%lld\n", std::accumulate(a + 1, a + (1 << m), 0LL));
		for (int i = 0; i < (int)opt.size(); i++) {
			printf("%d%c", opt[i], " \n"[i == (int)opt.size() - 1]);
		}
		std::fill(a + 1, a + (1 << n), 0);
	}
	return 0;
}


#include <bits/stdc++.h>

#define lson (u << 1)
#define rson (u << 1 | 1)

const int N = 1 << 22;

int T, n, m, a[N + 5];

int calc(int u) {
	if (!a[lson] && !a[rson]) {
		return u;
	}
	return a[lson] > a[rson] ? calc(lson) : calc(rson);
}
void erase(int u) {
	if (!a[lson] && !a[rson]) {
		a[u] = 0;
		return;
	}
	if (a[lson] > a[rson]) {
		a[u] = a[lson];
		erase(lson);
	} else {
		a[u] = a[rson];
		erase(rson);
	}
}
int main() {
	scanf("%d", &T);
	for (int cs = 1; cs <= T; cs++) {
		scanf("%d%d", &n, &m);
		for (int i = 1; i < (1 << n); i++) {
			scanf("%d", &a[i]);
		}
		std::vector<int> opt;
		for (int i = 1; i < (1 << m); i++) {
			while (calc(i) >= (1 << m)) {
				erase(i);
				opt.push_back(i);
			}
		}
		printf("%lld\n", std::accumulate(a + 1, a + (1 << m), 0LL));
		for (int i = 0; i < (int)opt.size(); i++) {
			printf("%d%c", opt[i], " \n"[i == (int)opt.size() - 1]);
		}
	}
	return 0;
}


#include <bits/stdc++.h>

#define lson (u << 1)
#define rson (u << 1 | 1)

const int N = 1 << 20;

int T, n, m, a[N + 5];

int calc(int u) {
	if (!a[lson] && !a[rson]) {
		return u;
	}
	return a[lson] > a[rson] ? calc(lson) : calc(rson);
}
void erase(int u) {
	if (!a[lson] && !a[rson]) {
		a[u] = 0;
		return;
	}
	if (a[lson] > a[rson]) {
		a[u] = a[lson];
		erase(lson);
	} else {
		a[u] = a[rson];
		erase(rson);
	}
}
int main() {
	scanf("%d", &T);
	for (int cs = 1; cs <= T; cs++) {
		scanf("%d%d", &n, &m);
		for (int i = 1; i < (1 << n); i++) {
			scanf("%d", &a[i]);
		}
		std::vector<int> opt;
		for (int i = 1; i < (1 << m); i++) {
			while (calc(i) >= (1 << m)) {
				erase(i);
				opt.push_back(i);
			}
		}
		printf("%lld\n", std::accumulate(a + 1, a + (1 << m), 0LL));
		for (int i = 0; i < (int)opt.size(); i++) {
			printf("%d%c", opt[i], " \n"[i == (int)opt.size() - 1]);
		}
	}
	return 0;
}


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

using namespace std;

//defines
typedef long long ll;
typedef long double ld;
#define TIME clock() * 1.0 / CLOCKS_PER_SEC
#define prev _prev
#define y0 _y0
#define kill _kill

//permanent constants
const ld pi = acos(-1.0);
const int day[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int digarr[10] = {6, 2, 5, 5, 4, 5, 6, 3, 7, 6};
const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {1, 0, -1, 0};
const int dxo[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
const int dyo[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
const int alf = 26;
const int dig = 10;
const int two = 2;
const int th = 3;
const ll prost = 239;
const ll btc = 30;
const ld eps = 1e-8;
const ll INF = (ll)(1e18 + 239);
const int BIG = (int)(1e9 + 239);
const int MOD = 1e9 + 7; //

//random
mt19937 rnd(239); //(chrono::high_resolution_clock::now().time_since_epoch().count());

//constants
const int M = (int)(2e5 + 239);
const int N = (int)(2e3 + 239);
const int L = 20;
const int T = (1 << 20) + 239;
const int B = 500;
const int X = 35;
const int T2 = 2 * T;

int h, g, a[T2], n;
bool mark[T];
int id[T], buf[T], timer;

bool cmp(int i, int j)
{
    return (a[i] < a[j]);
}

int pos;

void dfs_write(int p)
{
    id[pos++] = p;
    if (2 * p + 1 <= n)
    {
        dfs_write(2 * p);
        dfs_write(2 * p + 1);
    }
}

int dfs(int p, int sz)
{
    if ((1 << (g - 1)) <= p && p < (1 << g))
    {
        int l = timer;
        timer += sz;
        pos = l;
        dfs_write(p);
        sort(id + l, id + l + sz, cmp);
        mark[id[l]] = true;
        return id[l];
    }
    int l = timer;
    int new_sz = ((sz - 1) / 2);
    int s1 = dfs(2 * p, new_sz);
    int s2 = dfs(2 * p + 1, new_sz);
    merge(id + l, id + l + new_sz, id + l + new_sz, id + l + new_sz + new_sz, buf, cmp);
    buf[sz - 1] = p;
    for (int i = 0; i < sz; i++)
        id[i + l] = buf[i];
    timer++;
    if (cmp(s1, s2))
        s1 = s2;
    for (int i = 0; i < sz; i++)
        if (cmp(s1, id[i + l]))
        {
            mark[id[i + l]] = true;
            return id[i + l];
        }
    return 0;
}

void rm(int i)
{
    if (a[2 * i] == 0 && a[2 * i + 1] == 0)
    {
        a[i] = 0;
        mark[i] = false;
        return;
    }
    if (a[2 * i] > a[2 * i + 1])
    {
        a[i] = a[2 * i];
        mark[i] = mark[2 * i];
        rm(2 * i);
    }
    else
    {
        a[i] = a[2 * i + 1];
        mark[i] = mark[2 * i + 1];
        rm(2 * i + 1);
    }
}

void dfs_ans(int p)
{
    while (a[p] != 0 && !mark[p])
    {
        cout << p << " ";
        rm(p);
    }
    if (a[p] == 0)
        return;
    dfs_ans(2 * p);
    dfs_ans(2 * p + 1);
}

void solve()
{
    cin >> h >> g;
    n = (1 << h) - 1;
    for (int i = 1; i <= n; i++)
        cin >> a[i];
    for (int i = 1; i <= n; i++)
        mark[i] = false;
    timer = 0;
    dfs(1, n);
    ll ans = 0;
    for (int i = 1; i <= n; i++)
        if (mark[i])
            ans += a[i];
    cout << ans << "\n";
    dfs_ans(1);
    cout << "\n";
    for (int i = 0; i <= n; i++)
        a[i] = 0;
}

int32_t main()
{
#ifdef ONPC
    freopen("input", "r", stdin);
#endif
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int t;
    cin >> t;
    while (t--)
        solve();
    return 0;
}


#include <cstdio>
#include <algorithm>
#include <vector>
#include <set>
#include <map>

using namespace std;

#define f first
#define s second

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

int war[MAXN];
int kon[MAXN];
set<int> q[2 * MAXN];
map<int, bool> odw;

int main() {
    int z, n, g, mx;
    long long ans;
    scanf("%d", &z);
    while (z--) {
        ans = 0;
        scanf("%d %d", &n, &g);
        n = (1 << n) - 1;
        g = (1 << g) - 1;
        for (int i = 1; i <= n; i++)
            scanf("%d", &war[i]);
        for (int i = n; i > 0; i--) {
            q[i].insert(war[i]);
            if (i > g) {
                kon[i] = 0;
                for (int j = 0; j < 2; j++) {
                    while (!q[i * 2 + j].empty()) {
                        q[i].insert(*(q[i * 2 + j].begin()));
                        q[i * 2 + j].erase(q[i * 2 + j].begin());
                    }
                }
            }
            else {
                mx = max(kon[i * 2], kon[i * 2 + 1]);
                for (int j = 0; j < 2; j++) {
                    while (!q[i * 2 + j].empty()) {
                        if (*(q[i * 2 + j].begin()) > mx)
                            q[i].insert(*(q[i * 2 + j].begin()));
                        q[i * 2 + j].erase(q[i * 2 + j].begin());
                    }
                }
                kon[i] = *(q[i].begin());
                ans += (long long)(kon[i]);
                odw[kon[i]] = true;
            }
        }
        printf("%lld\n", ans);
        for (int i = n; i > 0; i--) {
            if (!odw[war[i]])
                printf("%d ", i);
        }
        q[1].clear();
        odw.clear();
        printf("\n");
    }
    return 0;
}


#include <cstdio>
#include <algorithm>
#include <vector>
#include <set>
#include <map>

using namespace std;

#define f first
#define s second

const int MAXN = (1 << 20) + 3;

int t[MAXN];
bool wz[MAXN];
bool zaj[MAXN];
vector<pair<int, int> > v;

int main() {
    int z, n, g, x;
    long long ans;
    scanf("%d", &z);
    while (z--) {
        ans = 0;
        scanf("%d %d", &n, &g);
        n = (1 << n) - 1;
        g = (1 << g) - 1;
        for (int i = 1; i <= g; i++)
            zaj[i] = false;
        for (int i = g + 1; i <= n; i++)
            zaj[i] = true;
        for (int i = 1; i <= n; i++)
            wz[i] = false;
        v.clear();
        for (int i = 1; i <= n; i++) {
            scanf("%d", &t[i]);
            v.emplace_back(t[i], i);
        }
        sort(v.begin(), v.end());
        for (auto it:v) {
            x = it.s;
            while (zaj[x])
                x /= 2;
            if (x == 0)
                continue;
            if (zaj[x * 2] == zaj[x * 2 + 1]) {
                zaj[x] = true;
                ans += (long long)(it.f);
                wz[it.s] = true;
            }
        }
        printf("%lld\n", ans);
        for (int i = n; i > 0; i--) {
            if (!wz[i])
                printf("%d ", i);
        }
        printf("\n");
    }
    return 0;
}


#include <cstdio>
#include <algorithm>
#include <vector>
#include <set>
#include <map>

using namespace std;

#define f first
#define s second

const int MAXN = (1 << 20) + 3;

int t[MAXN];
bool wz[MAXN];
bool zaj[MAXN];
vector<pair<int, int> > v;

int main() {
    int z, n, g, x;
    long long ans;
    scanf("%d", &z);
    while (z--) {
        ans = 0;
        scanf("%d %d", &n, &g);
        n = (1 << n) - 1;
        g = (1 << g) - 1;
        for (int i = 1; i <= g; i++)
            zaj[i] = false;
        for (int i = g + 1; i <= n; i++)
            zaj[i] = true;
        for (int i = 1; i <= n; i++)
            wz[i] = false;
        v.clear();
        for (int i = 1; i <= n; i++) {
            scanf("%d", &t[i]);
            v.emplace_back(t[i], i);
        }
        sort(v.begin(), v.end());
        for (auto it:v) {
            x = it.s;
            while (zaj[x])
                x /= 2;
            if (x == 0)
                continue;
            if (zaj[x * 2] == zaj[x * 2 + 1]) {
                zaj[x] = true;
                ans += (long long)(it.f);
                wz[it.s] = true;
            }
        }
        printf("%lld\n", ans);
        for (int i = 1; i <= n; i++) {
            if (!wz[i])
                printf("%d ", i);
        }
        printf("\n");
    }
    return 0;
}


#include <cstdio>
#include <algorithm>
#include <vector>
#include <cstring>
#include <set>
#include <map>

using namespace std;

#define f first
#define s second

const int MAXN = (1 << 20) + 3;

int war[MAXN];
int kon[MAXN];
set<int> q[2 * MAXN];
map<int, bool> odw;
static char stdinBuffer[1024];
static char* stdinDataEnd = stdinBuffer + sizeof (stdinBuffer);
static const char* stdinPos = stdinDataEnd;
int ret;

void readAhead(size_t amount) {
    size_t remaining = stdinDataEnd - stdinPos;
    if (remaining < amount) {
        memmove(stdinBuffer, stdinPos, remaining);
        size_t sz = fread(stdinBuffer + remaining, 1, sizeof (stdinBuffer) - remaining, stdin);
        stdinPos = stdinBuffer;
        stdinDataEnd = stdinBuffer + remaining + sz;
        if (stdinDataEnd != stdinBuffer + sizeof (stdinBuffer))
            *stdinDataEnd = 0;
    }
}

int fastread() {
    readAhead(16);
    ret = 0;
    while (*stdinPos < '0' || *stdinPos > '9')
        ++stdinPos;
    while (*stdinPos >= '0' && *stdinPos <= '9') {
        ret *= 10;
        ret += *stdinPos - '0';
        ++stdinPos;
    }
    return ret;
}

int main() {
    int z, n, g, mx;
    long long ans;
    z = fastread();
    while (z--) {
        ans = 0;
        n = fastread();
        g = fastread();
        n = (1 << n) - 1;
        g = (1 << g) - 1;
        for (int i = 1; i <= n; i++)
            war[i] = fastread();
        for (int i = n; i > 0; i--) {
            q[i].insert(war[i]);
            if (i > g) {
                kon[i] = 0;
                for (int j = 0; j < 2; j++) {
                    while (!q[i * 2 + j].empty()) {
                        q[i].insert(*(q[i * 2 + j].begin()));
                        q[i * 2 + j].erase(q[i * 2 + j].begin());
                    }
                }
            }
            else {
                mx = max(kon[i * 2], kon[i * 2 + 1]);
                for (int j = 0; j < 2; j++) {
                    while (!q[i * 2 + j].empty()) {
                        if (*(q[i * 2 + j].begin()) > mx)
                            q[i].insert(*(q[i * 2 + j].begin()));
                        q[i * 2 + j].erase(q[i * 2 + j].begin());
                    }
                }
                kon[i] = *(q[i].begin());
                ans += (long long)(kon[i]);
                odw[kon[i]] = true;
            }
        }
        printf("%lld\n", ans);
        for (int i = n; i > 0; i--) {
            if (!odw[war[i]])
                printf("%d ", i);
        }
        q[1].clear();
        odw.clear();
        printf("\n");
    }
    return 0;
}


#include <cstdio>
#include <algorithm>
#include <vector>
#include <set>
#include <map>

using namespace std;

#define f first
#define s second

const int MAXN = (1 << 20) + 3;

int war[MAXN];
int kon[MAXN];
set<int> q[2 * MAXN];
map<int, bool> odw;

int main() {
    int z, n, g, mx;
    long long ans;
    scanf("%d", &z);
    while (z--) {
        ans = 0;
        scanf("%d %d", &n, &g);
        n = (1 << n) - 1;
        g = (1 << g) - 1;
        for (int i = 1; i <= n; i++)
            scanf("%d", &war[i]);
        for (int i = n; i > 0; i--) {
            q[i].insert(war[i]);
            if (i > g) {
                kon[i] = 0;
                for (int j = 0; j < 2; j++) {
                    while (!q[i * 2 + j].empty()) {
                        q[i].insert(*(q[i * 2 + j].begin()));
                        q[i * 2 + j].erase(q[i * 2 + j].begin());
                    }
                }
            }
            else {
                mx = max(kon[i * 2], kon[i * 2 + 1]);
                for (int j = 0; j < 2; j++) {
                    while (!q[i * 2 + j].empty()) {
                        if (*(q[i * 2 + j].begin()) > mx)
                            q[i].insert(*(q[i * 2 + j].begin()));
                        q[i * 2 + j].erase(q[i * 2 + j].begin());
                    }
                }
                kon[i] = *(q[i].begin());
                ans += (long long)(kon[i]);
                odw[kon[i]] = true;
            }
        }
        printf("%lld\n", ans);
        for (int i = n; i > 0; i--) {
            if (!odw[war[i]])
                printf("%d ", i);
        }
        q[1].clear();
        odw.clear();
        printf("\n");
    }
    return 0;
}


#include <cstdio>
#include <algorithm>
#include <vector>
#include <set>
#include <map>

using namespace std;

#define f first
#define s second

const int MAXN = (1 << 20) + 3;

int war[MAXN];
int kon[MAXN];
set<int> q[2 * MAXN];
map<int, bool> odw;

int main() {
    int z, n, g, mx;
    long long ans;
    scanf("%d", &z);
    while (z--) {
        ans = 0;
        scanf("%d %d", &n, &g);
        n = (1 << n) - 1;
        g = (1 << g) - 1;
        for (int i = 1; i <= n; i++)
            scanf("%d", &war[i]);
        for (int i = n; i > 0; i--) {
            q[i].insert(war[i]);
            if (i > g) {
                for (int j = 0; j < 2; j++) {
                    while (!q[i * 2 + j].empty()) {
                        q[i].insert(*(q[i * 2 + j].begin()));
                        q[i * 2 + j].erase(q[i * 2 + j].begin());
                    }
                }
            }
            else {
                mx = max(kon[i * 2], kon[i * 2 + 1]);
                for (int j = 0; j < 2; j++) {
                    while (!q[i * 2 + j].empty()) {
                        if (*(q[i * 2 + j].begin()) > mx)
                            q[i].insert(*(q[i * 2 + j].begin()));
                        q[i * 2 + j].erase(q[i * 2 + j].begin());
                    }
                }
                kon[i] = *(q[i].begin());
                ans += (long long)(kon[i]);
                odw[kon[i]] = true;
            }
        }
        printf("%lld\n", ans);
        for (int i = n; i > 0; i--) {
            if (!odw[war[i]])
                printf("%d ", i);
        }
        q[1].clear();
        odw.clear();
        printf("\n");
    }
    return 0;
}


#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int ,int> pii;
typedef pair<ll ,ll> pll;
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define all(X) (X).begin(), (X).end()
#define sll(n) scanf("%lld",&n)
#define sll2(x,y) scanf("%lld%lld",&x,&y)
#define sll3(x,y,z) scanf("%lld%lld%lld",&x,&y,&z)
#define REP(i,x,y) for(ll i = x;i <= y;++i)
#define debug(x) cerr<<#x<<"::"<<x<<endl
#define debug2(x,y) cerr<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\n"
#define debug3(x,y,z) cerr<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<"\n"
#define debug4(x,y,z,w) cerr<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<"\t"<<#w<<" :: "<<w<<"\n"
ll power(ll x,ll y,ll z) {
	ll ret = 1;
	while(y > 0) {
		if(y & 1) ret = (ret*x)%z;
		x = (x*x)%z;
		y >>= 1;
	}
	return ret;
}
int main() {

	ll t;
	sll(t);
	while(t--) {
		ll h, g, tot = 0;
		sll2(h,g);
		ll n = (1LL << h) - 1, m = (1LL << g) - 1;
		vector<ll> A(n + 1), ans;
		REP(i,1,n) sll(A[i]), tot += A[i];
		for(ll i = 1;i <= n;) {
			if(!A[i]) {
				++i;
				continue;
			}
			ll rem = i;
			while(true) {
				ll lc = rem << 1, rc = rem << 1|1;
				if(lc > n) break;
				else if(!A[lc] && !A[rc]) break;
				else {
					if(A[lc] > A[rc]) rem = lc;
					else rem = rc;
				}
			}
			if(rem > m) {
				tot -= A[i];
				ans.pb(i);
				rem = i;
				while(true) {
					ll lc = rem << 1, rc = rem << 1|1;
					if(lc > n) break;
					else if(!A[lc] && !A[rc]) break;
					else {
						if(A[lc] > A[rc]) rem = lc;
						else rem = rc;
						A[rem / 2] = A[rem];
					}
				}
				A[rem] = 0;
			} else ++i;
		}
		printf("%lld\n", tot);
		for(auto u : ans) printf("%lld ", u);
		printf("\n");
	}

	return 0;
}


#include <algorithm>　　　 //STL 通用算法
#include <bitset>　　　　　//STL 位集容器
#include <cctype>         //字符处理
#include <cerrno> 　　　　 //定义错误码
#include <cfloat>　　　　 //浮点数处理
#include <ciso646>         //对应各种运算符的宏
#include <climits> 　　　　//定义各种数据类型最值的常量
#include <clocale> 　　　　//定义本地化函数
#include <cmath> 　　　　　//定义数学函数
#include <complex>　　　　 //复数类
#include <csignal>         //信号机制支持
#include <csetjmp>         //异常处理支持
#include <cstdarg>         //不定参数列表支持
#include <cstddef>         //常用常量
#include <cstdio> 　　　　 //定义输入／输出函数
#include <cstdlib> 　　　　//定义杂项函数及内存分配函数
#include <cstring> 　　　　//字符串处理
#include <ctime> 　　　　　//定义关于时间的函数
#include <cwchar> 　　　　 //宽字符处理及输入／输出
#include <cwctype> 　　　　//宽字符分类
#include <deque>　　　　　 //STL 双端队列容器
#include <exception>　　　 //异常处理类
#include <fstream> 　　　 //文件输入／输出
#include <functional>　　　//STL 定义运算函数（代替运算符）
#include <limits> 　　　　 //定义各种数据类型最值常量
#include <list>　　　　　　//STL 线性列表容器
#include <locale>         //本地化特定信息
#include <map>　　　　　　 //STL 映射容器
#include <memory>         //STL通过分配器进行的内存分配
#include<new>            //动态内存分配
#include <numeric>         //STL常用的数字操作
#include <iomanip> 　　　 //参数化输入／输出
#include <ios>　　　　　　 //基本输入／输出支持
#include <iosfwd>　　　　　//输入／输出系统使用的前置声明
#include <iostream> 　　　//数据流输入／输出
#include <istream>　　　　 //基本输入流
#include <iterator>        //STL迭代器
#include <ostream>　　　　 //基本输出流
#include <queue>　　　　　 //STL 队列容器
#include <set>　　　　　　 //STL 集合容器
#include <sstream>　　　　 //基于字符串的流
#include <stack>　　　　　 //STL 堆栈容器
#include <stdexcept>　　　 //标准异常类
#include <streambuf>　　　 //底层输入／输出支持
#include <string>　　　　　//字符串类
#include <typeinfo>        //运行期间类型信息
#include <utility>　　　　 //STL 通用模板类
#include <valarray>        //对包含值的数组的操作
#include <vector>　　　　　//STL 动态数组容器
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;

const int MAXN=2300005;
const int MOD=998244353;

inline int readint(){
	int x=0,f=1;char c=getchar();
	while(!isdigit(c)){if(c=='-')f=-1;c=getchar();}
	while(isdigit(c))x=(x<<3)+(x<<1)+c-'0',c=getchar();
	return x*f;
}
inline ll readll(){
	ll x=0;int f=1;char c=getchar();
	while(!isdigit(c)){if(c=='-') f=-1;c=getchar();}
	while(isdigit(c)) x=(x<<3ll)+(x<<1ll)+1ll*(c-'0'),c=getchar();
	return x*f;
}
int gcd(int x,int y){
	return y?gcd(y,x%y):x;
}
ll gcd(ll x,ll y){
	return y?gcd(y,x%y):x;
}

int n,h,g,a[MAXN];

vector<int>ans;

void del(int rt){
    int lv=a[rt<<1],rv=a[rt<<1|1];
    if(lv==0 && rv==0) a[rt]=0;
    else if(lv>=rv){
        a[rt]=a[rt<<1];
        del(rt<<1);
    }
    else {
        a[rt]=a[rt<<1|1];
        del(rt<<1|1);
    }
}
int getnxt(int rt){
    int lv=a[rt<<1],rv=a[rt<<1|1];
    if(lv==0 && rv==0)return rt;
    else if(lv>=rv)return getnxt(rt<<1);
    else return getnxt(rt<<1|1);
}

int main(){
    int T=readint();
	while(T--){
        h=readint(),g=readint();
        ans.clear();
        for(int i=1;i<=(1<<h+1);i++)a[i]=0;
        for(int i=1;i<(1<<h);i++) a[i]=readint();

        for(int i=1;i<(1<<g);i++)
            while(1){
                int nxt=getnxt(i);
                if(nxt<=(1<<g)-1) break;
                del(i);
                ans.push_back(i);
            }

        ll sum=0;
        for(int i=1;i<(1<<g);i++)sum+=a[i];
        printf("%lld\n",sum);
        for(auto x:ans) printf("%d ",x);
        puts("");
    }
}


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

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

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

#define INF 987654321
#define IINF 987654321987654321

int n,k;

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

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

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

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

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

	return res;
}

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

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

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

	}

	return 0;
}


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

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

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

#define INF 987654321
#define IINF 987654321987654321

int n,k;

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

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

	int i,j = 0;
	while(i < (int)v1.size()-1 || j < (int)v2.size()-1 ){
		if(v1[i] < v2[j]){
			if(v1[i] > M) res.push_back(v1[i]);
			i++;
		}
		else{
			if(v2[j] > M) res.push_back(v2[j]);
			j++;
		}
	}
	res.push_back(v[a]);
	if(a >= (1<<k) ) ans[a] = 0;
	else ans[a] = res[0];

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

	return res;
}

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

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

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

	}

	return 0;
}


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

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

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

#define INF 987654321
#define IINF 987654321987654321

int n,k;

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

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

	int i,j = 0;
	while(i < v1.size()-1 || j < v2.size()-1 ){
// 		printf("%d %d\n",v1[i],v2[j]);
		if(v1[i] < v2[j]){
			if(v1[i] > M) res.push_back(v1[i]);
			i++;
		}
		else{
			if(v2[j] > M) res.push_back(v2[j]);
			j++;
		}
	}
	res.push_back(v[a]);
	if(a >= (1<<k) ) ans[a] = 0;
	else ans[a] = res[0];

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

	return res;
}

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

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

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

	}

	return 0;
}


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

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

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

#define INF 987654321
#define IINF 987654321987654321

int n,k;

vector<int> v,ans;
int chk[(1<<20) + 5];

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

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

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

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

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

	}

	return 0;
}


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

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

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

#define INF 987654321
#define IINF 987654321987654321

int n,k;

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

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

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

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

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

	return res;
}

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

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

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

	}

	return 0;
}