import org.omg.CORBA.INTERNAL;
import sun.rmi.runtime.Log;

import java.io. *;
import java.util.*;
import static java.lang.Math.*;
public class Main {

    static final FastReader sc = new FastReader(System.in);
    static final PrintWriter pw = new PrintWriter(System.out);

    static int visited[];
    static ArrayList<ArrayList<Integer>> adjlist;
    static class edge implements  Comparator<edge>{
        int dest;
        int cost;

        public edge(int dest, int cost) {
            this.dest = dest;
            this.cost = cost;
        }

        @Override
        public int compare(edge o1, edge o2) {
            return o1.cost-o2.cost;
        }
    }

    static int dist[];
    static int nodesinSubtree[];
    static int color[];
    static ArrayList<Integer> res1;
    static ArrayList<Integer> res2;

    static class Graph{
        int V;
        ArrayList<ArrayList<edge>> adj;
        PriorityQueue<edge> pq;

        public Graph(int V){
            this.V = V;
            adj = new ArrayList<>(V);
            pq = new PriorityQueue<>(V,new edge(-1,-1));
            dist = new int[V];
            nodesinSubtree = new int[V];
            color = new int[V];
            Arrays.fill(color,-1);
            for(int i=0; i<V; i++){
                dist[i] = Integer.MAX_VALUE;
                adj.add(i,new ArrayList<>());
            }
        }

        void addedge(int u, int v, int w ){
            adj.get(u).add(new edge(v,w));
            //adj.get(v).add(new edge(u,w));
        }

        void rDfs(int src, int e){
            //nodes in a subtree
            nodesinSubtree[src] = 1;
            for(int i=0; i<adj.get(src).size(); i++){
                if(adj.get(src).get(i).dest==e){
                    continue;
                }
                rDfs(adj.get(src).get(i).dest,src);
                nodesinSubtree[src]+=nodesinSubtree[adj.get(src).get(i).dest];
            }
        }

        void uBFS(int src){
            Queue<edge> q = new LinkedList<>();
            color = new int[V];
            q.add(new edge(src,0));
            color[src] = 0;
            while (!q.isEmpty()){
                int u = q.poll().dest;
                for(int i=0; i<adj.get(u).size(); i++){
                    if(color[adj.get(u).get(i).dest]==color[u]){
                        color[adj.get(u).get(i).dest] = color[u]+1;
                        q.add(new edge(adj.get(u).get(i).dest,0));
                    }
                }
            }
        }

        int DFS(int src){
            Stack<edge> st = new Stack<>();
            boolean visited[] = new boolean[V];
            visited[src] = true;
            st.push(new edge(src,0));
            dist[src] = 0;
            color[src] = 1;
            while (!st.empty()){
                int u = st.peek().dest;
                st.pop();
                for(int i=0; i<adj.get(u).size(); i++){
                    if(color[adj.get(u).get(i).dest]==0) {
                        if (color[u] == 0) {
                            color[adj.get(u).get(i).dest] = 1;
                        } else {
                            color[adj.get(u).get(i).dest] = 0;
                        }
                        st.push(new edge(adj.get(u).get(i).dest,0));
                    }else{
                        if(color[u]==0 && color[adj.get(u).get(i).dest]==0){
                            return -1;
                        }
                        if(color[u]==1 && color[adj.get(u).get(i).dest]==1){
                            return -1;
                        }
                    }
                }
            }
            return 0;
        }
    }

/*
        void dijkstra(int src){
            for(int i=0; i<V; i++){
                dist[i] = Integer.MAX_VALUE;
            }
            dist[src] = 0;
            pq.add(new edge(src,0));
            while (!pq.isEmpty()){
                int u = pq.poll().dest;
                for(int i=0; i<adj.get(u).size(); i++){
                    int v = adj.get(u).get(i).dest;
                    int w = adj.get(u).get(i).cost;
                    if(dist[u]+w<=dist[v]){
                        dist[v] = dist[u] + w;
                        pq.add(new edge(v,dist[v]));
                    }
                }
            }

        }

    }

 */

    static  int I[];
    static  int  L[];
    static void LIS(int n, int arr[]){
        I = new int[n+1];
        L = new int[n];
        I[0] = Integer.MIN_VALUE;
        for(int i=1;i<=n; i++){
            I[i] = Integer.MAX_VALUE;
        }

        int lisLength = 1;

        for(int i=n-1; i>=0; i--) {
            int low = 1;
            int high = lisLength;
            int mid = 0;
            while (low < high) {
                mid = (low + high) >> 1;
                if (I[mid] <= arr[i]) {
                    high = mid;
                } else {
                    low = mid + 1;
                }
            }

            I[high] = arr[i];
            L[i] = high;
            if(lisLength<=high){
                lisLength++;
            }
        }
    }

    public static long gcd(long a, long b)
    {
        if (a == 0)
            return b;

        return gcd(b%a, a);
    }

    public static long lcm(long a, long b)
    {
        return (a*b)/gcd(a, b);
    }

    static long nCr(long n, long r)
    {
        return fact(n) / (fact(r) * fact(n - r));
    }

    // Returns factorial of n
    static long fact(long n)
    {
        long res = 1;
        for (int i = 2; i <= n; i++)
            res = (res * i)%MOD;
        return res;
    }

    static int MOD = 1000000007;
    static int power(int x, int y, int p)
    {

        // Initialize result
        int res = 1;

        // Update x if it is more than or
        // equal to p
        x = x % p;

        while (y > 0)
        {

            // If y is odd, multiply x
            // with result
            if (y % 2 == 1)
                res = (res * x) % p;

            // y must be even now
            y = y >> 1; // y = y/2
            x = (x * x) % p;
        }

        return res;
    }

    // Returns n^(-1) mod p
    static int modInverse(int n, int p)
    {
        return power(n, p-2, p);
    }

    static int nCrModPFermat(int n, int r,
                             int p)
    {

        if (r == 0)
            return 1;

        int[] fac = new int[n+1];
        fac[0] = 1;

        for (int i = 1 ;i <= n; i++)
            fac[i] = fac[i-1] * i % p;

        return (fac[n]* modInverse(fac[r], p)
                % p * modInverse(fac[n-r], p)
                % p) % p;
    }

    static int[] phi;

    static void generatePhi(int n){
        phi = new int[n+1];

        for(int i = 0; i<=n; i++){
            phi[i] = i;
        }

        for(int i=2; i<=n; i++){
            if(phi[i]==i){
                for(int j= i; j<=n; j+=i){
                    phi[j] -= (phi[j]/i);
                }
            }
        }
    }

    static int gcdCount(int n, int d){
        return phi[n/d];
    }

    static boolean prime[];
    static int smallestFactor[];

    static void sieveOfEratosthenes(int n)
    {
        prime = new boolean[n+1];
        smallestFactor = new int[n+1];

        smallestFactor[1] = 1;
        for(int i=2;i<n;i++) {
            prime[i] = true;
            smallestFactor[i] = i;
        }

        for(int p = 2; p*p <=n; p++)
        {
            if(prime[p])
            {
                smallestFactor[p] = p;
                for(int i = p*p; i <= n; i += p) {
                    prime[i] = false;
                    smallestFactor[i] = min(p,smallestFactor[i]);
                }
            }
        }

    }

    static double Logx(double n, int x){
        return log(n)/log(x);
    }

    static boolean isPrime(long n){
        if(n<2){
            return false;
        }
        for(int i = 2; i*i<=n; i++){
            if(n%i==0){
                return false;
            }
        }
        return true;
    }

    static int sumOfDigits(int n){
        int digitSum = 0;
        while (n>0){
            digitSum+=n%10;
            n/=10;
        }
        return digitSum;
    }

    static int a[];
    static int c[];

    static void PrefixFlip(int l){
        c = new int[l];
        for(int i =0; i<l; i++){
            if(a[i]==0){
                a[i]=1;
            }else{
                a[i]=0;
            }
        }
        for(int i =0; i<l; i++){
            c[i] = a[l-i-1];
        }
        for(int i =0; i<l; i++){
            a[i] = c[i];
        }

    }

    public static void main(String[] args) {

        int testcase = sc.nextInt();

        while (testcase-->0) {
            int n = sc.nextInt();
            a = new int[n];
            int b[] = new int[n];
            for(int i =0; i<n; i++){
                a[i] = sc.nextChar()-'0';
            }

            for(int i = 0; i<n; i++){
                b[i] = sc.nextChar()-'0';
            }

            int k = 0;

            int end = n-1;

            ArrayList<Integer> res = new ArrayList<>();

            while (end>0){
                if(a[end]==b[end]){
                    end--;
                    continue;
                }
                if(a[0]==b[end]){
                    k+=2;
                    res.add(1);
                    res.add(end+1);
                    PrefixFlip(1);
                    PrefixFlip(end+1);
                }else{
                    k+=1;
                    res.add(end+1);
                    PrefixFlip(end+1);
                }
            }

            if(a[0]!=b[0]){
                k+=1;
                res.add(1);
            }
            if(k!=0) {
                pw.print(k + " ");
            }else{
                pw.println(k);
            }
            for(int i =0 ;i<k; i++){
                if(i!=k-1){
                    pw.print(res.get(i)+" ");
                }else{
                    pw.println(res.get(i));
                }
            }

        }

        pw.flush();

    }

    static void debug(Object...obj) {
        System.err.println(Arrays.deepToString(obj));
    }

    static class FastReader {
        InputStream is;
        private byte[] inbuf = new byte[1024];
        private int lenbuf = 0, ptrbuf = 0;

        public FastReader(InputStream is) {
            this.is = is;
        }

        public int readByte(){
            if(lenbuf == -1)throw new InputMismatchException();
            if(ptrbuf >= lenbuf){
                ptrbuf = 0;
                try {
                    lenbuf = is.read(inbuf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if(lenbuf <= 0)
                    return -1;
            }
            return inbuf[ptrbuf++];
        }
        public boolean isSpaceChar(int c) {
            return !(c >= 33 && c <= 126);
        }
        public int skip() {
            int b;
            while ((b = readByte()) != -1 && isSpaceChar(b)) ;
            return b;
        }
        public String next(){
            int b = skip();
            StringBuilder sb = new StringBuilder();
            while(!(isSpaceChar(b))) {
                sb.appendCodePoint(b);
                b = readByte();
            }
            return sb.toString();
        }
        public String nextLine(){
            int b = readByte();
            StringBuilder sb = new StringBuilder();
            while(b != '\n' || b != '\r'){
                sb.appendCodePoint(b);
            }
            return sb.toString();
        }
        public int nextInt() {
            int num = 0, b;
            boolean minus = false;
            while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ;
            if (b == '-') {
                minus = true;
                b = readByte();
            }
            while (true) {
                if (b >= '0' && b <= '9') {
                    num = (num << 3) + (num << 1) + (b - '0');
                } else {
                    return minus ? -num : num;
                }
                b = readByte();
            }
        }
        public long nextLong() {
            long num = 0;
            int b;
            boolean minus = false;
            while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ;
            if (b == '-') {
                minus = true;
                b = readByte();
            }
            while (true) {
                if (b >= '0' && b <= '9') {
                    num = (num << 3) + (num << 1) + (b - '0');
                } else {
                    return minus ? -num : num;
                }
                b = readByte();
            }
        }
        public double nextDouble() {
            return Double.parseDouble(next());
        }
        public char[] next(int n){
            char[] buf = new char[n];
            int b = skip(), p = 0;
            while (p < n && !(isSpaceChar(b))) {
                buf[p++] = (char) b;
                b = readByte();
            }
            return n == p ? buf : Arrays.copyOf(buf, p);
        }
        public char nextChar () {
            return (char) skip();
        }
    }
}


import org.omg.CORBA.INTERNAL;
import sun.rmi.runtime.Log;

import java.io. *;
import java.util.*;
import static java.lang.Math.*;
public class Main {

    static final FastReader sc = new FastReader(System.in);
    static final PrintWriter pw = new PrintWriter(System.out);

    static int visited[];
    static ArrayList<ArrayList<Integer>> adjlist;
    static class edge implements  Comparator<edge>{
        int dest;
        int cost;

        public edge(int dest, int cost) {
            this.dest = dest;
            this.cost = cost;
        }

        @Override
        public int compare(edge o1, edge o2) {
            return o1.cost-o2.cost;
        }
    }

    static int dist[];
    static int nodesinSubtree[];
    static int color[];
    static ArrayList<Integer> res1;
    static ArrayList<Integer> res2;

    static class Graph{
        int V;
        ArrayList<ArrayList<edge>> adj;
        PriorityQueue<edge> pq;

        public Graph(int V){
            this.V = V;
            adj = new ArrayList<>(V);
            pq = new PriorityQueue<>(V,new edge(-1,-1));
            dist = new int[V];
            nodesinSubtree = new int[V];
            color = new int[V];
            Arrays.fill(color,-1);
            for(int i=0; i<V; i++){
                dist[i] = Integer.MAX_VALUE;
                adj.add(i,new ArrayList<>());
            }
        }

        void addedge(int u, int v, int w ){
            adj.get(u).add(new edge(v,w));
            //adj.get(v).add(new edge(u,w));
        }

        void rDfs(int src, int e){
            //nodes in a subtree
            nodesinSubtree[src] = 1;
            for(int i=0; i<adj.get(src).size(); i++){
                if(adj.get(src).get(i).dest==e){
                    continue;
                }
                rDfs(adj.get(src).get(i).dest,src);
                nodesinSubtree[src]+=nodesinSubtree[adj.get(src).get(i).dest];
            }
        }

        void uBFS(int src){
            Queue<edge> q = new LinkedList<>();
            color = new int[V];
            q.add(new edge(src,0));
            color[src] = 0;
            while (!q.isEmpty()){
                int u = q.poll().dest;
                for(int i=0; i<adj.get(u).size(); i++){
                    if(color[adj.get(u).get(i).dest]==color[u]){
                        color[adj.get(u).get(i).dest] = color[u]+1;
                        q.add(new edge(adj.get(u).get(i).dest,0));
                    }
                }
            }
        }

        int DFS(int src){
            Stack<edge> st = new Stack<>();
            boolean visited[] = new boolean[V];
            visited[src] = true;
            st.push(new edge(src,0));
            dist[src] = 0;
            color[src] = 1;
            while (!st.empty()){
                int u = st.peek().dest;
                st.pop();
                for(int i=0; i<adj.get(u).size(); i++){
                    if(color[adj.get(u).get(i).dest]==0) {
                        if (color[u] == 0) {
                            color[adj.get(u).get(i).dest] = 1;
                        } else {
                            color[adj.get(u).get(i).dest] = 0;
                        }
                        st.push(new edge(adj.get(u).get(i).dest,0));
                    }else{
                        if(color[u]==0 && color[adj.get(u).get(i).dest]==0){
                            return -1;
                        }
                        if(color[u]==1 && color[adj.get(u).get(i).dest]==1){
                            return -1;
                        }
                    }
                }
            }
            return 0;
        }
    }

/*
        void dijkstra(int src){
            for(int i=0; i<V; i++){
                dist[i] = Integer.MAX_VALUE;
            }
            dist[src] = 0;
            pq.add(new edge(src,0));
            while (!pq.isEmpty()){
                int u = pq.poll().dest;
                for(int i=0; i<adj.get(u).size(); i++){
                    int v = adj.get(u).get(i).dest;
                    int w = adj.get(u).get(i).cost;
                    if(dist[u]+w<=dist[v]){
                        dist[v] = dist[u] + w;
                        pq.add(new edge(v,dist[v]));
                    }
                }
            }

        }

    }

 */

    static  int I[];
    static  int  L[];
    static void LIS(int n, int arr[]){
        I = new int[n+1];
        L = new int[n];
        I[0] = Integer.MIN_VALUE;
        for(int i=1;i<=n; i++){
            I[i] = Integer.MAX_VALUE;
        }

        int lisLength = 1;

        for(int i=n-1; i>=0; i--) {
            int low = 1;
            int high = lisLength;
            int mid = 0;
            while (low < high) {
                mid = (low + high) >> 1;
                if (I[mid] <= arr[i]) {
                    high = mid;
                } else {
                    low = mid + 1;
                }
            }

            I[high] = arr[i];
            L[i] = high;
            if(lisLength<=high){
                lisLength++;
            }
        }
    }

    public static long gcd(long a, long b)
    {
        if (a == 0)
            return b;

        return gcd(b%a, a);
    }

    public static long lcm(long a, long b)
    {
        return (a*b)/gcd(a, b);
    }

    static long nCr(long n, long r)
    {
        return fact(n) / (fact(r) * fact(n - r));
    }

    // Returns factorial of n
    static long fact(long n)
    {
        long res = 1;
        for (int i = 2; i <= n; i++)
            res = (res * i)%MOD;
        return res;
    }

    static int MOD = 1000000007;
    static int power(int x, int y, int p)
    {

        // Initialize result
        int res = 1;

        // Update x if it is more than or
        // equal to p
        x = x % p;

        while (y > 0)
        {

            // If y is odd, multiply x
            // with result
            if (y % 2 == 1)
                res = (res * x) % p;

            // y must be even now
            y = y >> 1; // y = y/2
            x = (x * x) % p;
        }

        return res;
    }

    // Returns n^(-1) mod p
    static int modInverse(int n, int p)
    {
        return power(n, p-2, p);
    }

    static int nCrModPFermat(int n, int r,
                             int p)
    {

        if (r == 0)
            return 1;

        int[] fac = new int[n+1];
        fac[0] = 1;

        for (int i = 1 ;i <= n; i++)
            fac[i] = fac[i-1] * i % p;

        return (fac[n]* modInverse(fac[r], p)
                % p * modInverse(fac[n-r], p)
                % p) % p;
    }

    static int[] phi;

    static void generatePhi(int n){
        phi = new int[n+1];

        for(int i = 0; i<=n; i++){
            phi[i] = i;
        }

        for(int i=2; i<=n; i++){
            if(phi[i]==i){
                for(int j= i; j<=n; j+=i){
                    phi[j] -= (phi[j]/i);
                }
            }
        }
    }

    static int gcdCount(int n, int d){
        return phi[n/d];
    }

    static boolean prime[];
    static int smallestFactor[];

    static void sieveOfEratosthenes(int n)
    {
        prime = new boolean[n+1];
        smallestFactor = new int[n+1];

        smallestFactor[1] = 1;
        for(int i=2;i<n;i++) {
            prime[i] = true;
            smallestFactor[i] = i;
        }

        for(int p = 2; p*p <=n; p++)
        {
            if(prime[p])
            {
                smallestFactor[p] = p;
                for(int i = p*p; i <= n; i += p) {
                    prime[i] = false;
                    smallestFactor[i] = min(p,smallestFactor[i]);
                }
            }
        }

    }

    static double Logx(double n, int x){
        return log(n)/log(x);
    }

    static boolean isPrime(long n){
        if(n<2){
            return false;
        }
        for(int i = 2; i*i<=n; i++){
            if(n%i==0){
                return false;
            }
        }
        return true;
    }

    static int sumOfDigits(int n){
        int digitSum = 0;
        while (n>0){
            digitSum+=n%10;
            n/=10;
        }
        return digitSum;
    }

    static int a[];

    static void PrefixFlip(int l){
        for(int i =0; i<l; i++){
            if(a[i]==0){
                a[i]=1;
            }else{
                a[i]=0;
            }
        }
        for(int i =0; i<=l/2; i++){
            a[l-i-1] = a[l-i-1] + a[i] - (a[i]=a[l-i-1]);
        }
    }

    public static void main(String[] args) {

        int testcase = sc.nextInt();

        while (testcase-->0) {
            int n = sc.nextInt();
            a = new int[n];
            int b[] = new int[n];
            for(int i =0; i<n; i++){
                a[i] = sc.nextChar()-'0';
            }

            for(int i = 0; i<n; i++){
                b[i] = sc.nextChar()-'0';
            }

            int k = 0;

            int end = n-1;

            ArrayList<Integer> res = new ArrayList<>();

            while (end>0){
                if(a[end]==b[end]){
                    end--;
                    continue;
                }
                if(a[0]==b[end]){
                    k+=2;
                    res.add(1);
                    res.add(end+1);
                    PrefixFlip(1);
                    PrefixFlip(end+1);
                }else{
                    k+=1;
                    res.add(end+1);
                    PrefixFlip(end+1);
                }
            }

            if(a[0]!=b[0]){
                k+=1;
                res.add(1);
            }
            if(k!=0) {
                pw.print(k + " ");
            }else{
                pw.println(k);
            }
            for(int i =0 ;i<k; i++){
                if(i!=k-1){
                    pw.print(res.get(i)+" ");
                }else{
                    pw.println(res.get(i));
                }
            }

        }

        pw.flush();

    }

    static void debug(Object...obj) {
        System.err.println(Arrays.deepToString(obj));
    }

    static class FastReader {
        InputStream is;
        private byte[] inbuf = new byte[1024];
        private int lenbuf = 0, ptrbuf = 0;

        public FastReader(InputStream is) {
            this.is = is;
        }

        public int readByte(){
            if(lenbuf == -1)throw new InputMismatchException();
            if(ptrbuf >= lenbuf){
                ptrbuf = 0;
                try {
                    lenbuf = is.read(inbuf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if(lenbuf <= 0)
                    return -1;
            }
            return inbuf[ptrbuf++];
        }
        public boolean isSpaceChar(int c) {
            return !(c >= 33 && c <= 126);
        }
        public int skip() {
            int b;
            while ((b = readByte()) != -1 && isSpaceChar(b)) ;
            return b;
        }
        public String next(){
            int b = skip();
            StringBuilder sb = new StringBuilder();
            while(!(isSpaceChar(b))) {
                sb.appendCodePoint(b);
                b = readByte();
            }
            return sb.toString();
        }
        public String nextLine(){
            int b = readByte();
            StringBuilder sb = new StringBuilder();
            while(b != '\n' || b != '\r'){
                sb.appendCodePoint(b);
            }
            return sb.toString();
        }
        public int nextInt() {
            int num = 0, b;
            boolean minus = false;
            while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ;
            if (b == '-') {
                minus = true;
                b = readByte();
            }
            while (true) {
                if (b >= '0' && b <= '9') {
                    num = (num << 3) + (num << 1) + (b - '0');
                } else {
                    return minus ? -num : num;
                }
                b = readByte();
            }
        }
        public long nextLong() {
            long num = 0;
            int b;
            boolean minus = false;
            while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ;
            if (b == '-') {
                minus = true;
                b = readByte();
            }
            while (true) {
                if (b >= '0' && b <= '9') {
                    num = (num << 3) + (num << 1) + (b - '0');
                } else {
                    return minus ? -num : num;
                }
                b = readByte();
            }
        }
        public double nextDouble() {
            return Double.parseDouble(next());
        }
        public char[] next(int n){
            char[] buf = new char[n];
            int b = skip(), p = 0;
            while (p < n && !(isSpaceChar(b))) {
                buf[p++] = (char) b;
                b = readByte();
            }
            return n == p ? buf : Arrays.copyOf(buf, p);
        }
        public char nextChar () {
            return (char) skip();
        }
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.Collections;
import java.util.ArrayList;

public class _1382C2 {

	public static void main(String[] args) {
		FastScanner fs=new FastScanner();
		int T = 1;
		T = fs.nextInt();
		for(int tt = 1; tt <= T; tt++) {
			int n = fs.nextInt();
			char[] a = fs.next().toCharArray();
			char[] b = fs.next().toCharArray();

			ArrayList<Integer> ans = new ArrayList<Integer>();
			boolean flipped = false;
			int l = 0, h = n - 1;

			for(int i = n - 1; i >= 0; i--) {
				char ch = (flipped ? (char)('0' + '1' - a[h]) : a[l]); //a[h] ^ 1
				if(ch == b[i]) {
					ans.add(0);
				}
				ans.add(i);
				if(flipped) {
					h--;
				} else {
					l++;
				}

				flipped ^= true;
			}

			System.out.print(ans.size());
			for(int i: ans) {
				System.out.print(" " + (i + 1));
			}
			System.out.println();
		}
	}

	static class FastScanner {
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st=new StringTokenizer("");
		public String next() {
			while (!st.hasMoreElements())
				try {
					st=new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			return st.nextToken();
		}

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

		int[] readArray(int n) {
			int[] a=new int[n];
			for (int i=0; i<n; i++) a[i]=nextInt();
			return a;
		}

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


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.Collections;
import java.util.ArrayList;

public class _1382C1 {

	public static void main(String[] args) {
		FastScanner fs=new FastScanner();
		int T = 1;
		T = fs.nextInt();
		for(int tt = 1; tt <= T; tt++) {
			int n = fs.nextInt();
			char[] a = fs.next().toCharArray();
			char[] b = fs.next().toCharArray();

			ArrayList<Integer> ans = new ArrayList<Integer>();

			for(int i = 0; i < n; i++) {
				if(a[i] == b[i]) {
					continue;
				}
				ans.add(i);
				ans.add(0);
				ans.add(i);
			}

			System.out.print(ans.size() + " ");
			for(int i: ans) {
				System.out.print(i + 1 + " ");
			}
			System.out.println();
		}
	}

	static class FastScanner {
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st=new StringTokenizer("");
		public String next() {
			while (!st.hasMoreElements())
				try {
					st=new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			return st.nextToken();
		}

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

		int[] readArray(int n) {
			int[] a=new int[n];
			for (int i=0; i<n; i++) a[i]=nextInt();
			return a;
		}

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


/**
 * ******* Created  on 21/7/20 7:58 PM*******
 */

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

public class C1382 implements Runnable {

    private static final int MAX = (int) (1E5 + 5);
    private static final int MOD = (int) (1E9 + 7);
    private static final long Inf = (long) (1E14 + 10);
    private static final double eps = (double) (1E-9);

    private void solve() throws IOException {
        int t = 1;
        t = reader.nextInt();
        while (t-- > 0) {
            int n = reader.nextInt();
            String a = reader.next();
            String b = reader.next();

            boolean flag = false, invert =false;
            List<Integer> ans = new ArrayList<>();
            for(int i = a.length()-1, j =b.length()-1, k=0;j>=0; j--){
                if(a.charAt(i) == b.charAt(j) && !flag && !invert){
                    i--;
                    continue;
                }
                if(a.charAt(k) == b.charAt(j) && !flag && invert){
                    k++;
                    continue;
                }
                if(a.charAt(i) != b.charAt(j) && flag && !invert){
                    i--;
                    continue;
                }
                if(a.charAt(k) != b.charAt(j) && flag && invert){
                    k++;
                    continue;
                }

                if(flag){
                    if(invert){
                        check(a, k,j,ans, flag);
                        invert =false;
                        flag =false;
                        i--;
                    }else{
                        check(a, i,j,ans, flag);
                        k++;
                        invert =true;
                        flag = false;
                    }
                }else{
                    if(invert){
                        check(a, k,j,ans, flag);
                        invert =false;
                        flag =true;
                        i--;
                    }else{
                        check(a, i,j,ans, flag);
                        k++;
                        invert =true;
                        flag = true;
                    }
                }
            }

            for(int i=0;i< ans.size();i++){

            }
            writer.print(ans.size()+" ");
            for(int a1 : ans)writer.print( (a1+1) +" ");
            writer.println();
        }
    }

    private void check(String a, int i, int j, List<Integer> ans, boolean flag) {
        if(flag)
            if(a.charAt(j) == a.charAt(i)){
            ans.add(i);
            }else{
                ans.add(0);
                ans.add(j);
            }
        else
            if(a.charAt(j) != a.charAt(i)){
                ans.add(j);
            }else{
                ans.add(0);
                ans.add(j);
            }
    }

    public static void main(String[] args) throws IOException {
        try (Input reader = new StandardInput(); PrintWriter writer = new PrintWriter(System.out)) {
            new C1382().run();
        }
    }

    StandardInput reader;
    PrintWriter writer;

    @Override
    public void run() {
        try {
            reader = new StandardInput();
            writer = new PrintWriter(System.out);
            solve();
            reader.close();
            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    interface Input extends Closeable {
        String next() throws IOException;

        String nextLine() throws IOException;

        default int nextInt() throws IOException {
            return Integer.parseInt(next());
        }

        default long nextLong() throws IOException {
            return Long.parseLong(next());
        }

        default double nextDouble() throws IOException {
            return Double.parseDouble(next());
        }

        default int[] readIntArray() throws IOException {
            return readIntArray(nextInt());
        }

        default int[] readIntArray(int size) throws IOException {
            int[] array = new int[size];
            for (int i = 0; i < array.length; i++) {
                array[i] = nextInt();
            }
            return array;
        }

        default long[] readLongArray(int size) throws IOException {
            long[] array = new long[size];
            for (int i = 0; i < array.length; i++) {
                array[i] = nextLong();
            }
            return array;
        }
    }

    private static class StandardInput implements Input {
        private final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

        private StringTokenizer stringTokenizer;

        @Override
        public void close() throws IOException {
            reader.close();
        }

        @Override
        public String next() throws IOException {
            if (stringTokenizer == null || !stringTokenizer.hasMoreTokens()) {
                stringTokenizer = new StringTokenizer(reader.readLine());
            }
            return stringTokenizer.nextToken();
        }

        @Override
        public String nextLine() throws IOException {
            return reader.readLine();
        }
    }

}


/**
 * ******* Created  on 21/7/20 7:58 PM*******
 */

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

public class C1382 implements Runnable {

    private static final int MAX = (int) (1E5 + 5);
    private static final int MOD = (int) (1E9 + 7);
    private static final long Inf = (long) (1E14 + 10);
    private static final double eps = (double) (1E-9);
    char[] ansC;
    private void solve() throws IOException {
        int t = 1;
        t = reader.nextInt();
        while (t-- > 0) {
            int n = reader.nextInt();
            String a = reader.next();
            String b = reader.next();
            ansC = new char[n];
            boolean flag = false, invert =false;
            List<Integer> ans = new ArrayList<>();
            for(int i = a.length()-1, j =b.length()-1, k=0;j>=0; j--){

                if(a.charAt(i) == b.charAt(j) && !flag){
                    i--;
                    continue;
                }
                if(a.charAt(k) != b.charAt(j) && flag ){
                    k++;
                    continue;
                }

                if(flag){
                        check(a, i,j,ans, flag);
                        i--;
                }else{
                        check(a, k,j,ans, flag);
                        k++;
                }
                flag = !flag;
            }

            writer.print(ans.size()+" ");
            for(int a1 : ans)writer.print( (a1+1) +" ");
            writer.println();

        }
    }

    private void check(String a, int i, int j, List<Integer> ans, boolean flag) {

        if(flag)
            if(a.charAt(j) != a.charAt(i) || j==0){
                ans.add(j);
            }else{
                ans.add(0);
                ans.add(j);
                ansC[j] = a.charAt(i)=='1'?'0':'1';
            }
        else
            if(a.charAt(j) == a.charAt(i) || j ==0){
                ans.add(j);
            }else{
                ans.add(0);
                ans.add(j);
            }
    }

    public static void main(String[] args) throws IOException {
        try (Input reader = new StandardInput(); PrintWriter writer = new PrintWriter(System.out)) {
            new C1382().run();
        }
    }

    StandardInput reader;
    PrintWriter writer;

    @Override
    public void run() {
        try {
            reader = new StandardInput();
            writer = new PrintWriter(System.out);
            solve();
            reader.close();
            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    interface Input extends Closeable {
        String next() throws IOException;

        String nextLine() throws IOException;

        default int nextInt() throws IOException {
            return Integer.parseInt(next());
        }

        default long nextLong() throws IOException {
            return Long.parseLong(next());
        }

        default double nextDouble() throws IOException {
            return Double.parseDouble(next());
        }

        default int[] readIntArray() throws IOException {
            return readIntArray(nextInt());
        }

        default int[] readIntArray(int size) throws IOException {
            int[] array = new int[size];
            for (int i = 0; i < array.length; i++) {
                array[i] = nextInt();
            }
            return array;
        }

        default long[] readLongArray(int size) throws IOException {
            long[] array = new long[size];
            for (int i = 0; i < array.length; i++) {
                array[i] = nextLong();
            }
            return array;
        }
    }

    private static class StandardInput implements Input {
        private final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

        private StringTokenizer stringTokenizer;

        @Override
        public void close() throws IOException {
            reader.close();
        }

        @Override
        public String next() throws IOException {
            if (stringTokenizer == null || !stringTokenizer.hasMoreTokens()) {
                stringTokenizer = new StringTokenizer(reader.readLine());
            }
            return stringTokenizer.nextToken();
        }

        @Override
        public String nextLine() throws IOException {
            return reader.readLine();
        }
    }

}


/**
 * ******* Created  on 21/7/20 7:58 PM*******
 */

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

public class C1382 implements Runnable {

    private static final int MAX = (int) (1E5 + 5);
    private static final int MOD = (int) (1E9 + 7);
    private static final long Inf = (long) (1E14 + 10);
    private static final double eps = (double) (1E-9);
    char[] ansC;
    private void solve() throws IOException {
        int t = 1;
        t = reader.nextInt();
        while (t-- > 0) {
            int n = reader.nextInt();
            String a = reader.next();
            String b = reader.next();
            ansC = new char[n];
            boolean flag = false, invert =false;
            List<Integer> ans = new ArrayList<>();
            for(int i = a.length()-1, j =b.length()-1, k=0;j>=0; j--){
                ansC[j] =b.charAt(j);
                if(a.charAt(i) == b.charAt(j) && !flag && !invert){
                    i--;
                    continue;
                }
                if(a.charAt(k) == b.charAt(j) && !flag && invert){
                    k++;
                    continue;
                }
                if(a.charAt(i) != b.charAt(j) && flag && !invert){
                    i--;
                    continue;
                }
                if(a.charAt(k) != b.charAt(j) && flag && invert){
                    k++;
                    continue;
                }

                if(flag){
                    if(invert){
                        check(a, k,j,ans, flag);
                        invert =false;
                        flag =false;
                        i--;
                    }else{
                        check(a, i,j,ans, flag);
                        k++;
                        invert =true;
                        flag = false;
                    }
                }else{
                    if(invert){
                        check(a, k,j,ans, flag);
                        invert =false;
                        flag =true;
                        i--;
                    }else{
                        check(a, i,j,ans, flag);
                        k++;
                        invert =true;
                        flag = true;
                    }
                }
            }

            writer.print(ans.size()+" ");
            for(int a1 : ans)writer.print( (a1+1) +" ");
            writer.println(ansC);
        }
    }

    private void check(String a, int i, int j, List<Integer> ans, boolean flag) {
        ansC[j] = a.charAt(i)=='1'?'0':'1';
        if(flag)
            if(a.charAt(j) == a.charAt(i) || j==0){
                ans.add(j);
            }else{
                ans.add(0);
                ans.add(j);
                ansC[j] = a.charAt(i)=='1'?'0':'1';
            }
        else
            if(a.charAt(j) != a.charAt(i) || j ==0){
                ans.add(j);
            }else{
                ans.add(0);
                ans.add(j);
            }
    }

    public static void main(String[] args) throws IOException {
        try (Input reader = new StandardInput(); PrintWriter writer = new PrintWriter(System.out)) {
            new C1382().run();
        }
    }

    StandardInput reader;
    PrintWriter writer;

    @Override
    public void run() {
        try {
            reader = new StandardInput();
            writer = new PrintWriter(System.out);
            solve();
            reader.close();
            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    interface Input extends Closeable {
        String next() throws IOException;

        String nextLine() throws IOException;

        default int nextInt() throws IOException {
            return Integer.parseInt(next());
        }

        default long nextLong() throws IOException {
            return Long.parseLong(next());
        }

        default double nextDouble() throws IOException {
            return Double.parseDouble(next());
        }

        default int[] readIntArray() throws IOException {
            return readIntArray(nextInt());
        }

        default int[] readIntArray(int size) throws IOException {
            int[] array = new int[size];
            for (int i = 0; i < array.length; i++) {
                array[i] = nextInt();
            }
            return array;
        }

        default long[] readLongArray(int size) throws IOException {
            long[] array = new long[size];
            for (int i = 0; i < array.length; i++) {
                array[i] = nextLong();
            }
            return array;
        }
    }

    private static class StandardInput implements Input {
        private final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

        private StringTokenizer stringTokenizer;

        @Override
        public void close() throws IOException {
            reader.close();
        }

        @Override
        public String next() throws IOException {
            if (stringTokenizer == null || !stringTokenizer.hasMoreTokens()) {
                stringTokenizer = new StringTokenizer(reader.readLine());
            }
            return stringTokenizer.nextToken();
        }

        @Override
        public String nextLine() throws IOException {
            return reader.readLine();
        }
    }

}


/**
 * ******* Created  on 21/7/20 7:58 PM*******
 */

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

public class C1382 implements Runnable {

    private static final int MAX = (int) (1E5 + 5);
    private static final int MOD = (int) (1E9 + 7);
    private static final long Inf = (long) (1E14 + 10);
    private static final double eps = (double) (1E-9);
    char[] ansC;
    private void solve() throws IOException {
        int t = 1;
        t = reader.nextInt();
        while (t-- > 0) {
            int n = reader.nextInt();
            String a = reader.next();
            String b = reader.next();
            ansC = new char[n];
            boolean flag = false, invert =false;
            List<Integer> ans = new ArrayList<>();
            for(int i = a.length()-1, j =b.length()-1, k=0;j>=0; j--){

                if(a.charAt(i) == b.charAt(j) && !flag && !invert){
                    i--;
                    continue;
                }
                if(a.charAt(k) == b.charAt(j) && !flag && invert){
                    k++;
                    continue;
                }
                if(a.charAt(i) != b.charAt(j) && flag && !invert){
                    i--;
                    continue;
                }
                if(a.charAt(k) != b.charAt(j) && flag && invert){
                    k++;
                    continue;
                }

                if(flag){
                    if(invert){
                        check(a, k,j,ans, flag);
                        invert =false;
                        flag =false;
                        i--;
                    }else{
                        check(a, i,j,ans, flag);
                        k++;
                        invert =true;
                        flag = false;
                    }
                }else{
                    if(invert){
                        check(a, k,j,ans, flag);
                        invert =false;
                        flag =true;
                        i--;
                    }else{
                        check(a, i,j,ans, flag);
                        k++;
                        invert =true;
                        flag = true;
                    }
                }
            }

            writer.print(ans.size()+" ");
            for(int a1 : ans)writer.print( (a1+1) +" ");
            writer.println();

        }
    }

    private void check(String a, int i, int j, List<Integer> ans, boolean flag) {

        if(flag)
            if(a.charAt(j) == a.charAt(i) || j==0){
                ans.add(j);
            }else{
                ans.add(0);
                ans.add(j);
                ansC[j] = a.charAt(i)=='1'?'0':'1';
            }
        else
            if(a.charAt(j) != a.charAt(i) || j ==0){
                ans.add(j);
            }else{
                ans.add(0);
                ans.add(j);
            }
    }

    public static void main(String[] args) throws IOException {
        try (Input reader = new StandardInput(); PrintWriter writer = new PrintWriter(System.out)) {
            new C1382().run();
        }
    }

    StandardInput reader;
    PrintWriter writer;

    @Override
    public void run() {
        try {
            reader = new StandardInput();
            writer = new PrintWriter(System.out);
            solve();
            reader.close();
            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    interface Input extends Closeable {
        String next() throws IOException;

        String nextLine() throws IOException;

        default int nextInt() throws IOException {
            return Integer.parseInt(next());
        }

        default long nextLong() throws IOException {
            return Long.parseLong(next());
        }

        default double nextDouble() throws IOException {
            return Double.parseDouble(next());
        }

        default int[] readIntArray() throws IOException {
            return readIntArray(nextInt());
        }

        default int[] readIntArray(int size) throws IOException {
            int[] array = new int[size];
            for (int i = 0; i < array.length; i++) {
                array[i] = nextInt();
            }
            return array;
        }

        default long[] readLongArray(int size) throws IOException {
            long[] array = new long[size];
            for (int i = 0; i < array.length; i++) {
                array[i] = nextLong();
            }
            return array;
        }
    }

    private static class StandardInput implements Input {
        private final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

        private StringTokenizer stringTokenizer;

        @Override
        public void close() throws IOException {
            reader.close();
        }

        @Override
        public String next() throws IOException {
            if (stringTokenizer == null || !stringTokenizer.hasMoreTokens()) {
                stringTokenizer = new StringTokenizer(reader.readLine());
            }
            return stringTokenizer.nextToken();
        }

        @Override
        public String nextLine() throws IOException {
            return reader.readLine();
        }
    }

}


/**
 * ******* Created  on 21/7/20 7:58 PM*******
 */

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

public class C1382 implements Runnable {

    private static final int MAX = (int) (1E5 + 5);
    private static final int MOD = (int) (1E9 + 7);
    private static final long Inf = (long) (1E14 + 10);
    private static final double eps = (double) (1E-9);
    char[] ansC;
    private void solve() throws IOException {
        int t = 1;
        t = reader.nextInt();
        while (t-- > 0) {
            int n = reader.nextInt();
            String a = reader.next();
            String b = reader.next();
            ansC = new char[n];
            boolean flag = false, invert =false;
            List<Integer> ans = new ArrayList<>();
            for(int i = a.length()-1, j =b.length()-1, k=0;j>=0; j--){

                if(a.charAt(i) == b.charAt(j) && !flag){
                    i--;
                    continue;
                }
                if(a.charAt(k) != b.charAt(j) && flag ){
                    k++;
                    continue;
                }

                if(flag){
                        check(a, k,j,ans, flag);
                        i--;
                }else{
                        check(a, i,j,ans, flag);
                        k++;
                }
                flag = !flag;
            }

            writer.print(ans.size()+" ");
            for(int a1 : ans)writer.print( (a1+1) +" ");
            writer.println();

        }
    }

    private void check(String a, int i, int j, List<Integer> ans, boolean flag) {

        if(flag)
            if(a.charAt(j) == a.charAt(i) || j==0){
                ans.add(j);
            }else{
                ans.add(0);
                ans.add(j);
                ansC[j] = a.charAt(i)=='1'?'0':'1';
            }
        else
            if(a.charAt(j) != a.charAt(i) || j ==0){
                ans.add(j);
            }else{
                ans.add(0);
                ans.add(j);
            }
    }

    public static void main(String[] args) throws IOException {
        try (Input reader = new StandardInput(); PrintWriter writer = new PrintWriter(System.out)) {
            new C1382().run();
        }
    }

    StandardInput reader;
    PrintWriter writer;

    @Override
    public void run() {
        try {
            reader = new StandardInput();
            writer = new PrintWriter(System.out);
            solve();
            reader.close();
            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    interface Input extends Closeable {
        String next() throws IOException;

        String nextLine() throws IOException;

        default int nextInt() throws IOException {
            return Integer.parseInt(next());
        }

        default long nextLong() throws IOException {
            return Long.parseLong(next());
        }

        default double nextDouble() throws IOException {
            return Double.parseDouble(next());
        }

        default int[] readIntArray() throws IOException {
            return readIntArray(nextInt());
        }

        default int[] readIntArray(int size) throws IOException {
            int[] array = new int[size];
            for (int i = 0; i < array.length; i++) {
                array[i] = nextInt();
            }
            return array;
        }

        default long[] readLongArray(int size) throws IOException {
            long[] array = new long[size];
            for (int i = 0; i < array.length; i++) {
                array[i] = nextLong();
            }
            return array;
        }
    }

    private static class StandardInput implements Input {
        private final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

        private StringTokenizer stringTokenizer;

        @Override
        public void close() throws IOException {
            reader.close();
        }

        @Override
        public String next() throws IOException {
            if (stringTokenizer == null || !stringTokenizer.hasMoreTokens()) {
                stringTokenizer = new StringTokenizer(reader.readLine());
            }
            return stringTokenizer.nextToken();
        }

        @Override
        public String nextLine() throws IOException {
            return reader.readLine();
        }
    }

}


/**
 * ******* Created  on 21/7/20 7:58 PM*******
 */

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

public class C1382 implements Runnable {

    private static final int MAX = (int) (1E5 + 5);
    private static final int MOD = (int) (1E9 + 7);
    private static final long Inf = (long) (1E14 + 10);
    private static final double eps = (double) (1E-9);
    char[] ansC;
    private void solve() throws IOException {
        int t = 1;
        t = reader.nextInt();
        while (t-- > 0) {
            int n = reader.nextInt();
            String a = reader.next();
            String b = reader.next();
            ansC = new char[n];
            boolean flag = false;
            List<Integer> ans = new ArrayList<>();
            for(int i = a.length()-1, j =b.length()-1, k=0;j>=0; j--){

                if(a.charAt(i) == b.charAt(j) && !flag){
                    ansC[j] = b.charAt(j);
                    i--;
                    continue;
                }
                if(a.charAt(k) != b.charAt(j) && flag ){
                    ansC[j] =b.charAt(j);
                    k++;
                    continue;
                }

                if(flag){
                        char y = check(a,b, i,j,ans, flag);
                        ansC[j] = y;
                        i--;
                }else{
                        ansC[j]= check(a,b, k,j,ans, flag);
                        k++;
                }
                flag = !flag;
            }

            writer.print(ans.size()+" ");
            for(int a1 : ans)writer.print( (a1+1) +" ");
            writer.println();
          //  writer.println(ansC);
        }
    }

    private char check(String a, String b, int i, int j, List<Integer> ans, boolean flag) {
        char re = 'a';
        if(flag)
            if(b.charAt(j) == a.charAt(i) || j==0){
                ans.add(j);
                re = a.charAt(i)=='1'?'1':'0';
            }else{
                ans.add(0);
                ans.add(j);
                re = a.charAt(i)=='1'?'0':'1';
            }
        else
            if(b.charAt(j) != a.charAt(i) || j ==0){
                ans.add(j);
                re = a.charAt(i)=='1'?'0':'1';
            }else{
                ans.add(0);
                ans.add(j);
                re = a.charAt(i)=='1'?'1':'0';
            }
            return re;
    }

    public static void main(String[] args) throws IOException {
        try (Input reader = new StandardInput(); PrintWriter writer = new PrintWriter(System.out)) {
            new C1382().run();
        }
    }

    StandardInput reader;
    PrintWriter writer;

    @Override
    public void run() {
        try {
            reader = new StandardInput();
            writer = new PrintWriter(System.out);
            solve();
            reader.close();
            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    interface Input extends Closeable {
        String next() throws IOException;

        String nextLine() throws IOException;

        default int nextInt() throws IOException {
            return Integer.parseInt(next());
        }

        default long nextLong() throws IOException {
            return Long.parseLong(next());
        }

        default double nextDouble() throws IOException {
            return Double.parseDouble(next());
        }

        default int[] readIntArray() throws IOException {
            return readIntArray(nextInt());
        }

        default int[] readIntArray(int size) throws IOException {
            int[] array = new int[size];
            for (int i = 0; i < array.length; i++) {
                array[i] = nextInt();
            }
            return array;
        }

        default long[] readLongArray(int size) throws IOException {
            long[] array = new long[size];
            for (int i = 0; i < array.length; i++) {
                array[i] = nextLong();
            }
            return array;
        }
    }

    private static class StandardInput implements Input {
        private final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

        private StringTokenizer stringTokenizer;

        @Override
        public void close() throws IOException {
            reader.close();
        }

        @Override
        public String next() throws IOException {
            if (stringTokenizer == null || !stringTokenizer.hasMoreTokens()) {
                stringTokenizer = new StringTokenizer(reader.readLine());
            }
            return stringTokenizer.nextToken();
        }

        @Override
        public String nextLine() throws IOException {
            return reader.readLine();
        }
    }

}


import java.util.*;
import java.io.*;
import java.math.BigInteger;

public class codeforcesc {

//	static void methods() {
//		Integer arr[] = { 1, 2, 5, 1, 7, 8 };
//		Integer arr2[] = { 1, 1, 1, 1, 1, 1 };
//		List<Integer> l1 = new ArrayList<>();
//		l1 = Arrays.asList(arr);
//		 ll = new ArrayList<>(l1);
//		boolean bb = Arrays.equals(arr, arr2);
//		 Collections.reverse(ll);
//		ll.remove((Integer) 5);
//		System.out.println(ll);
//
//	}

//	static long gcd(long a, long b) {
//		if (b == 0)
//			return a;
//		return gcd(b, a % b);
//	}
      static void rev(int arr[],int st,int l) {
    	  for(int i=0;i<=l;i++) {
    		  arr[i]=arr[i]^1;
    	  }for(int i=0;i<=l/2;i++) {
    		  int t=arr[i];
    		  arr[i]=arr[l-i];
    		  arr[l-i]=t;
    	  }
      }

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int t=sc.nextInt();
		while(t-->0) {
			int n=sc.nextInt();
			sc.nextLine();
			char aa[]=sc.nextLine().toCharArray();
			char bb[]=sc.nextLine().toCharArray();
			int a[]=new int[n];
			int b[]=new int[n];
			for(int i=0;i<n;i++) {
				a[i]=aa[i]-'0';
				b[i]=bb[i]-'0';
			}
			int l=n-1;
			int st=0;
			ArrayList<Integer> ll=new ArrayList<>();
			while(l>0) {
				if(b[l]==a[l]) {
					l--;
					continue;
				}

				if(a[st]!=b[l]) {
					rev(a,st,l);
					ll.add(l+1);
					l--;
				}
				else {
					rev(a,0,0);
					ll.add(1);

					ll.add(l+1);
					rev(a,st,l);
					l--;
				}

			}
			if((a[l]^b[st])!=0) {
				ll.add(1);
			}

			System.out.print(ll.size()+" ");
			for(int i=0;i<ll.size();i++) {
				System.out.print(ll.get(i)+" ");
			}System.out.println();
		}
	}

}


import java.util.*;
import java.io.*;
import java.math.BigInteger;

public class codeforcesc {

//	static void methods() {
//		Integer arr[] = { 1, 2, 5, 1, 7, 8 };
//		Integer arr2[] = { 1, 1, 1, 1, 1, 1 };
//		List<Integer> l1 = new ArrayList<>();
//		l1 = Arrays.asList(arr);
//		 ll = new ArrayList<>(l1);
//		boolean bb = Arrays.equals(arr, arr2);
//		 Collections.reverse(ll);
//		ll.remove((Integer) 5);
//		System.out.println(ll);
//
//	}

//	static long gcd(long a, long b) {
//		if (b == 0)
//			return a;
//		return gcd(b, a % b);
//	}
      static void rev(int arr[],int st,int l) {
    	  for(int i=0;i<=l;i++) {
    		  arr[i]=arr[i]^1;
    	  }for(int i=0;i<l/2;i++) {
    		  int t=arr[i];
    		  arr[i]=arr[l-i];
    		  arr[l-i]=t;
    	  }
      }

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int t=sc.nextInt();
		while(t-->0) {
			int n=sc.nextInt();
			sc.nextLine();
			char aa[]=sc.nextLine().toCharArray();
			char bb[]=sc.nextLine().toCharArray();
			int a[]=new int[n];
			int b[]=new int[n];
			for(int i=0;i<n;i++) {
				a[i]=aa[i]-'0';
				b[i]=bb[i]-'0';
			}
			int l=n-1;
			int st=0;
			ArrayList<Integer> ll=new ArrayList<>();
			while(l>0) {
				if(b[l]==a[l]) {
					l--;
					continue;
				}

				if((a[st]^b[l])==1) {
					rev(a,st,l);
					ll.add(l+1);
					l--;
				}
				else {
					rev(a,st,st);
					ll.add(1);

					ll.add(l+1);
					rev(a,st,l);
					l--;
				}

			}
			if((a[l]^b[st])!=0) {
				ll.add(l+1);
			}

			System.out.print(ll.size()+" ");
			for(int i=0;i<ll.size();i++) {
				System.out.print(ll.get(i)+" ");
			}System.out.println();
		}
	}

}


import java.io.*;
import java.util.*;
import java.math.*;
import java.lang.*;
import static java.lang.Math.*;

public class Main implements Runnable
{
	static class InputReader
	{
		private InputStream stream;
		private byte[] buf = new byte[1024];
		private int curChar;
		private int numChars;
		private SpaceCharFilter filter;
		private BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

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

		public int read()
		{
			if (numChars==-1)
				throw new InputMismatchException();

			if (curChar >= numChars)
			{
				curChar = 0;
				try
				{
					numChars = stream.read(buf);
				}
				catch (IOException e)
				{
					throw new InputMismatchException();
				}

				if(numChars <= 0)
					return -1;
			}
			return buf[curChar++];
		}

		public String nextLine()
		{
			String str = "";
			try
			{
				str = br.readLine();
			}
			catch (IOException e)
			{
				e.printStackTrace();
			}
			return str;
		}
		public int nextInt()
		{
			int c = read();

			while(isSpaceChar(c))
				c = read();

			int sgn = 1;

			if (c == '-')
			{
				sgn = -1;
				c = read();
			}

			int res = 0;
			do
			{
				if(c<'0'||c>'9')
					throw new InputMismatchException();
				res *= 10;
				res += c - '0';
				c = read();
			}
			while (!isSpaceChar(c));

			return res * sgn;
		}

		public long nextLong()
		{
			int c = read();
			while (isSpaceChar(c))
				c = read();
			int sgn = 1;
			if (c == '-')
			{
				sgn = -1;
				c = read();
			}
			long res = 0;

			do
			{
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				res *= 10;
				res += c - '0';
				c = read();
			}
			while (!isSpaceChar(c));
				return res * sgn;
		}

		public double nextDouble()
		{
			int c = read();
			while (isSpaceChar(c))
				c = read();
			int sgn = 1;
			if (c == '-')
			{
				sgn = -1;
				c = read();
			}
			double res = 0;
			while (!isSpaceChar(c) && c != '.')
			{
				if (c == 'e' || c == 'E')
					return res * Math.pow(10, nextInt());
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				res *= 10;
				res += c - '0';
				c = read();
			}
			if (c == '.')
			{
				c = read();
				double m = 1;
				while (!isSpaceChar(c))
				{
					if (c == 'e' || c == 'E')
						return res * Math.pow(10, nextInt());
					if (c < '0' || c > '9')
						throw new InputMismatchException();
					m /= 10;
					res += (c - '0') * m;
					c = read();
				}
			}
			return res * sgn;
		}

		public String readString()
		{
			int c = read();
			while (isSpaceChar(c))
				c = read();
			StringBuilder res = new StringBuilder();
			do
			{
				res.appendCodePoint(c);
				c = read();
			}
			while (!isSpaceChar(c));

			return res.toString();
		}

		public boolean isSpaceChar(int c)
		{
			if (filter != null)
				return filter.isSpaceChar(c);
			return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
		}

		public String next()
		{
			return readString();
		}

		public interface SpaceCharFilter
		{
			public boolean isSpaceChar(int ch);
		}
	}
	public static void main(String args[]) throws Exception
	{
		new Thread(null, new Main(),"Main",1<<27).start();
	}

	public void run(){
        InputReader sc=new InputReader(System.in);
        PrintWriter out=new PrintWriter(System.out);
        int t=sc.nextInt();
        for(int h=0;h<t;h++)
        {
            int n=sc.nextInt();
            String s=sc.next();
            String w=sc.next();
            ArrayList<Integer>arr=new ArrayList<>();
            for(int i=0;i<n;i++)
            {
                if(s.charAt(i)!=w.charAt(i)){
                if(i==0)
                arr.add(1);
                else
                {
                    arr.add(i+1);
                    arr.add(1);
                    arr.add(i+1);
                }
                }
            }
            if(arr.size()==0)
            out.println("0");
            else
            {
                out.print(arr.size());
                for(int i=0;i<arr.size();i++)
                out.print(" "+arr.get(i));
                out.println();
            }

        }
        out.flush();
		out.close();
	}
}


import java.io.*;
import java.util.*;
import java.math.*;
import java.lang.*;
import static java.lang.Math.*;

public class Main implements Runnable
{
	static class InputReader
	{
		private InputStream stream;
		private byte[] buf = new byte[1024];
		private int curChar;
		private int numChars;
		private SpaceCharFilter filter;
		private BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

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

		public int read()
		{
			if (numChars==-1)
				throw new InputMismatchException();

			if (curChar >= numChars)
			{
				curChar = 0;
				try
				{
					numChars = stream.read(buf);
				}
				catch (IOException e)
				{
					throw new InputMismatchException();
				}

				if(numChars <= 0)
					return -1;
			}
			return buf[curChar++];
		}

		public String nextLine()
		{
			String str = "";
			try
			{
				str = br.readLine();
			}
			catch (IOException e)
			{
				e.printStackTrace();
			}
			return str;
		}
		public int nextInt()
		{
			int c = read();

			while(isSpaceChar(c))
				c = read();

			int sgn = 1;

			if (c == '-')
			{
				sgn = -1;
				c = read();
			}

			int res = 0;
			do
			{
				if(c<'0'||c>'9')
					throw new InputMismatchException();
				res *= 10;
				res += c - '0';
				c = read();
			}
			while (!isSpaceChar(c));

			return res * sgn;
		}

		public long nextLong()
		{
			int c = read();
			while (isSpaceChar(c))
				c = read();
			int sgn = 1;
			if (c == '-')
			{
				sgn = -1;
				c = read();
			}
			long res = 0;

			do
			{
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				res *= 10;
				res += c - '0';
				c = read();
			}
			while (!isSpaceChar(c));
				return res * sgn;
		}

		public double nextDouble()
		{
			int c = read();
			while (isSpaceChar(c))
				c = read();
			int sgn = 1;
			if (c == '-')
			{
				sgn = -1;
				c = read();
			}
			double res = 0;
			while (!isSpaceChar(c) && c != '.')
			{
				if (c == 'e' || c == 'E')
					return res * Math.pow(10, nextInt());
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				res *= 10;
				res += c - '0';
				c = read();
			}
			if (c == '.')
			{
				c = read();
				double m = 1;
				while (!isSpaceChar(c))
				{
					if (c == 'e' || c == 'E')
						return res * Math.pow(10, nextInt());
					if (c < '0' || c > '9')
						throw new InputMismatchException();
					m /= 10;
					res += (c - '0') * m;
					c = read();
				}
			}
			return res * sgn;
		}

		public String readString()
		{
			int c = read();
			while (isSpaceChar(c))
				c = read();
			StringBuilder res = new StringBuilder();
			do
			{
				res.appendCodePoint(c);
				c = read();
			}
			while (!isSpaceChar(c));

			return res.toString();
		}

		public boolean isSpaceChar(int c)
		{
			if (filter != null)
				return filter.isSpaceChar(c);
			return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
		}

		public String next()
		{
			return readString();
		}

		public interface SpaceCharFilter
		{
			public boolean isSpaceChar(int ch);
		}
	}
	public static void main(String args[]) throws Exception
	{
		new Thread(null, new Main(),"Main",1<<27).start();
	}

	public void run(){
        InputReader sc=new InputReader(System.in);
        PrintWriter out=new PrintWriter(System.out);
        int t=sc.nextInt();
        for(int h=0;h<t;h++)
        {
            int n=sc.nextInt();
            String s=sc.next();
            String w=sc.next();
            ArrayList<Integer>arr=new ArrayList<>();
            for(int i=0;i<n;i++)
            {
                if(s.charAt(i)!=w.charAt(i)){
                if(i==0)
                arr.add(1);
                else
                {
                    arr.add(i+1);
                    arr.add(1);
                }
                }
            }
            if(arr.size()==0)
            out.println("0");
            else
            {
                out.print(arr.size());
                for(int i=0;i<arr.size();i++)
                out.print(" "+arr.get(i));
                out.println();
            }

        }
        out.flush();
		out.close();
	}
}


//package CodeForces;

import java.util.ArrayList;
import java.util.Scanner;

public class PrefixFlip {

	static void reverse(char ch[],int k)
	{
		int i=0;int j=k;
		if(i==j)
		{
			if(ch[j]=='1')
			{
				ch[i]='0';
			}
			else
			{
				ch[i]='1';
			}
			return;
		}
		while(i<j)
		{
			char temp=ch[i];
			if(ch[j]=='1')
			{
				ch[i]='0';
			}
			else
			{
				ch[i]='1';
			}
			if(temp=='0')
			{
				ch[j]='1';
			}
			else
			{
				ch[j]='0';
			}
			i++;j--;
		}
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner s=new Scanner(System.in);
		int t=s.nextInt();
		while(t>0)
		{
			int n=s.nextInt();
			String str1=s.next();
			String str2=s.next();
			char ch1[]=str1.toCharArray();
			char ch2[]=str2.toCharArray();
			ArrayList<Integer> ans=new ArrayList<Integer>();
			int j=n-1;
			while(j>=0)
			{
				while(j>=0&&ch1[j]==ch2[j])
				{
					j--;
				}
				if(j<0)
				{
					break;
				}
				if(ch1[0]!=ch2[j])
				{
					ans.add(j+1);
					reverse(ch1,j);
					j--;
				}
				else
				{
					ans.add(1);
					if(ch1[0]=='0')
					{
						ch1[0]='1';
					}
					else
					{
						ch1[0]='0';
					}
				}
			}

			if(ans.size()==0)
			{
				System.out.println(0);
			}
			else
			{
				StringBuilder small=new StringBuilder();
				System.out.print(ans.size()+" ");
				for(int k=0;k<ans.size();k++)
				{
					System.out.print(ans.get(k)+" ");
				}
				System.out.println();
			}

//	       for(int k=0;k<ch1.length;k++)
//	       {
//	    	   System.out.print(ch1[k]);
//	       }
//	       System.out.println();
			t--;
		}

	}

}


//package CodeForces;

import java.util.ArrayList;
import java.util.Scanner;

public class PrefixFlip {

	static void reverse(char ch[],int k)
	{
		int i=0;int j=k;
		if(i==j)
		{
			if(ch[j]=='1')
			{
				ch[i]='0';
			}
			else
			{
				ch[i]='1';
			}
			return;
		}
		while(i<j)
		{
			char temp=ch[i];
			if(ch[j]=='1')
			{
				ch[i]='0';
			}
			else
			{
				ch[i]='1';
			}
			if(temp=='0')
			{
				ch[j]='1';
			}
			else
			{
				ch[j]='0';
			}
			i++;j--;
		}
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner s=new Scanner(System.in);
		int t=s.nextInt();
		while(t>0)
		{
			int n=s.nextInt();
			String str1=s.next();
			String str2=s.next();
			char ch1[]=str1.toCharArray();
			char ch2[]=str2.toCharArray();
			ArrayList<Integer> ans=new ArrayList<Integer>();
			int j=n-1;
			while(j>=0)
			{
				while(j>=0&&ch1[j]==ch2[j])
				{
					j--;
				}
				if(j<0)
				{
					break;
				}
				if(ch1[0]!=ch2[j])
				{
					ans.add(j+1);
					reverse(ch1,j-1);
					j--;
				}
				else
				{
					ans.add(1);
					if(ch1[0]=='0')
					{
						ch1[0]='1';
					}
					else
					{
						ch1[0]='0';
					}
				}
			}

			if(ans.size()==0)
			{
				System.out.println(0);
			}
			else
			{
				StringBuilder small=new StringBuilder();
				for(int k=0;k<ans.size();k++)
				{
					small.append(ans.get(k)+" ");
				}
				System.out.println(ans.size()+" "+small);
			}
			t--;
		}

	}

}


//package CodeForces;

import java.util.ArrayList;
import java.util.Scanner;

public class PrefixFlip {

	static void reverse(char ch[],int k)
	{
		int i=0;int j=k;
		if(i==j)
		{
			if(ch[j]=='1')
			{
				ch[i]='0';
			}
			else
			{
				ch[i]='1';
			}
			return;
		}
		while(i<j)
		{
			char temp=ch[i];
			if(ch[j]=='1')
			{
				ch[i]='0';
			}
			else
			{
				ch[i]='1';
			}
			if(temp=='0')
			{
				ch[j]='1';
			}
			else
			{
				ch[j]='0';
			}
			i++;j--;
		}
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner s=new Scanner(System.in);
		int t=s.nextInt();
		while(t>0)
		{
			int n=s.nextInt();
			String str1=s.next();
			String str2=s.next();
			char ch1[]=str1.toCharArray();
			char ch2[]=str2.toCharArray();
			ArrayList<Integer> ans=new ArrayList<Integer>();
			int j=n-1;
			while(j>=0)
			{
				while(j>=0&&ch1[j]==ch2[j])
				{
					j--;
				}
				if(j<0)
				{
					break;
				}
				if(ch1[0]!=ch2[j])
				{
					ans.add(j+1);
					reverse(ch1,j);
					j--;
				}
				else
				{
					ans.add(1);
					if(ch1[0]=='0')
					{
						ch1[0]='1';
					}
					else
					{
						ch1[0]='0';
					}
				}
			}

			if(ans.size()==0)
			{
				System.out.println(0);
			}
			else
			{
				StringBuilder small=new StringBuilder();
				System.out.print(ans.size()+" ");
				for(int k=0;k<ans.size();k++)
				{
					if(k==ans.size()-1)
					{
						System.out.print(ans.get(k));
					}
					else
					{
						System.out.print(ans.get(k)+" ");
					}
				}
				System.out.println();
			}

//	       for(int k=0;k<ch1.length;k++)
//	       {
//	    	   System.out.print(ch1[k]);
//	       }
//	       System.out.println();
			t--;
		}

	}

}


//package CodeForces;

import java.util.ArrayList;
import java.util.Scanner;

public class PrefixFlip {

	static void reverse(char ch[],int k)
	{
		int i=0;int j=k;
		if(i==j)
		{
			if(ch[j]=='1')
			{
				ch[i]='0';
			}
			else
			{
				ch[i]='1';
			}
			return;
		}
		while(i<j)
		{
			char temp=ch[i];
			if(ch[j]=='1')
			{
				ch[i]='0';
			}
			else
			{
				ch[i]='1';
			}
			if(temp=='0')
			{
				ch[j]='1';
			}
			else
			{
				ch[j]='0';
			}
			i++;j--;
		}
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner s=new Scanner(System.in);
		int t=s.nextInt();
		while(t>0)
		{
			int n=s.nextInt();
			String str1=s.next();
			String str2=s.next();
			char ch1[]=str1.toCharArray();
			char ch2[]=str2.toCharArray();
			ArrayList<Integer> ans=new ArrayList<Integer>();
			int j=n-1;
			while(j>=0)
			{
				while(j>=0&&ch1[j]==ch2[j])
				{
					j--;
				}
				if(j<0)
				{
					break;
				}
				if(ch1[0]!=ch2[j])
				{
					ans.add(j+1);
					reverse(ch1,j);
					j--;
				}
				else
				{
					ans.add(1);
					if(ch1[0]=='0')
					{
						ch1[0]='1';
					}
					else
					{
						ch1[0]='0';
					}
				}
			}

			if(ans.size()==0)
			{
				System.out.println(0);
			}
			else
			{
				StringBuilder small=new StringBuilder();
				for(int k=0;k<ans.size();k++)
				{
					small.append(ans.get(k)+" ");
				}
				System.out.println(ans.size()+" "+small);
			}

			t--;
		}

	}

}


//package CodeForces;

import java.util.ArrayList;
import java.util.Scanner;

public class PrefixFlip {

	static void reverse(char ch[],int k)
	{
		int i=0;int j=k;
		if(i==j)
		{
			if(ch[j]=='1')
			{
				ch[i]='0';
			}
			else
			{
				ch[i]='1';
			}
			return;
		}
		while(i<j)
		{
			char temp=ch[j];
			if(ch[i]=='1')
			{
				ch[j]='0';
			}
			else
			{
				ch[j]='1';
			}
			if(temp=='0')
			{
				ch[i]='1';
			}
			else
			{
				ch[i]='0';
			}
			i++;j--;
		}
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner s=new Scanner(System.in);
		int t=s.nextInt();
		while(t>0)
		{
			int n=s.nextInt();
			String str1=s.next();
			String str2=s.next();
			char ch1[]=str1.toCharArray();
			char ch2[]=str2.toCharArray();
			ArrayList<Integer> ans=new ArrayList<Integer>();
			int j=n-1;
			while(j>=0)
			{
				while(j>=0&&ch1[j]==ch2[j])
				{
					j--;
				}
				if(j<0)
				{
					break;
				}
				if(ch1[0]!=ch2[j])
				{
					ans.add(j+1);
					reverse(ch1,j);
					j--;
				}
				else
				{
					ans.add(1);
					if(ch1[0]=='0')
					{
						ch1[0]='1';
					}
					else
					{
						ch1[0]='0';
					}
				}
			}

			if(ans.size()==0)
			{
				System.out.println(0);
			}
			else
			{
				StringBuilder small=new StringBuilder();
				System.out.print(ans.size()+" ");
				for(int k=0;k<ans.size();k++)
				{
					if(k==ans.size()-1)
					{
						System.out.print(ans.get(k));
					}
					else
					{
						System.out.print(ans.get(k)+" ");
					}
				}
				System.out.println();
			}

//	       for(int k=0;k<ch1.length;k++)
//	       {
//	    	   System.out.print(ch1[k]);
//	       }
//	       System.out.println();
			t--;
		}

	}

}


//package CodeForces;

import java.util.ArrayList;
import java.util.Scanner;

public class PrefixFlip {

	static void reverse(char ch[],int k)
	{
		int i=0;int j=k;

		while(i<=j)
		{
			if(i==j)
			{
				if(ch[j]=='1')
				{
					ch[i]='0';
				}
				else
				{
					ch[i]='1';
				}
				return;
			}
			char temp=ch[j];
			if(ch[i]=='1')
			{
				ch[j]='0';
			}
			else
			{
				ch[j]='1';
			}
			if(temp=='0')
			{
				ch[i]='1';
			}
			else
			{
				ch[i]='0';
			}
			i++;j--;
		}
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner s=new Scanner(System.in);
		int t=s.nextInt();
		while(t>0)
		{
			int n=s.nextInt();
			String str1=s.next();
			String str2=s.next();
			char ch1[]=str1.toCharArray();
			char ch2[]=str2.toCharArray();
			ArrayList<Integer> ans=new ArrayList<Integer>();
			int j=n-1;
			while(j>=0)
			{
				while(j>=0&&ch1[j]==ch2[j])
				{
					j--;
				}
				if(j<0)
				{
					break;
				}
				if(ch1[0]!=ch2[j])
				{
					ans.add(j+1);
					reverse(ch1,j);
					j--;
				}
				else
				{
					ans.add(1);
					if(ch1[0]=='0')
					{
						ch1[0]='1';
					}
					else
					{
						ch1[0]='0';
					}
				}
			}

			if(ans.size()==0)
			{
				System.out.println(2+" "+n+" "+n);
			}
			else
			{
				StringBuilder small=new StringBuilder();
				System.out.print(ans.size()+" ");
				for(int k=0;k<ans.size();k++)
				{
					if(k==ans.size()-1)
					{
						System.out.print(ans.get(k));
					}
					else
					{
						System.out.print(ans.get(k)+" ");
					}
				}
				System.out.println();
			}

//	       for(int k=0;k<ch1.length;k++)
//	       {
//	    	   System.out.print(ch1[k]);
//	       }
//	       System.out.println();
			t--;
		}

	}

}


//package CodeForces;

import java.util.ArrayList;
import java.util.Scanner;

public class PrefixFlip {

	static void reverse(char ch[],int k)
	{
		int i=0;int j=k;

		while(i<=j)
		{
			if(i==j)
			{
				if(ch[j]=='1')
				{
					ch[i]='0';
				}
				else
				{
					ch[i]='1';
				}
				return;
			}
			char temp=ch[j];
			if(ch[i]=='1')
			{
				ch[j]='0';
			}
			else
			{
				ch[j]='1';
			}
			if(temp=='0')
			{
				ch[i]='1';
			}
			else
			{
				ch[i]='0';
			}
			i++;j--;
		}
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner s=new Scanner(System.in);
		int t=s.nextInt();
		while(t>0)
		{
			int n=s.nextInt();
			String str1=s.next();
			String str2=s.next();
			char ch1[]=str1.toCharArray();
			char ch2[]=str2.toCharArray();
			ArrayList<Integer> ans=new ArrayList<Integer>();
			int j=n-1;
			while(j>=0)
			{
				while(j>=0&&ch1[j]==ch2[j])
				{
					j--;
				}
				if(j<0)
				{
					break;
				}
				if(ch1[0]!=ch2[j])
				{
					ans.add(j+1);
					reverse(ch1,j);
					j--;
				}
				else
				{
					ans.add(1);
					if(ch1[0]=='0')
					{
						ch1[0]='1';
					}
					else
					{
						ch1[0]='0';
					}
				}
			}

			if(ans.size()==0)
			{
				System.out.println(2+" "+n+" "+n);
			}
			else
			{
				StringBuilder small=new StringBuilder();
				System.out.print(ans.size()+" ");
				for(int k=0;k<ans.size();k++)
				{
					if(k==ans.size()-1)
					{
						System.out.print(ans.get(k));
					}
					else
					{
						System.out.print(ans.get(k)+" ");
					}
				}
				System.out.println();
			}

	       for(int k=0;k<ch1.length;k++)
	       {
	    	   System.out.print(ch1[k]);
	       }
	       System.out.println();
			t--;
		}

	}

}


//package CodeForces;

import java.util.ArrayList;
import java.util.Scanner;

public class PrefixFlip {

	static void reverse(char ch[],int k)
	{
		int i=0;int j=k;
		if(i==j)
		{
			if(ch[j]=='1')
			{
				ch[i]='0';
			}
			else
			{
				ch[i]='1';
			}
			return;
		}
		while(i<j)
		{
			char temp=ch[j];
			if(ch[i]=='1')
			{
				ch[j]='0';
			}
			else
			{
				ch[j]='1';
			}
			if(temp=='0')
			{
				ch[i]='1';
			}
			else
			{
				ch[i]='0';
			}
			i++;j--;
		}
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner s=new Scanner(System.in);
		int t=s.nextInt();
		while(t>0)
		{
			int n=s.nextInt();
			String str1=s.next();
			String str2=s.next();
			char ch1[]=str1.toCharArray();
			char ch2[]=str2.toCharArray();
			ArrayList<Integer> ans=new ArrayList<Integer>();
			int j=n-1;
			while(j>=0)
			{
				while(j>=0&&ch1[j]==ch2[j])
				{
					j--;
				}
				if(j<0)
				{
					break;
				}
				if(ch1[0]!=ch2[j])
				{
					ans.add(j+1);
					reverse(ch1,j);
					j--;
				}
				else
				{
					ans.add(1);
					if(ch1[0]=='0')
					{
						ch1[0]='1';
					}
					else
					{
						ch1[0]='0';
					}
				}
			}

			if(ans.size()==0)
			{
				System.out.println(2+" "+n+" "+n);
			}
			else
			{
				StringBuilder small=new StringBuilder();
				System.out.print(ans.size()+" ");
				for(int k=0;k<ans.size();k++)
				{
					if(k==ans.size()-1)
					{
						System.out.print(ans.get(k));
					}
					else
					{
						System.out.print(ans.get(k)+" ");
					}
				}
				System.out.println();
			}

	       for(int k=0;k<ch1.length;k++)
	       {
	    	   System.out.print(ch1[k]);
	       }
	       System.out.println();
			t--;
		}

	}

}


#include <bits/stdc++.h>
using namespace std;
using pil = pair<int, long long>;
const int N = 2e5 + 5;
int n, m, k, q;
vector<int> a[N];
set<int> b;

int dist(int s, int m, int l) {
	return abs(m - s) + abs(l - m);
}

int dist(int s, int l) {
	return abs(l - s);
}

int pred(int x) {
	auto it = b.lower_bound(x);
	if (*it == x) {
		return x;
	}
	if (it == b.begin()) {
		return 0;
	}
	it--;
	return *it;
}

int succ(int x) {
	auto it = b.upper_bound(x);
	if (it == b.end()) {
		return 0;
	}
	return *it;
}

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

	cin >> n >> m >> k >> q;

	int maxn = 0;
	int minn = n + 1;
	for (int i = 0; i < k; i++) {
		int x, y;
		cin >> x >> y;
		a[x].emplace_back(y);
		maxn = max(x, maxn);
		minn = min(x, minn);
	}
	for (int i = 0; i < q; i++) {
		int x;
		cin >> x;
		b.insert(x);
	}

	long long dr;
	long long dl;
	long long skip = 0;

	if (minn > 1) {
		skip += *b.begin() - 1;
		skip += minn - 1;
	}

	vector<pil> sps;
	sps.push_back({1, 0});
	for (int i = minn; i <= maxn; i++) {
		if (a[i].empty()) {
			skip++;
			continue;
		}

		int lm = *min_element(a[i].begin(), a[i].end());
		int rm = *max_element(a[i].begin(), a[i].end());

		dr = LLONG_MAX;
		dl = LLONG_MAX;
		for (pil p : sps) {
			int sp = p.first;
			long long od = p.second;

			if (sp) {
				dl = min(dl, od + (long long) dist(sp, rm, lm));
				dr = min(dr, od + (long long) dist(sp, lm, rm));
			}
		}

		dl += skip, dr += skip;
		skip = 0;

		if (i == maxn) {
			break;
		}

		skip++;
		sps.clear();

		int ll = pred(lm);
		sps.push_back({ll, dl + (long long) dist(lm, ll)});
		int lr = succ(lm);
		sps.push_back({lr, dl + (long long) dist(lm, lr)});
		int rl = pred(rm);
		sps.push_back({rl, dr + (long long) dist(rm, ll)});
		int rr = succ(rm);
		sps.push_back({rr, dr + (long long) dist(rm, lr)});
	}

	cout << min(dl, dr) << '\n';

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
using pil = pair<int, long long>;
const int N = 2e5 + 5;
int n, m, k, q;
vector<int> a[N];
set<int> b;

int dist(int s, int m, int l) {
	return abs(m - s) + abs(l - m);
}

int dist(int s, int l) {
	return abs(l - s);
}

int pred(int x) {
	auto it = b.lower_bound(x);
	if (*it == x) {
		return x;
	}
	if (it == b.begin()) {
		return 0;
	}
	it--;
	return *it;
}

int succ(int x) {
	auto it = b.upper_bound(x);
	if (it == b.end()) {
		return 0;
	}
	return *it;
}

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

	cin >> n >> m >> k >> q;

	int maxn = 0;
	int minn = n + 1;
	for (int i = 0; i < k; i++) {
		int x, y;
		cin >> x >> y;
		a[x].emplace_back(y);
		maxn = max(x, maxn);
		minn = min(x, minn);
	}
	for (int i = 0; i < q; i++) {
		int x;
		cin >> x;
		b.insert(x);
	}

	long long dr;
	long long dl;
	long long skip = 0;

	int first = 1;
	if (minn > 1) {
		first = *b.begin();
		skip += first - 1;
		skip += minn - 1;
	}

	vector<pil> sps;
	sps.push_back({first, 0});
	for (int i = minn; i <= maxn; i++) {
		if (a[i].empty()) {
			skip++;
			continue;
		}

		for (pil &p : sps) {
			p.second += (long long) skip;
		}
		skip = 0;

		int lm = *min_element(a[i].begin(), a[i].end());
		int rm = *max_element(a[i].begin(), a[i].end());

		dr = LLONG_MAX;
		dl = LLONG_MAX;
		for (pil p : sps) {
			int sp = p.first;
			long long od = p.second;

			if (sp) {
				dl = min(dl, od + (long long) dist(sp, rm, lm));
				dr = min(dr, od + (long long) dist(sp, lm, rm));
			}
		}

		if (i == maxn) {
			break;
		}

		skip++;
		sps.clear();

		int ll = pred(lm);
		sps.push_back({ll, dl + (long long) dist(lm, ll)});
		int lr = succ(lm);
		sps.push_back({lr, dl + (long long) dist(lm, lr)});
		int rl = pred(rm);
		sps.push_back({rl, dr + (long long) dist(rm, rl)});
		int rr = succ(rm);
		sps.push_back({rr, dr + (long long) dist(rm, rr)});
	}

	cout << min(dl, dr) << '\n';

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
using pil = pair<int, long long>;
const int N = 2e5 + 5;
int n, m, k, q;
vector<int> a[N];
set<int> b;

int dist(int s, int m, int l) {
	return abs(m - s) + abs(l - m);
}

int dist(int s, int l) {
	return abs(l - s);
}

int pred(int x) {
	auto it = b.lower_bound(x);
	if (*it == x) {
		return x;
	}
	if (it == b.begin()) {
		return 0;
	}
	it--;
	return *it;
}

int succ(int x) {
	auto it = b.upper_bound(x);
	if (it == b.end()) {
		return 0;
	}
	return *it;
}

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

	cin >> n >> m >> k >> q;

	int maxn = 0;
	int minn = n + 1;
	for (int i = 0; i < k; i++) {
		int x, y;
		cin >> x >> y;
		a[x].emplace_back(y);
		maxn = max(x, maxn);
		minn = min(x, minn);
	}
	for (int i = 0; i < q; i++) {
		int x;
		cin >> x;
		b.insert(x);
	}

	long long dr;
	long long dl;
	long long skip = 0;

	if (minn > 1) {
		skip += *b.begin() - 1;
		skip += minn - 1;
	}

	vector<pil> sps;
	sps.push_back({1, 0});
	for (int i = minn; i <= maxn; i++) {
		if (a[i].empty()) {
			skip++;
			continue;
		}

		for (pil &p : sps) {
			p.second += skip;
		}
		skip = 0;

		int lm = *min_element(a[i].begin(), a[i].end());
		int rm = *max_element(a[i].begin(), a[i].end());

		dr = LLONG_MAX;
		dl = LLONG_MAX;
		for (pil p : sps) {
			int sp = p.first;
			long long od = p.second;

			if (sp) {
				dl = min(dl, od + (long long) dist(sp, rm, lm));
				dr = min(dr, od + (long long) dist(sp, lm, rm));
			}
		}

		if (i == maxn) {
			break;
		}

		skip++;
		sps.clear();

		int ll = pred(lm);
		sps.push_back({ll, dl + (long long) dist(lm, ll)});
		int lr = succ(lm);
		sps.push_back({lr, dl + (long long) dist(lm, lr)});
		int rl = pred(rm);
		sps.push_back({rl, dr + (long long) dist(rm, rl)});
		int rr = succ(rm);
		sps.push_back({rr, dr + (long long) dist(rm, rr)});
	}

	cout << min(dl, dr) << '\n';

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using piii = pair<int, pii>;
using plll = pair<ll, pll>;
using pib = pair<int, bool>;
using pdi = pair<double, int>;
using pid = pair<int, double>;
using ld = long double;
using piiii = pair<pii, pii>;

#define endl "\n"
#define inf 0x3f3f3f3f3f
#define f first
#define s second

int n, m, k, q, x, y;
ll dp[200001][2], c[200001][2];
vector<int>  val, col, arr[200001];

ll mindist(int r1, int s, int r2, int d){
    if (r1==r2) return abs(s-d);
    if (s<=d){
        int idx = lower_bound(col.begin(), col.end(), d)-col.begin();
        if (col[idx]>d&&idx>0) idx--;
        return abs(col[idx]-s)+abs(col[idx]-d);
    }else{
        int idx = lower_bound(col.begin(), col.end(), d)-col.begin();
        return abs(col[idx]-s)+abs(col[idx]-d);
    };
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> m >> k >> q;
    for (int i=0; i<k; i++){
        cin >> x >> y;
        val.push_back(x);
        arr[x].push_back(y);
    }
    sort(val.begin(), val.end());
    val.erase(unique(val.begin(), val.end()), val.end());
    for (int i=0; i<q; i++){
        cin >> x;
        col.push_back(x);
    }
    for (int i=1; i<=n; i++){
        sort(arr[i].begin(), arr[i].end());
    }
    sort(col.begin(), col.end());
    memset(dp, inf, sizeof dp);
    dp[0][0] = dp[0][1] = 0;
    c[0][0] = c[0][1] = 1;
    ll prev = 1;
    for (int i=1; i<=val.size(); i++){
        ll cur = val[i-1];
        ll l = arr[i][0];
        ll r = arr[i][arr[i].size()-1];
        dp[i][0] = min(dp[i-1][0]+mindist(prev, c[i-1][0], cur, r), dp[i-1][1]+mindist(prev, c[i-1][1], cur, r))+r-l+cur-prev;
        c[i][0] = l;
        dp[i][1] = min(dp[i-1][0]+mindist(prev, c[i-1][0], cur, l), dp[i-1][1]+mindist(prev, c[i-1][1], cur,  l))+r-l+cur-prev;
        c[i][1] = r;
        prev = cur;
    }
    cout << min(dp[val.size()][0], dp[val.size()][1]) << endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using piii = pair<int, pii>;
using plll = pair<ll, pll>;
using pib = pair<int, bool>;
using pdi = pair<double, int>;
using pid = pair<int, double>;
using ld = long double;
using piiii = pair<pii, pii>;

#define endl "\n"
#define inf 0x3f3f3f3f3f
#define f first
#define s second

ll n, m, k, q, x, y;
ll dp[200001][2], c[200001][2];
vector<ll>  val, col, arr[200001];

ll mindist(ll r1, ll s, ll r2, ll d){
    if (r1==r2) return abs(s-d);
    int lo = 0;
    int hi = col.size()-1;
    while (lo<=hi){
        int mid = (lo+hi)/2;
        if (col[mid]<=d) lo = mid+1;
        else hi = mid-1;
    }
    int low = lo-1;
    int high = lo;
    if (lo<0){
        return abs(s-col[high])+abs(d-col[high]);
    }
    else if (high>=col.size()){
        return abs(s-col[low])+abs(d-col[low]);
    }else{
        return min(abs(s-col[low])+abs(d-col[low]), abs(s-col[high])+abs(d-col[high]));
    }
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> m >> k >> q;
    for (int i=0; i<k; i++){
        cin >> x >> y;
        val.push_back(x);
        arr[x].push_back(y);
    }
    sort(val.begin(), val.end());
    val.erase(unique(val.begin(), val.end()), val.end());
    for (int i=0; i<q; i++){
        cin >> x;
        col.push_back(x);
    }
    for (int i=1; i<=n; i++){
        sort(arr[i].begin(), arr[i].end());
    }
    sort(col.begin(), col.end());
    dp[0][0] = dp[0][1] = 0;
    c[0][0] = c[0][1] = 1;
    ll prev = 1;
    for (int i=1; i<=val.size(); i++){
        ll cur = val[i-1];
        ll l = arr[cur][0];
        ll r = arr[cur][arr[cur].size()-1];
        dp[i][0] = min(dp[i-1][0]+mindist(prev, c[i-1][0], cur, r), dp[i-1][1]+mindist(prev, c[i-1][1], cur, r))+r-l+cur-prev;
        c[i][0] = l;
        dp[i][1] = min(dp[i-1][0]+mindist(prev, c[i-1][0], cur, l), dp[i-1][1]+mindist(prev, c[i-1][1], cur, l))+r-l+cur-prev;
        c[i][1] = r;
        prev = cur;
    }
    cout << min(dp[val.size()][0], dp[val.size()][1]) << endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using piii = pair<int, pii>;
using plll = pair<ll, pll>;
using pib = pair<int, bool>;
using pdi = pair<double, int>;
using pid = pair<int, double>;
using ld = long double;
using piiii = pair<pii, pii>;

#define endl "\n"
#define inf 0x3f3f3f3f3f
#define f first
#define s second

int n, m, k, q, x, y;
ll dp[200001][2], c[200001][2];
vector<int>  val, col, arr[200001];

ll mindist(int r1, int s, int r2, int d){
    if (r1==r2) return abs(s-d);
    int lo = 0;
    int hi = col.size()-1;
    while (lo<=hi){
        int mid = (lo+hi)/2;
        if (col[mid]<=d) lo = mid+1;
        else hi = mid-1;
    }
    int low = lo-1;
    int high = lo;
    if (high>=col.size()){
        return abs(s-col[low])+abs(d-col[low]);
    }else{
        return min(abs(s-col[low])+abs(d-col[low]), abs(s-col[high])+abs(d-col[high]));
    }
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> m >> k >> q;
    for (int i=0; i<k; i++){
        cin >> x >> y;
        val.push_back(x);
        arr[x].push_back(y);
    }
    sort(val.begin(), val.end());
    val.erase(unique(val.begin(), val.end()), val.end());
    for (int i=0; i<q; i++){
        cin >> x;
        col.push_back(x);
    }
    for (int i=1; i<=n; i++){
        sort(arr[i].begin(), arr[i].end());
    }
    sort(col.begin(), col.end());
    memset(dp, inf, sizeof dp);
    dp[0][0] = dp[0][1] = 0;
    c[0][0] = c[0][1] = 1;
    ll prev = 1;
    for (int i=1; i<=val.size(); i++){
        ll cur = val[i-1];
        ll l = arr[i][0];
        ll r = arr[i][arr[i].size()-1];
        dp[i][0] = min(dp[i-1][0]+mindist(prev, c[i-1][0], cur, r), dp[i-1][1]+mindist(prev, c[i-1][1], cur, r))+r-l+cur-prev;
        c[i][0] = l;
        dp[i][1] = min(dp[i-1][0]+mindist(prev, c[i-1][0], cur, l), dp[i-1][1]+mindist(prev, c[i-1][1], cur,  l))+r-l+cur-prev;
        c[i][1] = r;
        prev = cur;
    }
    cout << min(dp[val.size()][0], dp[val.size()][1]) << endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using piii = pair<int, pii>;
using plll = pair<ll, pll>;
using pib = pair<int, bool>;
using pdi = pair<double, int>;
using pid = pair<int, double>;
using ld = long double;
using piiii = pair<pii, pii>;

#define endl "\n"
#define inf 0x3f3f3f3f3f
#define f first
#define s second

int n, m, k, q, x, y;
ll dp[200001][2], c[200001][2];
vector<int>  val, col, arr[200001];

ll mindist(int r1, int s, int r2, int d){
    if (r1==r2) return abs(s-d);
    int lo = 0;
    int hi = col.size()-1;
    while (lo<=hi){
        int mid = (lo+hi)/2;
        if (col[mid]<=d) lo = mid+1;
        else hi = mid-1;
    }
    int low = lo-1;
    int high = low;
    if (high>=col.size()){
        return abs(s-col[low])+abs(d-col[low]);
    }else{
        return min(abs(s-col[low])+abs(d-col[low]), abs(s-col[high])+abs(d-col[high]));
    }
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> m >> k >> q;
    for (int i=0; i<k; i++){
        cin >> x >> y;
        val.push_back(x);
        arr[x].push_back(y);
    }
    sort(val.begin(), val.end());
    val.erase(unique(val.begin(), val.end()), val.end());
    for (int i=0; i<q; i++){
        cin >> x;
        col.push_back(x);
    }
    for (int i=1; i<=n; i++){
        sort(arr[i].begin(), arr[i].end());
    }
    sort(col.begin(), col.end());
    memset(dp, inf, sizeof dp);
    dp[0][0] = dp[0][1] = 0;
    c[0][0] = c[0][1] = 1;
    ll prev = 1;
    for (int i=1; i<=val.size(); i++){
        ll cur = val[i-1];
        ll l = arr[i][0];
        ll r = arr[i][arr[i].size()-1];
        dp[i][0] = min(dp[i-1][0]+mindist(prev, c[i-1][0], cur, r), dp[i-1][1]+mindist(prev, c[i-1][1], cur, r))+r-l+cur-prev;
        c[i][0] = l;
        dp[i][1] = min(dp[i-1][0]+mindist(prev, c[i-1][0], cur, l), dp[i-1][1]+mindist(prev, c[i-1][1], cur,  l))+r-l+cur-prev;
        c[i][1] = r;
        prev = cur;
    }
    cout << min(dp[val.size()][0], dp[val.size()][1]) << endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using piii = pair<int, pii>;
using plll = pair<ll, pll>;
using pib = pair<int, bool>;
using pdi = pair<double, int>;
using pid = pair<int, double>;
using ld = long double;
using piiii = pair<pii, pii>;

#define endl "\n"
#define inf 0x3f3f3f3f3f
#define f first
#define s second

ll n, m, k, q, x, y, mn[200001], mx[200001];
ll dp[200001][2], c[200001][2];
vector<ll>  val, col;

ll mindist(ll r1, ll s, ll r2, ll d){
    if (r1==r2) return abs(s-d);
    int lo = 0;
    int hi = col.size()-1;
    while (lo<=hi){
        int mid = (lo+hi)/2;
        if (col[mid]<=d) lo = mid+1;
        else hi = mid-1;
    }
    ll ret;
    int low = lo-1;
    int high = lo;
    if (low<0){
        ret =  abs(s-col[high])+abs(d-col[high]);
    }else if (high>=col.size()){
        ret = abs(s-col[low])+abs(d-col[low]);
    }else{
        ret = min(abs(s-col[low])+abs(d-col[low]), abs(s-col[high])+abs(d-col[high]));
    }
    return ret;
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> m >> k >> q;
    memset(mn, inf, sizeof mn);
    for (int i=0; i<k; i++){
        cin >> x >> y;
        val.push_back(x);
        mn[x] = min(mn[x], y);
        mx[x] = max(mx[x], y);
    }
    sort(val.begin(), val.end());
    val.erase(unique(val.begin(), val.end()), val.end());
    for (int i=0; i<q; i++){
        cin >> x;
        col.push_back(x);
    }
    sort(col.begin(), col.end());
    dp[0][0] = dp[0][1] = 0;
    c[0][0] = c[0][1] = 1;
    ll prev = 1;
    for (int i=1; i<=val.size(); i++){
        ll cur = val[i-1];
        ll l = mn[cur];
        ll r = mx[cur];
        dp[i][0] = min(dp[i-1][0]+mindist(prev, c[i-1][0], cur, r), dp[i-1][1]+mindist(prev, c[i-1][1], cur, r))+r-l+cur-prev;
        c[i][0] = l;
        dp[i][1] = min(dp[i-1][0]+mindist(prev, c[i-1][0], cur, l), dp[i-1][1]+mindist(prev, c[i-1][1], cur, l))+r-l+cur-prev;
        c[i][1] = r;
        prev = cur;
    }
    cout << min(dp[val.size()][0], dp[val.size()][1]) << endl;
    return 0;
}