This is the easy version of the problem. The difference between the versions is the constraint on $n$ and the required number of operations. You can make hacks only if all versions of the problem are solved.

There are two binary strings $a$ and $b$ of length $n$ (a binary string is a string consisting of symbols $0$ and $1$). In an operation, you select a prefix of $a$, and simultaneously invert the bits in the prefix ($0$ changes to $1$ and $1$ changes to $0$) and reverse the order of the bits in the prefix.

For example, if $a=001011$ and you select the prefix of length $3$, it becomes $011011$. Then if you select the entire string, it becomes $001001$.

Your task is to transform the string $a$ into $b$ in at most $3n$ operations. It can be proved that it is always possible.


-----Input-----

The first line contains a single integer $t$ ($1\le t\le 1000$)  — the number of test cases. Next $3t$ lines contain descriptions of test cases.

The first line of each test case contains a single integer $n$ ($1\le n\le 1000$)  — the length of the binary strings.

The next two lines contain two binary strings $a$ and $b$ of length $n$.

It is guaranteed that the sum of $n$ across all test cases does not exceed $1000$.


-----Output-----

For each test case, output an integer $k$ ($0\le k\le 3n$), followed by $k$ integers $p_1,\ldots,p_k$ ($1\le p_i\le n$). Here $k$ is the number of operations you use and $p_i$ is the length of the prefix you flip in the $i$-th operation.


-----Example-----
Input
5
2
01
10
5
01011
11100
2
01
01
10
0110011011
1000110100
1
0
1

Output
3 1 2 1
6 5 2 5 3 1 2
0
9 4 1 2 10 4 1 2 1 5
1 1



-----Note-----

In the first test case, we have $01\to 11\to 00\to 10$.

In the second test case, we have $01011\to 00101\to 11101\to 01000\to 10100\to 00100\to 11100$.

In the third test case, the strings are already the same. Another solution is to flip the prefix of length $2$, which will leave $a$ unchanged.

Difficulty: EASY
Tags: String algorithms

Data structures

Constructive algorithms
Skill types: Data structures

Starter code:

Solutions:
import math
value = int(input())
for T in range(value):
	n = int(input())
	sa = input()
	sb = input()
	(count, ans) = (0, [])
	for i in range(1, n + 1):
		i = -i
		if sa[i] != sb[i]:
			if n + 1 + i == 1:
				count += 1
				ans.append(1)
			else:
				count += 3
				ans.append(n + 1 + i)
				ans.append(1)
				ans.append(n + 1 + i)
	print(count, end=' ')
	for v in ans:
		print(v, end=' ')
	print('')


import os
import sys
from io import BytesIO, IOBase
import math

def main():
	pass
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def reversepart(p):
	s1[0:p + 1] = reversed(s1[0:p + 1])
	for i in range(0, p + 1):
		s1[i] = s1[i] ^ 1
for xyz in range(0, int(input())):
	n = int(input())
	s1 = list(input())
	s2 = list(input())
	for i in range(0, n):
		s1[i] = int(s1[i])
		s2[i] = int(s2[i])
	ans = []
	for i in range(n - 1, -1, -1):
		if s2[i] != s1[i]:
			if s2[i] == s1[0]:
				ans.append(1)
				s1[0] = s1[0] ^ 1
				ans.append(i + 1)
				reversepart(i)
			else:
				ans.append(i + 1)
				reversepart(i)
	print(len(ans), end=' ')
	print(*ans)


from sys import stdin
t = int(stdin.readline())
for _ in range(t):
	n = int(stdin.readline())
	a = stdin.readline().strip()
	b = stdin.readline().strip()
	res = []
	if a[0] != b[0]:
		res.append(1)
	for i in range(1, n):
		if a[i] != b[i]:
			res.append(i + 1)
			res.append(1)
			res.append(i + 1)
	print(len(res), *res)


import sys
r = sys.stdin.readline

def revAndnot(bit, idx):
	tmp = ''
	for i in range(idx):
		tmp += '1' if bit[i] == '0' else '0'
	return tmp[::-1] + bit[idx:]
for _ in range(int(r())):
	n = int(r())
	a = r().strip()
	b = r().strip()
	ans = []
	flip = False
	idx = 0
	for i in range(n - 1, -1, -1):
		if flip ^ (a[idx] == b[i]):
			ans.append(str(1))
		ans.append(str(i + 1))
		if flip:
			idx -= i
		else:
			idx += i
		flip ^= True
	print(len(ans), end=' ')
	print(' '.join(ans))


def func(s):
	st = ''
	for i in s:
		if i == '1':
			st += '0'
		else:
			st += '1'
	return st[::-1]
t = int(input())
while t:
	n = int(input())
	s1 = input()
	s2 = input()
	i = n - 1
	l = []
	while i >= 0:
		if s1[i] != s2[i]:
			if s1[0] == s2[i]:
				l.append(1)
				s1 = func(s1[1:i + 1])
				l.append(i + 1)
			else:
				s1 = func(s1[:i + 1])
				l.append(i + 1)
		i -= 1
	print(len(l), *l)
	t -= 1


t = int(input())
for _ in range(t):
	n = int(input())
	s1 = input()
	s2 = input()
	ans = 0
	moves = []
	for i in range(n):
		if s1[i] != s2[i]:
			ans += 3
			moves.append(i + 1)
			moves.append(1)
			moves.append(i + 1)
	print(ans, end=' ')
	for i in moves:
		print(i, end=' ')
	print()


def tc():
	n = int(input())
	a = input()
	b = input()
	segs = []
	prev = a[0]
	for (i, ch) in enumerate(a[1:]):
		if ch != prev:
			segs.append(i + 1)
		prev = ch
	bsegs = []
	bprev = b[0]
	for (i, ch) in enumerate(b[1:]):
		if ch != bprev:
			bsegs.append(i + 1)
		bprev = ch
	if a[-1] != b[-1]:
		segs.append(n)
	segs.extend(bsegs[::-1])
	print(len(segs), ' '.join(map(str, segs)))
T = int(input())
for _ in range(T):
	tc()


import os
import sys
from io import BytesIO, IOBase
import math
import itertools
import bisect
import heapq

def main():
	pass
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def binary(n):
	return bin(n).replace('0b', '')

def decimal(s):
	return int(s, 2)

def pow2(n):
	p = 0
	while n > 1:
		n //= 2
		p += 1
	return p

def primeFactors(n):
	l = []
	while n % 2 == 0:
		l.append(2)
		n = n / 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			l.append(i)
			n = n / i
	if n > 2:
		l.append(int(n))
	return l

def isPrime(n):
	if n == 1:
		return False
	else:
		root = int(n ** 0.5)
		root += 1
		for i in range(2, root):
			if n % i == 0:
				return False
		return True

def maxPrimeFactors(n):
	maxPrime = -1
	while n % 2 == 0:
		maxPrime = 2
		n >>= 1
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			maxPrime = i
			n = n / i
	if n > 2:
		maxPrime = n
	return int(maxPrime)

def countcon(s, i):
	c = 0
	ch = s[i]
	for i in range(i, len(s)):
		if s[i] == ch:
			c += 1
		else:
			break
	return c

def lis(arr):
	n = len(arr)
	lis = [1] * n
	for i in range(1, n):
		for j in range(0, i):
			if arr[i] > arr[j] and lis[i] < lis[j] + 1:
				lis[i] = lis[j] + 1
	maximum = 0
	for i in range(n):
		maximum = max(maximum, lis[i])
	return maximum

def isSubSequence(str1, str2):
	m = len(str1)
	n = len(str2)
	j = 0
	i = 0
	while j < m and i < n:
		if str1[j] == str2[i]:
			j = j + 1
		i = i + 1
	return j == m

def maxfac(n):
	root = int(n ** 0.5)
	for i in range(2, root + 1):
		if n % i == 0:
			return n // i
	return n

def p2(n):
	c = 0
	while n % 2 == 0:
		n //= 2
		c += 1
	return c

def seive(n):
	primes = [True] * (n + 1)
	primes[1] = primes[0] = False
	for i in range(2, n + 1):
		if primes[i]:
			for j in range(i + i, n + 1, i):
				primes[j] = False
	p = []
	for i in range(0, n + 1):
		if primes[i]:
			p.append(i)
	return p

def ncr(n, r, p):
	num = den = 1
	for i in range(r):
		num = num * (n - i) % p
		den = den * (i + 1) % p
	return num * pow(den, p - 2, p) % p

def denofactinverse(n, m):
	fac = 1
	for i in range(1, n + 1):
		fac = fac * i % m
	return pow(fac, m - 2, m)

def numofact(n, m):
	fac = 1
	for i in range(1, n + 1):
		fac = fac * i % m
	return fac
for xyz in range(0, int(input())):
	n = int(input())
	a = input() + '1'
	b = input() + '1'
	(cona, conb) = ([], [])
	for i in range(1, n + 1):
		if a[i] != a[i - 1]:
			cona.append(i)
		if b[i] != b[i - 1]:
			conb.append(i)
	cona += conb[::-1]
	print(len(cona), *cona)


for _ in range(int(input())):
	n = int(input())
	a = list(input())
	b = list(input())
	res = []
	count = 0
	if a[0] != b[0]:
		count = 1
		res.append(1)
	for i in range(1, n):
		if a[i] != b[i]:
			res.append(i + 1)
			res.append(1)
			res.append(i + 1)
	print(len(res), ' '.join(map(str, res)))


def gr(a):
	for i in range(len(a)):
		if a[i] == '1':
			a[i] = '0'
		else:
			a[i] = '1'
	return a[::-1]
for i in range(int(input())):
	n = int(input())
	a = [i for i in input()] + ['1']
	b = [i for i in input()] + ['1']
	(d1, d2) = ([], [])
	if a == b:
		print(0)
	else:
		for i in range(n):
			if a[i] != b[i]:
				d1.append(i + 1)
				d1.append(1)
				d1.append(i + 1)
		print(len(d1), *d1)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input()))
	b = list(map(int, input()))
	m = []
	while a:
		while a and a[-1] == 0:
			del a[-1]
		if a:
			if a[0] == 1:
				m.append(len(a))
				a = [1 - i for i in reversed(a)]
			else:
				i = 0
				while a[i] == 0:
					i += 1
				m.append(i)
				a = [1] * i + a[i:]
	c = 0
	for i in range(n - 1, -1, -1):
		if b[i] != c:
			m.append(i + 1)
			c = 1 - c
	print(len(m), *m)


R1 = lambda : list(map(int, input().split()))
R2 = lambda : int(input())
t = R2()
for _ in range(t):
	n = R2()
	(a, b) = (input(), input())
	d = []
	for j in range(n):
		if a[j] != b[j]:
			d += [j + 1, 1, j + 1]
	print(len(d), *d)


import sys
input = sys.stdin.readline

def I():
	return input().strip()

def II():
	return int(input().strip())

def LI():
	return [*map(int, input().strip().split())]
import copy
import string, math, time, functools, random, fractions
from heapq import heappush, heappop, heapify
from bisect import bisect_left, bisect_right
from collections import deque, defaultdict, Counter, OrderedDict
from itertools import permutations, combinations, groupby, count, filterfalse
from operator import itemgetter
for _ in range(II()):
	n = II()
	s1 = I()
	s2 = I()
	l = []
	k = 0
	for i in range(n):
		if s1[i] != s2[i]:
			l.append(i + 1)
			l.append(1)
			l.append(i + 1)
			k = k + 3
	print(k, end=' ')
	for i in range(len(l)):
		print(l[i], end=' ')
	print()


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from collections import Counter
import heapq
for _ in range(int(input())):
	n = int(input())
	s = input()
	t = input()
	ans = []
	for i in range(n):
		if s[n - i - 1] != t[n - i - 1]:
			ans.append(n - i)
			ans.append(1)
			ans.append(n - i)
	if ans == []:
		print(0)
	else:
		print(len(ans), end=' ')
		print(*ans)


import sys, bisect, string, math, time, functools, random, fractions
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
from itertools import permutations, combinations, groupby
rep = range
R = range

def Golf():
	(n, *t) = map(int, open(0).read().split())

def I():
	return int(input())

def S_():
	return input()

def IS():
	return input().split()

def LS():
	return [i for i in input().split()]

def MI():
	return map(int, input().split())

def LI():
	return [int(i) for i in input().split()]

def LI_():
	return [int(i) - 1 for i in input().split()]

def NI(n):
	return [int(input()) for i in range(n)]

def NI_(n):
	return [int(input()) - 1 for i in range(n)]

def StoLI():
	return [ord(i) - 97 for i in input()]

def ItoS(n):
	return chr(n + 97)

def LtoS(ls):
	return ''.join([chr(i + 97) for i in ls])

def RA():
	return map(int, open(0).read().split())

def GI(V, E, ls=None, Directed=False, index=1):
	org_inp = []
	g = [[] for i in range(V)]
	FromStdin = True if ls == None else False
	for i in range(E):
		if FromStdin:
			inp = LI()
			org_inp.append(inp)
		else:
			inp = ls[i]
		if len(inp) == 2:
			(a, b) = inp
			c = 1
		else:
			(a, b, c) = inp
		if index == 1:
			a -= 1
			b -= 1
		aa = (a, c)
		bb = (b, c)
		g[a].append(bb)
		if not Directed:
			g[b].append(aa)
	return (g, org_inp)

def GGI(h, w, search=None, replacement_of_found='.', mp_def={'#': 1, '.': 0}, boundary=1):
	mp = [boundary] * (w + 2)
	found = {}
	for i in R(h):
		s = input()
		for char in search:
			if char in s:
				found[char] = (i + 1) * (w + 2) + s.index(char) + 1
				mp_def[char] = mp_def[replacement_of_found]
		mp += [boundary] + [mp_def[j] for j in s] + [boundary]
	mp += [boundary] * (w + 2)
	return (h + 2, w + 2, mp, found)

def TI(n):
	return GI(n, n - 1)

def accum(ls):
	rt = [0]
	for i in ls:
		rt += [rt[-1] + i]
	return rt

def bit_combination(n, base=2):
	rt = []
	for tb in R(base ** n):
		s = [tb // base ** bt % base for bt in R(n)]
		rt += [s]
	return rt

def gcd(x, y):
	if y == 0:
		return x
	if x % y == 0:
		return y
	while x % y != 0:
		(x, y) = (y, x % y)
	return y

def YN(x):
	print(['NO', 'YES'][x])

def Yn(x):
	print(['No', 'Yes'][x])

def show(*inp, end='\n'):
	if show_flg:
		print(*inp, end=end)
mo = 2305843009213693951
inf = float('inf')
FourNb = [(-1, 0), (1, 0), (0, 1), (0, -1)]
EightNb = [(-1, 0), (1, 0), (0, 1), (0, -1), (1, 1), (-1, -1), (1, -1), (-1, 1)]
compas = dict(zip('WENS', FourNb))
cursol = dict(zip('LRUD', FourNb))
l_alp = string.ascii_lowercase
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
input = lambda : sys.stdin.readline().rstrip()

class Comb:

	def __init__(self, n, mo=10 ** 9 + 7):
		self.fac = [0] * (n + 1)
		self.inv = [1] * (n + 1)
		self.fac[0] = 1
		self.fact(n)
		for i in range(1, n + 1):
			self.fac[i] = i * self.fac[i - 1] % mo
			self.inv[n] *= i
			self.inv[n] %= mo
		self.inv[n] = pow(self.inv[n], mo - 2, mo)
		for i in range(1, n):
			self.inv[n - i] = self.inv[n - i + 1] * (n - i + 1) % mo
		return

	def fact(self, n):
		return self.fac[n]

	def invf(self, n):
		return self.inv[n]

	def comb(self, x, y):
		if y < 0 or y > x:
			return 0
		return self.fac[x] * self.inv[x - y] * self.inv[y] % mo
show_flg = False
show_flg = True
ans = 0

def make0(s):
	n = len(s)
	s += [0]
	rt = []
	for i in range(n):
		if s[i] != s[i + 1]:
			rt += (i + 1,)
	return rt
for _ in range(I()):
	n = I()
	s = [int(i) for i in input()]
	t = [int(i) for i in input()]
	ans = 0
	(rs, rt) = (make0(s), make0(t))
	ans = rs + rt[::-1]
	print(len(ans), *ans)


def solve():
	n = int(input())
	a = input()
	b = input()
	k = 0
	ans = []
	for i in range(n - 1, 0, -1):
		if a[i] != b[i]:
			if a[0] != b[i]:
				k += 2
			else:
				k += 3
				ans.append(1)
			ans.append(i + 1)
			ans.append(i)
			a = a[1:i + 1] + b[i] + a[i + 1:]
	if a[0] != b[0]:
		k += 1
		ans.append(1)
	print(k, ' '.join(map(str, ans)))
for _ in range(int(input())):
	solve()


t = int(input())
while t:
	t = t - 1
	n = int(input())
	a = list(input())
	b = input()
	cnt = 0
	l = []
	for i in range(n - 1, -1, -1):
		if a[i] == b[i]:
			continue
		elif i == 0:
			l.append(1)
			cnt += 1
		else:
			if a[0] != a[i]:
				l.append(1)
				cnt += 1
				temp = int(a[0]) ^ 1
				a[0] = temp
			l.append(i + 1)
			l.append(i)
			a = a[1:i] + a[i:] + [0]
			cnt += 2
	print(cnt, end=' ')
	for i in l:
		print(i, end=' ')
	print()


t = int(input())
while t:
	n = int(input())
	u = input()
	v = input()
	a = []
	b = []
	for i in range(n):
		a.append(int(u[i]))
		b.append(int(v[i]))
	k = n - 1
	c = []
	while k >= 0:
		if a[0] == b[k]:
			c.append(1)
			c.append(k + 1)
			a.reverse()
			for i in range(len(a)):
				if a[i] == 1:
					a[i] = 0
				else:
					a[i] = 1
			a.pop()
			k -= 1
		else:
			c.append(k + 1)
			a.reverse()
			for i in range(len(a)):
				if a[i] == 1:
					a[i] = 0
				else:
					a[i] = 1
			a.pop()
			k -= 1
	if len(c) == 0:
		print(0)
	else:
		print(len(c), end=' ')
		for i in range(len(c) - 1):
			print(c[i], end=' ')
		print(c[len(c) - 1])
	t -= 1


import sys
LI = lambda : list(map(int, sys.stdin.readline().strip('\n').split()))
MI = lambda : map(int, sys.stdin.readline().strip('\n').split())
SI = lambda : sys.stdin.readline().strip('\n')
II = lambda : int(sys.stdin.readline().strip('\n'))
for _ in range(II()):
	(n, a, b) = (II(), SI() + '0', SI() + '0')
	(one, two) = ([], [])
	for i in range(n):
		if a[i + 1] != a[i]:
			one.append(i + 1)
		if b[i + 1] != b[i]:
			two.append(i + 1)
	ans = one + two[::-1]
	print(len(ans), *ans)


import sys
import math

def II():
	return int(sys.stdin.readline())

def LI():
	return list(map(int, sys.stdin.readline().split()))

def MI():
	return map(int, sys.stdin.readline().split())

def SI():
	return sys.stdin.readline().strip()
t = II()
for q in range(t):
	n = II()
	a = list(SI())
	b = list(SI())
	l = 0
	r = n - 1
	ans = []
	temp = 0
	for i in range(n - 1, -1, -1):
		if b[i] == a[0]:
			ans.append(1)
			if a[0] == '0':
				a[0] = '1'
			else:
				a[0] = '0'
			ans.append(n - temp)
		else:
			ans.append(n - temp)
		for j in range(n - temp):
			if a[j] == '0':
				a[j] = '1'
			else:
				a[j] = '0'
		if temp != 0:
			a = a[:n - temp][::-1] + a[n - temp:]
		else:
			a = a[:n - temp][::-1]
		temp += 1
	ans = [len(ans)] + ans
	print(*ans)


def rev(a):
	ops = []
	for i in range(1, len(a)):
		if a[i] != a[i - 1]:
			ops.append(i)
			a[0] = a[i]
	if a[0] == '1':
		ops.append(len(a))
	return ops
tot = int(input().strip())
while tot > 0:
	n = int(input().strip())
	a = list(input().strip())
	b = list(input().strip())
	ops = rev(a) + list(reversed(rev(b)))
	print(str(len(ops)) + ' ' + ' '.join([str(op) for op in ops]))
	tot -= 1


import sys
for _ in range(int(input())):
	n = int(input())
	a = sys.stdin.readline()
	b = sys.stdin.readline()
	k = 0
	p = []
	l = []
	a0 = a[0]
	lenl = 0
	for i in range(n - 1, 0, -1):
		r = i
		for h in l:
			r = abs(r - h)
		if lenl == 0:
			ai = a[r]
		else:
			ai = str(1 - int(a[r]))
		if ai != b[i]:
			if a0 != ai:
				p += [1]
				k += 1
				a0 = ai
			l = [i] + l
			a0 = b[i]
			lenl = 1 - lenl
			k += 1
			p += [i + 1]
	if a0 != b[0]:
		p += [1]
		k += 1
	p = [k] + p
	print(*p)


import sys
import math, bisect
from collections import defaultdict
from itertools import groupby, accumulate
from heapq import heapify, heappop, heappush
from collections import deque, Counter, OrderedDict

def neo():
	return map(int, input().split())

def Neo():
	return list(map(int, input().split()))
for _ in range(int(input())):
	N = int(input())
	a = input()
	b = input()
	A = list(a)
	B = list(b)
	Ans = []
	for i in range(N):
		A[i] = int(A[i])
		B[i] = int(B[i])
	for i in B[::-1]:
		if i == A[0]:
			Ans.append(1)
		Ans.append(len(A))
		for j in range(len(A)):
			if A[j] == 0:
				A[j] = 1
			else:
				A[j] = 0
		A = A[::-1]
		A = A[:-1]
	print(len(Ans))
	print(*Ans)


import sys
input = lambda : sys.stdin.readline().strip()
t = int(input())
while t:
	t -= 1
	n = int(input())
	a = list(map(int, list(input())))
	b = list(map(int, list(input())))
	k = []
	l = 0
	r = n - 1
	inv = 0
	for i in range(n, 0, -1):
		if a[r] ^ inv != b[i - 1]:
			if a[l] ^ inv == b[i - 1]:
				k.append(1)
				a[l] = 1 - a[l]
			k.append(i)
			inv = 1 - inv
			(r, l) = (l, r)
		if r >= l:
			r -= 1
		else:
			r += 1
	print(len(k), end=' ')
	for i in k:
		print(i, end=' ')
	print()


def examA():
	T = I()
	for _ in range(T):
		(N, M) = LI()
		A = LI()
		B = LI()
		B = set(B)
		cur = -1
		for a in A:
			if a in B:
				cur = a
				break
		if cur == -1:
			print('NO')
			continue
		print('YES')
		print('1', cur)
	return

def examB():
	T = I()
	ans = []
	for _ in range(T):
		N = I()
		A = LI()
		cur = -1
		for i in range(N):
			if A[i] > 1:
				if i % 2 == 0:
					cur = 1
				else:
					cur = 0
				break
		if cur == -1:
			if N % 2 == 1:
				cur = 1
			else:
				cur = 0
		if cur == 0:
			print('Second')
		else:
			print('First')
	return

def examC():
	T = I()
	for _ in range(T):
		N = I()
		a = SI()
		b = SI()
		A = []
		B = []
		for i in a:
			A.append(int(i))
		for i in b:
			B.append(int(i))
		cur = []
		for l in range(N - 1, -1, -1):
			if A[l] == B[l]:
				continue
			if A[0] == B[l]:
				A[0] = 1 ^ A[0]
				cur.append(1)
			ne_A = []
			for i in A[:l + 1]:
				ne_A.append(1 ^ i)
			A = ne_A[::-1] + A[l + 1:]
			cur.append(l + 1)
		print(len(cur), ' '.join(map(str, cur)))
	return

def examD():
	ans = 0
	print(ans)
	return

def examE():
	ans = 0
	print(ans)
	return

def examC2():
	T = I()
	for _ in range(T):
		N = I()
		a = SI()
		b = SI()
		A = []
		B = []
		for i in a:
			A.append(int(i))
		for i in b:
			B.append(int(i))
		cur = []
		rev = 0
		L = 0
		R = N - 1
		for l in range(N - 1, -1, -1):
			if rev == 0:
				if A[R] == B[l]:
					R -= 1
					continue
				if A[L] == B[l]:
					A[L] = 1 ^ A[L]
					cur.append(1)
				cur.append(l + 1)
				L += 1
			else:
				if A[L] != B[l]:
					L += 1
					continue
				if A[R] != B[l]:
					A[R] = 1 ^ A[R]
					cur.append(1)
				cur.append(l + 1)
				R -= 1
			rev ^= 1
		print(len(cur), ' '.join(map(str, cur)))
	return
from decimal import getcontext, Decimal as dec
import sys, bisect, heapq
from copy import deepcopy
from heapq import heappop, heappush, heapify
from collections import Counter, defaultdict, deque
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

def I():
	return int(input())

def LI():
	return list(map(int, sys.stdin.readline().split()))

def DI():
	return dec(input())

def LDI():
	return list(map(dec, sys.stdin.readline().split()))

def LSI():
	return list(map(str, sys.stdin.readline().split()))

def LS():
	return sys.stdin.readline().split()

def SI():
	return sys.stdin.readline().strip()
global mod, mod2, inf, alphabet, _ep
mod = 10 ** 9 + 7
mod2 = 998244353
inf = 10 ** 18
_ep = dec('0.000000000001')
alphabet = [chr(ord('a') + i) for i in range(26)]
alphabet_convert = {chr(ord('a') + i): i for i in range(26)}
getcontext().prec = 28
sys.setrecursionlimit(2 * 10 ** 5)
examC2()


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from math import gcd, ceil

def pre(s):
	n = len(s)
	pi = [0] * n
	for i in range(1, n):
		j = pi[i - 1]
		while j and s[i] != s[j]:
			j = pi[j - 1]
		if s[i] == s[j]:
			j += 1
		pi[i] = j
	return pi

def prod(a):
	ans = 1
	for each in a:
		ans = ans * each
	return ans

def lcm(a, b):
	return a * b // gcd(a, b)

def binary(x, length=16):
	y = bin(x)[2:]
	return y if len(y) >= length else '0' * (length - len(y)) + y

def invert(x):
	r = len(x)
	ans = ['0'] * r
	for i in range(r):
		if x[i] == '0':
			ans[i] = '1'
	return ''.join(ans)
for _ in range(int(input()) if True else 1):
	n = int(input())
	s1 = input()
	s2 = input()
	ans = []
	invert = 0
	v = 0
	for i in range(n - 1, -1, -1):
		reverse = n - v // 2 - 1 if v % 2 else v // 2
		rr = s1[reverse]
		if invert % 2:
			rr = str(1 - int(rr))
		if rr == s2[i]:
			ans += [1]
		invert += 1
		ans += [i + 1]
		v += 1
	print(len(ans), *ans)


for _ in range(int(input())):
	n = int(input())
	s = input()
	s1 = input()
	if s == s1:
		print('0')
		continue
	ans = []
	s2 = s + '0'
	i = 0
	while s2[i] == '1':
		i += 1
	if i != 0:
		ans.append(i)
	for j in range(i + 1, n + 1):
		if s2[j] == '1' and s2[j - 1] == '0':
			tmp = j
		elif s2[j] == '0' and s2[j - 1] == '1':
			ans.append(tmp)
			ans.append(j)
	s3 = '0' + s1
	if s1.count('0') == n:
		print(len(ans), *ans)
		continue
	tmp = n
	for j in range(n, -1, -1):
		if s3[j] == '0' and s3[j - 1] == '1':
			tmp = j - 1
		elif s3[j] == '1' and s3[j - 1] == '0':
			ans.append(tmp)
			if j != 1:
				ans.append(j - 1)
	print(len(ans), *ans)


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(input()[:-1])
	b = list(input()[:-1])
	ans = []
	z = ['0' for i in range(n)]
	while a != z:
		if a[0] == '0':
			a[0] = '1'
			ans.append(1)
		else:
			for i in range(n - 1, -1, -1):
				if a[i] == '0':
					continue
				else:
					ans.append(i + 1)
					for j in range(i + 1):
						if a[j] == '0':
							a[j] = '1'
						else:
							a[j] = '0'
					re = reversed(a[:i + 1])
					a[:i + 1] = re
					break
	while a != b:
		for i in range(n - 1, -1, -1):
			if a[i] != b[i]:
				ans.append(i + 1)
				for j in range(i + 1):
					if a[j] == '0':
						a[j] = '1'
					else:
						a[j] = '0'
				re = reversed(a[:i + 1])
				a[:i + 1] = re
				break
	print(len(ans), *ans)


for i in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	d = []
	for c in range(1, n + 1):
		if a == b:
			print(len(d), *d)
			break
		if int(a[-c]) == int(b[-c]):
			continue
		else:
			if int(a[0]) == int(b[-c]):
				d.append(1)
				if int(a[0]) == 0:
					a = list(a)
					a[0] = '1'
					a = ''.join(a)
				else:
					a = list(a)
					a[0] = '0'
					a = ''.join(a)
			if a == b:
				print(len(d), *d)
				break
			d.append(n - c + 1)
			k = a[:n - c + 1].replace('1', 'k')
			k = k.replace('0', 'm')
			x = k.replace('k', '0')
			x = x.replace('m', '1')
			x = list(x)
			if len(x) == n:
				f = x[::-1]
				a = ''.join(f)
			else:
				f = x[::-1] + list(a[-(c - 1):])
				a = ''.join(f)
			if a == b:
				print(len(d), *d)
				break


import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush, nsmallest
from math import ceil, floor, gcd, fabs, factorial, fmod, sqrt, inf, log
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction
from decimal import Decimal
mod = pow(10, 9) + 7
mod2 = 998244353

def data():
	return sys.stdin.readline().strip()

def out(var, end='\n'):
	sys.stdout.write(str(var) + end)

def outa(*var, end='\n'):
	sys.stdout.write(' '.join(map(str, var)) + end)

def l():
	return list(sp())

def sl():
	return list(ssp())

def sp():
	return map(int, data().split())

def ssp():
	return map(str, data().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [l1d(n, val) for j in range(m)]
for _ in range(int(data())):
	n = int(data())
	a = list(data())
	b = data()
	answer = []
	flip = False
	(i, j, k) = (0, n - 1, n - 1)
	while k >= 0:
		if not flip:
			if b[k] == a[j]:
				k -= 1
				j -= 1
			else:
				if b[k] == a[i]:
					answer.append(1)
					answer.append(j - i + 1)
					flip = True
				else:
					answer.append(j - i + 1)
					flip = True
				i += 1
				k -= 1
		elif b[k] != a[i]:
			k -= 1
			i += 1
		else:
			if b[k] != a[j]:
				answer.append(1)
				answer.append(j - i + 1)
				flip = False
			else:
				answer.append(j - i + 1)
				flip = False
			j -= 1
			k -= 1
	out(len(answer), end=' ')
	outa(*answer)


import time, math as mt, bisect, sys
from sys import stdin, stdout
from collections import deque
from fractions import Fraction
from collections import Counter
from collections import OrderedDict
pi = 3.141592653589793

def II():
	return int(stdin.readline())

def IO():
	return stdin.readline()

def IP():
	return map(int, stdin.readline().split())

def L():
	return list(map(int, stdin.readline().split()))

def P(x):
	return stdout.write(str(x) + '\n')

def PI(x, y):
	return stdout.write(str(x) + ' ' + str(y) + '\n')

def lcm(a, b):
	return a * b // gcd(a, b)

def gcd(a, b):
	if a == 0:
		return b
	elif b == 0:
		return a
	if a > b:
		return gcd(a % b, b)
	else:
		return gcd(a, b % a)

def readTree():
	v = int(input())
	adj = [set() for i in range(v + 1)]
	for i in range(v - 1):
		(u1, u2) = In()
		adj[u1].add(u2)
		adj[u2].add(u1)
	return (adj, v)

def bfs(adj, v):
	visited = [False] * (v + 1)
	q = deque()
	while q:
		pass

def sieve():
	li = [True] * 1000001
	(li[0], li[1]) = (False, False)
	for i in range(2, len(li), 1):
		if li[i] == True:
			for j in range(i * i, len(li), i):
				li[j] = False
	prime = []
	for i in range(1000001):
		if li[i] == True:
			prime.append(i)
	return prime

def setBit(n):
	count = 0
	while n != 0:
		n = n & n - 1
		count += 1
	return count
mx = 10 ** 7
spf = [mx] * (mx + 1)

def SPF():
	spf[1] = 1
	for i in range(2, mx + 1):
		if spf[i] == mx:
			spf[i] = i
			for j in range(i * i, mx + 1, i):
				if i < spf[j]:
					spf[j] = i
	return
mod = 10 ** 9 + 7

def solve(i):
	n = II()
	(a, b) = (input(), input())
	if a == b:
		print(0)
		return
	else:
		fin = []
		count = 0
		for i in range(n):
			if a[i] == '1':
				if i == 0:
					fin.append(1)
				else:
					fin.append(i)
					fin.append(i + 1)
		for i in range(n - 1, -1, -1):
			if b[i] == '1':
				if i == 0:
					fin.append(1)
				else:
					fin.append(i + 1)
					fin.append(i)
		fin.insert(0, len(fin))
		fin2 = []
		for i in range(n):
			if a[i] == '0':
				if i == 0:
					fin2.append(1)
				else:
					fin2.append(i)
					fin2.append(i + 1)
		for i in range(n - 1, -1, -1):
			if b[i] == '0':
				if i == 0:
					fin2.append(1)
				else:
					fin2.append(i + 1)
					fin2.append(i)
		fin2.insert(0, len(fin2))
		if fin2[0] < fin[0]:
			print(*fin2)
		else:
			print(*fin)
t = II()
for i in range(t):
	solve(i)


from sys import stdin, stdout
input = stdin.readline

def solve():
	n = int(input())
	a = input().strip()
	b = input().strip()
	(c, s, e) = (0, 0, n - 1)
	d = []
	for i in range(n - 1, -1, -1):
		if c % 2 == 0:
			if a[e] != b[i]:
				if a[s] != b[i]:
					d.append(i + 1)
				else:
					d.append(1)
					d.append(i + 1)
				(s, e) = (e, s + 1)
				c += 1
			else:
				e = e - 1
		elif a[e] == b[i]:
			if a[s] == b[i]:
				d.append(i + 1)
			else:
				d.append(1)
				d.append(i + 1)
			(s, e) = (e, s - 1)
			c += 1
		else:
			e = e + 1
	print(len(d), *d)

def main():
	for t in range(int(input())):
		solve()
main()


t = int(input())
cases = []
for i in range(t):
	input()
	a = list(input())
	b = list(input())
	cases.append([a, b])

def solve(case):
	a = case[0]
	b = case[1]
	flips = []
	for (i, c) in enumerate(a):
		if i != len(a) - 1:
			if a[i + 1] != c:
				flips.append(str(i + 1))
	if a[-1] == '1':
		aa = ['1'] * len(a)
	else:
		aa = ['0'] * len(a)
	flag = True
	for j in range(len(a) - 1, -1, -1):
		if flag:
			if aa[j] != b[j]:
				flips.append(str(j + 1))
				flag = False
		elif aa[j] == b[j]:
			flips.append(str(j + 1))
			flag = True
	print(len(flips), ' '.join(flips))
for case in cases:
	solve(case)


def solve(n, stringA, stringB):
	index = n - 1
	times = 0
	ans = []
	reversed = 0
	while index >= 0:
		target = stringA[index]
		if reversed == 1:
			if stringA[index] == '0':
				target = '1'
			else:
				target = '0'
		if target != stringB[index]:
			cmpThis = stringA[0]
			if reversed == 1:
				if stringA[0] == '0':
					cmpThis = '1'
				else:
					cmpThis = '0'
			if cmpThis == stringB[index]:
				times += 1
				ans.append(1)
			if reversed == 0:
				reversed = 1
			else:
				reversed = 0
			times += 1
			ans.append(index + 1)
			stringA.reverse()
		stringA.pop(-1)
		index -= 1
	print(times, end=' ')
	for x in ans:
		print(x, end=' ')
	print('')
for _ in range(int(input())):
	n = int(input())
	a = list(input())
	b = list(input())
	solve(n, a, b)


t = int(input())
while t:
	n = int(input())
	s1 = input()
	s2 = input()
	ans = []
	check = 0
	b = 0
	f = 0
	count = 0
	for i in range(n - 1, -1, -1):
		if check == 0:
			if s2[i] == s1[n - 1 - b]:
				b = b + 1
			elif s2[i] != s1[0 + f]:
				check = 1
				f = f + 1
				ans.append(i + 1)
				count = count + 1
			else:
				check = 1
				f = f + 1
				ans.append(1)
				ans.append(i + 1)
				count = count + 2
		elif s2[i] != s1[0 + f]:
			f = f + 1
		elif s2[i] == s1[n - 1 - b]:
			check = 0
			b = b + 1
			ans.append(i + 1)
			count = count + 1
		else:
			check = 0
			b = b + 1
			ans.append(1)
			ans.append(i + 1)
			count = count + 2
	print(count, end=' ')
	print(*ans)
	t = t - 1


for _ in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	kk = [0]
	for i in range(n - 1, -1, -1):
		if a[i] != b[i]:
			kk[0] += 3
			kk.append(i + 1)
			kk.append(1)
			kk.append(i + 1)
	print(*kk)


import sys
input = sys.stdin.readline
import heapq
import copy
import math
import decimal

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s) - 1])

def insr2():
	s = input()
	return s[:len(s) - 1]

def invr():
	return map(int, input().split())

def pr_list(a):
	print(*a, sep=' ')

def main():
	tests = inp()
	mod = 1000000007
	limit = 10 ** 18
	ans = 0
	stack = []
	hashm = {}
	arr = []
	heapq.heapify(arr)
	for test in range(tests):
		n = inp()
		a = insr()
		b = insr()
		for i in range(n):
			a[i] = int(a[i])
		for i in range(n):
			b[i] = int(b[i])
		ans = []

		def rotate(l, r, arr):
			dum = []
			for i in range(r, l - 1, -1):
				dum.append(not arr[i])
			arr[l:r + 1] = dum
			return arr
		for i in range(n - 1, -1, -1):
			if a[i] != b[i]:
				if a[0] != a[i]:
					ans.append(1)
					a[0] = not a[0]
				ans.append(i + 1)
				a = rotate(0, i, a)
		ans = [len(ans)] + ans
		pr_list(ans)
main()


from sys import stdin
input = lambda : stdin.readline().strip()
lin = lambda : list(map(int, input().split()))
iin = lambda : int(input())
main = lambda : map(int, input().split())
from math import ceil, sqrt, factorial, log
from collections import deque
from bisect import bisect_left
mod = 998244353
mod = 1000000007

def gcd(a, b):
	(a, b) = (max(a, b), min(a, b))
	while a % b != 0:
		(a, b) = (b, a % b)
	return b

def moduloinverse(a):
	return pow(a, mod - 2, mod)

def solve(we):
	n = iin()
	a = list(input())
	b = list(input())
	t = -1
	ans = []
	a = a + ['0']
	for i in range(n + 1):
		if a[i] == '0':
			if t != -1 and t < i - 1:
				ans.append(t + 1)
				ans.append(i)
			elif t == -1 and i != 0:
				ans.append(i)
			t = i
	t = '0'
	for i in range(n - 1, -1, -1):
		if b[i] != t:
			ans.append(i + 1)
			if t == '1':
				t = '0'
			else:
				t = '1'
	print(str(len(ans)), *ans)
qwe = 1
qwe = iin()
for _ in range(qwe):
	solve(_ + 1)


from collections import deque
t = int(input())
for _ in range(t):
	n = int(input())
	a = deque(list(input().rstrip()))
	b = list(input().rstrip())
	ans = []
	c = 0
	for i in range(n - 1, -1, -1):
		if c % 2 == 0:
			if (int(a[0]) + c) % 2 == int(b[i]):
				ans.append(1)
		elif (int(a[-1]) + c) % 2 == int(b[i]):
			ans.append(1)
		c += 1
		if c % 2 == 0:
			a.pop()
		else:
			a.popleft()
		ans.append(i + 1)
	ans.insert(0, len(ans))
	print(*ans)


t = int(input())

def op(s):
	s = s[::-1]
	for i in range(0, len(s)):
		if s[i] == '0':
			s[i] = '1'
		else:
			s[i] = '0'
	return s
for _ in range(t):
	n = int(input())
	a = list(input())
	b = list(input())
	operations = []
	for i in range(len(a) - 1, -1, -1):
		if a[i] == b[i]:
			continue
		elif a[0] != b[i]:
			operations.append(i + 1)
			a[0:i + 1] = op(a[0:i + 1])
		else:
			operations.append(1)
			a[0:1] = op(a[0:1])
			operations.append(i + 1)
			a[0:i + 1] = op(a[0:i + 1])
	print(len(operations), end=' ')
	for i in operations:
		print(i, end=' ')
	print()


import io, os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
for _ in range(int(input())):
	n = int(input())
	a = input().strip()
	b = input().strip()
	i = 0
	ans = []
	st = chr(a[0])
	x = n - 1
	y = 1
	while i != n:
		if st == chr(b[-i - 1]):
			ans.append(1)
			ans.append(n - i)
		else:
			ans.append(n - i)
		if not i % 2:
			if chr(a[x]) == '1':
				st = '0'
			else:
				st = '1'
			x -= 1
		else:
			if chr(a[y]) == '1':
				st = '1'
			else:
				st = '0'
			y += 1
		i += 1
	print(len(ans), end=' ')
	for x in ans:
		print(x, end=' ')
	print('')


t = int(input())

def pre_flip(string, pre_length):
	new_string = ''
	for x in range(pre_length):
		if string[x] == '0':
			new_string += '1'
		else:
			new_string += '0'
	return new_string[::-1] + string[pre_length:]
for x in range(t):
	a = int(input())
	b = str(input())
	c = str(input())
	answer = [0]
	x = 1
	while x <= a and b != c:
		if b[-x] != c[-x]:
			if b[0] == c[-x]:
				b = pre_flip(b, 1)
				answer.append(1)
			b = pre_flip(b, a + 1 - x)
			answer.append(a + 1 - x)
		x += 1
	answer[0] = len(answer) - 1
	for x in answer:
		print(x, end=' ', flush=True)
	print('\n')


import io
import os
from collections import Counter, defaultdict, deque

def solve(N, A, B):

	def f(A):
		p = []
		front = []
		i = 0
		while i < N:
			x = A[i]
			if not front:
				front.append([x, 1])
			elif front[-1][0] != x:
				if len(front) == 1:
					front.append([x, 1])
				else:
					assert len(front) == 2
					p.append(front[0][1])
					front[0][0] ^= 1
					front[0][1] = front[0][1] + front[1][1]
					front[1][0] = x
					front[1][1] = 1
					assert front[0][0] != front[1][0]
			else:
				assert front[-1][0] == x
				front[-1][1] += 1
			i += 1
		if len(front) == 2:
			p.append(front[0][1])
			front[0][0] ^= 1
		return (front[0][0], p)
	(parity1, p1) = f(A)
	(parity2, p2) = f(B)
	if parity1 != parity2:
		p1.append(N)
	p2 = deque(p2)
	while p1 and p2 and (p1[-1] == p2[-1]):
		p1.pop()
		p2.pop()
	p2 = list(p2)[::-1]
	return str(len(p1) + len(p2)) + ' ' + ' '.join([str(x) for x in p1 + p2])
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
T = int(input())
for t in range(T):
	(N,) = [int(x) for x in input().split()]
	A = list(map(int, input().decode().rstrip()))
	B = list(map(int, input().decode().rstrip()))
	ans = solve(N, A, B)
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	a = input()
	b = input()
	al = 0
	ah = len(a) - 1
	ac = len(a) - 1
	ao = 0
	is_flipped = False
	res = []
	for (i, c) in enumerate(reversed(b)):
		if (c == a[ac]) ^ is_flipped:
			if ac == ah:
				ah -= 1
				ac -= 1
			else:
				al += 1
				ac += 1
			continue
		if (c != a[ao]) ^ is_flipped:
			res.append(ah - al + 1)
			if ac == ah:
				(ac, ao) = (ao, ac)
				ac += 1
				al += 1
			else:
				(ac, ao) = (ao, ac)
				ac -= 1
				ah -= 1
			is_flipped = not is_flipped
			continue
		else:
			res.append(1)
			res.append(ah - al + 1)
			if ac == ah:
				(ac, ao) = (ao, ac)
				ac += 1
				al += 1
			else:
				(ac, ao) = (ao, ac)
				ac -= 1
				ah -= 1
			is_flipped = not is_flipped
			continue
	print(len(res), *res)


import sys, math, collections
from collections import defaultdict

def file():
	sys.stdin = open('input.py', 'r')
	sys.stdout = open('output.py', 'w')

def get_array():
	l = list(map(int, input().split()))
	return l

def get_2_ints():
	(a, b) = map(int, input().split())
	return (a, b)

def get_3_ints():
	(a, b, c) = map(int, input().split())
	return (a, b, c)

def sod(n):
	(n, c) = (str(n), 0)
	for i in n:
		c += int(i)
	return c

def getFloor(A, x):
	(left, right) = (0, len(A) - 1)
	floor = -1
	while left <= right:
		mid = (left + right) // 2
		if A[mid] == x:
			return A[mid]
		elif x < A[mid]:
			right = mid - 1
		else:
			floor = A[mid]
			left = mid + 1
	return floor

def main():
	for tt in range(int(input())):
		n = int(input())
		l1 = list(input())
		l2 = list(input())
		ans = []
		for i in range(n - 1, -1, -1):
			if i == 0:
				if l1[i] != l2[i]:
					ans.append(1)
			elif l1[i] != l2[i]:
				if l1[i] == l1[0]:
					temp = l1[:i + 1]
					for j in range(len(temp)):
						if temp[j] == '1':
							temp[j] = '0'
						else:
							temp[j] = '1'
					temp.reverse()
					l1 = temp[:] + l1[i + 1:]
					ans.append(i + 1)
				else:
					if l1[0] == '1':
						l1[0] = '0'
					else:
						l1[0] = '1'
					ans.append(1)
					temp = l1[:i + 1]
					for j in range(len(temp)):
						if temp[j] == '1':
							temp[j] = '0'
						else:
							temp[j] = '1'
					temp.reverse()
					l1 = temp[:] + l1[i + 1:]
					ans.append(i + 1)
		if len(ans) == 0:
			print(0)
		else:
			print(len(ans), *ans)
main()


for _ in range(int(input())):
	n = int(input())
	a = [int(x) for x in input()]
	b = [int(x) for x in input()]
	ans = []
	j = n - 1
	k = -1
	head = 0
	comp = 0
	for i in range(n - 1, -1, -1):
		if comp ^ a[j] != b[i]:
			if a[head] != a[j]:
				ans.append(1)
				a[head] = a[j]
			ans.append(i + 1)
			(head, j) = (j, head)
			k *= -1
			comp ^= 1
		j += k
	print(len(ans), *ans)


def change(s):
	n = len(s)
	p = ''
	for ch in s:
		if ch == '0':
			p += '1'
		else:
			p += '0'
	return p
t = int(input())
while t > 0:
	n = int(input())
	a = input()
	b = input()
	list = []
	for i in range(n):
		if a[0] != b[n - 1 - i]:
			list.append(n - i)
			temp = change(a[:n - i])
			a = temp[::-1] + a[n - i:]
		else:
			list.append(1)
			temp = change(a[:1])
			a = temp[::-1] + a[1:]
			list.append(n - i)
			temp = change(a[:n - i])
			a = temp[::-1] + a[n - i:]
	z = len(list)
	print(z, end=' ')
	for j in range(z):
		print(list[j], end=' ')
	print()
	t -= 1


for _ in range(int(input())):
	n = int(input())
	a = list(input())
	b = list(input())
	cc = []
	while len(a) > 0:
		if a[-1] == b[-1]:
			b.pop()
			a.pop()
		elif b[-1] == a[0]:
			cc.append(1)
			if a[0] == '1':
				a[0] = '0'
			else:
				a[0] = '1'
			cc.append(len(a))
			bb = []
			for i in range(len(a)):
				c = a.pop()
				if c == '1':
					c = '0'
				else:
					c = '1'
				bb.append(c)
			a = bb
			a.pop()
			b.pop()
		else:
			cc.append(len(a))
			bb = []
			for i in range(len(a)):
				c = a.pop()
				if c == '1':
					c = '0'
				else:
					c = '1'
				bb.append(c)
			a = bb
			a.pop()
			b.pop()
	print(len(cc), *cc)


for vishal in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	ans = []
	avals = []
	(i, j, k) = (0, n - 1, 0)
	while k < n:
		if k % 2 == 0:
			avals.append(a[i])
			i += 1
		else:
			avals.append(str(int(not int(a[j]))))
			j -= 1
		k += 1
	k = 0
	for i in range(len(b) - 1, -1, -1):
		if avals[k] != b[i]:
			ans.append(i)
		else:
			ans.append(0)
			ans.append(i)
		k += 1
	print(len(ans), end=' ')
	for i in ans:
		print(i + 1, end=' ')
	print(end='\n')


for _ in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	count = 0
	ans = []
	for i in range(n):
		if a[i] != b[i] and i == 0:
			count += 1
			ans += [1]
		elif a[i] != b[i]:
			count += 3
			ans += [i + 1, 1, i + 1]
	print(count, *ans, sep=' ')


for t in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	ans = []
	for i in range(n):
		if a[i] != b[i]:
			ans.append(i + 1)
			ans.append(1)
			ans.append(i + 1)
	print(len(ans), *ans)


def flip(l):
	ll = []
	for i in reversed(range(len(l))):
		ll.append((l[i] + 1) % 2)
	return ll
inp = lambda : map(int, list(input()))
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(inp())
	b = list(inp())
	ans = []
	for i in reversed(range(n)):
		if a[i] != b[i]:
			ans = [i + 1, 1, i + 1] + ans
	print(len(ans), end=' ')
	for i in ans:
		print(i, end=' ')
	print()


for h in range(int(input())):
	n = int(input())
	s1 = input()
	s2 = input()
	if s1 == s2:
		print(0)
	else:
		ans = []
		i = 0
		j = n - 1
		k = n - 1
		inv = 0
		while i <= j:
			if inv == 0:
				if s1[i] == s2[k]:
					ans.append(1)
					ans.append(j - i + 1)
				else:
					ans.append(j - i + 1)
				i += 1
			else:
				if s1[j] != s2[k]:
					ans.append(1)
					ans.append(j - i + 1)
				else:
					ans.append(j - i + 1)
				j -= 1
			k -= 1
			inv = inv ^ 1
		print(len(ans), *ans)


t = int(input())
while t != 0:
	n = int(input())
	a = list(input())
	b = list(input())
	ans = []
	st = 0
	en = n - 1
	inv_f = drn_f = 0
	count = 0
	j = n - 1
	while count < n:
		count += 1
		if inv_f == 0:
			if drn_f == 0:
				if a[en] == b[j]:
					en -= 1
					j -= 1
					pass
				elif a[st] != b[j]:
					ans.append(j)
					st += 1
					inv_f = 1
					drn_f = 1
					j -= 1
				else:
					ans.append(0)
					ans.append(j)
					st += 1
					inv_f = 1
					drn_f = 1
					j -= 1
			elif a[st] == b[j]:
				st += 1
				j -= 1
			elif a[en] != b[j]:
				ans.append(j)
				en -= 1
				j -= 1
				inv_f = 1
				drn_f = 0
			else:
				ans.append(0)
				ans.append(j)
				en -= 1
				j -= 1
				inv_f = 1
				drn_f = 0
		elif drn_f == 0:
			if a[en] != b[j]:
				en -= 1
				j -= 1
				pass
			elif a[st] == b[j]:
				ans.append(j)
				st += 1
				inv_f = 0
				drn_f = 1
				j -= 1
			else:
				ans.append(0)
				ans.append(j)
				st += 1
				inv_f = 0
				drn_f = 1
				j -= 1
		elif a[st] != b[j]:
			st += 1
			j -= 1
		elif a[en] == b[j]:
			ans.append(j)
			en -= 1
			j -= 1
			inv_f = 0
			drn_f = 0
		else:
			ans.append(0)
			ans.append(j)
			en -= 1
			j -= 1
			inv_f = 0
			drn_f = 0
	print(len(ans), end=' ')
	for i in range(len(ans)):
		print(ans[i] + 1, end=' ')
	print()
	t -= 1


t = int(input())
for _ in range(t):
	n = int(input())
	a = input()
	b = input()
	s = 0
	li = []
	for i in range(n):
		if a[i] == b[i]:
			pass
		else:
			li.append(i + 1)
			li.append(1)
			li.append(i + 1)
			s += 3
	print(s, *li, sep=' ')


def change(s):
	ar = ''
	for i in s:
		if i == '1':
			ar += '0'
		else:
			ar += '1'
	return ar[::-1]
num = int(input())
for i in range(num):
	answer = []
	length = int(input())
	ori = input()
	des = input()
	for i in range(length - 1, -1, -1):
		if ori[i] != des[i]:
			if ori[0] == ori[i]:
				ori = change(ori[:i + 1]) + ori[i + 1:]
				answer.append(str(i + 1))
			else:
				ori = change(ori[0]) + ori[1:]
				ori = change(ori[:i + 1]) + ori[i + 1:]
				answer.append(str(1))
				answer.append(str(i + 1))
	if answer:
		print(len(answer), end=' ')
		print(' '.join(answer))
	else:
		print(0)


def f(A, j, n):
	for i in range(j + 1):
		if A[i] == '1':
			A[i] = '0'
		else:
			A[i] = '1'
	p = A[0:j + 1]
	res = p[::-1]
	if j != n - 1:
		ls = A[j + 1:n]
		return res + ls
	else:
		return res
t = int(input())
while t > 0:
	t = t - 1
	n = int(input())
	a = input()
	b = input()
	A = list(a)
	B = list(b)
	l = []
	for j in range(n - 1, -1, -1):
		if B[j] != A[j]:
			if j == 0:
				l.append(1)
			elif A[0] == A[j]:
				l.append(j + 1)
				A = f(A, j, n)
			else:
				l.append(1)
				l.append(j + 1)
				A = f(A, j, n)
	print(len(l), end=' ')
	for i in l:
		print(i, end=' ')
	print()


t = int(input())
for _ in range(0, t):
	n = int(input())
	a = input()
	b = input()
	k = 0
	swap_arr = []
	for i in range(0, n - 1):
		if a[i] != a[i + 1]:
			k += 1
			swap_arr.append(i + 1)
	if a[n - 1] != b[n - 1]:
		k += 1
		swap_arr.append(n)
	for i in reversed(range(1, n)):
		if b[i] != b[i - 1]:
			swap_arr.append(i)
			k += 1
	s = str(k)
	for i in range(0, k):
		s += ' ' + str(swap_arr[i])
	print(s)


import sys
input = lambda : sys.stdin.readline().rstrip('\n\r')
for _ in range(int(input())):
	n = int(input())
	(a, b) = (list(input()), input())
	moves = []
	for i in range(n - 1, -1, -1):
		if a[i] != b[i]:
			if a[0] == b[i]:
				moves.append(1)
				a[0] = '0' if a[0] == '1' else '1'
			moves.append(i + 1)
			a[:i + 1] = a[:i + 1][::-1]
			for j in range(i + 1):
				a[j] = '0' if a[j] == '1' else '1'
	print(len(moves), *moves)


import os
import sys
from math import *
from collections import *
from fractions import *
from bisect import *
from heapq import *
from io import BytesIO, IOBase

def vsInput():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def value():
	return tuple(map(int, input().split()))

def array():
	return [int(i) for i in input().split()]

def Int():
	return int(input())

def Str():
	return input()

def arrayS():
	return [i for i in input().split()]

def operate(x):
	global a
	temp = []
	for i in range(x):
		temp.append(a[i] ^ 1)
	a[0:x] = temp[::-1]
for _ in range(Int()):
	n = Int()
	a = [int(i) for i in input()]
	b = [int(i) for i in input()]
	steps = []
	for i in range(n - 1, -1, -1):
		if a[i] != b[i]:
			if a[i] == a[0]:
				steps.append(i + 1)
				operate(i + 1)
			else:
				steps.append(1)
				a[0] ^= 1
				steps.append(i + 1)
				operate(i + 1)
	print(len(steps), end=' ')
	print(*steps)


def operate(string, prefix):
	s = ''
	for i in string[:prefix]:
		if i == '0':
			s += '1'
		else:
			s += '0'
	return s[::-1] + string[prefix:]
t = int(input())
for _ in range(t):
	n = int(input())
	a = input()
	b = input()
	i = n - 1
	ans = []
	while i > 0:
		if a[i] == b[i]:
			i -= 1
			continue
		if a[0] != a[i]:
			a = operate(a, 1)
			ans.append(1)
		a = operate(a, i + 1)
		ans.append(i + 1)
		i -= 1
	if a[0] != b[0]:
		ans.append(1)
	print(len(ans), *ans, sep=' ')


for _ in range(int(input())):
	n = int(input())
	a = list(str(input()))
	b = list(str(input()))
	ans = []
	for i in range(n):
		if a[i] != b[i]:
			ans.append(i + 1)
			ans.append(1)
			ans.append(i + 1)
	print(len(ans), *ans)


def rev(s):
	ans = ''
	for _ in s:
		if _ == '0':
			ans += '1'
		else:
			ans += '0'
	return ans
t = int(input())
for _ in range(t):
	n = int(input())
	a = input()
	b = input()
	count = 0
	prefix = []
	for i in range(n):
		if a[i] != b[i]:
			count += 3
			prefix += [i + 1, 1, i + 1]
	print(str(count) + ' ' + ' '.join(map(str, prefix)))


for _ in range(int(input())):
	n = int(input())
	a = [int(i) for i in list(input())]
	b = [int(i) for i in list(input())]
	ans = []
	for i in range(n - 1, -1, -1):
		if a[i] == b[i]:
			continue
		else:
			if a[0] == a[i]:
				ans.append(i + 1)
			else:
				ans.append(1)
				ans.append(i + 1)
				a[0] ^= 1
			for j in range(i + 1):
				a[j] ^= 1
			a[:i + 1] = a[:i + 1][::-1]
	print(len(ans), *ans)


import math
import bisect
import copy
import time
from collections import Counter
for _ in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	ans = []
	for i in range(n - 1, -1, -1):
		if a[i] == b[i]:
			continue
		else:
			ans = ans + [i + 1, 1, i + 1]
	print(len(ans), *ans)


def change(a, i):
	for k in range(i + 1):
		a[k] = '1' if a[k] == '0' else '0'
	a[:i + 1] = a[:i + 1][::-1]
	return a
T = int(input())
for _ in range(T):
	n = int(input())
	a = input()
	b = input()
	a = list(a)
	ans = []
	for i in range(n - 1, -1, -1):
		if a[i] != b[i]:
			if a[i] == a[0]:
				ans.append(i + 1)
				a = change(a, i)
			else:
				a = change(a, 0)
				ans.append(0 + 1)
				a = change(a, i)
				ans.append(i + 1)
	print(len(ans), ' '.join(map(str, ans)))


import sys
readline = sys.stdin.readline
t = int(readline())
for _ in range(t):
	n = int(readline())
	A = list(map(int, list(readline().rstrip())))
	B = list(map(int, list(readline().rstrip())))
	ans = []
	reverse = False
	for i in range(n):
		a_1 = abs(A[-1] - 1) if reverse else A[-1]
		a_0 = abs(A[0] - 1) if reverse else A[0]
		if a_1 == B[-1]:
			pass
		elif a_1 == a_0:
			ans.append(n - i)
			A.reverse()
			reverse = ~reverse
		else:
			ans.append(1)
			A[0] = abs(A[0] - 1)
			ans.append(n - i)
			A.reverse()
			reverse = ~reverse
		assert (abs(A[-1] - 1) if reverse else A[-1]) == B[-1]
		A.pop()
		B.pop()
	print(len(ans), *ans)


t = int(input())
for i in range(t):
	n = int(input())
	a = [int(val) for val in list(input())]
	b = [int(val) for val in list(input())]
	if a == b:
		print(0)
	else:
		count = 0
		res = []
		for j in range(n - 1, 0, -1):
			if a[j] != b[j]:
				if a[j] == a[0]:
					count += 1
					res.append(str(j + 1))
					a[:j + 1] = [1 - a[idx] for idx in range(j, -1, -1)]
				else:
					count += 2
					res.append('1')
					a[0] = 1 - a[0]
					res.append(str(j + 1))
					a[:j + 1] = [1 - a[idx] for idx in range(j, -1, -1)]
		if a[0] != b[0]:
			count += 1
			res.append('1')
		print(count, ' '.join(res))


t = int(input())
for ts in range(t):
	n = int(input())
	s1 = input()
	s2 = input()
	k = 0
	lst = []
	for ch in range(n):
		if ch == 0 and s1[ch] != s2[ch]:
			k += 1
			lst.append(1)
		elif s1[ch] != s2[ch]:
			k += 3
			lst.append(ch + 1)
			lst.append(1)
			lst.append(ch + 1)
	print(k, end=' ')
	for i in lst:
		print(i, end=' ')


from sys import stdin
g = lambda : stdin.readline().strip()
gl = lambda : g().split()
gil = lambda : [int(var) for var in gl()]
gcl = lambda : list(g())
from collections import deque
(t,) = gil()
for _ in range(t):
	(n,) = gil()
	(a, b) = ([int(var) for var in g()], [int(var) for var in g()])
	r = n - 1
	ans = []
	while r != 0:
		if a[-1] == b[r]:
			a.pop()
			r -= 1
		else:
			if a[0] == a[-1]:
				ans.append(r + 1)
				a.reverse()
				for i in range(len(a)):
					a[i] ^= 1
			else:
				a[0] ^= 1
				ans.append(1)
				ans.append(r + 1)
				a.reverse()
				for i in range(len(a)):
					a[i] ^= 1
			r -= 1
			a.pop()
	if a[0] != b[0]:
		ans.append(1)
	print(len(ans), end=' ')
	for a in ans:
		print(a, end=' ')
	print()


t = int(input())
for _ in range(t):
	n = int(input())
	a = input()
	b = input()
	l = []
	for i in range(n):
		if a[i] != b[i]:
			if i > 0:
				l.append(i + 1)
			l.append(1)
			if i > 0:
				l.append(i + 1)
	print(len(l), end=' ')
	for i in range(len(l)):
		print(l[i], end=' ')
	print()


t = int(input())
for _ in range(t):
	n = int(input())
	res = []
	a = input()
	b = input()
	i = n - 1
	while i > 0:
		if a[i] != b[i]:
			res.append(str(i + 1))
			res.append('1')
			res.append(str(i + 1))
		i -= 1
	if a[0] != b[0]:
		res.append('1')
	print(len(res), end=' ')
	for x in res:
		print(x, end=' ')
	print()


T = int(input())
for _ in range(T):
	n = int(input())
	oper_count = 0
	a = list(map(int, input()))
	b = list(map(int, input()))
	index = n - 1
	ans = []
	while index >= 0:
		if a[0] != b[index]:
			oper_count += 1
			new_a = []
			for i in range(0, index + 1):
				new_a.insert(0, (a[i] + 1) % 2)
			ans.append(index + 1)
			a[0:index + 1] = new_a
			while index >= 0 and b[index] == a[index]:
				index -= 1
		else:
			oper_count += 1
			a[0] = a[0] + 1 % 2
			ans.append(1)
	ans.insert(0, len(ans))
	print(' '.join(map(str, ans)))


for _ in range(int(input())):
	n = int(input())
	s = input()
	s += '0'
	t = input()
	t += '0'
	t = t[::-1]
	result = []
	for i in range(n):
		if s[i] != s[i + 1]:
			result.append(i + 1)
	for j in range(n):
		if t[j] != t[j + 1]:
			result.append(n - j)
	print(len(result), *result)


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(i) for i in input()] + [0]
	b = [int(i) for i in input()] + [0]
	op1 = []
	op2 = []
	for i in range(1, n + 1):
		if a[i] != a[i - 1]:
			op1.append(i)
		if b[i] != b[i - 1]:
			op2.append(i)
	op = op1 + op2[::-1]
	print(len(op), *op)


for _ in range(int(input())):
	n = int(input())
	a = [int(x) for x in input()]
	b = [int(x) for x in input()]
	ans = []
	for i in range(n - 1, -1, -1):
		if a[i] != b[i]:
			if a[i] != a[0]:
				ans.append(1)
				a[0] = 1 - a[0]
			ans.append(i + 1)
			for i in range(i + 1):
				a[i] = 1 - a[i]
			a[:i + 1] = a[:i + 1][::-1]
	print(len(ans), *ans)


for _ in range(int(input())):
	n = int(input())
	s = input()
	sm = input()
	a = list(s)
	asm = list(sm[::-1])
	i = 0
	l = []
	while i < n and list(sm) != a:
		if a[0] == sm[n - i - 1]:
			if a[0] == '0':
				a[0] = '1'
			else:
				a[0] = '0'
			l.append(1)
		for x in range(n - i):
			if a[x] == '1':
				a[x] = '0'
			else:
				a[x] = '1'
		a[:n - i] = a[n - i - 1::-1]
		l.append(n - i)
		i += 1
	print(len(l), *l)


def reverse(liste, begin, end):
	index = begin
	for element in reversed(liste[begin:end]):
		liste[index] = element
		index += 1
	return liste

def rearrange(liste, length):
	for i in range(length):
		liste[i] = 1 - liste[i]
	liste = reverse(liste, 0, length) + liste[length:]
t = int(input())
answer = []
for _ in range(t):
	n = int(input())
	a = list(map(int, list(input())))
	b = list(map(int, list(input())))
	end = n - 1
	count = 0
	temp_answer = [0]
	while end >= 0:
		if a[end] != b[end]:
			if a[0] == b[end]:
				count += 1
				temp_answer.append(str(1))
				rearrange(a, 1)
			count += 1
			rearrange(a, end + 1)
			temp_answer.append(str(end + 1))
		end -= 1
	temp_answer[0] = str(count)
	answer.append(' '.join(temp_answer))
print('\n'.join(answer))


tc = int(input())
while tc:
	n = int(input())
	s = input()
	t = input()
	ans = []
	for i in range(n):
		if s[i] != t[i]:
			ans.append(i + 1)
			ans.append(1)
			ans.append(i + 1)
	l = len(ans)
	print(l, end=' ')
	for v in ans:
		print(v, end=' ')
	print()
	tc -= 1


t = int(input())
while t:
	t -= 1
	n = int(input())
	l1 = input()
	l2 = input()
	ans = []
	revert = False
	index = -1
	for i in range(n):
		if i % 2 == 0:
			index += 1
			c1 = index
		else:
			c1 = n - index - 1
		if l1[c1] != l2[n - i - 1]:
			if i % 2 == 0:
				ans.append(n - i)
			else:
				ans.append(1)
				ans.append(n - i)
		elif i % 2 == 0:
			ans.append(1)
			ans.append(n - i)
		else:
			ans.append(n - i)
	print(len(ans), end=' ')
	print(*ans)


for _ in range(int(input())):
	n = int(input())
	a = list(input())
	b = list(input())
	if a[0] == '1':
		s = True
	else:
		s = False
	if b[0] == '1':
		t = True
	else:
		t = False
	(d, f) = ([], [])
	for i in range(1, n):
		if a[i] != a[i - 1]:
			d.append(i)
			s = not s
		if b[i] != b[i - 1]:
			f.append(i)
			t = not t
	if t != s:
		d.append(n)
	d += f[::-1]
	print(len(d), *d)


def solve():
	n = int(input())
	t1 = input()
	a = []
	for i in range(len(t1)):
		a.append(int(t1[i]))
	t1 = input()
	b = []
	for i in range(len(t1)):
		b.append(int(t1[i]))
	(l, r) = (0, n - 1)
	inv = 0
	ans = []
	for i in range(n - 1, -1, -1):
		if inv == 0:
			if a[r] == b[i]:
				r -= 1
				continue
			if a[l] == b[i]:
				ans.append(0)
			ans.append(i)
			inv ^= 1
			l += 1
		else:
			if a[l] ^ 1 == b[i]:
				l += 1
				continue
			if a[r] ^ 1 == b[i]:
				ans.append(0)
			ans.append(i)
			inv ^= 1
			r -= 1
	print(len(ans), end=' ')
	for item in ans:
		print(item + 1, end=' ')
	print()
t = int(input())
for i in range(t):
	solve()


t = int(input())
for _ in range(t):
	n = int(input())
	list1 = list(map(int, input()))
	list2 = list(map(int, input()))
	ans = []
	start = 0
	invert = 0
	for i in range(n - 1, 0 - 1, -1):
		aim = list2[i]
		if invert % 2 == 0 and list1[start + i] == aim or (invert % 2 != 0 and list1[start - i] != aim):
			continue
		if invert % 2 == 0 and list1[start] == aim or (invert % 2 != 0 and list1[start] != aim):
			ans.append(1)
			ans.append(i + 1)
		else:
			ans.append(i + 1)
		if invert % 2 == 0:
			start = start + i
		else:
			start = start - i
		invert += 1
	print(len(ans), end=' ')
	for val in ans:
		print(val, end=' ')
	print()


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(i) for i in list(input())[:n]]
	b = [int(i) for i in list(input())[:n]]
	ans = []
	c = []
	for i in range(1, n):
		if a[i] != a[i - 1]:
			ans.append(i)
	if a[-1] != b[-1]:
		ans.append(n)
	for i in range(1, n):
		if b[i] != b[i - 1]:
			c.append(i)
	ans = ans + c[::-1]
	print(len(ans), ' '.join([str(i) for i in ans]))


def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s)])

def invr():
	return map(int, input().split())
n = inp()
for i in range(n):
	m = inp()
	ans = []
	a = insr()
	for p in range(m):
		a[p] = int(a[p])
	b = insr()
	for p in range(m):
		b[p] = int(b[p])
	for j in range(m):
		if a[j] != b[j]:
			ans.append(j + 1)
			tmp = []
			for k in range(j + 1):
				a[k] = (a[k] + 1) % 2
			a[j] = (a[j] + 1) % 2
			ans.append(1)
			ans.append(j + 1)
			tmp = []
			for k in range(j + 1):
				a[k] = (a[k] + 1) % 2
	q = len(ans)
	res = str(q) + ' '
	for x in ans:
		res += str(x) + ' '
	print(res)


for _ in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	wyniki = []
	for x in range(n):
		if a[x] != b[x]:
			wyniki += [x + 1, 1, x + 1]
	print(len(wyniki), *wyniki)


t = int(input())
for i in range(t):
	n = int(input())
	a = input()
	b = input()
	la = list(a)
	l = []
	j = n - 1
	while j >= 0:
		if int(la[j]) == int(b[j]):
			j -= 1
		else:
			if int(la[0]) == int(b[j]):
				l.append(int('1'))
				la[0] = int(la[0]) ^ 1
			k = 0
			m = j
			while k < m:
				la[k] = int(la[k]) ^ 1
				la[m] = int(la[m]) ^ 1
				(la[k], la[m]) = (la[m], la[k])
				k += 1
				m -= 1
			if m == k:
				la[m] = int(la[m]) ^ 1
			l.append(j + 1)
			j -= 1
	print(len(l), end=' ')
	print(*l)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(input())
	b = list(input())
	l = []
	l2 = []
	if a == b:
		print(0)
		continue
	for i in range(n - 1):
		if a[i] != a[i + 1]:
			l.append(i + 1)
		if b[i] != b[i + 1]:
			l2.append(i + 1)
	if a[-1] != b[-1]:
		l2.append(n)
	l = l + l2[::-1]
	print(len(l), *l)


tc = int(input())
for ctr in range(tc):
	n = int(input())
	a = list(input().strip())
	b = list(input().strip())
	k = []
	for i in range(0, n):
		if a[i] != b[i]:
			if i > 0:
				k.append(i + 1)
			k.append(1)
			if i > 0:
				k.append(i + 1)
	print(len(k), *k)


from math import *
from collections import *
from random import *
from bisect import *
import sys
input = sys.stdin.readline
d = {'1': '0', '0': '1'}
t = int(input())
while t:
	t -= 1
	n = int(input())
	a = input().rstrip('\n')
	b = input().rstrip('\n')
	a = list(a)
	b = list(b)
	r = []
	lp = n - 1
	tu = 0
	while lp >= 0:
		if a[lp] == b[lp]:
			lp -= 1
			continue
		if a[0] == b[lp]:
			r.append(1)
			a[0] = d[a[0]]
			tu += 1
			continue
		else:
			r.append(lp + 1)
			for i in range(lp + 1):
				a[i] = d[a[i]]
			a = a[:lp + 1][-1::-1] + a[lp + 1:]
			lp -= 1
	print(len(r), end=' ')
	print(*r)


import sys

def input():
	return sys.stdin.readline().rstrip()

def input_split():
	return [int(i) for i in input().split()]
testCases = int(input())
answers = []
for _ in range(testCases):
	n = int(input())
	arr = [int(i) for i in input()]
	barr = [int(i) for i in input()]
	ans = []
	for i in range(n):
		index = n - 1 - i
		desired = barr[index]
		if arr[index] == desired:
			continue
		else:
			target = arr[0]
			if arr[0] == 1 - desired:
				ans.append(index + 1)
			else:
				ans.append(1)
				arr[0] = 1 - arr[0]
				ans.append(index + 1)
			temp = [1 - arr[i] for i in range(index + 1)]
			temp.reverse()
			arr = temp + arr[index + 1:]
	answers.append(ans)
for ans in answers:
	print(len(ans), end=' ')
	print(*ans, sep=' ')


import math
import sys
t = int(input())
for _ in range(t):
	n = int(input())
	a = input()
	b = input()
	ans = []
	for i in range(n):
		if a[i] != b[i]:
			ans.append(i + 1)
			ans.append(1)
			ans.append(i + 1)
	if len(ans) == 0:
		print(0)
	else:
		print(len(ans), *ans)


for _ in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	ans = []
	for i in range(n):
		if a[i] != b[i]:
			ans += [i + 1, 1, i + 1]
	print(len(ans), *ans)


import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__

def flip(s1, ind):
	temp = s1[:ind + 1]
	for x in range(ind + 1):
		temp[x] = 1 - temp[x]
	temp = temp[::-1]
	for x in range(ind + 1):
		s1[x] = temp[x]
t = int(input())
for _ in range(t):
	n = int(input())
	s1 = [int(x) for x in input()]
	s2 = [int(x) for x in input()]
	i = n - 1
	ans = []
	while i >= 1:
		if s1[i] != s2[i]:
			a = s1[i]
			b = s2[i]
			aa = s1[0]
			if a == aa:
				ans.append(str(i + 1))
				flip(s1, i)
			else:
				ans.append(str(1))
				s1[0] = 1 - s1[0]
				ans.append(str(i + 1))
				flip(s1, i)
		i -= 1
	if s1[0] != s2[0]:
		ans.append(str(1))
	print(len(ans), ' '.join(ans))


for _ in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	l = []
	while a != b:
		x = ''
		if b[n - 1] == a[n - 1]:
			n = n - 1
			continue
		elif b[n - 1] != a[0]:
			for i in range(n):
				if a[n - i - 1] == '0':
					x += '1'
				else:
					x += '0'
			l.append(n)
			a = x + a[n:]
			n = n - 1
		else:
			if a[0] == '1':
				a = '0' + a[1:]
			else:
				a = '1' + a[1:]
			l.append(1)
			for i in range(n):
				if a[n - i - 1] == '0':
					x += '1'
				else:
					x += '0'
			a = x + a[n:]
			l.append(n)
			n = n - 1
	print(len(l), sep=' ', end=' ')
	for i in l:
		print(i, sep=' ', end=' ')
	print(sep='')


def fu(a, x):
	for i in range(x):
		a[i] = str((int(a[i]) + 1) % 2)
	a[:x] = a[:x][::-1]
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(input())
	b = list(input())
	c = []
	for i in range(n):
		if a[n - i - 1] != b[n - i - 1]:
			if a[0] == b[n - i - 1]:
				c.append(n - i)
				fu(a, n - i)
				c.append(1)
				fu(a, 1)
				c.append(n - i)
				fu(a, n - i)
			else:
				c.append(n - i)
				fu(a, n - i)
	print(len(c), *c)


def op(x, n):
	return list(reversed([1 - i for i in x[0:n]])) + x[n:]

def go(a, b, ans):
	if len(a) == 0:
		return ans
	if a[-1] == b[-1]:
		return go(a[:-1], b[:-1], ans)
	if a[0] != b[-1]:
		return go(op(a, len(a))[:-1], b[:-1], ans + [len(a)])
	for i in range(len(a) - 2, -1, -1):
		if a[i] != a[0]:
			continue
		a = op(a, i + 1)
		a = op(a, len(b))
		x = go(a[:-1], b[:-1], ans + [i + 1, len(b)])
		if x is not None:
			return x
	return None
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input()))
	b = list(map(int, input()))
	if a != b:
		ans = go(a, b, [])
		print(len(ans), *ans)
	else:
		print(2, len(a), len(a))


from sys import stdin, stdout
input = stdin.readline

def write(n, sep='\n'):
	stdout.write(str(n))
	stdout.write(sep)

def gil():
	return list(map(int, input().split()))
for _t in range(int(input())):
	n = int(input())
	a = list(map(int, list(input().strip())))
	b = list(map(int, list(input().strip())))
	o = []
	for i in range(n):
		if a[i] != b[i]:
			o.extend([i + 1, 1, i + 1])
	print(len(o), *o)


(z, zz) = (input, lambda : list(map(int, z().split())))
fast = lambda : stdin.readline().strip()
zzz = lambda : [int(i) for i in fast().split()]
(szz, graph, mod, szzz) = (lambda : sorted(zz()), {}, 10 ** 9 + 7, lambda : sorted(zzz()))
from string import *
from re import *
from collections import *
from queue import *
from sys import *
from collections import *
from math import *
from heapq import *
from itertools import *
from bisect import *
from collections import Counter as cc
from math import factorial as f
from bisect import bisect as bs
from bisect import bisect_left as bsl
from itertools import accumulate as ac

def lcd(xnum1, xnum2):
	return xnum1 * xnum2 // gcd(xnum1, xnum2)

def prime(x):
	p = ceil(x ** 0.5) + 1
	for i in range(2, p):
		if x % i == 0 and x != 2 or x == 0:
			return 0
	return 1

def dfs(u, visit, graph):
	visit[u] = 1
	for i in graph[u]:
		if not visit[i]:
			dfs(i, visit, graph)
num = int(z())
for _ in range(num):
	n = int(z())
	T1 = fast()
	T2 = fast()
	out = []
	for i in range(n - 1):
		if T1[i] != T1[i + 1]:
			out.append(i + 1)
	curr = T1[-1]
	for i in range(n - 1, -1, -1):
		if T2[i] != curr:
			out.append(i + 1)
			curr = '0' if curr == '1' else '1'
	print(len(out), *out)


for i in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	ans = []
	for j in range(n - 1):
		if a[j] != a[j + 1]:
			ans.append(j + 1)
	if a[-1] != b[-1]:
		ans.append(n)
	ans2 = []
	for j in range(n - 1):
		if b[j] != b[j + 1]:
			ans2.append(j + 1)
	ans2 = ans2[::-1]
	print(len(ans) + len(ans2), end=' ')
	print(*ans + ans2)


for _ in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	flag = 0
	lft = 0
	rht = n - 1
	res = []
	for i in range(n - 1, -1, -1):
		if flag == 0:
			if b[i] == a[lft]:
				res.append(1)
			lft += 1
		else:
			if b[i] != a[rht]:
				res.append(1)
			rht -= 1
		res.append(i + 1)
		flag = 1 - flag
	print(len(res), *res)


def bin_not(a):
	NOT = ''
	for i in a:
		NOT += str((int(i) + 1) % 2)
	return NOT
for _ in range(int(input())):
	n = int(input())
	a = input()[::-1]
	b = input()[::-1]
	k = 0
	s = []
	for i in range(n):
		if a[i] == b[i]:
			continue
		if b[i] == a[-1]:
			k += 1
			s += ['1']
		k += 1
		s += [str(n - i)]
		a = a[:i] + bin_not(a[i:])[::-1]
	print(k, ' '.join(s))


t = int(input())
for _ in range(t):
	n = int(input())
	a = input()
	b = input()
	pos1 = 0
	ans = []
	pos2 = n - 1
	rev = 0
	for i in range(n - 1, -1, -1):
		if rev == 0:
			if b[i] == a[pos1]:
				ans.append(1)
			ans.append(i + 1)
			rev = 1
			pos1 += 1
		else:
			if b[i] != a[pos2]:
				ans.append(1)
			ans.append(i + 1)
			rev = 0
			pos2 -= 1
	print(len(ans), end=' ')
	for num in ans:
		print(num, end=' ')
	print()


def solve():
	n = int(input())
	a = input()
	b = input()
	ans = []
	for i in range(n):
		if a[i] != b[i]:
			ans.extend([i + 1, 1, i + 1])
	print(len(ans), ' '.join((str(v) for v in ans)))
t = int(input())
for _ in range(t):
	solve()


import sys, os
from io import BytesIO, IOBase
import collections, itertools, bisect, heapq, math, string
from decimal import *
BUFSIZE = 8192
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def main():
	t = int(input())
	for i in range(t):
		n = int(input())
		a = str(input())
		b = str(input())
		(ra, rb) = (a[::-1], b[::-1])
		ans = []
		for j in range(n):
			if ra[j] == rb[j]:
				continue
			ans.append(n - j)
			ans.append(1)
			ans.append(n - j)
		if len(ans) == 0:
			print(0)
		else:
			print(len(ans))
			print(' '.join([str(x) for x in ans]))
main()


def inv(s):
	s1 = ''
	for i in s:
		if i == '0':
			s1 += '1'
		else:
			s1 += '0'
	return s1[::-1]
t = int(input())
for i in range(t):
	n = int(input())
	s1 = input()
	s2 = input()
	ans = []
	for i in range(len(s1) - 1, -1, -1):
		if s1[i] != s2[i]:
			if s1[0] != s2[i]:
				ans.append(i + 1)
				s1 = inv(s1[:i + 1]) + s1[i + 1:]
			else:
				ans += [1, i + 1]
				s1 = inv(s1[0]) + s1[1:]
				s1 = inv(s1[:i + 1]) + s1[i + 1:]
	print(len(ans), *ans)


for q in range(int(input())):
	n = int(input())
	a = list(input())
	b = input()
	res = []
	for i in range(n - 1, -1, -1):
		if a[i] != b[i]:
			if a[0] == b[i]:
				if a[0] == '0':
					a[0] = '1'
				else:
					a[0] = '0'
				res.append(1)
			res.append(i + 1)
			for j in range(i // 2 + 1):
				(a[j], a[i - j]) = (a[i - j], a[j])
				if a[j] == '0':
					a[j] = '1'
				else:
					a[j] = '0'
				if i - j != j:
					if a[i - j] == '0':
						a[i - j] = '1'
					else:
						a[i - j] = '0'
	print(len(res), end=' ')
	print(*res)


import sys
ii = lambda : sys.stdin.readline().strip()
idata = lambda : [int(x) for x in ii().split()]

def solve():
	n = int(ii())
	a = ii()
	b = ii()
	ans = []
	for i in range(n - 1, -1, -1):
		if a[i] != b[i]:
			ans += [i + 1]
			ans += [1]
			ans += [i + 1]
	print(len(ans), *ans)
	return
for t in range(int(ii())):
	solve()


def chng(c):
	if c == '1':
		return '0'
	else:
		return '1'

def ulta(ss, i):
	frt = ss[:i + 1]
	bck = ss[i + 1:]
	frt = list(frt[::-1])
	for j in range(len(frt)):
		frt[j] = chng(frt[j])
	frt = ''.join(frt)
	return frt + bck
for _ in range(int(input())):
	n = int(input().strip())
	sa = input().strip()
	sb = input().strip()
	if sa == sb:
		print(0)
		continue
	ft = sa[0]
	i = n
	ans = 0
	arr = []
	while i > 0:
		i -= 1
		if sa[i] == sb[i]:
			continue
		elif ft != sb[i]:
			ans += 1
			arr.append(i + 1)
			sa = ulta(sa, i)
			ft = sa[0]
		elif ft == sb[i] and i == 0:
			break
		else:
			ans += 1
			arr.append(1)
			sa = ulta(sa, 0)
			if i != 0:
				ans += 1
				arr.append(i + 1)
			sa = ulta(sa, i)
			ft = sa[i]
	print(ans, *arr)


z = input
mod = 10 ** 9 + 7
from collections import *
from queue import *
from sys import *
from collections import *
from math import *
from heapq import *
from itertools import *
from bisect import *
from collections import Counter as cc
from math import factorial as f

def lcd(xnum1, xnum2):
	return xnum1 * xnum2 // gcd(xnum1, xnum2)

def prime(x):
	p = ceil(x ** 0.5) + 1
	for i in range(2, p):
		if x % i == 0:
			return 0
	return 1
num = int(z())
for _ in range(num):
	n = int(z())
	l1 = list(z())
	l2 = list(z())
	c = 0
	lp = []
	j = 0
	ans = 0
	lp = []
	for i in l1:
		if i != l2[j]:
			ans = j
			lp.append(ans + 1)
			ans = 1
			lp.append(ans)
			lp.append(j + 1)
			c += 1
		j += 1
	print(c * 3, *lp)


def main(r):
	n = int(input())
	a = input()
	b = input()
	len = 0
	swaps = ''
	for i in range(n):
		if a[i] != b[i]:
			len += 3
			swaps += str(i + 1) + ' 1 ' + str(i + 1) + ' '
	print(len, swaps)
t = int(input())
for i in range(t):
	main(i)


import sys
import os, io
input = sys.stdin.readline

def read_int():
	return map(int, input().split())

def read_list():
	return list(map(int, input().split()))

def print_list(l):
	print(' '.join(map(str, l)))
N = int(input())
for i in range(N):
	n = int(input())
	a = input().strip()
	b = input().strip()
	res = []
	rev = False
	(start, end) = (0, n - 1)
	for i in range(n - 1, -1, -1):
		if not rev:
			if b[i] == a[end]:
				end -= 1
			else:
				if b[i] == a[start]:
					res.append(1)
				res.append(i + 1)
				start += 1
				rev = not rev
		elif b[i] != a[start]:
			start += 1
		else:
			if b[i] != a[end]:
				res.append(1)
			res.append(i + 1)
			end -= 1
			rev = not rev
	print_list([len(res)] + res)


def prefixFlip(s1, s2, n):
	operations = []
	k = 0
	for i in range(n):
		if s1[i] != s2[i]:
			operations.append(str(i + 1))
			operations.append(str(1))
			operations.append(str(i + 1))
			k += 3
	return [str(k)] + operations
t = int(input())
for g in range(t):
	n = int(input())
	s1 = input()
	s2 = input()
	ans = prefixFlip(s1, s2, n)
	print(' '.join(ans))


import sys

def input():
	return sys.stdin.readline().strip()

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def list4d(a, b, c, d, e):
	return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]

def ceil(x, y=1):
	return int(-(-x // y))

def INT():
	return int(input())

def MAP():
	return map(int, input().split())

def LIST(N=None):
	return list(MAP()) if N is None else [INT() for i in range(N)]

def Yes():
	print('Yes')

def No():
	print('No')

def YES():
	print('YES')

def NO():
	print('NO')
INF = 10 ** 19
MOD = 10 ** 9 + 7
for _ in range(INT()):
	N = INT()
	S = list(map(int, input()))
	T = list(map(int, input()))
	ans = []
	r = N
	for i in range(N):
		if S[0] == T[-1]:
			if len(S) == 1:
				break
			ans.append(1)
			S[0] ^= 1
		ans.append(r)
		for i in range(len(S)):
			S[i] ^= 1
		S = S[::-1]
		S.pop()
		T.pop()
		r -= 1
	ans.insert(0, len(ans))
	print(*ans)


__MULTITEST = True

def flip(arr, pref):
	sav = list(arr[:pref])
	for i in range(pref):
		arr[i] = sav[pref - i - 1] ^ 1

def solve():
	n = int(input())
	a = [int(d) for d in str(input())]
	b = [int(d) for d in str(input())]
	op = []
	p = n - 1
	while p >= 0:
		if a[0] == b[p]:
			op.append(1)
			a[0] ^= 1
		else:
			op.append(p + 1)
			flip(a, p + 1)
			p -= 1
	print(len(op), end=' ')
	for x in op:
		print(x, end=' ')
	print('')
t = int(input()) if __MULTITEST else 1
for tt in range(t):
	solve()


def do():
	n = int(input())
	l = input()
	goal = input()
	dat = []
	l = [1 if x == '1' else 0 for x in l]
	import math
	for i in range(math.ceil(len(l) // 2)):
		dat.append(l[i])
		if l[len(l) - 1 - i] == 0:
			dat.append(1)
		else:
			dat.append(0)
	if len(l) & 1 == 1:
		dat.append(l[len(l) // 2])
	res = []
	for i in range(n):
		tb = int(goal[n - 1 - i])
		if tb == dat[i]:
			res.append(1)
		res.append(n - i)
	if len(res) == 0:
		print(0)
	else:
		resstr = ' '.join(list(map(str, res)))
		print('{0} {1}'.format(len(res), resstr))
q = int(input())
for _ in range(q):
	do()


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input()))
	b = list(map(int, input()))
	ans = []
	f = 0
	(l, r) = (0, n - 1)
	i = n - 1
	while l <= r:
		if f == 0:
			if a[r] ^ f != b[i]:
				if a[l] ^ f == b[i]:
					ans.append(1)
				ans.append(r - l + 1)
				f ^= 1
				l += 1
			else:
				r -= 1
		elif a[l] ^ f != b[i]:
			if a[r] ^ f == b[i]:
				ans.append(1)
			ans.append(r - l + 1)
			f ^= 1
			r -= 1
		else:
			l += 1
		i -= 1
	print(len(ans), *ans)


import sys, math
from collections import deque, defaultdict
import operator as op
from functools import reduce
I = sys.stdin.readline

def ii():
	return int(I().strip())

def li():
	return list(map(int, I().strip().split()))

def mi():
	return map(int, I().strip().split())

def ncr(n, r, p):
	num = den = 1
	for i in range(r):
		num = num * (n - i) % p
		den = den * (i + 1) % p
	return num * pow(den, p - 2, p) % p

def gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x

def valid(row, col, rows, cols, rcross, lcross):
	return rows[row] == 0 and cols[col] == 0 and (rcross[col + row] == 0) and (lcross[col - row] == 0)

def div(n):
	tmp = []
	for i in range(2, int(n ** 0.5) + 1):
		if n % i == 0:
			cnt = 0
			while n % i == 0:
				n = n // i
				cnt += 1
			tmp.append((i, cnt))
	if n > 1:
		tmp.append((n, 1))
	return tmp

def isPrime(n):
	if n <= 1:
		return False
	elif n <= 2:
		return True
	else:
		flag = True
		for i in range(2, int(n ** 0.5) + 1):
			if n % i == 0:
				flag = False
				break
		return flag

def s(b):
	d = defaultdict(int)
	while b > 0:
		tmp = b % 10
		if d[tmp] > 0:
			return False
		d[tmp] = 1
		b = b // 10
	return True

def main():
	for _ in range(ii()):
		n = ii()
		s = I().strip()
		s2 = I().strip()
		ans = []
		for i in range(1, n):
			if s[i] != s[i - 1]:
				ans.append(i)
		if s[n - 1] == '1':
			ans.append(n)
		tmp = []
		for i in range(1, n):
			if s2[i] != s2[i - 1]:
				tmp.append(i)
		if s2[n - 1] == '1':
			tmp.append(n)
		ans += tmp[::-1]
		ans = [len(ans)] + ans
		print(*ans)
main()


t = int(input())
for _ in range(t):
	n = int(input())
	l1 = [int(x) for x in list(input())]
	l2 = [int(x) for x in list(input())]
	if l1 == l2:
		print(0)
	else:
		init = l1[0]
		j = 0
		c = []
		while j < n:
			while j < n and l1[j] == init:
				j += 1
			if j <= n:
				c.append(j)
			init = [0, 1][[1, 0].index(init)]
			visited = 0
			while j < n and l1[j] == init:
				j += 1
		j = 0
		d = []
		if init != l2[-1]:
			doop = 1
		else:
			doop = 0
		while j < n:
			while j < n and l2[j] == init:
				j += 1
			if j > 0 and j < n:
				d.append(j)
			init = [0, 1][[1, 0].index(init)]
			visited = 0
			while j < n and l2[j] == init:
				j += 1
		if doop:
			d.append(n)
		d.reverse()
		c += d
		print(len(c), *c)


t = int(input())
for _ in range(t):
	n = int(input())
	a = input()
	b = input()
	q = []
	for i in range(n):
		if a[i] != b[i]:
			if i == 0:
				q.append(1)
			else:
				q.append(i + 1)
				q.append(1)
				q.append(i + 1)
	print(len(q), *q)


from os import path
import sys
from heapq import heappush, heappop
from functools import cmp_to_key as ctk
from collections import deque, defaultdict as dd
from bisect import bisect, bisect_left, bisect_right, insort, insort_left, insort_right
from itertools import permutations
from datetime import datetime
from math import ceil, sqrt, log, gcd

def ii():
	return int(input())

def si():
	return input().rstrip()

def mi():
	return map(int, input().split())

def li():
	return list(mi())
abc = 'abcdefghijklmnopqrstuvwxyz'
mod = 1000000007
inf = float('inf')
vow = ['a', 'e', 'i', 'o', 'u']
(dx, dy) = ([-1, 1, 0, 0], [0, 0, 1, -1])

def bo(i):
	return ord(i) - ord('a')
file = 1

def solve():
	for _ in range(ii()):
		n = ii()
		a = si()
		b = si()
		a += '0'
		b += '0'
		ans = []
		for i in range(n):
			if a[i] != a[i + 1]:
				ans.append(i + 1)
		for i in range(n - 1, -1, -1):
			if b[i] != b[i + 1]:
				ans.append(i + 1)
		print(len(ans), end=' ')
		print(*ans)
if file:
	if path.exists('input.txt'):
		sys.stdin = open('input.txt', 'r')
		sys.stdout = open('output.txt', 'w')
	else:
		input = sys.stdin.readline
solve()


t = int(input())
for k in range(t):
	n = int(input())
	a = input()
	b = input()
	li = []
	cnt = 0
	for i in range(n - 1, -1, -1):
		if a[i] == b[i]:
			cnt += 1
		elif b[i] != a[0]:
			li.append(i + 1)
			c = ''
			for j in range(i, -1, -1):
				if a[j] == '1':
					c += '0'
				else:
					c += '1'
			c += a[i + 1:n]
			a = c
		else:
			c = ''
			li.append(1)
			if a[0] == '0':
				c += '1'
			else:
				c += '0'
			c += a[1:n]
			a = c
			li.append(i + 1)
			c = ''
			for j in range(i, -1, -1):
				if a[j] == '1':
					c += '0'
				else:
					c += '1'
			c += a[i + 1:n]
			a = c
	print(len(li), end=' ')
	for i in range(0, len(li)):
		print(li[i], end=' ')
	print()


t = int(input())
for i in range(t):
	n = int(input())
	a = input()
	b = input()
	if n == 1:
		if a == b:
			print('0')
		else:
			print('1 1')
	else:
		res = 0
		c = ''
		if a[0] != b[0]:
			c += ' 1'
			res += 1
		for i in range(1, n):
			if a[i] != b[i]:
				c += ' ' + str(i + 1) + ' 1 ' + str(i + 1)
				res += 3
		print(str(res) + c)


def flip(a):
	temp = []
	i = 0
	while i < len(a):
		while i < len(a) - 1 and a[i + 1] == a[i]:
			i += 1
		if i != len(a) - 1:
			temp.append(i + 1)
			r = a[i]
		i += 1
	if a[-1] == '0':
		temp.append(len(a))
	return temp
for _ in range(int(input())):
	n = int(input())
	a = input()
	A = flip(a)
	b = input()
	if a == b:
		print(0)
	else:
		B = flip(b)
		B = B[::-1]
		k = A + B
		k.insert(0, len(k))
		print(*k)


def solve():
	n = int(input())
	a = input().strip()
	b = input().strip()
	ans = []
	for i in range(len(a)):
		if a[i] != b[i]:
			if i > 0:
				ans.append(i + 1)
			ans.append(1)
			if i > 0:
				ans.append(i + 1)
	print(len(ans))
	for i in ans:
		print(i, end=' ')
T = int(input())
for _ in range(T):
	solve()
	print('')


def reverse(n):
	if n == '0':
		return '1'
	return '0'
for _ in range(int(input())):
	piles = int(input())
	string1 = list(input())
	string2 = list(input())
	operations = []
	for i in range(piles - 1, -1, -1):
		if string1[0] == string2[i]:
			operations.append(1)
		string1[0] = reverse(string1[0])
		operations.append(i + 1)
		for j in range(len(string1)):
			string1[j] = reverse(string1[j])
		string1.reverse()
		string1.pop()
	print(len(operations), end=' ')
	print(*operations)


from math import ceil
for i in range(int(input())):
	n = int(input())
	ss = input()
	sss = input()
	s1 = []
	s2 = []
	for j in ss:
		s1.append(int(j))
	for j in sss:
		s2.append(int(j))
	ans1 = 0
	ans2 = []
	for j in range(len(s1) - 1, -1, -1):
		if s1[j] != s2[j]:
			if s1[0] == s1[j]:
				ans1 += 1
				ans2.append(j + 1)
				if (j + 1) % 2 == 1:
					s1[(j + 1) // 2] += 1
					s1[(j + 1) // 2] %= 2
					for k in range((j + 1) // 2):
						(s1[k], s1[2 * ((j + 1) // 2) - k]) = ((s1[2 * ((j + 1) // 2) - k] + 1) % 2, (s1[k] + 1) % 2)
				else:
					for k in range((j + 1) // 2):
						(s1[k], s1[2 * ((j + 1) // 2) - k - 1]) = ((s1[2 * ((j + 1) // 2) - 1 - k] + 1) % 2, (s1[k] + 1) % 2)
			else:
				ans1 += 2
				ans2.append(1)
				s1[0] = (s1[0] + 1) % 2
				ans2.append(j + 1)
				if (j + 1) % 2 == 1:
					s1[(j + 1) // 2] += 1
					s1[(j + 1) // 2] %= 2
					for k in range((j + 1) // 2):
						(s1[k], s1[2 * ((j + 1) // 2) - k]) = ((s1[2 * ((j + 1) // 2) - k] + 1) % 2, (s1[k] + 1) % 2)
				else:
					for k in range((j + 1) // 2):
						(s1[k], s1[2 * ((j + 1) // 2) - 1 - k]) = ((s1[2 * ((j + 1) // 2) - 1 - k] + 1) % 2, (s1[k] + 1) % 2)
	print(ans1, end=' ')
	print(*ans2)


import os
import sys
from io import BytesIO, IOBase
import math

def main():
	pass
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def decimalToBinary(n):
	return bin(n).replace('0b', '')

def binaryToDecimal(n):
	return int(n, 2)

def fun(n, m):
	s1 = decimalToBinary(n)
	s2 = decimalToBinary(m)
	return abs(binaryToDecimal(s1 + s2) - binaryToDecimal(s2 + s1))
for xyz in range(0, int(input())):
	n = int(input())
	s1 = input()
	s2 = input()
	s1 += '1'
	s2 += '1'
	l1 = []
	l2 = []
	for i in range(0, n):
		if s1[i] != s1[i + 1]:
			l1.append(i + 1)
		if s2[i] != s2[i + 1]:
			l2.append(i + 1)
	l2.reverse()
	print(len(l1) + len(l2), *l1, *l2)


import bisect
import decimal
from decimal import Decimal
import os
from collections import Counter
import bisect
from collections import defaultdict
import math
import random
import heapq
from math import sqrt
import sys
from functools import reduce, cmp_to_key
from collections import deque
import threading
from itertools import combinations
from io import BytesIO, IOBase
from itertools import accumulate
decimal.getcontext().prec = 46

def primeFactors(n):
	prime = set()
	while n % 2 == 0:
		prime.add(2)
		n = n // 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			prime.add(i)
			n = n // i
	if n > 2:
		prime.add(n)
	return list(prime)

def getFactors(n):
	factors = []
	i = 1
	while i <= math.sqrt(n):
		if n % i == 0:
			if n // i == i:
				factors.append(i)
			else:
				factors.append(i)
				factors.append(n // i)
		i = i + 1
	return factors

def modefiedSieve():
	mx = 10 ** 7 + 1
	sieve = [-1] * mx
	for i in range(2, mx):
		if sieve[i] == -1:
			sieve[i] = i
			for j in range(i * i, mx, i):
				if sieve[j] == -1:
					sieve[j] = i
	return sieve

def SieveOfEratosthenes(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	num = []
	for p in range(2, n + 1):
		if prime[p]:
			num.append(p)
	return num

def lcm(a, b):
	return a * b // math.gcd(a, b)

def sort_dict(key_value):
	return sorted(key_value.items(), key=lambda kv: (kv[1], kv[0]))

def list_input():
	return list(map(int, input().split()))

def num_input():
	return map(int, input().split())

def string_list():
	return list(input())

def decimalToBinary(n):
	return bin(n).replace('0b', '')

def binaryToDecimal(n):
	return int(n, 2)

def DFS(n, s, adj):
	visited = [False for i in range(n + 1)]
	stack = []
	stack.append(s)
	while len(stack):
		s = stack[-1]
		stack.pop()
		if not visited[s]:
			visited[s] = True
		for node in adj[s]:
			if not visited[node]:
				stack.append(node)

def maxSubArraySum(a, size):
	maxint = 10 ** 10
	max_so_far = -maxint - 1
	max_ending_here = 0
	for i in range(0, size):
		max_ending_here = max_ending_here + a[i]
		if max_so_far < max_ending_here:
			max_so_far = max_ending_here
		if max_ending_here < 0:
			max_ending_here = 0
	return max_so_far

def Reverse(a, end):
	start = 0
	while start < end:
		(a[start], a[end]) = (a[end], a[start])
		start += 1
		end -= 1

def solve():
	n = int(input())
	a = list(input())
	b = list(input())
	ans = []
	i = n - 1
	while i >= 0:
		if a[i] == b[i]:
			i -= 1
		elif a[0] == b[i]:
			ans.append(1)
			if a[0] == '0':
				a[0] = '1'
			else:
				a[0] = '0'
			ans.append(i + 1)
			for j in range(i + 1):
				if a[j] == '0':
					a[j] = '1'
				else:
					a[j] = '0'
			Reverse(a, i)
			i -= 1
		else:
			ans.append(i + 1)
			for j in range(i + 1):
				if a[j] == '0':
					a[j] = '1'
				else:
					a[j] = '0'
			Reverse(a, i)
			i -= 1
	print(len(ans), *ans)
t = 1
t = int(input())
for _ in range(t):
	solve()


from sys import stdin
from bisect import bisect_left as bl
from bisect import bisect_right as br

def input():
	return stdin.readline()[:-1]

def intput():
	return int(input())

def sinput():
	return input().split()

def intsput():
	return map(int, sinput())

class RangedList:

	def __init__(self, start, stop, val=0):
		self.shift = 0 - start
		self.start = start
		self.stop = stop
		self.list = [val] * (stop - start)

	def __setitem__(self, key, value):
		self.list[key + self.shift] = value

	def __getitem__(self, key):
		return self.list[key + self.shift]

	def __repr__(self):
		return str(self.list)

	def __iter__(self):
		return iter(self.list)

def dprint(*args, **kwargs):
	if debugging:
		print(*args, **kwargs)
debugging = 1
t = intput()
for _ in range(t):
	n = intput()
	a = list(map(int, input()))
	b = list(map(int, input()))
	operations = []
	for i in range(len(a) - 1, -1, -1):
		l = i + 1
		if a[i] != b[i]:
			if a[0] != b[i]:
				pass
			else:
				a[0] = (a[0] + 1) % 2
				operations.append(1)
			operations.append(l)
			a = a[:l][::-1] + a[l:]
			a = [int(not bool(k)) for k in a]
	print(len(operations), *operations)


import sys
input = lambda : sys.stdin.readline().rstrip('\r\n')
Read = lambda : list(map(int, input().split()))
Num = lambda : int(input())
exec("try:sys.stdin=open('in.txt','r');sys.stdout=open('out.txt','w')\nexcept:pass")

def solve():
	n = Read()[0]
	a = input()
	b = input()
	ans = []
	for i in range(n - 1):
		if a[i] != a[i + 1]:
			ans.append(i + 1)
	if a[-1] != b[-1]:
		ans.append(n)
	ans2 = []
	for i in range(n - 1):
		if b[i] != b[i + 1]:
			ans2.append(i + 1)
	ans = ans + ans2[::-1]
	print(len(ans), *ans)
for _ in range(Num()):
	solve()


t = int(input())
for _ in range(t):
	n = int(input())
	a = input()
	b = input()
	output = ''
	changes = 0
	for i in range(n):
		if a[i] != b[i]:
			output += ' ' + str(i + 1) + ' 1 ' + str(i + 1)
			changes += 3
	print(str(changes) + output)


from sys import stdin, stdout
t = int(stdin.readline())
for _ in range(t):
	n = int(stdin.readline())
	a = stdin.readline().strip()
	b = stdin.readline().strip()
	ans = []
	idx = 0
	flip = False
	for i in range(n - 1, -1, -1):
		if not flip and a[idx] == b[i] or (flip and a[idx] != b[i]):
			ans.append(1)
		ans.append(i + 1)
		if flip:
			idx -= i
		else:
			idx += i
		flip = not flip
	stdout.write(str(len(ans)) + ' ')
	if len(ans) > 0:
		stdout.write(' '.join(map(str, ans)) + '\n')


for i in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	d = []
	for c in range(1, n + 1):
		if a == b:
			print(len(d), *d)
			break
		if int(a[-c]) == int(b[-c]):
			continue
		else:
			if int(a[0]) == int(b[-c]):
				d.append(1)
				if int(a[0]) == 0:
					a = list(a)
					a[0] = '1'
					a = ''.join(a)
				else:
					a = list(a)
					a[0] = '0'
					a = ''.join(a)
			if a == b:
				print(len(d), *d)
				break
			d.append(n - c + 1)
			x = a[:n - c + 1].translate({48: 49, 49: 48})
			if len(x) == n:
				a = x[::-1]
			else:
				a = x[::-1] + a[-(c - 1):]
			if a == b:
				print(len(d), *d)
				break


from sys import stdout
from sys import stdin

def get():
	return stdin.readline().strip()

def getf():
	return [int(i) for i in get().split()]

def put(a, end='\n'):
	stdout.write(str(a) + end)

def putf(a, sep=' ', end='\n'):
	stdout.write(sep.join(map(str, a)) + end)

def pin():
	res = ''
	a = stdin.read(1)
	while a not in {' ', '\n'}:
		res += a
		a = stdin.read(1)
	return res

def do(s, x):
	for i in range(x + 1):
		s[i] = (s[i] + 1) % 2
	s = s[:i + 1][::-1] + s[i + 1:]
	return s

def solve(n, a, b):
	ans = [0]
	for i in range(n - 1, -1, -1):
		if a[i] != b[i]:
			if a[0] != b[i]:
				a = do(a, i)
				ans += [i + 1]
				ans[0] += 1
			else:
				a[0] = (a[0] + 1) % 2
				a = do(a, i)
				ans += [1, i + 1]
				ans[0] += 2
	return ans

def main():
	t = int(pin())
	for i in range(t):
		n = int(pin())
		a = [int(i) for i in pin()]
		b = [int(i) for i in pin()]
		putf(solve(n, a, b))
main()


def solve(ls1, ls2, debug=0):
	n = len(ls1)
	result = []
	for i in range(n):
		if i % 2:
			j = -1 - i // 2
			x = ls1[j] ^ 1
		else:
			j = i // 2
			x = ls1[j]
		y = ls2[-i - 1]
		if debug:
			print(f'i: {i}, j: {j}, x: {x}, y: {y}')
		if x == y:
			result.append(1)
		result.append(n - i)
	return result

def main(istr, ostr):
	t = int(istr.readline())
	for _ in range(t):
		n = int(istr.readline())
		ls1 = list(map(int, istr.readline().strip()))
		ls2 = list(map(int, istr.readline().strip()))
		result = solve(ls1, ls2)
		print(len(result), *result, file=ostr)
import sys
main(sys.stdin, sys.stdout)


import sys, os.path
from collections import deque
from fractions import Fraction as f

def IO():
	if os.path.exists('input.txt'):
		sys.stdin = open('input.txt', 'r')
		sys.stdout = open('output.txt', 'w')
	else:
		input = sys.stdin.readline
		print = sys.stdout.write

def nextInt():
	return int(input())

def nextTuple():
	return [int(a) for a in input().split()]

def nextArray():
	return list(map(int, input().split()))

def nextLine():
	return input()

def nextStringArray():
	return list(input().split())
IO()
t = nextInt()
for _ in range(t):
	sb = []
	n = nextInt()
	a = list(nextLine())
	b = list(nextLine())
	ptr = n - 1
	for i in range(n):
		if b[ptr] == a[0]:
			sb.append('1')
		if True:
			sb.append(str(ptr + 1))
			for j in range(ptr + 1):
				if a[j] == '0':
					a[j] = '1'
				else:
					a[j] = '0'
		a = a[0:ptr + 1][::-1] + a[ptr + 1:]
		ptr -= 1
	print(len(sb), end=' ')
	print(' '.join(sb), end='')
	print()


def solve(a, b):
	ans = []
	for i in range(len(a) - 1):
		if a[i] != a[i + 1]:
			ans.append(i + 1)
	curr = a[-1]
	for i in range(len(a) - 1, -1, -1):
		if curr != b[i]:
			ans.append(i + 1)
			curr = not curr
	print(len(ans), ' '.join(map(str, ans)))

def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		a = input()
		b = input()
		a = [i == '1' for i in a]
		b = [i == '1' for i in b]
		solve(a, b)
main()


import heapq
from collections import deque
from collections import defaultdict
import sys
sys.setrecursionlimit(100000)

def rec(a, b):
	n = len(a)
	ans = []
	for ptr in range(n - 1, -1, -1):
		if a[ptr] == b[ptr]:
			continue
		elif a[0] != b[ptr]:
			ans.append(ptr + 1)
			a = check(a, ptr + 1)
		else:
			a = check(a, 1)
			ans.append(1)
			a = check(a, ptr + 1)
			ans.append(ptr + 1)
	return ans

def check(a, m):
	dct = {'1': '0', '0': '1'}
	return ''.join(list(map(lambda x: dct[x], list(a[:m])))[::-1]) + a[m:]
I = lambda : list(map(int, input().split(' ')))
for _ in range(int(input())):
	n = I()[0]
	a = input()
	b = input()
	ans = rec(a, b)
	k = str(len(ans))
	print(k + ' ' + ' '.join(map(str, ans)))


from collections import Counter
import string
import math
import sys
from fractions import Fraction

def array_int():
	return [int(i) for i in sys.stdin.readline().split()]

def vary(arrber_of_variables):
	if arrber_of_variables == 1:
		return int(sys.stdin.readline())
	if arrber_of_variables >= 2:
		return map(int, sys.stdin.readline().split())

def makedict(var):
	return dict(Counter(var))
testcases = vary(1)
for _ in range(testcases):
	n = vary(1)
	a = list(input())
	b = list(input())
	ans = 0
	lengths = []
	for i in range(n):
		if a[n - 1 - i] != b[n - 1 - i]:
			if a[0] != b[n - 1 - i]:
				ans += 1
				lengths.append(n - i)
				for j in range((n - i) // 2):
					(a[j], a[n - 1 - i - j]) = (a[n - 1 - i - j], a[j])
				for j in range(n - i):
					a[j] = str(int(a[j]) ^ 1)
			else:
				ans += 1
				lengths.append(1)
				a[0] = str(int(a[0]) ^ 1)
				ans += 1
				lengths.append(n - i)
				for j in range((n - i) // 2):
					(a[j], a[n - 1 - i - j]) = (a[n - 1 - i - j], a[j])
				for j in range(n - i):
					a[j] = str(int(a[j]) ^ 1)
	print(ans, *lengths)


t = int(input())
for i in range(t):
	n = int(input())
	a = input()
	b = input()
	d = []
	for j in range(n):
		if a[j] != b[j]:
			d += [j + 1, 1, j + 1]
	print(len(d), *d)


from sys import stdin

def inp():
	return stdin.buffer.readline().rstrip().decode('utf8')

def itg():
	return int(stdin.buffer.readline())

def mpint():
	return map(int, stdin.buffer.readline().split())
from collections import deque
for case in range(itg()):
	n = itg()
	a = deque(inp())
	b = inp()
	idx = n - 1
	ans = []
	rev = False
	while idx != -1:
		if rev:
			begin = -1
			end = 0
		else:
			begin = 0
			end = -1
		if (a[end] == b[idx]) ^ rev:
			idx -= 1
			if rev:
				a.popleft()
			else:
				a.pop()
			continue
		if (a[begin] == b[idx]) ^ rev:
			ans.append(1)
		ans.append(idx + 1)
		if rev:
			a.pop()
		else:
			a.popleft()
		rev = not rev
		idx -= 1
	print(len(ans), *ans)


def solve(n, a, b):
	l = []
	h = a.copy()
	for i in range(n - 1, -1, -1):
		if a[i] != b[i] and i != 0:
			if a.count(b[i]) == 0:
				l.append(i)
				for j in range(i + 1):
					a[j] = str((int(a[j]) + 1) % 2)
				continue
			if a == b:
				return l
			x = a.index(str((int(a[i]) + 1) % 2))
			if x > i:
				l.append(i)
				for j in range(i + 1):
					a[j] = str((int(a[j]) + 1) % 2)
				continue
			l.append(x)
			for j in range(x + 1):
				h[j] = str((int(a[x - j]) + 1) % 2)
			a = h.copy()
			l.append(i)
			for j in range(i + 1):
				h[j] = str((int(a[i - j]) + 1) % 2)
			a = h.copy()
		if i == 0:
			if a[i] != b[i]:
				l.append(0)
	return l
t = int(input())
for we in range(t):
	n = int(input())
	a = list(input())
	b = list(input())
	l = solve(n, a, b)
	for i in range(len(l)):
		l[i] += 1
	print(len(l), *l)


for _ in range(int(input())):
	n = int(input())
	num1 = input()
	num2 = input()
	count = 0
	l = []
	li = []
	if n == 1:
		if num1 != num2:
			count += 1
			l.append(1)
	elif num1 == num2:
		pass
	else:
		for i in range(n - 1):
			if num1[i] == num1[i + 1]:
				pass
			else:
				count += 1
				l.append(i + 1)
			if num2[i] == num2[i + 1]:
				pass
			else:
				count += 1
				li.append(i + 1)
		if num1[-1] == '1':
			count += 1
			l.append(n)
		if num2[-1] == '1':
			count += 1
			li.append(n)
	print(count, *l, *li[::-1])


from sys import stdin, stdout
stdin.readline

def mp():
	return list(map(int, stdin.readline().strip().split()))

def it():
	return int(stdin.readline().strip())
from collections import defaultdict as dd, Counter as C, deque
from math import ceil, gcd
for _ in range(it()):
	n = it()
	a = list(input())
	b = list(input())
	v = []
	(j, k) = (n - 1, n - 1)
	t = 0
	while j >= 0:
		if b[k] == a[j]:
			j -= 1
			k -= 1
		else:
			if k != 0:
				v.append(k + 1)
				v.append(1)
				v.append(k + 1)
			else:
				v.append(1)
			j -= 1
			k -= 1
	if v:
		print(len(v), *v, end=' ')
		print()
	else:
		print(0)


for _ in range(int(input())):
	n = int(input())
	a = list(input())
	b = list(input())
	order = []
	i = 0
	current = a[0]
	while i < n:
		if a[i] != current:
			order.append(i)
			current = a[i]
		i += 1
	if a[-1] == '1':
		order.append(n)
	i = n - 1
	current = b[-1]
	if current == '1':
		order.append(n)
	while i >= 0:
		if b[i] != current:
			order.append(i + 1)
			current = b[i]
		i -= 1
	print(len(order), *order)


def func(a, n):
	for i in range(n):
		a[i] = 1 - a[i]
	n1 = int(n / 2)
	for i in range(n1):
		t = a[i]
		a[i] = a[n - 1 - i]
		a[n - 1 - i] = t
y = int(input())
for i in range(y):
	n = int(input())
	a = list(map(int, list(input())))
	b = list(map(int, list(input())))
	ans = []
	for j in range(1, n):
		if a[-j] != b[-j]:
			if a[0] != b[-j]:
				func(a, n - j + 1)
				ans.append(n - j + 1)
			else:
				func(a, 1)
				ans.append(1)
				func(a, n - j + 1)
				ans.append(n - j + 1)
	if a[0] != b[0]:
		ans.append(1)
	print(len(ans), ' ', end='', sep='')
	for j in ans:
		print(j, ' ', sep='', end='')
	print()


t = int(input())
for x in range(t):
	n = int(input())
	a = input()
	b = input()
	k = 0
	K = []
	for y in range(n):
		if b[n - y - 1] == a[0]:
			k += 1
			K += [1]
			a = str(int(a[0]) ^ 1) + a[1:]
		k += 1
		K += [n - y]
		c = a
		a = ''
		for z in range(n - y - 1, -1, -1):
			a += str(int(c[z]) ^ 1)
	print(k, end=' ')
	for y in K:
		print(y, end=' ')
	print()


list_input = lambda : list(map(int, input().split()))
int_input = lambda : int(input())

def count_vals(l):
	d = {}
	for i in l:
		d[i] = d.get(i, 0) + 1
	return d

def bit_inv(b):
	if b == '1':
		return '0'
	else:
		return '1'

def opp(s, n):
	for i in range((n + 1) // 2):
		(s[i], s[n - i]) = (bit_inv(s[n - i]), bit_inv(s[i]))
	if (n + 1) % 2:
		s[(n + 1) // 2] = bit_inv(s[(n + 1) // 2])
	return s

def solve(a, b, n):
	l = []
	for i in range(n - 1, 0, -1):
		x = a[0]
		if x == b[i]:
			a[0] = bit_inv(a[0])
			l.append(1)
			l.append(i + 1)
		else:
			l.append(i + 1)
		a = opp(a, i)
	if a[0] != b[0]:
		l.append(1)
		a[0] = bit_inv(a[0])
	return l
for _ in range(int(input())):
	n = int_input()
	a = list(input())
	b = list(input())
	l = solve(a, b, n)
	print(len(l), end=' ')
	print(*l)


import sys
input = sys.stdin.readline

def print(val):
	sys.stdout.write(str(val) + ' ')

def prog():
	for _ in range(int(input())):
		n = int(input())
		a = input().strip() + '0'
		b = input().strip() + '0'
		if a == b:
			print(0)
			sys.stdout.write('\n')
		else:
			loca = []
			locb = []
			start = False
			last = 0
			for i in range(n + 1):
				if a[i] == '1':
					if start == False:
						start = True
						last = i
				elif start:
					start = False
					loca.append([last, i - 1])
			start = False
			last = 0
			for i in range(n + 1):
				if b[i] == '1':
					if start == False:
						start = True
						last = i
				elif start:
					start = False
					locb.append([last, i - 1])
			locb = locb[::-1]
			amount = 2 * len(loca) + 3 * len(locb)
			if len(loca):
				amount -= loca[0][0] == 0
			print(amount)
			for (L, R) in loca:
				if L > 0:
					print(L)
				print(R + 1)
			for (L, R) in locb:
				print(R + 1)
				print(R - L + 1)
				print(R + 1)
			sys.stdout.write('\n')
prog()


t = int(input())
for _ in range(t):
	n = int(input())
	a = input()
	b = input()
	res = ''
	cnts = 0
	for i in range(n):
		if a[i] != b[i]:
			res += ' {} {} {}'.format(i + 1, 1, i + 1)
			cnts += 3
	print(str(cnts) + res)


for _ in range(int(input())):
	N = int(input())
	A = input()
	A += '0'
	B = input()
	B += '0'
	B = B[::-1]
	Ans = []
	for i in range(N):
		if A[i] != A[i + 1]:
			Ans.append(i + 1)
	for j in range(N):
		if B[j] != B[j + 1]:
			Ans.append(N - j)
	print(len(Ans), *Ans)


cases = int(input())
for t in range(cases):
	n = int(input())
	a = input()
	b = input()
	s = 0
	out = []
	temp = []
	for i in range(n - 1):
		if a[i] != a[i + 1]:
			out.append(i + 1)
			s += 1
		if b[i] != b[i + 1]:
			temp.append(i + 1)
			s += 1
	if a[-1] == '1':
		out.append(n)
		s += 1
	if b[-1] == '1':
		temp.append(n)
		s += 1
	print(s, *out + temp[::-1])


t = int(input())

def operation(x):
	res = []
	for i in range(len(x)):
		if x[i] == '0':
			res.append('1')
		else:
			res.append('0')
	return res[::-1]
for i in range(t):
	n = int(input())
	a = list(input())
	b = list(input())
	res = ''
	kol = 0
	for x in range(n - 1, 0, -1):
		if a[x] != b[x]:
			if a[0] != b[x]:
				a[:x + 1] = operation(a[:x + 1])
				res = ' '.join([res, str(x + 1)])
				kol += 1
			else:
				a[0:1] = operation(a[0:1])
				a[:x + 1] = operation(a[:x + 1])
				res = ' '.join([res, str(1), str(x + 1)])
				kol += 2
	if a[0] != b[0]:
		res = ' '.join([res, str(1)])
		kol += 1
	print(''.join([str(kol), res]))


from __future__ import division, print_function
import sys
from atexit import register
if sys.version_info[0] < 3:
	from io import BytesIO as stream
else:
	from io import StringIO as stream
sys.stdin = stream(sys.stdin.read())
input = lambda : sys.stdin.readline().rstrip('\r\n')
sys.stdout = stream()
register(lambda : sys.__stdout__.write(sys.stdout.getvalue()))

def read_int():
	return int(input())

def read_ints():
	return list(map(int, input().split(' ')))

def flip(a):
	return 0 if a == 1 else 1

def solve():
	n = read_int()
	a = list(map(int, input()))
	b = list(map(int, input()))
	end = n - 1
	operations = []
	while end > -1:
		if a[end] != b[end]:
			if a[0] == a[end]:
				flipped = list(map(flip, a[:end + 1]))
				flipped.reverse()
				a[:end + 1] = flipped
				operations.append(end + 1)
			else:
				a[0] = flip(a[0])
				operations.append(1)
				flipped = list(map(flip, a[:end + 1]))
				flipped.reverse()
				a[:end + 1] = flipped
				operations.append(end + 1)
			a[end] = b[end]
		end -= 1
	print(len(operations), ' '.join(map(str, operations)))
T = read_int()
for _ in range(T):
	solve()


import os, io
from sys import stdout
import collections
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def primes(n):
	sieve = [True] * n
	for i in range(3, int(n ** 0.5) + 1, 2):
		if sieve[i]:
			sieve[i * i::2 * i] = [False] * ((n - i * i - 1) // (2 * i) + 1)
	return [2] + [i for i in range(3, n, 2) if sieve[i]]

def binomial_coefficient(n, k):
	if 0 <= k <= n:
		ntok = 1
		ktok = 1
		for t in range(1, min(k, n - k) + 1):
			ntok *= n
			ktok *= t
			n -= 1
		return ntok // ktok
	else:
		return 0

def powerOfK(k, max):
	if k == 1:
		return [1]
	if k == -1:
		return [-1, 1]
	result = []
	n = 1
	while n <= max:
		result.append(n)
		n *= k
	return result

def divisors(n):
	i = 1
	result = []
	while i * i <= n:
		if n % i == 0:
			if n / i == i:
				result.append(i)
			else:
				result.append(i)
				result.append(n / i)
		i += 1
	return result

def distance(xa, ya, xb, yb):
	return ((ya - yb) ** 2 + (xa - xb) ** 2) ** 0.5

def digitsSum(n):
	if n == 0:
		return 0
	r = 0
	while n > 0:
		r += n % 10
		n //= 10
	return r

def isValid(i, j, n, m):
	return i >= 0 and i < n and (j >= 0) and (j < m)

def print_grid(grid):
	for line in grid:
		print(' '.join(map(str, line)))

def kadane(a, size):
	max_so_far = 0
	max_ending_here = 0
	for i in range(0, size):
		max_ending_here = max_ending_here + a[i]
		if max_so_far < max_ending_here:
			max_so_far = max_ending_here
		if max_ending_here < 0:
			max_ending_here = 0
	return max_so_far

def prefixSum(arr):
	for i in range(1, len(arr)):
		arr[i] = arr[i] + arr[i - 1]
	return arr

def ceil(n, d):
	if n % d == 0:
		return n // d
	else:
		return n // d + 1

def _abs(a, b):
	return int(abs(a - b))

def evenodd(l):
	even = [e for e in l if e & 1 == 0]
	odd = [e for e in l if e & 1]
	return (even, odd)

def op(arr):
	for i in range(len(arr)):
		if arr[i] == 1:
			arr[i] = 0
		else:
			arr[i] = 1
	r = arr[::-1]
	return r
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, list(input().decode('utf-8').strip())))
	b = list(map(int, list(input().decode('utf-8').strip())))
	fr = []
	for e in b[::-1]:
		if e == 1:
			fr.append(0)
		else:
			fr.append(1)
	i = len(a) - 1
	operations = []
	while i >= 0:
		if a[i] == b[i]:
			i -= 1
			continue
		if a[0] != a[i]:
			if a[0] == 1:
				a[0] = 0
			else:
				a[0] = 1
			operations.append(1)
		na = op(a[:i + 1]) + a[i + 1:]
		operations.append(i + 1)
		i -= 1
		a = na
	print(len(operations), ' '.join(list(map(str, operations))))


from sys import stdin, stdout
import math
from itertools import permutations, combinations
from collections import defaultdict
from collections import Counter
from bisect import bisect_left
import sys
from queue import PriorityQueue
import operator as op
from functools import reduce
mod = 1000000007

def L():
	return list(map(int, stdin.readline().split()))

def In():
	return map(int, stdin.readline().split())

def I():
	return int(stdin.readline())

def printIn(ob):
	return stdout.write(str(ob) + '\n')

def powerLL(n, p):
	result = 1
	while p:
		if p & 1:
			result = result * n % mod
		p = int(p / 2)
		n = n * n % mod
	return result

def ncr(n, r):
	r = min(r, n - r)
	numer = reduce(op.mul, range(n, n - r, -1), 1)
	denom = reduce(op.mul, range(1, r + 1), 1)
	return numer // denom

def myCode():
	n = int(input())
	a = input()
	b = input()
	wyniki = []
	for x in range(n):
		if a[x] != b[x]:
			wyniki += [x + 1, 1, x + 1]
	print(len(wyniki), *wyniki)

def main():
	for t in range(I()):
		myCode()
main()


f = lambda : map(int, input().split())

def inv(p):
	for i in range(len(p)):
		if p[i] == '1':
			p[i] = '0'
		elif p[i] == '0':
			p[i] = '1'
	return list(reversed(p))
for _ in range(int(input())):
	n = int(input())
	s = list((x for x in input()))
	a = list((x for x in input()))
	l = list()
	count = 0
	while s != a:
		count += 1
		if s[0] != a[-1]:
			s = inv(s)
			l.append(len(s))
			s = s[:-1]
			a = a[:-1]
		else:
			if s[0] == '1':
				s[0] = '0'
			else:
				s[0] = '1'
			l.append(1)
	print(count, *l)


t = int(input())
while t:
	t += -1
	n = int(input())
	s1 = list(input())
	s2 = list(input())
	ans = []
	ch = '1'
	if s1[0] == '1':
		ch = '0'
	for i in range(1, n):
		if s1[i] == ch:
			ans.append(i)
			if ch == '1':
				ch = '0'
			else:
				ch = '1'
	if s1[n - 1] == '1':
		ans.append(n)
	ch = '1'
	for i in range(n - 1, -1, -1):
		if s2[i] == ch:
			ans.append(i + 1)
			if ch == '1':
				ch = '0'
			else:
				ch = '1'
	print(len(ans), *ans)


for _ in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	ans = []
	f = [0] * n
	g = [0] * n
	k1 = k2 = 0
	for i in range(1, n):
		if a[i] != a[i - 1]:
			f[k1] = i
			k1 += 1
	for i in range(1, n):
		if b[i] != b[i - 1]:
			g[k2] = i
			k2 += 1
	if a[n - 1] != b[n - 1]:
		f[k1] = n
		k1 += 1
	print(k1 + k2, end=' ')
	for i in range(k1):
		print(f[i], end=' ')
	for i in range(k2 - 1, -1, -1):
		print(g[i], end=' ')
	print('')


from sys import stdin, stdout
from sys import maxsize
input = stdin.readline

def solve():
	pass

def update(a, i):
	for j in range(i + 1):
		a[j] = 1 - a[j]
	for j in range(i // 2 + 1):
		(a[j], a[i - j]) = (a[i - j], a[j])
test = 1
test = int(input().strip())
for t in range(0, test):
	n = int(input().strip())
	s1 = list(input().strip())
	s2 = list(input().strip())
	a = [int(x) for x in s1]
	b = [int(x) for x in s2]
	ans = []
	temp = a[0]
	for i in range(n - 1, -1, -1):
		if a[i] != b[i]:
			if temp != b[i]:
				ans.append(i + 1)
				update(a, i)
				temp = b[i]
			else:
				ans.append(1)
				a[0] = 1 - a[0]
				ans.append(i + 1)
				update(a, i)
				temp = b[i]
	print(len(ans), end=' ')

	def onedarr(a):
		for el in a:
			print(el, end=' ')
		print()
	onedarr(ans)
	ans = solve()


t = int(input().split()[0])
for case in range(t):
	n = int(input().split()[0])
	a = [int(x) for x in input().split()[0]]
	b = [int(x) for x in input().split()[0]]
	l = 0
	r = n - 1
	last = n - 1
	while r > -1:
		if a[r] == b[r]:
			r -= 1
			last -= 1
		else:
			break
	ans = []
	flips = 0
	ltor = True
	while l - 1 < r:
		if ltor:
			if (a[l] + flips) % 2 == b[last]:
				ans.append(1)
				ans.append(last + 1)
			else:
				ans.append(last + 1)
			flips += 1
			l += 1
			last -= 1
			while l < n and (a[l] + flips) % 2 == b[last]:
				l += 1
				last -= 1
		else:
			if (a[r] + flips) % 2 == b[last]:
				ans.append(1)
				ans.append(last + 1)
			else:
				ans.append(last + 1)
			flips += 1
			r -= 1
			last -= 1
			while r > -1 and (a[r] + flips) % 2 == b[last]:
				r -= 1
				last -= 1
		ltor = not ltor
	print(str(len(ans)) + ' ', end='')
	print(*ans)


import sys
import math
import bisect
from sys import stdin, stdout
from math import gcd, floor, sqrt, log
from collections import defaultdict
from bisect import bisect_left as bl, bisect_right as br
from collections import Counter
ip = lambda : int(stdin.readline())
inp = lambda : map(int, stdin.readline().split())
t = ip()
for _ in range(t):
	n = ip()
	a = input()
	b = input()
	a = list(a)
	b = list(b)
	ans = []
	for i in range(n - 1, -1, -1):
		if a[i] == b[i]:
			pass
		elif a[0] == b[i]:
			if i == 0:
				ans.append(0)
				if a[0] == '0':
					a[0] == '1'
				else:
					a[0] == '0'
			else:
				ans.append(0)
				if a[0] == '1':
					a[0] = '0'
				else:
					a[0] = '1'
				ans.append(i)
				for j in range(i + 1):
					if a[j] == '0':
						a[j] = '1'
					else:
						a[j] = '0'
				ch = a[:i + 1]
				a = ch[::-1] + a[i + 1:]
		else:
			ans.append(i)
			for j in range(i + 1):
				if a[j] == '0':
					a[j] = '1'
				else:
					a[j] = '0'
			ch = a[:i + 1]
			a = ch[::-1] + a[i + 1:]
	ans = [i + 1 for i in ans]
	ans = [len(ans)] + ans
	print(*ans)


import math
import collections
t = int(input())
for i in range(t):
	n = int(input())
	a = input()
	b = input()
	ans = []
	for i in range(n):
		if a[i] != b[i]:
			if i > 0:
				ans.append(i + 1)
			ans.append(1)
			if i > 0:
				ans.append(i + 1)
	print(len(ans), *ans)


for _ in range(int(input())):
	n = int(input())
	a = list(str(input()))
	b = list(str(input()))
	zero = 0
	one = 0
	A = []
	B = []
	if ''.join(a) == ''.join(b):
		print(0)
	else:
		for i in range(n):
			if a[i] == '0':
				zero += 1
			elif a[i] == '1':
				one += 1
			if b[i] == '0':
				zero += 1
			elif b[i] == '1':
				one += 1
		if zero > one:
			for ii in range(n):
				if a[ii] == '1':
					if ii != 0:
						A.append(ii)
					A.append(ii + 1)
				if b[ii] == '1':
					if ii != 0:
						B.append(ii)
					B.append(ii + 1)
			B.reverse()
			A.extend(B)
			print(len(A), *A)
		else:
			for ii in range(n):
				if a[ii] == '0':
					if ii != 0:
						A.append(ii)
					A.append(ii + 1)
				if b[ii] == '0':
					if ii != 0:
						B.append(ii)
					B.append(ii + 1)
			B.reverse()
			A.extend(B)
			print(len(A), *A)


import sys
input = lambda : sys.stdin.readline().rstrip('\r\n')
for _ in range(int(input())):
	n = int(input())
	s = input()
	t = input()
	ans = [i for i in range(1, n) if s[i] != s[i - 1]]
	if s[-1] == '1':
		ans.append(n)
	c = [i for i in range(1, n) if t[i] != t[i - 1]]
	if t[-1] == '1':
		c.append(n)
	ans += c[::-1]
	print(len(ans), *ans)


t = int(input())
for _ in range(t):
	n = int(input())
	a = input()
	b = input()
	ans = []
	for i in range(n - 1, -1, -1):
		if a[i] == b[i]:
			pass
		else:
			flag = 0
			if b[i] == a[0]:
				flag = 1
				ans.append(1)
			ans.append(i + 1)
			s = ''
			for j in range(i, -1, -1):
				if j == 0 and flag:
					s += a[j]
				elif a[j] == '1':
					s += '0'
				else:
					s += '1'
			a = s
	ans = [len(ans)] + ans
	print(*ans)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(input())
	b = list(input())
	ans = [0]
	for i in range(n - 1, -1, -1):
		if a[i] != b[i]:
			if a[0] != b[i]:
				ans[0] += 1
				temp = a[:i + 1]
				for j in range(i + 1):
					if a[j] == '1':
						temp[j] = '0'
					else:
						temp[j] = '1'
				for j in range(i + 1):
					a[j] = temp[i - j]
				ans.append(i + 1)
			else:
				ans[0] += 2
				j = 0
				if a[j] == '1':
					a[j] = '0'
				else:
					a[j] = '1'
				temp = a[:i + 1]
				for j in range(i + 1):
					if a[j] == '1':
						temp[j] = '0'
					else:
						temp[j] = '1'
				for j in range(i + 1):
					a[j] = temp[i - j]
				ans.append(1)
				ans.append(i + 1)
	print(' '.join(map(str, ans)))


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, list(input())))
	b = list(map(int, list(input())))
	i = n - 1
	ans = []
	while i >= 0:
		if a[i] == b[i]:
			i -= 1
		else:
			if a[0] == b[i]:
				ans.append(1)
				a[0] ^= 1
				ans.append(i + 1)
				c = []
				if i + 1 < n:
					c = a[i + 1:]
				for i in range(i + 1):
					a[i] ^= 1
				a = a[0:i + 1][::-1] + c
			else:
				c = []
				if i + 1 < n:
					c = a[i + 1:]
				for i in range(i + 1):
					a[i] ^= 1
				a = a[0:i + 1][::-1] + c
				ans.append(i + 1)
			i -= 1
	print(len(ans), *ans)


for _ in range(int(input())):
	n = int(input())
	lis = []
	l1 = input()
	l2 = input()
	(a1, b1) = ([], [])
	for i in range(0, n, 1):
		a1.append(int(l1[i]))
		b1.append(int(l2[i]))
	for i in range(n - 1, -1, -1):
		if a1[i] == b1[i]:
			pass
		elif a1[0] == b1[i]:
			lis.append(1)
			if a1[0] == 1:
				a1[0] = 0
			else:
				a1[0] = 1
			lis.append(i + 1)
			for k in range(0, i + 1, 1):
				if a1[k] == 0:
					a1[k] = 1
				else:
					a1[k] = 0
			for j in range(0, i // 2 + 1, 1):
				(a1[j], a1[i - j]) = (a1[i - j], a1[j])
		else:
			lis.append(i + 1)
			for k in range(0, i + 1, 1):
				if a1[k] == 0:
					a1[k] = 1
				else:
					a1[k] = 0
			for j in range(0, i // 2 + 1, 1):
				(a1[j], a1[i - j]) = (a1[i - j], a1[j])
	print(len(lis), end=' ')
	print(*lis)


t = int(input())
for _ in range(t):
	n = int(input())
	a = input()
	b = input()
	c = []
	d = a[::-1]
	e = b[::-1]
	cnt = 0
	rev = 0
	upd = a[:]
	if a == b:
		print(0)
	else:
		for i in range(n):
			if d[i] != e[i] and rev % 2 == 0 or (d[i] == e[i] and rev % 2 != 0):
				if rev % 2 == 0:
					if upd[0] != e[i]:
						c.append(n - i)
					else:
						c.append(1)
						c.append(n - i)
						if upd[0] == '0':
							upd = '1' + upd[1:]
						else:
							upd = '0' + upd[1:]
				elif upd[0] == e[i]:
					c.append(n - i)
				else:
					c.append(1)
					c.append(n - i)
					if upd[0] == '0':
						upd = '1' + upd[1:]
					else:
						upd = '0' + upd[1:]
				rev += 1
				z = upd[:n - i]
				z = z[::-1]
				upd = z + e[:i]
				d = upd[::-1]
			else:
				cnt += 1
		print(len(c), *c)


import sys
import math
from functools import reduce
import bisect

def getN():
	return int(input())

def getNM():
	return map(int, input().split())

def getList():
	return list(map(int, input().split()))

def input():
	return sys.stdin.readline().rstrip()

def index(a, x):
	i = bisect.bisect_left(a, x)
	if i != len(a) and a[i] == x:
		return i
	return False
for _ in range(int(input())):
	n = getN()
	a = list(map(int, list(input())))
	b = list(map(int, list(input())))
	c = [0] * n
	for i in range(n):
		if a[i] == b[i]:
			c[i] = 1
	kth = 0
	op = []
	if a[0] != b[0]:
		a[0] = b[0]
		kth = 1
		op.append(1)
	for i in range(n):
		if a[i] != b[i]:
			kth += 3
			op.append(i + 1)
			op.append(1)
			op.append(i + 1)
	print(kth, *op)


from sys import stdin, stdout

def main():
	t = int(stdin.readline())
	for case in range(t):
		n = int(stdin.readline())
		a = stdin.readline()
		b = stdin.readline()
		k = 0
		seq = []
		flips = 0
		checks = n
		(ax, ay, d) = (0, n - 1, 1)
		by = n - 1
		while checks > 0:
			ch = a[ax]
			if flips % 2 == 1:
				if a[ax] == '1':
					ch = '0'
				else:
					ch = '1'
			if ch == b[by]:
				k += 2
				seq.append(1)
			else:
				k += 1
			ax += d
			d *= -1
			(ax, ay) = (ay, ax)
			seq.append(checks)
			flips += 1
			checks -= 1
			by -= 1
		stdout.write(str(k))
		stdout.write(' ')
		stdout.write(' '.join(map(str, seq)))
		stdout.write('\n')
	return
main()


from sys import stdin
for _ in range(int(input())):
	n = int(input())
	a = stdin.readline().rstrip()
	b = stdin.readline().rstrip()
	x = []
	if n == 1:
		if a == b:
			print(0)
		else:
			print(1, 1)
	elif a == b:
		print(0)
	else:
		current = 0
		if a[0] == '1':
			x.append(1)
		for i in range(1, n):
			if a[i] == '1':
				if current == 0:
					x.append(i)
				x.append(i + 1)
		current = 0
		for i in range(n - 1, -1, -1):
			if b[i] == '1':
				if current == 0:
					x.append(i + 1)
					current = 1
			if b[i] == '0':
				if current == 1:
					if current == 1:
						x.append(i + 1)
						current = 0
		print(len(x), *x)


from sys import stdin, stdout
import math
from itertools import permutations, combinations
from collections import defaultdict
from bisect import bisect_left
from bisect import bisect_right

def L():
	return list(map(int, stdin.readline().split()))

def In():
	return map(int, stdin.readline().split())

def I():
	return int(stdin.readline())
P = 1000000007

def main():
	for t in range(I()):
		n = I()
		lis = []
		arr1 = list(input())
		arr2 = list(input())
		for i in range(n):
			if arr1[i] != arr2[i]:
				lis.append(i + 1)
				lis.append(1)
				lis.append(i + 1)
		print(len(lis), end=' ')
		print(*lis)
main()


from collections import defaultdict

def inp():
	return map(int, input().split(' '))

def intinp():
	return int(input())

def swap(a, i):
	if a[i] == '0':
		a[i] = '1'
	else:
		a[i] = '0'

def solution():
	n = intinp()
	a = list(input())
	b = list(input())
	i = n - 1
	ans = []
	while i > -1:
		while i > -1 and a[i] == b[i]:
			i -= 1
		if i == -1:
			print(len(ans), end=' ')
			if len(ans) == 0:
				print()
				return
			else:
				print(*ans)
				return
		if a[0] == b[i]:
			ans.append(1)
			swap(a, 0)
		ans.append(i + 1)
		first = 0
		last = i
		while first < last:
			swap(a, first)
			swap(a, last)
			(a[first], a[last]) = (a[last], a[first])
			first += 1
			last -= 1
		if first == last:
			swap(a, last)
testcases = 1
testcases = int(input())
for _ in range(testcases):
	solution()


import sys
import math
from decimal import *

def I():
	return int(sys.stdin.readline())

def IL():
	return list(map(int, sys.stdin.readline().strip().split()))

def ILS():
	return list(map(str, sys.stdin.readline().strip().split()))

def solve():
	tt = I()
	for ii in range(tt):
		n = I()
		a = input()
		b = input()
		a = [int(x) for x in a]
		b = [int(x) for x in b]
		a0 = a[0]
		flip = 0
		l = 1
		r = n - 1
		i = n - 1
		j = n - 1
		ans = []
		while i >= 0:
			flag = 0
			aj = a[j]
			if flip == 1:
				if aj == 0:
					aj = 1
				else:
					aj = 0
			if j == 0:
				aj = a0
			if aj != b[i]:
				flag = 1
				flip = not flip
				if aj != a0:
					ans.append(1)
				ans.append(i + 1)
				if aj == 0:
					a0 = 1
				else:
					a0 = 0
			if j == l and j == r:
				j = 0
			elif j == r:
				r -= 1
				if flag == 1:
					j = l
				else:
					j = r
			else:
				l += 1
				if flag == 1:
					j = r
				else:
					j = l
			i -= 1
		print(len(ans), end=' ')
		for xx in ans:
			print(xx, end=' ')
		print()
solve()


import math
from sys import stdin, stdout
for _ in range(int(input())):
	n = int(input())
	s1 = list(input())
	s2 = list(input())
	ans = []
	c = 0
	for i in range(n):
		if s1[i] == s2[n - 1 - i]:
			ans.append(str(1))
			c += 1
		ans.append(str(n - i))
		ans.append(str(n - i - 1))
		c += 2
	ans.pop()
	c -= 1
	print(str(c) + ' ' + ' '.join(ans))


t = int(input())
for i in range(t):
	n = int(input())
	s = input()
	s1 = input()
	l = []
	j = 0
	while j < n:
		if s[j] == '0':
			a = 0
			while s[j] == '0':
				a += 1
				j += 1
				if j == n:
					break
			l.append(a * '0')
		else:
			a = 0
			while s[j] == '1':
				a += 1
				j += 1
				if j == n:
					break
			l.append(a * '1')
	l1 = []
	j = 0
	while j < n:
		if s1[j] == '0':
			a = 0
			while s1[j] == '0':
				a += 1
				j += 1
				if j == n:
					break
			l1.append(a * '0')
		else:
			a = 0
			while s1[j] == '1':
				a += 1
				j += 1
				if j == n:
					break
			l1.append(a * '1')
	ans = []
	a = 0
	for j in range(len(l) - 1):
		a += len(l[j])
		ans.append(a)
	if l[-1][0] != l1[-1][0]:
		ans.append(n)
	a = len(l1[-1])
	for j in range(len(l1) - 2, -1, -1):
		ans.append(n - a)
		a += len(l1[j])
	print(len(ans))
	print(*ans)


T = int(input())
for _ in range(T):
	n = int(input())
	(a, b) = (input(), input())
	res = []
	for i in range(n):
		ca = a[i // 2] if i % 2 == 0 else a[-1 - i // 2]
		if i % 2:
			ca = '0' if ca == '1' else '1'
		cb = b[-1 - i]
		if ca == cb:
			res.append(1)
		res.append(n - i)
	print(len(res), *res)


def cal(n, i):
	if i % 2 == 0:
		return n
	else:
		return n ^ 1
for j in range(int(input())):
	n = int(input())
	s1 = [int(i) for i in input()]
	s2 = [int(i) for i in input()]
	ans = []
	for i in range(len(s2) - 1, 0, -1):
		if s1[i] == s2[i]:
			s1 = s1[:-1]
			continue
		elif s1[0] == s2[i] ^ 1:
			ans.append(i + 1)
			s1 = [i ^ 1 for i in s1][::-1][:-1]
		else:
			ans.append(1)
			s1[0] ^= 1
			ans.append(i + 1)
			s1 = [i ^ 1 for i in s1][::-1][:-1]
	if s1[0] != s2[0]:
		ans.append(1)
	if len(ans) > 0:
		print(len(ans), *ans)
	else:
		print(0)


from sys import stdin
tt = int(input())
for loop in range(tt):
	n = int(input())
	a = input()
	b = input()
	ans = []
	for i in range(n):
		if a[i] != b[i]:
			if i != 0:
				ans.append(i + 1)
				ans.append(1)
				ans.append(i + 1)
			else:
				ans.append(1)
	print(len(ans), *ans)


import sys
from collections import deque, defaultdict
I = sys.stdin.readline

def ii():
	return int(I().strip())

def li():
	return list(map(int, I().strip().split()))

def mi():
	return map(int, I().strip().split())

def main():
	for _ in range(ii()):
		n = ii()
		a = I().strip()
		b = I().strip()
		pr_a = []
		pr_b = []
		for i in range(n - 1):
			if a[i] != a[i + 1]:
				pr_a.append(i + 1)
			if b[i] != b[i + 1]:
				pr_b.append(i + 1)
		if a[-1] == '1':
			pr_a.append(n)
		if b[-1] == '1':
			pr_b.append(n)
		pr_b.reverse()
		ans = pr_a + pr_b
		print(len(ans), *ans)
main()


t1 = int(input())
for _ in range(t1):
	n = int(input())
	a2 = input()
	a = []
	for i in range(n):
		a.append(a2[i])
	b = input()
	ans = []
	for i in range(n - 1, -1, -1):
		if a[i] != b[i]:
			temp = []
			for j in range(i + 1):
				temp.append(a[j])
			temp2 = []
			for j in range(i, -1, -1):
				if temp[j] == '0':
					temp2.append('1')
				else:
					temp2.append('0')
			if a[0] != b[i]:
				ans.append(str(i + 1))
				for j in range(i + 1):
					a[j] = temp2[j]
			else:
				ans.append('1')
				ans.append(str(i + 1))
				for j in range(i + 1):
					a[j] = temp2[j]
	print(len(ans))
	if len(ans) > 0:
		print(' '.join(ans))


i = int(input())
while i > 0:
	i = i - 1
	c = int(input())
	d = input()
	e = input()
	z = c
	o = 0
	p = []
	a = []
	b = []
	for n in range(c):
		a.append(int(d[n]))
		b.append(int(e[n]))
	while z:
		if a[z - 1] == b[z - 1]:
			z = z - 1
		elif a[0] == b[z - 1]:
			p.append(1)
			p.append(z)
			o = o + 2
			z = z - 1
			c = a[1:z + 1]
			for j in range(z):
				if c[z - 1 - j] == 1:
					a[j] = 0
				else:
					a[j] = 1
		else:
			p.append(z)
			o = o + 1
			z = z - 1
			c = a[1:z + 1]
			for j in range(z):
				if c[z - 1 - j] == 1:
					a[j] = 0
				else:
					a[j] = 1
	print(o, end=' ')
	for u in range(len(p)):
		print(p[u], end=' ')


import sys
li = lambda : [int(x) for x in sys.stdin.readline().strip().split()]
rw = lambda : sys.stdin.readline().strip().split()
ni = lambda : int(sys.stdin.readline().strip())
nsi = lambda : sys.stdin.readline().strip()
from collections import defaultdict as df
from math import *
t = ni()
for _ in range(t):
	count = 0
	ans = []
	d = []
	dd = []
	n = ni()
	s = nsi()
	ss = nsi()
	for i in s:
		d.append(i)
	for j in ss:
		dd.append(j)
	if s == ss:
		print(0)
	else:
		for i in range(len(d)):
			if d[i] != dd[i]:
				ans.append(i + 1)
				ans.append(1)
				ans.append(i + 1)
		print(len(ans), *ans)


def apply(A, p):
	A[:p] = reversed(A[:p])
	for i in range(p):
		if A[i] == '1':
			A[i] = '0'
		else:
			A[i] = '1'

def brute(n, A, B):
	result = []
	for i in range(n - 1, 0, -1):
		if A[i] != B[i]:
			if A[0] != A[i]:
				result.append(1)
				apply(A, 1)
			result.append(i + 1)
			apply(A, i + 1)
	if A[0] != B[0]:
		result.append(1)
		apply(A, 1)
	return result

def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		A = list(input())
		B = list(input())
		result = brute(n, A, B)
		print(f"{len(result)} {' '.join((str(x) for x in result))}")
main()


for _ in range(int(input())):
	n = int(input())
	s1 = input()
	s2 = input()
	ans = []
	for i in range(n):
		if s1[i] != s2[i]:
			if i == 0:
				ans.append(1)
				continue
			ans.append(i + 1)
			ans.append(1)
			ans.append(i + 1)
	l = len(ans)
	print(l, end=' ')
	for i in range(l):
		print(ans[i], end=' ')
	print('')


for t in range(int(input())):
	n = int(input())
	a = list(map(int, input()))
	b = list(map(int, input()))
	c = 0
	d = []
	i = n - 1
	while i > 0:
		if a[i] != b[i]:
			if a[i] == a[0]:
				c += 1
				d.append(i + 1)
				s = [1 - a[i - j] for j in range(i + 1)]
				s.extend(a[i + 1:])
				a = s
			else:
				c += 2
				d.append(1)
				a[0] = 1 - a[0]
				d.append(i + 1)
				s = [1 - a[i - j] for j in range(i + 1)]
				s.extend(a[i + 1:])
				a = s
		i -= 1
	if a[0] != b[0]:
		c += 1
		d.append(1)
	print(c, *d)


def invert(k):
	for i in range(k):
		A[i] = 1 - A[i]
	for i in range(k // 2):
		(A[i], A[k - i - 1]) = (A[k - i - 1], A[i])
for TEST in range(int(input())):
	n = int(input())
	A = list(map(int, input()))
	B = list(map(int, input()))
	ans = []
	for i in range(n - 1, 0, -1):
		if A[0] == B[i]:
			ans.append(1)
		ans.append(i + 1)
		invert(i + 1)
	if A[0] != B[0]:
		ans.append(1)
	print(len(ans), *ans)


def answer(n, A, B):
	A = list(A)
	B = list(B)
	r = n - 1
	l = []
	while A != B and r >= 0:
		if B[r] == A[r]:
			r -= 1
		elif B[r] != A[0]:
			for i in range(0, r + 1):
				if A[i] == '0':
					A[i] = '1'
				else:
					A[i] = '0'
			A = A[:r + 1][::-1] + A[r + 1:]
			l.append(r + 1)
			r -= 1
		else:
			if A[0] == '0':
				A[0] = '1'
			else:
				A[0] = '0'
			l.append(1)
			for i in range(0, r + 1):
				if A[i] == '0':
					A[i] = '1'
				else:
					A[i] = '0'
			A = A[:r + 1][::-1] + A[r + 1:]
			l.append(r + 1)
			r -= 1
	return [len(l)] + l
t = int(input())
for i in range(t):
	n = int(input())
	a = input()
	b = input()
	print(*answer(n, a, b))


import sys
import math
from collections import defaultdict, Counter
t = int(input())
for i in range(t):
	n = int(input())
	a = list(input())
	b = list(input())
	start = a[0]
	next = n - 1
	prev = 1
	c = 0
	l = []
	for j in range(n - 1, 0, -1):
		if start == b[j]:
			l += [1, j + 1]
		else:
			l += [j + 1]
		c += 1
		if c & 1:
			start = str(int(a[next]) ^ 1)
			next -= 1
		else:
			start = a[prev]
			prev += 1
	if start != b[0]:
		l.append(1)
	print(len(l), *l)


def fun(a, k):
	b = a[:k]
	a = a[k:]
	b = b[::-1]
	b = list(map(lambda x: (x + 1) % 2, b))
	return b + a
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, list(input())))
	b = list(map(int, list(input())))
	k = []
	for i in range(n - 1, -1, -1):
		if b[i] != a[i]:
			if a[0] != b[i]:
				k.append(i + 1)
				a = fun(a, i + 1)
			else:
				k.append(1)
				a[0] = (a[0] + 1) % 2
				k.append(i + 1)
				a = fun(a, i + 1)
	k.insert(0, len(k))
	print(*k)


t = int(input())
for _ in range(t):
	n = int(input())
	a = input()
	b = input()
	ans_count = 0
	ans = []
	for i in range(n):
		if a[i] != b[i]:
			ans += [i + 1, 1, i + 1]
			ans_count += 3
	print(ans_count, *ans)


import sys
input = sys.stdin.readline

def reverse(L):
	return L[::-1]
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().rstrip()))
	b = list(map(int, input().rstrip()))
	ans = []
	for i in range(n):
		if a[n - i - 1] != b[n - i - 1]:
			if a[0] == b[n - i - 1]:
				ans.append(1)
				a[0] ^= 1
			for j in range(n - i):
				a[j] ^= 1
			a[:n - i] = reverse(a[:n - i])
			ans.append(n - i)
	print(len(ans), *ans)


import sys, math
import io, os
from bisect import bisect_left as bl, bisect_right as br, insort
from heapq import heapify, heappush, heappop
from collections import defaultdict as dd, deque, Counter

def data():
	return sys.stdin.readline().strip()

def mdata():
	return list(map(int, data().split()))

def outl(var):
	sys.stdout.write(' '.join(map(str, var)) + '\n')

def out(var):
	sys.stdout.write(str(var) + '\n')
from fractions import Fraction
INF = float('inf')
mod = int(1000000000.0) + 7
for t in range(int(data())):
	n = int(data())
	a = data()
	b = data()
	ind = n - 1
	ans = []
	i = 0
	k = -1
	for i in range(n - 1, -1, -1):
		if (n - 1 - i) % 2 == 0:
			if a[(n - i) // 2] == b[i]:
				ans.append(1)
			ans.append(i + 1)
		else:
			if a[n - (n - i) // 2] != b[i]:
				ans.append(1)
			ans.append(i + 1)
	print(str(len(ans)) + ' ' + ' '.join(map(str, ans)))


for i in range(int(input())):
	a = int(input())
	b = input()
	c = input()
	d = []
	for j in range(a):
		if c[j] != b[j]:
			d.append(j + 1)
			d.append(1)
			d.append(j + 1)
	d.insert(0, len(d))
	for j in d:
		print(j, end=' ')
	print()


def flip_prefix(ind):
	global n, a, b
	rev = {0: 1, 1: 0}
	temp = ''.join([str(rev[int(x)]) for x in a[ind::-1]])
	a = temp + a[ind + 1:]

def main():
	global n, a, b
	t = int(input())
	for i in range(t):
		n = int(input())
		a = input()
		b = input()
		k = 0
		res = []
		for i in range(n):
			if a[i] != b[i]:
				flip_prefix(i)
				flip_prefix(0)
				flip_prefix(i)
				res.append(i + 1)
				res.append(1)
				res.append(i + 1)
				k += 3
		print(k, end=' ')
		print(*res)
n = 0
a = ''
b = ''
main()


import math
from sys import stdin, stdout
for _ in range(int(input())):
	n = int(input())
	s1 = list(input())
	s2 = list(input())
	ans = []
	c = 0
	f = 0
	j = 0
	for i in range(n - 1, -1, -1):
		if f == 0:
			if s1[j] == s2[i]:
				ans.append(str(1))
				c += 1
		elif s1[j] != s2[i]:
			ans.append(str(1))
			c += 1
		ans.append(str(i + 1))
		c += 1
		if f == 0:
			f = 1
			k = n - 1 - j
			j = k
		else:
			f = 0
			k = n - 1 - j
			j = k + 1
	print(str(c) + ' ' + ' '.join(ans))


def func():
	n = int(input())
	a = input()
	b = input()
	indarr = []
	for i in range(n):
		if a[i] != b[i] and i != 0:
			indarr += [i + 1, 1, i + 1]
		elif a[i] != b[i] and i == 0:
			indarr.append(1)
	g = len(indarr)
	indarr = [g] + indarr
	for i in indarr:
		print(i, end=' ')
	print()
t = int(input())
for i in range(t):
	func()


for _ in range(int(input())):
	n = int(input())
	stk1 = input()
	stk2 = input()
	lk = []
	lst1 = [int(x) for x in stk1]
	lst2 = [int(x) for x in stk2]
	for i in range(len(lst2) - 1, -1, -1):
		if lst2[i] == lst1[0]:
			lk.append(1)
			lk.append(i + 1)
			lst1[0] = 1 - lst1[0]
			lst1 = [1 - x for x in lst1[0:i + 1]][::-1] + lst1[i + 1:]
		else:
			lk.append(i + 1)
			lst1 = [1 - x for x in lst1[0:i + 1]][::-1] + lst1[i + 1:]
	print(len(lk), *lk)


for _ in range(int(input())):
	n = int(input())
	a = input()
	l1 = list(a)
	b = input()
	l2 = list(b)
	t1 = []
	t2 = []
	i = 0
	while i < n:
		while i < n and l1[i] == '0':
			i += 1
		if i == n:
			break
		if i > 0:
			t1.append(i - 1)
		while i < n and l1[i] == '1':
			l1[i] = '0'
			i += 1
		t1.append(i - 1)
	i = 0
	while i < n:
		while i < n and l2[i] == '0':
			i += 1
		if i == n:
			break
		if i > 0:
			t2.append(i - 1)
		while i < n and l2[i] == '1':
			l2[i] = '0'
			i += 1
		t2.append(i - 1)
	t2.reverse()
	t = t1 + t2
	print(len(t), end=' ')
	for i in range(len(t)):
		print(t[i] + 1, end=' ')
	print()


for _ in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	a = a + '0'
	ans = list()
	for i in range(1, n + 1):
		if a[i] != a[i - 1]:
			ans.append(i - 1)
	a = ''
	last = '0'
	for i in range(n - 1, -1, -1):
		if b[i] != last:
			ans.append(i)
			if last == '0':
				last = '1'
			else:
				last = '0'
	print(len(ans), end=' ')
	for i in ans:
		print(i + 1, end=' ')
	print()


def inverse(seq):
	return [[1, 0][i] for i in seq][::-1]

def sol(start, goal, length):
	k = length
	actions = []
	while start != goal:
		k -= 1
		if start[k] == goal[k]:
			pass
		else:
			if start[0] == goal[k]:
				start[0] = 1 - start[0]
				actions += [1]
			start = inverse(start[:k + 1]) + start[k + 1:]
			actions += [k + 1]
	return actions
t = int(input())
for _ in range(t):
	l = int(input())
	s1 = [int(i) for i in input()]
	s2 = [int(i) for i in input()]
	p = [str(i) for i in sol(s1, s2, l)]
	print(len(p))
	print(' '.join(p))


def flip(a):
	for i in range(len(a)):
		if a[i] == 1:
			a[i] = 0
		else:
			a[i] = 1
	a.reverse()
	return a
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, list(input())))
	b = list(map(int, list(input())))
	i = 0
	j = n - 1
	ans = []
	while j >= 0:
		if a[i] != b[j]:
			ans.append(j - i + 1)
		else:
			ans.append(1)
			ans.append(j - i + 1)
		a = flip(a)
		a.pop()
		j -= 1
	ans.insert(0, len(ans))
	print(*ans)


T = int(input())
while T > 0:
	n = int(input())
	arr1 = input()
	arr2 = input()
	if arr1 == arr2:
		print(0)
	elif n == 1:
		print(1, 1)
	else:
		res = []
		temp = arr1[0]
		for i in range(1, n):
			if arr1[i] != temp:
				res.append(i)
				temp = arr1[i]
		if arr1[-1] == '1':
			res.append(n)
		temp = '0'
		for i in range(n):
			if arr2[n - 1 - i] != temp:
				temp = arr2[n - 1 - i]
				res.append(n - i)
		print(len(res), *res)
	T -= 1


def f(a, b):
	i = len(a) - 1
	while i != -1:
		if a[i] != b[i]:
			return i
		i -= 1
	return -1

def g(s):
	if not s:
		return ''
	return ('1' if s[-1] == '0' else '0') + g(s[:-1])
t = int(input())
while t > 0:
	t -= 1
	n = int(input())
	a = input()
	b = input()
	l = []
	while 1:
		i = f(a, b)
		if i == -1:
			break
		if a[0] == b[i]:
			l.append(1)
			a = g(a[0]) + a[1:]
		l.append(i + 1)
		a = g(a[:i + 1]) + a[i + 1:]
	print(len(l), end=' ')
	for i in l:
		print(i, end=' ')
	print()


import math
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(input())
	b = list(input())
	d = [0]
	for i in range(n - 1, -1, -1):
		if a[i] != b[i]:
			d.append(i + 1)
			d.append(1)
			d.append(i + 1)
			d[0] += 3
	print(*d)


t = int(input())
for _ in range(t):
	n = int(input())
	(a, b) = (list(map(int, input())), list(map(int, input())))
	(ans, res) = (0, [])
	for j in range(n - 1, -1, -1):
		if a[j] == b[j]:
			continue
		if a[0] == b[j]:
			res.append(1)
			ans += 1
			a[0] = 1 - a[0]
		ans += 1
		res.append(j + 1)
		for k in range(j + 1):
			a[k] = 1 - a[k]
		a[:j + 1] = reversed(a[:j + 1])
	print(ans, ' '.join(map(str, res)))


for _ in range(int(input())):
	n = int(input())
	a = list(input().strip())
	b = list(input().strip())
	l = []
	op = 0
	for i in range(n - 1, 0, -1):
		if a[i] != b[i]:
			l.append(i + 1)
			l.append(1)
			l.append(i + 1)
			op += 3
	if a[0] != b[0]:
		l.append(1)
		op += 1
	if op >= 1:
		print(op, *l)
	else:
		print(op)


for _ in range(int(input())):
	n = int(input())
	A = input()
	B = input()
	A = [int(i) for i in A]
	B = [int(i) for i in B]
	k = 0
	ans = []
	cnt = n - 1
	fp = False
	for i in range(n - 1, -1, -1):
		if fp:
			if A[cnt - i] != B[i]:
				continue
			elif A[cnt] == A[cnt - i]:
				k += 1
				ans.append(i)
				fp = False
				cnt -= 1
			else:
				k += 1
				ans.append(0)
				if i > 0:
					k += 1
					ans.append(i)
					fp = False
					cnt -= 1
		elif A[cnt] == B[i]:
			cnt -= 1
			continue
		elif A[cnt] == A[cnt - i]:
			k += 1
			ans.append(i)
			fp = True
		else:
			k += 1
			ans.append(0)
			if i > 0:
				k += 1
				ans.append(i)
				fp = True
	print(k, end=' ')
	for i in ans:
		print(i + 1, end=' ')
	print()


def do(s, x):
	for i in range(x + 1):
		s[i] = (s[i] + 1) % 2
	for i in range(x // 2 + 1):
		(s[i], s[x - i]) = (s[x - i], s[i])
	return s

def solve(n, a, b):
	ans = [0]
	for i in range(n - 1, -1, -1):
		if a[i] != b[i]:
			if a[0] != b[i]:
				a = do(a, i)
				ans += [i + 1]
				ans[0] += 1
			else:
				a[0] = (a[0] + 1) % 2
				a = do(a, i)
				ans += [1, i + 1]
				ans[0] += 2
	return ans

def main():
	t = int(input())
	for i in range(t):
		(n, a, b) = (int(input()), [int(i) for i in input()], [int(i) for i in input()])
		print(*solve(n, a, b))
main()


import io
import os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def get_str():
	return input().decode().strip()

def rint():
	return map(int, input().split())

def oint():
	return int(input())
t = oint()
for _ in range(t):
	n = oint()
	a = get_str()
	b = get_str()
	a = list(map(int, a))
	b = list(map(int, b))
	ans = []
	for i in range(n - 1, -1, -1):
		if a[i] == b[i]:
			continue
		if i == 0:
			if a[0] == b[i]:
				continue
			else:
				ans.append(1)
				a[0] = a[0] ^ 1
			continue
		if a[0] == b[i]:
			ans.append(1)
			a[0] = a[0] ^ 1
		ans.append(i + 1)
		a[:i + 1] = a[:i + 1][::-1]
		for ii in range(i + 1):
			a[ii] = a[ii] ^ 1
	print(len(ans), *ans)


for _ in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	res = []
	for i in range(n):
		if a[i] != b[i]:
			res.append(i + 1)
			res.append(1)
			res.append(i + 1)
	print(len(res), *res)


for u in range(int(input())):
	n = int(input())
	s = input()
	r = input()
	l = []
	for i in range(n - 1, -1, -1):
		if s[i] != r[i]:
			l.extend([i + 1, 1, i + 1])
	print(len(l), *l)


def stringflip(l, s):
	news = ''
	for i in range(l):
		if s[l - i - 1] == '0':
			news += '1'
		else:
			news += '0'
	if l != n:
		news += s[l:]
	return news

def flip(n, a, b):
	if a == b:
		print(0)
		return
	moves = 0
	array = []
	for i in range(n):
		if a[0] == b[n - i - 1]:
			array.append(1)
			array.append(n - i)
			moves += 2
			stringflip(1, a)
			a = stringflip(n - i, a)
		else:
			array.append(n - i)
			moves += 1
			a = stringflip(n - i, a)
	print(moves, end=' ')
	for x in array:
		print(x, end=' ')
	print()
	return
t = int(input())
a = []
for j in range(t):
	n = int(input())
	d = input()
	e = input()
	a.append([n, d, e])
for x in a:
	flip(*x)


import math
import heapq, bisect
import sys
from collections import deque, defaultdict
from fractions import Fraction
mod = 10 ** 9 + 7
mod1 = 998244353
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

class TreeNode:

	def __init__(self, k, v):
		self.key = k
		self.value = v
		self.left = None
		self.right = None
		self.parent = None
		self.height = 1
		self.num_left = 1
		self.num_total = 1

class AvlTree:

	def __init__(self):
		self._tree = None

	def add(self, k, v):
		if not self._tree:
			self._tree = TreeNode(k, v)
			return
		node = self._add(k, v)
		if node:
			self._rebalance(node)

	def _add(self, k, v):
		node = self._tree
		while node:
			if k < node.key:
				if node.left:
					node = node.left
				else:
					node.left = TreeNode(k, v)
					node.left.parent = node
					return node.left
			elif node.key < k:
				if node.right:
					node = node.right
				else:
					node.right = TreeNode(k, v)
					node.right.parent = node
					return node.right
			else:
				node.value = v
				return

	@staticmethod
	def get_height(x):
		return x.height if x else 0

	@staticmethod
	def get_num_total(x):
		return x.num_total if x else 0

	def _rebalance(self, node):
		n = node
		while n:
			lh = self.get_height(n.left)
			rh = self.get_height(n.right)
			n.height = max(lh, rh) + 1
			balance_factor = lh - rh
			n.num_total = 1 + self.get_num_total(n.left) + self.get_num_total(n.right)
			n.num_left = 1 + self.get_num_total(n.left)
			if balance_factor > 1:
				if self.get_height(n.left.left) < self.get_height(n.left.right):
					self._rotate_left(n.left)
				self._rotate_right(n)
			elif balance_factor < -1:
				if self.get_height(n.right.right) < self.get_height(n.right.left):
					self._rotate_right(n.right)
				self._rotate_left(n)
			else:
				n = n.parent

	def _remove_one(self, node):
		replacement = node.left or node.right
		if node.parent:
			if AvlTree._is_left(node):
				node.parent.left = replacement
			else:
				node.parent.right = replacement
			replacement.parent = node.parent
			node.parent = None
		else:
			self._tree = replacement
			replacement.parent = None
		node.left = None
		node.right = None
		node.parent = None
		self._rebalance(replacement)

	def _remove_leaf(self, node):
		if node.parent:
			if AvlTree._is_left(node):
				node.parent.left = None
			else:
				node.parent.right = None
			self._rebalance(node.parent)
		else:
			self._tree = None
		node.parent = None
		node.left = None
		node.right = None

	def remove(self, k):
		node = self._get_node(k)
		if not node:
			return
		if AvlTree._is_leaf(node):
			self._remove_leaf(node)
			return
		if node.left and node.right:
			nxt = AvlTree._get_next(node)
			node.key = nxt.key
			node.value = nxt.value
			if self._is_leaf(nxt):
				self._remove_leaf(nxt)
			else:
				self._remove_one(nxt)
			self._rebalance(node)
		else:
			self._remove_one(node)

	def get(self, k):
		node = self._get_node(k)
		return node.value if node else -1

	def _get_node(self, k):
		if not self._tree:
			return None
		node = self._tree
		while node:
			if k < node.key:
				node = node.left
			elif node.key < k:
				node = node.right
			else:
				return node
		return None

	def get_at(self, pos):
		x = pos + 1
		node = self._tree
		while node:
			if x < node.num_left:
				node = node.left
			elif node.num_left < x:
				x -= node.num_left
				node = node.right
			else:
				return (node.key, node.value)
		raise IndexError('Out of ranges')

	@staticmethod
	def _is_left(node):
		return node.parent.left and node.parent.left == node

	@staticmethod
	def _is_leaf(node):
		return node.left is None and node.right is None

	def _rotate_right(self, node):
		if not node.parent:
			self._tree = node.left
			node.left.parent = None
		elif AvlTree._is_left(node):
			node.parent.left = node.left
			node.left.parent = node.parent
		else:
			node.parent.right = node.left
			node.left.parent = node.parent
		bk = node.left.right
		node.left.right = node
		node.parent = node.left
		node.left = bk
		if bk:
			bk.parent = node
		node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1
		node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right)
		node.num_left = 1 + self.get_num_total(node.left)

	def _rotate_left(self, node):
		if not node.parent:
			self._tree = node.right
			node.right.parent = None
		elif AvlTree._is_left(node):
			node.parent.left = node.right
			node.right.parent = node.parent
		else:
			node.parent.right = node.right
			node.right.parent = node.parent
		bk = node.right.left
		node.right.left = node
		node.parent = node.right
		node.right = bk
		if bk:
			bk.parent = node
		node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1
		node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right)
		node.num_left = 1 + self.get_num_total(node.left)

	@staticmethod
	def _get_next(node):
		if not node.right:
			return node.parent
		n = node.right
		while n.left:
			n = n.left
		return n
avl = AvlTree()

class SegmentTree1:

	def __init__(self, data, default='z', func=lambda a, b: min(a, b)):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)

class SegmentTree:

	def __init__(self, data, default=0, func=lambda a, b: a + b):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)

class Factorial:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorials = [1, 1]
		self.invModulos = [0, 1]
		self.invFactorial_ = [1, 1]

	def calc(self, n):
		if n <= -1:
			print('Invalid argument to calculate n!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.factorials):
			return self.factorials[n]
		nextArr = [0] * (n + 1 - len(self.factorials))
		initialI = len(self.factorials)
		prev = self.factorials[-1]
		m = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * i % m
		self.factorials += nextArr
		return self.factorials[n]

	def inv(self, n):
		if n <= -1:
			print('Invalid argument to calculate n^(-1)')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		p = self.MOD
		pi = n % p
		if pi < len(self.invModulos):
			return self.invModulos[pi]
		nextArr = [0] * (n + 1 - len(self.invModulos))
		initialI = len(self.invModulos)
		for i in range(initialI, min(p, n + 1)):
			next = -self.invModulos[p % i] * (p // i) % p
			self.invModulos.append(next)
		return self.invModulos[pi]

	def invFactorial(self, n):
		if n <= -1:
			print('Invalid argument to calculate (n^(-1))!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.invFactorial_):
			return self.invFactorial_[n]
		self.inv(n)
		nextArr = [0] * (n + 1 - len(self.invFactorial_))
		initialI = len(self.invFactorial_)
		prev = self.invFactorial_[-1]
		p = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * self.invModulos[i % p] % p
		self.invFactorial_ += nextArr
		return self.invFactorial_[n]

class Combination:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorial = Factorial(MOD)

	def ncr(self, n, k):
		if k < 0 or n < k:
			return 0
		k = min(k, n - k)
		f = self.factorial
		return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD

def powm(a, n, m):
	if a == 1 or n == 0:
		return 1
	if n % 2 == 0:
		s = powm(a, n // 2, m)
		return s * s % m
	else:
		return a * powm(a, n - 1, m) % m

def sort_list(list1, list2):
	zipped_pairs = zip(list2, list1)
	z = [x for (_, x) in sorted(zipped_pairs)]
	return z

def product(l):
	por = 1
	for i in range(len(l)):
		por *= l[i]
	return por

def binarySearchCount(arr, n, key):
	left = 0
	right = n - 1
	count = 0
	while left <= right:
		mid = int((right + left) / 2)
		if arr[mid] <= key:
			count = mid + 1
			left = mid + 1
		else:
			right = mid - 1
	return count

def countdig(n):
	c = 0
	while n > 0:
		n //= 10
		c += 1
	return c

def countGreater(arr, n, k):
	l = 0
	r = n - 1
	leftGreater = n
	while l <= r:
		m = int(l + (r - l) / 2)
		if arr[m] >= k:
			leftGreater = m
			r = m - 1
		else:
			l = m + 1
	return n - leftGreater
for ik in range(int(input())):
	n = int(input())
	s = list(input())
	for i in range(n):
		s[i] = int(s[i])
	s1 = list(input())
	for i in range(n):
		s1[i] = int(s1[i])
	ans = []
	cou = 0
	lastz = s[0]
	su = 0
	for i in range(n - 1, -1, -1):
		si = s[su + (-1) ** cou * i]
		if cou % 2 == 1:
			si = 1 - si
		if s1[i] != si:
			if lastz == s1[i]:
				ans.append(1)
			ans.append(i + 1)
			lastz = s1[i]
			if cou % 2 == 1:
				su -= i
			else:
				su += i
			cou += 1
	print(len(ans), *ans)


def one_comp(a):
	b = ''
	for k in str(a):
		if k == '0':
			b = b + '1'
		else:
			b = b + '0'
	return b[::-1]
for _ in range(int(input())):
	n = int(input())
	a = str(input())
	b = str(input())
	if a == b:
		print(0)
		continue
	flip = []
	for i in range(n - 1, -1, -1):
		if a[i] == b[n - (i + 1)]:
			if a[i] == a[0]:
				flip.append(i + 1)
				a = one_comp(a[0:i + 1]) + a[i + 1:]
			else:
				flip.append(1)
				flip.append(i + 1)
				a = str(str((int(a[0]) + 1) % 2) + a[1:])
				a = one_comp(a[0:i + 1]) + a[i + 1:]
		else:
			continue
	if a[0] == b[n - 1]:
		flip.append(1)
	flip.append(n)
	print(len(flip), end=' ')
	for t in flip:
		print(t, end=' ')
	print()


t = int(input())
while t:
	t -= 1
	n = int(input())
	l1 = input()
	l2 = input()
	ans = []
	for i in range(n - 1, -1, -1):
		if l1[i] != l2[i]:
			ans.append(i + 1)
			ans.append(1)
			ans.append(i + 1)
	print(len(ans), end=' ')
	print(*ans)


def order(s):
	init = s[0]
	res = []
	for i in range(len(s) - 1):
		if s[i] != s[i + 1]:
			init = s[i + 1]
			res.append(i + 1)
	if init == '1':
		res.append(len(s))
	return res
for _ in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	res = order(a) + order(b)[::-1]
	print(len(res), *res)


import sys
input = lambda : sys.stdin.readline().strip()
t = int(input())
while t:
	t -= 1
	n = int(input())
	a = list(input())
	b = list(input())
	k = []
	i = n - 1
	while i > 0:
		while i > 0 and a[i] == b[i]:
			i -= 1
		if i == 0:
			break
		if int(a[0]) != int(b[i]):
			k.append(i + 1)
			for j in range((i + 1) // 2):
				(a[j], a[i - j]) = (a[i - j], a[j])
				if a[j] == '1':
					a[j] = '0'
				else:
					a[j] = '1'
				if a[i - j] == '1':
					a[i - j] = '0'
				else:
					a[i - j] = '1'
			if (i + 1) % 2:
				if a[(i + 1) // 2] == '1':
					a[(i + 1) // 2] = '0'
				else:
					a[(i + 1) // 2] = '1'
			i -= 1
		else:
			k.append(1)
			k.append(i + 1)
			if a[0] == '1':
				a[0] = '0'
			else:
				a[0] = '1'
			for j in range((i + 1) // 2):
				(a[j], a[i - j]) = (a[i - j], a[j])
				if a[j] == '1':
					a[j] = '0'
				else:
					a[j] = '1'
				if a[i - j] == '1':
					a[i - j] = '0'
				else:
					a[i - j] = '1'
			if (i + 1) % 2:
				if a[(i + 1) // 2] == '1':
					a[(i + 1) // 2] = '0'
				else:
					a[(i + 1) // 2] = '1'
			i -= 1
	if a[0] != b[0]:
		k.append(1)
	print(len(k), end=' ')
	for i in k:
		print(i, end=' ')
	print()


import io, os
from collections import deque
RL = lambda : list(map(int, input().split(' ')))

def flip(a):
	if a == '1':
		b = '0'
		return b
	b = '1'
	return b
T = int(input())
for _ in range(T):
	n = int(input())
	a = input()
	b = input()
	flag = 0
	ind1 = 1
	ind2 = n - 1
	ind = n - 1
	i = n - 1
	cnt = 0
	first = a[0]
	ans = []
	while ind1 < ind2:
		if flag == 0:
			ind = ind2
			ind2 -= 1
		else:
			ind = ind1
			ind1 += 1
		if cnt % 2:
			toch = flip(a[ind])
		else:
			toch = a[ind]
		if toch != b[i]:
			if toch != first:
				ans.append(1)
			ans.append(i + 1)
			first = flip(toch)
			if flag == 0:
				flag = 1
			else:
				flag = 0
			cnt += 1
		i -= 1
	if flag == 0:
		ind = ind2
		ind2 -= 1
	else:
		ind = ind1
		ind1 += 1
	if cnt % 2:
		toch = flip(a[ind])
	else:
		toch = a[ind]
	if toch != b[i]:
		if toch != first:
			ans.append(1)
		ans.append(i + 1)
		first = flip(toch)
	if first != b[0]:
		ans.append(1)
	print(len(ans), end=' ')
	print(*ans)


for _ in range(int(input())):
	n = int(input())
	s = input()
	target = input()
	ans = []
	for i in range(n - 1, -1, -1):
		if s[i] == target[i]:
			continue
		ans.append(i + 1)
		ans.append(1)
		ans.append(i + 1)
	print(len(ans), end=' ')
	print(*ans)


def flip(x):
	if x == '1':
		return '0'
	else:
		return '1'
t = int(input())
for i in range(t):
	n = int(input())
	a = list(input())
	b = list(input())
	lst = []
	k = 0
	b.reverse()
	for j in range(1, n):
		if a[j] != a[j - 1]:
			a[0] = flip(a[0])
			k = k + 1
			lst.append(j)
	for j in range(n):
		if b[j] != a[0]:
			a[0] = flip(a[0])
			k = k + 1
			lst.append(n - j)
	print(k, end=' ')
	for j in lst:
		print(j, end=' ')
	print('')


import os
import re
import sys
from bisect import bisect, bisect_left, insort, insort_left
from collections import Counter, defaultdict, deque
from copy import deepcopy
from decimal import Decimal
from fractions import gcd
from io import BytesIO, IOBase
from itertools import accumulate, combinations, combinations_with_replacement, groupby, permutations, product
from math import acos, asin, atan, ceil, cos, degrees, factorial, hypot, log2, pi, radians, sin, sqrt, tan
from operator import itemgetter, mul
from string import ascii_lowercase, ascii_uppercase, digits

def inp():
	return int(input())

def inlist():
	return list(map(int, input().split()))

def instr():
	s = input()
	return list(s[:len(s)])

def invr():
	return map(int, input().split())

def perform(a, i):
	temp = ''
	for k in range(i + 1):
		if a[k] == '0':
			temp += '1'
		else:
			temp += '0'
	temp = temp[::-1] + a[i + 1:]
	return temp

def main():
	t = inp()
	for _ in range(t):
		n = inp()
		a = input()
		b = input()
		res = []
		for i in range(n - 1, -1, -1):
			if a[i] == b[i]:
				continue
			if a[0] == b[i]:
				res.append(1)
				a = perform(a, 0)
			res.append(i + 1)
			a = perform(a, i)
		print(len(res), *res)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))

def input():
	return sys.stdin.readline().rstrip('\r\n')
main()


def help():
	n = int(input())
	a = input()
	b = input()
	ans = []
	A = []
	B = []
	for (i, j) in zip(a, b):
		A.append(int(i) - int('0'))
		B.append(int(j) - int('0'))
	fir = A[0]
	for i in range(n - 1, -1, -1):
		curr = A[i]
		currb = B[i]
		if curr == currb:
			continue
		if currb == A[0]:
			ans.append(1)
			A[0] = 1 - A[0]
			A[0:i + 1] = reversed(A[0:i + 1])
			for j in range(i + 1):
				A[j] = 1 - A[j]
			ans.append(i + 1)
		else:
			A[0:i + 1] = reversed(A[0:i + 1])
			for j in range(i + 1):
				A[j] = 1 - A[j]
			ans.append(i + 1)
	print(len(ans), end=' ')
	for i in ans:
		print(i, end=' ')
	print()
t = int(input())
for T in range(t):
	help()


t = int(input())
while t:
	n = int(input())
	s1 = input()
	s2 = input()
	dif = []
	for i in range(n):
		if s1[i] != s2[i]:
			dif.append(i)
	dif.reverse()
	print(3 * len(dif), end=' ')
	for i in dif:
		print(i + 1, 1, i + 1, end=' ')
	print()
	t -= 1


def inv(st):
	s = []
	for i in st:
		if i == '1':
			s = ['0'] + s
		else:
			s = ['1'] + s
	return ''.join(s)

def match(ar, br, k, li):
	n = len(ar)
	for i in range(n - 1, -1, -1):
		if ar[i] != br[i]:
			break
	else:
		return (k, li)
	x = br[i]
	if ar[0] != x:
		k += 1
		li.append(i + 1)
		return match(inv(ar[:i + 1]), br[:i + 1], k, li)
	else:
		for j in range(i - 1, -1, -1):
			if ar[j] == x:
				a = inv(ar[j + 1:i + 1]) + ar[:j + 1]
				k += 2
				li += [j + 1, i + 1]
				return match(a, br[:i + 1], k, li)
t = int(input())
for _ in range(t):
	n = int(input())
	ar = input()
	br = input()
	(m, ls) = match(ar, br, 0, [])
	ls = [m] + ls
	print(*ls)


from copy import copy

def main():
	t = int(input())
	for _ in range(t):
		ans = []
		n = int(input())
		a = [int(i) for i in input()]
		b = [int(i) for i in input()]

		def change(c):
			if c == 1:
				return 0
			else:
				return 1

		def flip(a, i):
			n = i + 1
			ans.append(n)
			temp = copy(a)
			for i in range(n):
				a[i] = temp[n - i - 1]
				a[i] = change(a[i])
		for i in range(n - 1, 0, -1):
			if a[i] != b[i]:
				start = a[0]
				if start != a[i]:
					ans.append(1)
					a[0] = change(a[0])
					flip(a, i)
				else:
					flip(a, i)
		if a[0] != b[0]:
			ans.append(1)
		print(len(ans), end=' ')
		for i in ans:
			print(i, end=' ')
		print()
main()


t = int(input())
while t:
	n = int(input())
	s1 = input()
	s2 = input()
	dif = []
	flag = False
	s1 = s1 + '0'
	for i in range(n):
		if s1[i] != s1[i + 1]:
			dif.append(i)
	flag = False
	for i in range(n - 1, -1, -1):
		if s2[i] == '1' and flag == False:
			flag = True
			dif.append(i)
		elif s2[i] == '0' and flag == True:
			flag = False
			dif.append(i)
	print(len(dif), end=' ')
	for i in dif:
		print(i + 1, end=' ')
	print()
	t -= 1


for _ in range(int(input())):
	n = int(input())
	a = input()[:n]
	b = input()[:n]
	m = list(a)
	ans = []
	for x in range(len(a) - 1, 0, -1):
		if m[0] != b[x]:
			ans.append(len(m))
			for i in range(len(m)):
				if m[i] == '1':
					m[i] = '0'
				else:
					m[i] = '1'
			m.reverse()
			m.pop()
		else:
			if m[0] == '0':
				m[0] = '1'
			else:
				m[0] = '0'
			ans.append(1)
			ans.append(len(m))
			for i in range(len(m)):
				if m[i] == '1':
					m[i] = '0'
				else:
					m[i] = '1'
			m.reverse()
			m.pop()
	if m[0] != b[0]:
		ans.append(1)
	print(len(ans), *ans)


def func(a, b, n):
	ans = []
	for i in range(n):
		if i == 0:
			if a[i] == b[i]:
				pass
			else:
				ans.append(i + 1)
		elif a[i] == b[i]:
			pass
		else:
			ans += [i + 1, 1, i + 1]
	return ans
for _ in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	final = func(a, b, n)
	print(len(final), *final)


for _ in range(int(input())):
	n = int(input())
	a = list(input())
	b = list(input())
	se = []
	while n != 0:
		if a[-1] == b[-1]:
			n -= 1
			a.pop()
			b.pop()
		elif n != 1:
			if a[0] == b[-1]:
				se.append(1)
				se.append(n)
				b.pop()
				for i in range(n):
					if a[i] == '1':
						a[i] = '0'
					else:
						a[i] = '1'
				a.reverse()
				a.pop()
				n -= 1
			else:
				se.append(n)
				b.pop()
				for i in range(n):
					if a[i] == '1':
						a[i] = '0'
					else:
						a[i] = '1'
				a.reverse()
				a.pop()
				n -= 1
		else:
			se.append(1)
			n -= 1
	print(len(se), *se)


for _ in range(int(input())):
	n = int(input())
	m = 0
	a = input()
	b = input()
	if a == b:
		print(0)
	else:
		b = list(b[::-1])
		for i in range(len(b)):
			if b[i] == '0':
				b[i] = '1'
			else:
				b[i] = '0'
		l = 1
		k = []
		for i in range(1, len(a)):
			if a[i] != a[i - 1]:
				k.append(l)
				m += 1
			l += 1
		b = ''.join(b)
		if a[-1] == b[-1]:
			a = a[-1] * len(a)
		else:
			a = str(int(not int(a[-1]))) * len(a)
			m += 1
			k.append(n)
		l = 1
		k2 = []
		for i in range(1, len(a)):
			if b[i] != b[i - 1]:
				k2.append(l)
				m += 1
			l += 1
		k2 = k2[::-1]
		k += k2
		m += 1
		k.append(n)
		print(m, *k)


def operation(ind, a):
	b = a[:ind]
	b = b.replace('0', '2')
	b = b.replace('1', '0')
	b = b.replace('2', '1')
	b = b[::-1]
	a = b + a[ind:]
	return a
t = int(input())
for i in range(t):
	n = int(input())
	a = input()
	b = input()
	l = []
	for j in range(n - 1, -1, -1):
		if a[j] != b[j]:
			if a[0] != a[j]:
				a = operation(1, a)
				l.append('1')
			a = operation(j + 1, a)
			l.append(str(j + 1))
	ans = str(len(l))
	if len(l) == 0:
		print(ans)
	else:
		for ele in l:
			ans = ans + ' ' + ele
		print(ans)


def reverse_num(number, reversed):
	answer = number if reversed else (int(number) + 1) % 2
	return str(answer)

def main():
	_ = input()
	a = list(input())
	b = list(input())
	ops = []
	(l, r) = (0, len(a) - 1)
	rev = True
	for i in range(len(a) - 1, -1, -1):
		if b[i] != reverse_num(a[r], rev):
			if i == 0:
				ops.append(1)
			else:
				if reverse_num(a[r], rev) != reverse_num(a[l], rev):
					ops.append(1)
				ops.append(i + 1)
				if rev:
					(l, r) = (r, l + 1)
				else:
					(l, r) = (r, l - 1)
				rev = not rev
		else:
			r = r + (-1 if rev else 1)
	print(len(ops), end=' ')
	for op in ops:
		print(op, end=' ')
	print()
t = int(input())
for _ in range(t):
	main()


for _ in range(int(input())):
	n = int(input())
	l1 = input()
	l2 = input()
	l1 = list(l1)
	l2 = list(l2)
	c = 0
	ans = list()
	for i in range(n):
		if i == 0:
			if l1[i] != l2[i]:
				c += 1
				ans.append(1)
		elif l1[i] != l2[i]:
			c += 3
			ans.extend([i + 1, 1, i + 1])
	print(c, end=' ')
	for i in ans:
		print(i, end=' ')
	print()


t = int(input())
for _ in range(t):
	n = int(input())
	s = input()
	t = input()
	moves = []
	for i in range(n - 1):
		if s[i] != s[i + 1]:
			moves.append(i + 1)
	if s[-1] != t[-1]:
		moves.append(n)
	last = t[-1]
	for i in range(n - 2, -1, -1):
		if t[i] != last:
			moves.append(i + 1)
			if last == '1':
				last = '0'
			else:
				last = '1'
	print(len(moves), ' '.join(map(str, moves)))


def main():
	n = int(input())
	s = input()
	t = input()
	f = []
	for i in range(n):
		if s[i] != t[i]:
			f = [i + 1] + f
	print(len(f) * 3, end=' ')
	for i in f:
		print('{} {} {}'.format(i, 1, i), end=' ')
	print('')
for _ in range(int(input())):
	main()


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, list(input())))
	b = list(map(int, list(input())))
	array = []
	a0 = a[0]
	j = n - 1
	k = 1
	for i in range(n - 1, -1, -1):
		if b[i] == a0:
			array.append(1)
		array.append(i + 1)
		if (n - i) % 2 == 1:
			a0 = (a[j] + 1) % 2
			j -= 1
		else:
			a0 = a[k]
			k += 1
	print(len(array), *array)


def solve():
	n = int(input())
	(a, b) = (list(map(int, list(input()))), list(map(int, list(input()))))
	ans = []
	for i in reversed(range(n)):
		if a[i] != b[i]:
			if a[0] == b[i]:
				ans.append(1)
				a[0] = 1 - a[0]
			ans.append(i + 1)
			for j in range(i + 1):
				a[j] = 1 - a[j]
			(first, last) = (0, i)
			while first < last:
				(a[first], a[last]) = (a[last], a[first])
				first += 1
				last -= 1
	print(len(ans), end=' ')
	print(*ans)
t = int(input())
for _ in range(t):
	solve()


def solution(s1, s2):
	s1 = s1 + '0'
	s2 = s2 + '0'
	l1 = []
	l2 = []
	for i in range(1, len(s1)):
		if s1[i] != s1[i - 1]:
			l1.append(i)
		if s2[i] != s2[i - 1]:
			l2.append(i)
	ans = l1 + l2[::-1]
	return ans
for _ in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	ans = solution(a, b)
	print(len(ans), end=' ')
	print(*ans)


from sys import stdin
inp = lambda : stdin.readline().strip()
t = int(inp())
for _ in range(t):
	n = int(inp())
	a = list(inp())
	b = list(inp())
	consA = []
	consB = []
	start = 0
	for i in range(1, n):
		if a[i] != a[i - 1]:
			consA.append(i - 1)
			start = i
	if a[-1] != b[-1]:
		consA.append(n - 1)
	for i in range(1, n):
		if b[i] != b[i - 1]:
			consB.append(i - 1)
			start = i
	ans = []
	for i in consA:
		ans.append(i + 1)
	for i in consB[::-1]:
		ans.append(i + 1)
	print(len(ans), *ans)


def solve(s):
	new = ''
	for i in s:
		if i == '1':
			new += '0'
		else:
			new += '1'
	return new[::-1]
for nt in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	ans = []
	i = 1
	count = 0
	if a[0] != b[0]:
		ans.append(1)
	while i < n:
		if i + 1 < n:
			if a[i] != b[i] and a[i + 1] != b[i + 1]:
				if a[i] == a[i + 1]:
					ans.append(i + 2)
					ans.append(2)
					ans.append(i + 2)
					i += 2
				else:
					if i + 2 >= n or a[i + 2] == b[i + 2]:
						ans.append(i + 2)
						ans.append(1)
						ans.append(2)
						ans.append(1)
						ans.append(i + 2)
						i += 3
						continue
					if a[i + 2] == a[i]:
						ans.append(i + 3)
						ans.append(3)
						ans.append(i + 3)
						i += 3
					else:
						ans.append(i + 3)
						ans.append(1)
						ans.append(3)
						ans.append(1)
						ans.append(i + 3)
						i += 3
			elif a[i] == b[i] and a[i + 1] != b[i + 1]:
				i += 1
			elif a[i] != b[i] and a[i + 1] == b[i + 1]:
				ans.append(i + 1)
				ans.append(1)
				ans.append(i + 1)
				i += 2
			else:
				i += 2
		else:
			if a[i] != b[i]:
				ans.append(i + 1)
				ans.append(1)
				ans.append(i + 1)
			break
	print(len(ans), end=' ')
	print(*ans)


t = int(input())
for w in range(t):
	n = int(input())
	a = list(input())
	b = list(input())
	c = 0
	l = []
	for i in range(n):
		if a[i] != b[i]:
			c += 3
			l += [i + 1, 1, i + 1]
	print(c, *l)


import sys
inp = sys.stdin.read().split()
ii = 0
t = int(inp[ii])
ii += 1
for _ in range(t):
	toprint = []
	n = int(inp[ii])
	ii += 1
	c = list(map(int, inp[ii]))
	ii += 1
	b = list(map(int, inp[ii]))
	ii += 1
	count = 0
	for i in range(n - 1, -1, -1):
		if count % 2 == 0:
			lead = c[count // 2]
		else:
			lead = 1 - c[-count // 2]
		y = b[i]
		if i == 0:
			if lead == y:
				pass
			else:
				toprint.append(1)
		else:
			if lead == y:
				toprint.append(1)
			toprint.append(i + 1)
		count += 1
	print(len(toprint), ' '.join(list(map(str, toprint))))


import os
import sys
from io import BytesIO, IOBase

def operation(A, pos):
	for i in range(pos + 1):
		if A[i] == 1:
			A[i] = 0
		else:
			A[i] = 1
	(i, j) = (0, pos)
	while i < j:
		(A[i], A[j]) = (A[j], A[i])
		i += 1
		j -= 1

def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		a = input()
		b = input()
		A = list()
		B = list()
		for i in range(n):
			A.append(int(a[i]))
			B.append(int(b[i]))
		k = 0
		p = list()
		for i in range(n - 1, 0, -1):
			if A[i] != B[i]:
				if A[0] != B[i]:
					k += 1
					p.append(i + 1)
					operation(A, i)
				else:
					k += 2
					if A[0] == 0:
						A[0] = 1
					else:
						A[0] = 0
					p.append(1)
					p.append(i + 1)
					operation(A, i)
		if A[0] != B[0]:
			A[0] = B[0]
			k += 1
			p.append(1)
		print(k, end=' ')
		for prefix in p:
			print(prefix, end=' ')
		print()
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = lambda s: self.buffer.write(s.encode()) if self.writable else None

	def read(self):
		if self.buffer.tell():
			return self.buffer.read().decode('ascii')
		return os.read(self._fd, os.fstat(self._fd).st_size).decode('ascii')

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline().decode('ascii')

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

def print(*args, sep=' ', end='\n', file=sys.stdout, flush=False):
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(end)
	if flush:
		file.flush()
(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
sys.setrecursionlimit(10000)
main()


import sys

def rs():
	return sys.stdin.readline().rstrip()

def ri():
	return int(sys.stdin.readline())

def ria():
	return list(map(int, sys.stdin.readline().split()))

def ws(s):
	sys.stdout.write(s)
	sys.stdout.write('\n')

def wi(n):
	sys.stdout.write(str(n))
	sys.stdout.write('\n')

def wia(a, sep=' '):
	sys.stdout.write(sep.join([str(x) for x in a]))
	sys.stdout.write('\n')

def solve(n, a, b):
	res = []
	for i in range(n - 1, -1, -1):
		if a[i] == b[i]:
			continue
		if a[0] == b[i]:
			a[0] = 1 - a[0]
			res.append(1)
		if i % 2 == 0:
			a[i // 2] = 1 - a[i // 2]
		for j in range((i + 1) // 2):
			(a[j], a[i - j]) = (1 - a[i - j], 1 - a[j])
		res.append(i + 1)
	return [len(res)] + res

def main():
	for _ in range(ri()):
		n = ri()
		a = [int(si) for si in rs()]
		b = [int(si) for si in rs()]
		wia(solve(n, a, b))
main()


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(input())
	b = list(input())
	for i in range(n):
		a[i] = int(a[i])
		b[i] = int(b[i])
	p = []
	i = 0
	while i < n:
		if a[i] == b[i]:
			i += 1
		else:
			p += [i + 1]
			p += [1]
			p += [i + 1]
			i += 1
	print(len(p), end=' ')
	for i in p:
		print(i, end=' ')
	print()


T = int(input())

def go(x, k):
	ans = x[:]
	for i in range(k + 1):
		ans[i] = x[k - i] ^ 1
	return ans
for t in range(T):
	n = int(input())
	source = [int(i) for i in input()]
	target = [int(i) for i in input()]
	pointer = n - 1
	ans = []
	while pointer > 0:
		if source[pointer] != target[pointer]:
			if source[0] == target[pointer]:
				ans.append(1)
				source[0] ^= 1
			ans.append(pointer + 1)
			source = go(source, pointer)
		pointer -= 1
	if source[0] != target[0]:
		ans.append(1)
	m = len(ans)
	ans = [m] + ans
	print(*ans)


from sys import stdin
for _ in range(int(stdin.readline())):
	n = int(stdin.readline())
	s1 = stdin.readline().strip()
	s2 = stdin.readline().strip()
	res = []
	for i in range(n):
		if s1[i] != s2[i]:
			res.append(i + 1)
			res.append(1)
			res.append(i + 1)
	print(len(res), *res)


for _ in range(int(input())):
	num = int(input())
	a = input()
	b = input()
	ans = [-1]
	for i in range(num):
		if a[i] != b[i]:
			ans += [i + 1, 1, i + 1]
	ans[0] = len(ans) - 1
	print(*ans)


t = int(input())
for i in range(t):
	n = int(input())
	a = input()
	b = input()
	ans = []
	flag = True
	if a[0] != b[0]:
		ans.append(1)
		flag = False
	for j in range(1, n):
		if a[j] != b[j]:
			ans.append(j + 1)
			ans.append(1)
			ans.append(j + 1)
			flag = False
	if flag:
		print(0)
		continue
	print(len(ans), end=' ')
	for j in ans[:-1]:
		print(j, end=' ')
	print(ans[-1])


import sys
input = sys.stdin.readline
from collections import *
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input()[:-1]))
	b = list(map(int, input()[:-1]))
	ope = []
	for i in range(n - 1, -1, -1):
		if a[i] == b[i]:
			continue
		if a[0] ^ b[i] == 1:
			ope.append(i)
		else:
			ope.append(0)
			ope.append(i)
			a[0] ^= 1
		for j in range(i + 1):
			a[j] ^= 1
		for j in range((i + 1) // 2):
			(a[j], a[i - j]) = (a[i - j], a[j])
	if a != b:
		1 / 0
	print(len(ope), *list(map(lambda x: x + 1, ope)))


for _ in range(int(input())):
	n = int(input())
	s = input()
	t = input()
	print(sum([3 if p[1] != p[0] else 0 for p in zip(s, t)]))
	for i in range(n):
		if s[i] != t[i]:
			print(i + 1, 1, i + 1, end=' ')
	print()


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, list(input())))
	b = list(map(int, list(input())))
	ls = []
	flip = 0
	idx = 0
	for i in range(n - 1, -1, -1):
		if (b[i] == a[idx]) ^ flip:
			ls.append(1)
		ls.append(i + 1)
		if flip:
			idx -= i
		else:
			idx += i
		flip = 1 - flip
	print(len(ls), *ls)


def func(n, a, b):
	arr = []
	for i in range(n - 1, -1, -1):
		if a[i] == b[i]:
			continue
		if i == 0:
			arr.append(1)
			a[0] = (a[0] + 1) % 2
			continue
		if a[0] == b[i]:
			a[0] = (a[0] + 1) % 2
			arr.append(1)
		arr.append(i + 1)
		for i in range(i + 1):
			a[i] = (a[i] + 1) % 2
		s = 0
		t = i
		while s < t:
			(a[s], a[t]) = (a[t], a[s])
			s += 1
			t -= 1
	print(len(arr), *arr)
for _ in range(int(input())):
	n = int(input())
	a = [int(i) for i in input()]
	b = [int(i) for i in input()]
	func(n, a, b)


str1 = 'First'
str2 = 'Second'

def rev(c, cnt):
	if cnt % 2 == 0:
		return c
	if c == '0':
		c = '1'
	else:
		c = '0'
	return c

def solve():
	n = int(input())
	a = input()
	b = input()
	ans = list()
	(l, r) = (0, n - 1)
	for i in range(n):
		if i % 2 == 0:
			if rev(a[l], i) == b[n - i - 1]:
				ans.append(1)
			l += 1
		else:
			if rev(a[r], i) == b[n - i - 1]:
				ans.append(1)
			r -= 1
		ans.append(n - i)
	print(len(ans), sep=' ', end=' ')
	for num in ans:
		print(num, sep=' ', end=' ')
	print()

def main():
	t = int(input())
	for i in range(t):
		solve()
main()


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(input())
	b = list(input())
	ans = []
	for i in range(n - 1, -1, -1):
		if a[i] == b[i]:
			pass
		elif a[0] != b[i]:
			ans.append(i + 1)
			a[:i + 1] = [str(1 ^ int(num)) for num in a[:i + 1]]
			a[:i + 1] = a[:i + 1][::-1]
		else:
			ans.append(1)
			a[0] = str(1 ^ int(a[0]))
			ans.append(i + 1)
			a[:i + 1] = [str(1 ^ int(num)) for num in a[:i + 1]]
			a[:i + 1] = a[:i + 1][::-1]
	print(len(ans), end=' ')
	print(*ans)


import os
import sys
from io import BytesIO, IOBase
import threading
from bisect import bisect_right
from math import gcd, log
from collections import Counter

def main():
	n = int(input())
	a = [int(c) for c in input()]
	b = [int(c) for c in input()]
	ans = []
	for i in range(n - 1, -1, -1):
		if a[i] != b[i]:
			if b[i] == a[0]:
				ans.append(0)
				a[0] = 1 - b[i]
			for j in range((i + 1) // 2):
				(a[j], a[i - j]) = (a[i - j], a[j])
			for j in range(i + 1):
				a[j] = 1 - a[j]
			ans.append(i)
	for i in range(len(ans)):
		ans[i] += 1
	print(len(ans), *ans)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
for _ in range(int(input())):
	main()


def rp(s):
	c = ''
	for i in range(len(s)):
		if s[i] == '0':
			c += '1'
		else:
			c += '0'
	return c[::-1]
q = int(input())
for t in range(q):
	n = int(input())
	a = input()
	b = input()
	j = n - 1
	i = 0
	inv = 0
	ninv = 1
	p = []
	while j >= 0:
		if abs(inv - int(a[0])) == int(b[j]):
			a = rp(a[0]) + a[1:]
			p.append(1)
			a = rp(a[:j + 1]) + a[j + 1:]
			p.append(j + 1)
		else:
			a = rp(a[:j + 1]) + a[j + 1:]
			p.append(j + 1)
		j -= 1
	print(len(p), end=' ')
	print(*p)


def main():
	t = int(input())
	for i in range(t):
		n = int(input())
		str1 = input()
		str2 = input()
		anslist = []
		if str1 == str2:
			print(0)
		else:
			cnt = 0
			tmp = 0
			while cnt < n:
				cnt += 1
				flag = 0
				if cnt % 2 == 1:
					flag = cnt // 2
				else:
					flag = -cnt // 2
				if tmp % 2 == 0 and str1[flag] == str2[-cnt] or (tmp % 2 == 1 and str1[flag] != str2[-cnt]):
					anslist.append(1)
				anslist.append(n - cnt + 1)
				tmp += 1
			print(len(anslist), end=' ')
			for j in range(len(anslist) - 1):
				print(anslist[j], end=' ')
			print(anslist[len(anslist) - 1])
main()


for _ in range(int(input())):
	n = int(input())
	a = input() + '0'
	b = input() + '0'
	l1 = []
	l2 = []
	for i in range(1, n + 1):
		if a[i] != a[i - 1]:
			l1.append(i)
		if b[i] != b[i - 1]:
			l2.append(i)
	l2.reverse()
	l1.extend(l2)
	print(len(l1), *l1)


for t in range(int(input())):
	n = int(input())
	a = list(map(int, list(input())))
	b = list(map(int, list(input())))
	index = n - 1
	move = 0
	hist = []
	(flipped, reverse) = (False, False)
	(start, end) = (0, n - 1)
	while index > -1:
		if index == 0:
			if a[end] == b[index] and (not flipped) or (a[end] != b[index] and flipped):
				pass
			else:
				move = move + 1
				hist.append(1)
			index = index - 1
		elif a[end] == b[index] and (not flipped) or (a[end] != b[index] and flipped):
			index = index - 1
			if start < end:
				end = end - 1
			else:
				end = end + 1
		elif a[start] == b[index] and (not flipped) or (a[start] != b[index] and flipped):
			move = move + 1
			hist.append(1)
			a[start] = 1 - a[start]
		else:
			move = move + 1
			hist.append(abs(start - end) + 1)
			if start < end:
				(start, end) = (end, start + 1)
			else:
				(start, end) = (end, start - 1)
			if flipped:
				flipped = False
			else:
				flipped = True
			index = index - 1
	print(move, ' '.join(list(map(str, hist))))


def upd(a, q):
	a1 = a[0:q]
	a2 = a[q:]
	for x in range(q):
		a1[x] = 1 - a1[x]
	a1.reverse()
	a1.extend(a2)
	return a1
t = int(input())
for i in range(t):
	n = int(input())
	a = input()
	b = input()
	a = [int(x) for x in a]
	b = [int(x) for x in b]
	k = 0
	l = []
	for j in range(n - 1, -1, -1):
		if a[j] != b[j]:
			if a[0] == b[j]:
				a[0] = 1 - a[0]
				a = upd(a, j + 1)
				k += 2
				l.append(1)
				l.append(j + 1)
			else:
				a = upd(a, j + 1)
				k += 1
				l.append(j + 1)
	print(k, end=' ')
	print(*l)


t = int(input())
for i in range(t):
	n = int(input())
	a = input()
	b = input()
	count = 0
	r = []
	if n == 1:
		if a[0] != b[0]:
			print('1 1')
		else:
			print('0')
	else:
		for j in range(n):
			if a[j] != b[j]:
				count = count + 1
				r.append(j + 1)
				r.append(1)
				r.append(j + 1)
		print(count * 3, end=' ')
		for k in range(len(r)):
			print(r[k], end=' ')
		print()


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(input())
	b = list(input())
	la = []
	flgA = int(a[0])
	for i in range(n - 1):
		if a[i] != a[i + 1]:
			la.append(i + 1)
			flgA = (flgA - 1) % 2
	lb = []
	flgB = int(b[0])
	for i in range(n - 1):
		if b[i] != b[i + 1]:
			lb.append(i + 1)
			flgB = (flgB - 1) % 2
	if flgA != flgB:
		la.append(n)
	ans = la + list(reversed(lb))
	print(len(ans), ' '.join((str(n) for n in ans)))


from sys import *
t = int(stdin.readline())
for _i in range(t):
	n = int(stdin.readline())
	a = stdin.readline()
	b = stdin.readline()
	l = []
	count = 0
	for i in range(n):
		if a[i] != b[i]:
			l.append(i + 1)
			count += 1
	if count == 0:
		print(count)
	else:
		print(3 * count, end=' ')
		for j in range(len(l)):
			print(l[j], 1, l[j], end=' ')
		print()


import sys
import string
from heapq import *
from bisect import *
from collections import Counter, defaultdict
from math import fsum, sqrt, gcd, ceil, factorial
from operator import *
from itertools import accumulate
inf = float('inf')
flush = lambda : sys.stdout.flush
comb = lambda x, y: factorial(x) // factorial(y) // factorial(x - y)
en = lambda x: list(enumerate(x))
ip = lambda : input()
ii = lambda : int(input())
r = lambda : map(int, input().split())
rr = lambda : list(r())

def solve():
	n = ii()
	(a, b) = (ip(), ip())
	arr = []
	for i in range(n):
		if a[i] != b[i]:
			arr += [i + 1, 1, i + 1]
	print(len(arr), *arr)
for _ in range(ii()):
	solve()


import sys
from collections import defaultdict
mod = 10 ** 9 + 7
INF = float('inf')

def inp():
	return int(sys.stdin.readline())

def inpl():
	return list(map(int, sys.stdin.readline().split()))
t = inp()
for _ in range(t):
	n = inp()
	a = input()
	b = input()
	res = []
	for i in range(n - 1):
		if a[i] != a[i + 1]:
			res.append(i + 1)
	now = a[-1]
	for i in range(n)[::-1]:
		if now != b[i]:
			res.append(i + 1)
			now = b[i]
	print(len(res), *res)


import os, sys, time, collections, math, pprint, itertools as it, operator as op, bisect as bs, functools as fn
(maxx, localsys, mod) = (1 << 60, 0, int(1000000000.0 + 7))
nCr = lambda n, r: reduce(mul, range(n - r + 1, n + 1), 1) // factorial(r)
ceil = lambda n, x: (n + x - 1) // x
(osi, oso) = ('/home/priyanshu/Documents/cp/input.txt', '/home/priyanshu/Documents/cp/output.txt')
if os.path.exists(osi):
	sys.stdin = open(osi, 'r')
	sys.stdout = open(oso, 'w')
input = sys.stdin.readline
from functools import reduce

def maps():
	return map(int, input().split())
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().rstrip('\n')))
	b = list(map(int, input().rstrip('\n')))
	ans = []
	for i in range(n):
		if a[i] != b[i]:
			ans.append(i + 1)
			ans.append(1)
			ans.append(i + 1)
	print(len(ans), *ans)


for _ in range(int(input())):
	N = int(input())
	A = list(input())
	B = list(input())
	countlist = []
	countlistB = []
	count = 1
	for i in range(1, N):
		if A[i] != A[i - 1]:
			countlist.append(count)
			count = 1
		else:
			count += 1
	countlist.append(count)
	countB = 1
	for i in range(1, N):
		if B[i] != B[i - 1]:
			countlistB.append(countB)
			countB = 1
		else:
			countB += 1
	countlistB.append(countB)
	prefix = []
	prefix.append(countlist[0])
	for i in range(1, len(countlist)):
		prefix.append(countlist[i] + prefix[-1])
	prefixB = []
	prefixB.append(countlistB[0])
	for i in range(1, len(countlistB)):
		prefixB.append(countlistB[i] + prefixB[-1])
	ans = 0
	if len(countlist) % 2 == 0:
		if A[0] == B[-1]:
			ans = len(prefix) + len(prefixB) - 1
			print(ans, end=' ')
			for i in range(len(prefix)):
				print(prefix[i], end=' ')
			for i in range(len(prefixB) - 2, -1, -1):
				print(prefixB[i], end=' ')
			print()
		else:
			ans = len(prefix) + len(prefixB)
			print(ans, end=' ')
			for i in range(len(prefix)):
				print(prefix[i], end=' ')
			for i in range(len(prefixB) - 1, -1, -1):
				print(prefixB[i], end=' ')
			print()
	elif A[0] != B[-1]:
		ans = len(prefix) + len(prefixB) - 1
		print(ans, end=' ')
		for i in range(len(prefix)):
			print(prefix[i], end=' ')
		for i in range(len(prefixB) - 2, -1, -1):
			print(prefixB[i], end=' ')
		print()
	else:
		ans = len(prefix) + len(prefixB)
		print(ans, end=' ')
		for i in range(len(prefix)):
			print(prefix[i], end=' ')
		for i in range(len(prefixB) - 1, -1, -1):
			print(prefixB[i], end=' ')
		print()


for _ in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	operations = []
	for i in range(n - 1):
		if a[i] != a[i + 1]:
			operations.append(i + 1)
	if a[-1] == '1':
		operations.append(n)
	if b[-1] == '1':
		operations.append(n)
	for i in reversed(range(1, n)):
		if b[i] != b[i - 1]:
			operations.append(i)
	print(len(operations), end=' ')
	print(*operations)


def solve(s):
	new = ''
	for i in s:
		if i == '1':
			new += '0'
		else:
			new += '1'
	return new[::-1]
for nt in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	ans = []
	for i in range(n - 1, -1, -1):
		if a[i] != b[i]:
			if a[i] == a[0]:
				ans.append(i + 1)
				a = solve(a[0:i + 1]) + a[i + 1:]
			else:
				ans.append(1)
				ans.append(i + 1)
				a = solve(a[i] + a[1:i + 1]) + a[i + 1:]
	print(len(ans), end=' ')
	print(*ans)


I = lambda : list(map(int, input().split()))
import sys
for tc in range(int(input())):
	(n,) = I()
	s = list(input())
	t = list(input())
	ans = []
	for i in range(n - 1, -1, -1):
		if s[i] != t[i]:
			if s[0] != s[i]:
				if s[0] != s[i]:
					s[0] = s[i]
					ans.append(1)
			for j in range(i + 1):
				s[j] = '1' if s[j] == '0' else '0'
			ans.append(i + 1)
			if i == n - 1:
				s = s[::-1]
			else:
				temp = s[:i + 1]
				temp = temp[::-1]
				s = temp + s[i + 1:]
	print(len(ans), *ans)


import math
t = int(input())
rez = []

def rev(a, n):
	l = 0
	r = n
	for j in range(n + 1):
		if a[j] == '0':
			a[j] = '1'
		else:
			a[j] = '0'
	while l < r:
		buf = a[l]
		a[l] = a[r]
		a[r] = buf
		l += 1
		r -= 1
	return a
for i in range(t):
	n = int(input())
	A = input()
	B = input()
	a = []
	b = []
	for j in range(n):
		a.append(A[j])
		b.append(B[j])
	k = 0
	m = n - 1
	p = ''
	while m > 0:
		if a[m] == b[m]:
			m -= 1
		else:
			if a[0] == b[m]:
				k += 2
				p += '1 '
				p += str(m + 1) + ' '
				a = rev(a, 0)
				a = rev(a, m)
			else:
				k += 1
				p += str(m + 1) + ' '
				a = rev(a, m)
			m -= 1
	if a[0] != b[0]:
		k += 1
		p += '1 '
	rez.append(str(k) + ' ' + p)
for i in range(len(rez)):
	print(rez[i])


for _ in range(int(input())):
	n = int(input())
	s = input()
	s1 = input()
	s = list((str(x) for x in s))
	s1 = list((str(x) for x in s1))
	c = []
	for i in range(n - 1, -1, -1):
		if s[len(s) - 1] == s1[i]:
			s = s[:len(s) - 1]
			continue
		if int(s1[i]) + int(s[0]) == 1:
			c.append(i + 1)
			s = s[:i + 1]
			for i in range(len(s)):
				if s[i] == '0':
					s[i] = '1'
				else:
					s[i] = '0'
			s = s[::-1]
			s = s[:len(s) - 1]
			continue
		if s1[i] == s[0]:
			if s1[i] == '1':
				s[0] = '0'
				c.append(1)
			else:
				c.append(1)
				s[0] = '1'
			c.append(i + 1)
			s = s[:i + 1]
			for i in range(len(s)):
				if s[i] == '0':
					s[i] = '1'
				else:
					s[i] = '0'
			s = s[::-1]
			s = s[:len(s) - 1]
			continue
	if len(c) == 0:
		print(0)
		continue
	print(len(c), *c)


from sys import stdin
input = stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	a = input().rstrip()
	b = input().rstrip()
	out = []
	for i in range(n - 1):
		if b[n - i - 1] == a[i]:
			out.append(1)
			out.append(n - i)
			out.append(n - i - 1)
		else:
			out.append(n - i)
			out.append(n - i - 1)
	if a[-1] != b[0]:
		out.append(1)
	print(len(out), *out)


import math
from collections import *
import sys
from functools import reduce
import time
from itertools import groupby

def input():
	return sys.stdin.readline()

def get_ints():
	return map(int, input().strip().split())

def get_list():
	return list(get_ints())

def get_string():
	return list(input().strip().split())

def printxsp(*args):
	return print(*args, end='')

def printsp(*args):
	return print(*args, end=' ')
DIRECTIONS = [(+0, +1), (+0, -1), (+1, +0), (+1, -1)]
NEIGHBOURS = [(-1, -1), (-1, +0), (-1, +1), (+0, -1), (+1, +1), (+1, +0), (+1, -1), (+0, +1)]
CAPS_ALPHABETS = {chr(i + ord('A')): i for i in range(26)}
SMOL_ALPHABETS = {chr(i + ord('a')): i for i in range(26)}
INF = float('inf')
S34t = time.time()
for _testcases_ in range(int(input())):
	n = int(input())
	a = list(input())
	b = list(input())
	j = n - 1
	moves = 0
	ans = []
	while j >= 0:
		if a[j] != b[j]:
			if a[0] == b[j]:
				moves += 1
				if a[0] == '1':
					a[0] = '0'
				else:
					a[0] = '1'
				ans.append(1)
			for i in range((j + 2) // 2):
				if i == j - i:
					if a[i] == '1':
						a[i] = '0'
					else:
						a[i] = '1'
				else:
					(a[i], a[j - i]) = (a[j - i], a[i])
					if a[i] == '0':
						a[i] = '1'
					else:
						a[i] = '0'
					if a[j - i] == '0':
						a[j - i] = '1'
					else:
						a[j - i] = '0'
			moves += 1
			ans.append(j + 1)
		j -= 1
	print(moves, *ans)
S34p = time.time()


def ind(a, b, m):
	while m > -1:
		if a[m] == b[m]:
			m -= 1
		else:
			return m
	return m

def co(a, m):
	for i in range(m + 1):
		if a[i] == '0':
			a[i] = '1'
		else:
			a[i] = '0'
	b = a[:m + 1]
	b.reverse()
	a = b + a[m + 1:]
	return a
for i in range(int(input())):
	ans = []
	n = int(input())
	a = input()
	a = list(a)
	b = input()
	b = list(b)
	m = n - 1
	m = ind(a, b, m)
	while m != -1:
		if a[0] == b[m]:
			ans.append(1)
			a = co(a, 0)
		else:
			ans.append(m + 1)
			a = co(a, m)
			m -= 1
		m = ind(a, b, m)
	if len(ans) > 0:
		print(len(ans), end=' ')
		for j in range(len(ans) - 1):
			print(ans[j], end=' ')
		print(ans[len(ans) - 1])
	else:
		print(0)


import sys
import math

def II():
	return int(sys.stdin.readline())

def LI():
	return list(map(int, sys.stdin.readline().split()))

def MI():
	return map(int, sys.stdin.readline().split())

def SI():
	return sys.stdin.readline().strip()
t = II()
for q in range(t):
	n = II()
	a = list(SI())
	b = list(SI())
	ans = []
	count = 0
	for i in range(n - 1, -1, -1):
		if count % 2 == 0:
			ind = count // 2
		else:
			ind = n - (count + 1) // 2
			if a[ind] == '0':
				a[ind] = '1'
			else:
				a[ind] = '0'
		if b[i] == a[ind]:
			ans.append(1)
			ans.append(n - count)
		else:
			ans.append(n - count)
		count += 1
	ans = [len(ans)] + ans
	print(*ans)


production = True
import sys, math, collections

def input(input_format=0, multi=0):
	if multi > 0:
		return [input(input_format) for i in range(multi)]
	else:
		next_line = sys.stdin.readline()[:-1]
		if input_format >= 10:
			use_list = False
			input_format = int(str(input_format)[-1])
		else:
			use_list = True
		if input_format == 0:
			formatted_input = [next_line]
		elif input_format == 1:
			formatted_input = list(map(int, next_line.split()))
		elif input_format == 2:
			formatted_input = list(map(float, next_line.split()))
		elif input_format == 3:
			formatted_input = list(next_line)
		elif input_format == 4:
			formatted_input = list(map(int, list(next_line)))
		elif input_format == 5:
			formatted_input = next_line.split()
		else:
			formatted_input = [next_line]
		return formatted_input if use_list else formatted_input[0]

def out(output_line, output_format=0, newline=True):
	formatted_output = ''
	if output_format == 0:
		formatted_output = str(output_line)
	elif output_format == 1:
		formatted_output = ' '.join(map(str, output_line))
	elif output_format == 2:
		formatted_output = '\n'.join(map(str, output_line))
	print(formatted_output, end='\n' if newline else '')

def log(*args):
	if not production:
		print('$$$', end='')
		print(*args)
enu = enumerate
ter = lambda a, b, c: b if a else c
ceil = lambda a, b: -(-a // b)

def mapl(iterable, format=0):
	if format == 0:
		return list(map(int, iterable))
	elif format == 1:
		return list(map(str, iterable))
	elif format == 2:
		return list(map(list, iterable))

def f(a, l, r):
	k = []
	for i in a[l:r]:
		k.append(ter(i, 0, 1))
	c = a[:l] + k[::-1] + a[r:]
	return c

def g(n):
	d = []
	t = 0
	for (i, j) in zip(range(n // 2 + 1), range(n - 1, n // 2 - 1, -1)):
		t += 1
		d.append((i, t & 1))
		if len(d) == n:
			break
		t += 1
		d.append((j, t & 1))
	return d

def solve():
	n = input(11)
	a = input(4)
	b = input(4)
	s = []
	log(a)
	d = g(n)
	log(d)
	dn = []
	for (i, (j, k)) in enu(d):
		dn.append((j, k, b[n - i - 1]))
	d = dn
	log(d)
	log(d)
	s2 = []
	if n == 1:
		if a == b:
			out(0)
		else:
			out('1 1')
		return
	log(a)
	for (i, (j, k, bi)) in enu(d):
		log(i, bi, j, k)
		if k:
			k = ter(a[j], 0, 1)
		else:
			k = a[j]
		log(a[j], k)
		if bi != k:
			s2.append(1)
		s2.append(n - i)
	out([len(s2)] + s2, 1)
	return
for i in range(input(11)):
	solve()


import math
for ii in range(int(input())):
	n = int(input())
	a = list(input())
	b = list(input())
	j = 0
	ans = []
	for i in range(n - 1, -1, -1):
		if a[i] != b[i]:
			if a[0] != b[i]:
				ans.append(i + 1)
				for j in range(i, -1, -1):
					if a[j] == '1':
						a[j] = '0'
					else:
						a[j] = '1'
				a = a[i::-1] + a[i + 1:]
			else:
				ans.append(1)
				ans.append(i + 1)
				if a[0] == '0':
					a[0] = '1'
				else:
					a[0] = '0'
				for j in range(i, -1, -1):
					if a[j] == '1':
						a[j] = '0'
					else:
						a[j] = '1'
				a = a[i::-1] + a[i + 1:]
	print(len(ans), *ans)


import sys
t = int(input())
for _ in range(t):
	n = int(input())
	a = input()
	b = input()
	var = 0
	ans = []
	for i in range(n):
		if a[i] == b[i]:
			continue
		ans.append(i + 1)
		ans.append(1)
		ans.append(i + 1)
		var += 3
	print(var, end=' ')
	for i in ans:
		print(i, end=' ')
	print()


import sys

def flip(src, n):
	old_prefix = src[:n]
	for i in range(n):
		src[n - i - 1] = 1 - int(old_prefix[i])

def transform(src, target):
	res = []
	for i in range(len(src)):
		if src[i] != target[i]:
			flip(src, i + 1)
			res.append(i + 1)
			flip(src, 1)
			res.append(1)
			flip(src, i + 1)
			res.append(i + 1)
	print(len(res), *res)
t = int(sys.stdin.readline().strip())
for i in range(t):
	N = int(sys.stdin.readline().strip())
	a = []
	for i in sys.stdin.readline().strip():
		a.append(i)
	b = []
	for i in sys.stdin.readline().strip():
		b.append(i)
	transform(a, b)


from collections import defaultdict as dd
from collections import deque
import bisect
import heapq

def ri():
	return int(input())

def rl():
	return list(map(int, input().split()))

def solve():
	n = ri()
	s = list(map(int, input()))
	t = list(map(int, input()))
	moves = []
	prev_flip = n - 1
	rng = [0, n - 1]

	def UPDATE(pos, prev_flip, rng):
		d = prev_flip - pos
		prev_flip = pos
		(a, b) = rng
		if a < b:
			rng = [b - d, a]
		else:
			rng = [b + d, a]
		return (prev_flip, rng)

	def STATUS(pos, rng):
		(a, b) = rng
		if a <= b:
			orig = s[a + pos]
			return orig
		else:
			orig = s[a - pos]
			return 1 - orig
	for pos in range(n - 1, 0, -1):
		if STATUS(pos, rng) == t[pos]:
			continue
		elif STATUS(0, rng) != t[pos]:
			moves.append(pos + 1)
			(prev_flip, rng) = UPDATE(pos, prev_flip, rng)
		else:
			moves.append(1)
			moves.append(pos + 1)
			(prev_flip, rng) = UPDATE(pos, prev_flip, rng)
	if STATUS(0, rng) != t[0]:
		moves.append(1)
	print(len(moves), *moves)
mode = 'T'
if mode == 'T':
	t = ri()
	for i in range(t):
		solve()
else:
	solve()


import sys, math, cmath, time
start_time = time.time()

def solve():
	n = inp()
	a = insr() + '#'
	b = insr() + '#'
	if a == b:
		print(0)
		return
	q = []
	w = []
	for i in range(len(a) - 1):
		if a[i] != a[i + 1]:
			q.append(i + 1)
	for i in range(len(b) - 1):
		if b[i] != b[i + 1]:
			w.append(i + 1)
	w.reverse()
	if a[-2] != b[-2]:
		w.pop(0)
	qw = q + w
	print(len(qw), *qw)

def main():
	global tt
	if not ONLINE_JUDGE:
		sys.stdin = open('input.txt', 'r')
		sys.stdout = open('output.txt', 'w')
	t = 1
	t = inp()
	for tt in range(t):
		solve()
	if not ONLINE_JUDGE:
		print('Time Elapsed :', time.time() - start_time, 'seconds')
		sys.stdout.close()

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	return input().strip()

def invr():
	return map(int, input().split())

def counter(a):
	q = [0] * max(a)
	for i in range(len(a)):
		q[a[i] - 1] = q[a[i] - 1] + 1
	return q

def counter_elements(a):
	q = dict()
	for i in range(len(a)):
		if a[i] not in q:
			q[a[i]] = 0
		q[a[i]] = q[a[i]] + 1
	return q

def string_counter(a):
	q = [0] * 26
	for i in range(len(a)):
		q[ord(a[i]) - 97] = (q[ord(a[i]) - 97], 1)
	return q

def factors(n):
	q = []
	for i in range(1, int(n ** 0.5) + 1):
		if n % i == 0:
			q.append(i)
			q.append(n // i)
	return list(sorted(list(set(q))))

def prime_factors(n):
	q = []
	while n % 2 == 0:
		q.append(2)
		n = n // 2
	for i in range(3, int(n ** 0.5) + 1, 2):
		while n % i == 0:
			q.append(i)
			n = n // i
	if n > 2:
		q.append(n)
	return list(sorted(q))

def transpose(a):
	(n, m) = (len(a), len(a[0]))
	b = [[0] * n for i in range(m)]
	for i in range(m):
		for j in range(n):
			b[i][j] = a[j][i]
	return b
ONLINE_JUDGE = __debug__
if ONLINE_JUDGE:
	input = sys.stdin.readline
main()


t = int(input())
for _ in range(t):
	n = int(input())
	a = input()
	b = input()
	ans = []
	cur = a[0]
	inv = 0
	in0 = 0
	arr = [0] * n
	ar1 = [i for i in range(1, (n + 1) // 2 + 1)]
	ar2 = [i for i in range(n, (n + 1) // 2, -1)]
	na = []
	for i in range(len(ar2)):
		na.append(ar1[i] - 1)
		na.append(ar2[i] - 1)
	if n % 2 == 1:
		na.append(ar1[-1] - 1)
	cnt = 0
	for i in range(n - 1, -1, -1):
		tmp = a[na[cnt]]
		if cnt % 2 == 1:
			cur = '0' if tmp == '1' else '1'
		else:
			cur = tmp
		if b[i] == cur:
			ans.append(1)
			ans.append(i + 1)
		else:
			ans.append(i + 1)
		inv += 1
		cnt += 1
	print(len(ans), end=' ')
	for i in ans:
		print(i, end=' ')
	print()


t = int(input())
for _ in range(t):
	n = int(input())
	a = input()
	b = input()
	c = 0
	va = []
	vb = []
	if a == b:
		print(0)
	else:
		for i in range(1, n):
			if a[i] != a[i - 1]:
				c += 1
				va.append(str(i))
		if a[n - 1] != b[n - 1]:
			c += 1
			va.append(str(n))
		for i in range(1, n):
			if b[i] != b[i - 1]:
				c += 1
				vb.append(str(i))
		print(' '.join([str(len(va) + len(vb))] + va + vb[::-1]))


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(input().rstrip('\n'))
	arr1 = list(input().rstrip('\n'))
	arr = [int(x) for x in arr]
	arr1 = [int(x) for x in arr1]
	result = []
	if arr == arr1:
		print(0)
	else:
		if n == 1:
			print('1 1')
			continue
		prev = arr[0]
		for i in range(1, n):
			if prev != arr[i]:
				result.append(i)
			prev = arr[i]
		if arr[-1] == 1:
			last = 1
		else:
			last = 0
		for i in range(n - 1, -1, -1):
			if arr1[i] != last:
				result.append(i + 1)
				if last == 0:
					last = 1
				else:
					last = 0
		print(len(result), end=' ')
		print(*result)


I = input
for _ in [0] * int(I()):
	I()
	i = 0
	r = []
	for (x, y) in zip(I(), I()):
		i += 1
		r += ([i, 1, i], [])[x == y]
	print(len(r), *r)


import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, copy, functools
inf = 10 ** 20
eps = 1.0 / 10 ** 10
mod = 10 ** 9 + 7
mod2 = 998244353
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]

def LI():
	return list(map(int, sys.stdin.readline().split()))

def LLI():
	return [list(map(int, l.split())) for l in sys.stdin.readlines()]

def LI_():
	return [int(x) - 1 for x in sys.stdin.readline().split()]

def LF():
	return [float(x) for x in sys.stdin.readline().split()]

def LS():
	return sys.stdin.readline().split()

def I():
	return int(sys.stdin.readline())

def F():
	return float(sys.stdin.readline())

def S():
	return input()

def pf(s):
	return print(s, flush=True)

def pe(s):
	return print(str(s), file=sys.stderr)

def JA(a, sep):
	return sep.join(map(str, a))

def JAA(a, s, t):
	return s.join((t.join(map(str, b)) for b in a))

def IF(c, t, f):
	return t if c else f

def YES(c):
	return IF(c, 'YES', 'NO')

def Yes(c):
	return IF(c, 'Yes', 'No')

def main():
	t = I()
	rr = []
	for _ in range(t):
		n = I()
		a = [int(c) for c in S()]
		b = [int(c) for c in S()]
		l = 0
		r = n - 1
		sf = 0
		t = []
		for i in range(n - 1, 0, -1):
			c = b[i]
			if sf:
				if a[l] != c:
					l += 1
				else:
					if a[r] != c:
						t.append(1)
					t.append(i + 1)
					r -= 1
					sf = 0
			elif a[r] == c:
				r -= 1
			else:
				if a[l] == c:
					t.append(1)
				t.append(i + 1)
				l += 1
				sf = 1
		c = b[0]
		if sf and a[r] == c or (not sf and a[l] != c):
			t.append(1)
		rr.append(JA([len(t)] + t, ' '))
	return JA(rr, '\n')
print(main())


def incon():
	A = input()
	A = [int(x) for x in A]
	return A

def reverse(A):
	for i in range(len(A)):
		A[i] = (A[i] + 1) % 2
	return A
T = int(input())
while T > 0:
	T -= 1
	N = int(input())
	A = incon()
	B = incon()
	ans = []
	for i in range(N):
		if A[N - i - 1] != B[N - i - 1]:
			if A[0] == B[N - i - 1]:
				ans.append(1)
				A[0] = (A[0] + 1) % 2
				ans.append(N - i)
			else:
				ans.append(N - i)
			A[0:N - i] = reverse(A[0:N - i])
			A[0:N - i] = A[0:N - i][::-1]
	print(len(ans), end=' ')
	for j in ans:
		print(j, end=' ')
	print()


for _ in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	l = []
	a = list(a)
	b = list(b)
	i = 0
	if a == b:
		print(0)
		continue
	while i < n - 1:
		if a[i] != a[i + 1]:
			l.append(i + 1)
		i += 1
	if a[-1] == '1':
		l.append(n)
	a = b
	k = []
	i = 0
	while i < n - 1:
		if a[i] != a[i + 1]:
			k.append(i + 1)
		i += 1
	if a[-1] == '1':
		k.append(n)
	l = l + k[::-1]
	print(len(l), end=' ')
	for i in l:
		print(i, end=' ')
	print()


def flip(bit):
	if bit == 0:
		return 1
	else:
		return 0
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input()))
	b = list(map(int, input()))
	check = n - 1
	steps = 0
	ans = []
	for i in range(n - 1, -1, -1):
		if a[i] == b[check]:
			check -= 1
			continue
		if a[0] == b[check]:
			a[0] = flip(a[0])
			steps += 1
			ans.append(1)
		for j in range(check + 1):
			a[j] = flip(a[j])
		a[:check + 1] = a[check::-1]
		steps += 1
		ans.append(check + 1)
		check -= 1
	print(steps, ' '.join(map(str, ans)))


for _ in range(int(input())):
	n = int(input())
	a = input('')
	a = list(map(int, a))
	b = input('')
	b = list(map(int, b))
	if a == b:
		print(0)
		continue

	def reverse(n):
		for i in range(n // 2):
			a[i] ^= 1
			a[n - i - 1] ^= 1
			(a[i], a[n - i - 1]) = (a[n - i - 1], a[i])
		if n & 1:
			a[n // 2] ^= 1
	ans = []
	count = 0
	flip = 0
	last = n - 1
	start = 0
	index = 0
	j = n - 1
	while start <= last and j >= 0:
		if index % 2 == 0:
			if flip & 1:
				a[start] ^= 1
			if a[start] == b[j]:
				count += 1
				a[start] ^= 1
				ans.append(1)
			count += 1
			flip += 1
			ans.append(j + 1)
			start += 1
		else:
			if flip & 1:
				a[last] ^= 1
			if a[last] == b[j]:
				count += 1
				a[last] ^= 1
				ans.append(1)
			count += 1
			flip += 1
			ans.append(j + 1)
			last -= 1
		j -= 1
		index += 1
	print(count, *ans)


for _ in range(int(input())):
	n = int(input())
	arr = input()
	arr2 = input()
	ans = []
	for i in range(n):
		if arr[i] != arr2[i]:
			ans.append(i + 1)
			ans.append(1)
			ans.append(i + 1)
	print(len(ans), *ans)


for tt in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	rev = False
	index = 0
	moves = []
	for i in range(n - 1, 0, -1):
		if rev:
			bit = str((int(a[n - 1 - index]) + 1) % 2)
			if bit == b[i]:
				moves.append(1)
			moves.append(i + 1)
			index += 1
			rev = False
		else:
			bit = a[index]
			if bit == b[i]:
				moves.append(1)
			moves.append(i + 1)
			rev = True
	if rev:
		bit = str((int(a[n - 1 - index]) + 1) % 2)
	else:
		bit = a[index]
	if bit != b[0]:
		moves.append(1)
	s = ''
	for i in moves:
		s += str(i) + ' '
	print(len(moves), s)


for _ in range(int(input())):
	n = int(input())
	s = input()
	b = input()
	A = []
	for i in range(n - 1):
		if s[i] != s[i + 1]:
			A.append(i + 1)
	if s[-1] == '1':
		A.append(n)
	A1 = []
	for i in range(n - 1):
		if b[i] != b[i + 1]:
			A1.append(i + 1)
	if b[-1] == '1':
		A1.append(n)
	A += A1[::-1]
	print(len(A), *A)


t = int(input())
while t:
	n = int(input())
	a = list(map(int, input()))
	b = list(map(int, input()))
	ans = []
	for i in range(n - 1, -1, -1):
		if a[i] != b[i]:
			if a[0] == b[i]:
				a[0] = 1 - a[0]
				ans.append(1)
			(j, k) = (0, i)
			while j < k:
				(a[j], a[k]) = (1 - a[k], 1 - a[j])
				j += 1
				k -= 1
			if j == k:
				a[j] = 1 - a[j]
			ans.append(i + 1)
	print(len(ans), end=' ')
	for x in ans:
		print(x, end=' ')
	print()
	t -= 1


for i in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	res = []
	e = n
	for j in range(n):
		if j % 2 == 0:
			d = a[j // 2]
		else:
			d = a[n - j // 2 - 1]
			d = (d == '1') * '0' + '1' * (d == '0')
		c = b[n - j - 1]
		if c == d:
			res.append(1)
			res.append(e)
		else:
			res.append(e)
		e = e - 1
	print(len(res), *res)


import sys
RI = lambda : [int(x) for x in sys.stdin.readline().strip().split()]
rw = lambda : sys.stdin.readline().strip().split()
ri = lambda : int(sys.stdin.readline().strip())
rs = lambda : sys.stdin.readline().strip()
from collections import defaultdict as df
from math import *
t = ri()
for _ in range(t):
	n = ri()
	l = list(rs())
	l = [int(i) for i in l]
	m = list(rs())
	m = [int(i) for i in m]
	bit = 1
	ans = []
	for i in range(n - 1, -1, -1):
		if l[i] == m[i]:
			continue
		if l[i] != l[0]:
			ans.append(1)
			l[0] ^= 1
			ans.append(i + 1)
			for j in range(i + 1):
				l[j] ^= 1
			l = list(reversed(l[:i + 1])) + l[i + 1:]
		else:
			ans.append(i + 1)
			for j in range(i + 1):
				l[j] ^= 1
			l = list(reversed(l[:i + 1])) + l[i + 1:]
	print(len(ans), *ans)


for _ in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	ans = []
	(inv, rev) = (0, 0)
	cnt = 0
	for i in range(n - 1, -1, -1):
		c = cnt // 2
		if cnt % 2 == 1:
			rev = n - c - 1
		else:
			rev = c
		front = a[rev]
		if inv % 2 == 1:
			if front == '0':
				front = '1'
			else:
				front = '0'
		if front == b[i]:
			ans.append(1)
		inv += 1
		ans.append(i + 1)
		cnt += 1
	print(len(ans), *ans)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
testcases = int(input())
for j in range(testcases):
	n = int(input())
	vala = [int(b) for b in input()]
	valb = [int(b) for b in input()]
	outy = []
	flips = 0
	adj = 0
	for s in range(n - 1, -1, -1):
		if flips % 2 == 0:
			ind = adj + s
			if (vala[ind] + flips) % 2 == valb[s]:
				continue
			else:
				ind0 = adj
				if (vala[ind0] + flips) % 2 != valb[s]:
					if flips % 2 == 0:
						adj += s
					else:
						adj -= s
				else:
					if flips % 2 == 0:
						adj += s
					else:
						adj -= s
					outy.append(1)
				outy.append(s + 1)
				flips += 1
		else:
			ind = adj - s
			if (vala[ind] + flips) % 2 == valb[s]:
				continue
			else:
				ind0 = adj
				if (vala[ind0] + flips) % 2 != valb[s]:
					if flips % 2 == 0:
						adj += s
					else:
						adj -= s
				else:
					if flips % 2 == 0:
						adj += s
					else:
						adj -= s
					outy.append(1)
				outy.append(s + 1)
				flips += 1
	print(len(outy), *outy)


t = int(input())
for _ in range(t):
	n = int(input())
	a = input()
	b = input()
	aToAllOnesOrZeros = []
	for i in range(n - 1):
		if a[i] != a[i + 1]:
			aToAllOnesOrZeros.append(i + 1)
	bToAllOnesOrZeros = []
	for i in range(n - 1):
		if b[i] != b[i + 1]:
			bToAllOnesOrZeros.append(i + 1)
	res = aToAllOnesOrZeros
	if a[-1] != b[-1]:
		res.append(n)
	for i in range(len(bToAllOnesOrZeros) - 1, -1, -1):
		res.append(bToAllOnesOrZeros[i])
	l = len(res)
	res.insert(0, l)
	print(' '.join([str(x) for x in res]))


t = int(input())
while t != 0:
	n = int(input())
	s1 = list(input())
	s2 = list(input())
	i = n - 1
	ans = []
	count = 0
	start = 0
	endi = n - 1
	flag = 0
	for i in range(n - 1, -1, -1):
		if flag == 0:
			if s2[i] == s1[start]:
				ans.append(1)
			ans.append(i + 1)
			start += 1
			flag = 1
		else:
			if s2[i] != s1[endi]:
				ans.append(1)
			ans.append(i + 1)
			flag = 0
			endi -= 1
	print(len(ans), *ans)
	t -= 1


from sys import stdin
from collections import deque
mod = 10 ** 9 + 7
import sys
import random
from queue import PriorityQueue
from collections import Counter as cc
from bisect import bisect_right
from bisect import bisect_left
from collections import defaultdict
from math import sqrt, factorial, gcd, log2, inf, ceil
import heapq
input = lambda : sys.stdin.readline().rstrip()
from sys import stdin, stdout
from heapq import heapify, heappush, heappop
from itertools import permutations
from math import factorial as f

def ncr(n, r, p):
	num = den = 1
	for i in range(r):
		num = num * (n - i) % p
		den = den * (i + 1) % p
	return num * pow(den, p - 2, p) % p
import sys
t = int(input())
for _ in range(t):
	n = int(input())
	s1 = list(input())
	s2 = list(input())
	ans = []
	cnt = 0
	for i in range(n - 1, -1, -1):
		if s1[i] == s2[i]:
			continue
		else:
			if s2[i] == s1[0]:
				ans.append(1)
				s1[0] = str(1 ^ int(s1[0]))
			for j in range(i + 1):
				s1[j] = str(1 ^ int(s1[j]))
			ans.append(i + 1)
			s1 = s1[:i + 1][::-1] + s1[i + 1:]
	print(len(ans), *ans)


def op(s, end):
	l = []
	for i in range(end, -1, -1):
		if s[i] == '0':
			l.append('1')
		else:
			l.append('0')
	return l

def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		a = list(input())
		b = list(input())
		o = []
		z = n - 1
		while z >= 0:
			if a[z] == b[z]:
				z -= 1
				continue
			if a[0] != b[z]:
				o.append(z + 1)
				a = op(a, z)
				z -= 1
			else:
				o.append('1')
				a[0] = '1' if a[0] == '0' else '0'
		print(len(o), *o)
main()


import sys
input = sys.stdin.readline
from collections import deque
t = int(input())
for _ in range(t):
	n = int(input())
	a = input()
	b = input()
	q = deque([i for i in range(n)])
	rev = False
	ans = []
	for i in range(n)[::-1]:
		if rev:
			a_ind = q[0]
			if a[a_ind] != b[i]:
				q.popleft()
				continue
			else:
				if a[q[-1]] == b[i]:
					ans.append(i + 1)
				else:
					ans.append(1)
					ans.append(i + 1)
				q.pop()
				rev = False
		else:
			a_ind = q[-1]
			if a[a_ind] == b[i]:
				q.pop()
				continue
			else:
				if a[q[0]] != b[i]:
					ans.append(i + 1)
				else:
					ans.append(1)
					ans.append(i + 1)
				q.popleft()
				rev = True
	print(len(ans), *ans)


t = int(input())
for i in range(t):
	n = int(input())
	realbin = list(input())
	truebin = list(input())
	counter = 0
	moves = []
	for i in range(n):
		if realbin[i] != truebin[i]:
			if i == 0:
				counter += 1
				moves.append(i + 1)
			else:
				counter += 3
				moves.append(i + 1)
				moves.append(1)
				moves.append(i + 1)
	print(counter, *moves)


from collections import deque

def f(v):
	if v == '1':
		return '0'
	else:
		return '1'

def e(x):
	if x % 2 == 0:
		return a[x // 2]
	else:
		return f(a[-(x // 2 + 1)])
for _ in range(int(input())):
	n = int(input())
	a = list(input())
	b = list(input())
	m = []
	i = 1
	while i <= n:
		if e(i - 1) == b[-i]:
			m.append(1)
			m.append(n - i + 1)
		else:
			m.append(n - i + 1)
		i += 1
	print(len(m), *m)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, list(input())))
	b = list(map(int, list(input())))
	changes = []
	k = 0
	for i in range(n):
		if a[n - i - 1] != b[n - i - 1]:
			if a[0] == b[n - i - 1]:
				k += 1
				changes.append(1)
				a[0] = int(not a[0])
			for j in range(n - i):
				a[j] = int(not a[j])
			for j in range((n - i) // 2):
				(a[j], a[n - i - j - 1]) = (a[n - i - j - 1], a[j])
			k += 1
			changes.append(n - i)
	print(k, *changes)


t = int(input())
for _ in range(t):
	n = int(input())
	res = []
	A = list(map(int, list(input())))
	B = list(map(int, list(input())))
	for i in range(n):
		if A[-1 - i] == B[-1 - i]:
			continue
		elif A[0] == B[-1 - i]:
			res.append(1)
			A[0] = 1 - A[0]
			res.append(n - i)
			for j in range(n - i):
				A[j] = 1 - A[j]
			for j in range((n - i) // 2):
				(A[j], A[n - i - 1 - j]) = (A[n - i - 1 - j], A[j])
		else:
			res.append(n - i)
			for j in range(n - i):
				A[j] = 1 - A[j]
			for j in range((n - i) // 2):
				(A[j], A[n - i - 1 - j]) = (A[n - i - 1 - j], A[j])
	print(len(res), *res)


def solve(n, b1, b2):
	array1 = list((int(s) for s in b1))
	array2 = list((int(s) for s in b2))
	first = array1[0]
	res = []
	for i in range(n - 1):
		if array2[n - 1 - i] == first:
			res.append(1)
			first = 1 - first
		if i % 2:
			first = array1[i // 2 + 1]
		else:
			first = 1 - array1[n - 1 - i // 2]
		res.append(n - i)
	if first != array2[0]:
		res.append(1)
	print(*[len(res)] + res)
t = int(input())
for i in range(t):
	n = int(input())
	b1 = input()
	b2 = input()
	solve(n, b1, b2)


t = int(input())
for i in range(t):
	n = int(input())
	a = [int(val) for val in list(input())]
	b = [int(val) for val in list(input())]
	if a == b:
		print(0)
	else:
		count = 0
		res = []
		is_flip = False
		left = 0
		right = n - 1
		j = n - 1
		a_zero = a[0]
		while left < right:
			if is_flip:
				curr_j = 1 - a[left]
			else:
				curr_j = a[right]
			if curr_j != b[j]:
				if is_flip:
					a_zero = 1 - a[right]
				else:
					a_zero = a[left]
				if curr_j == a_zero:
					count += 1
					res.append(str(j + 1))
					is_flip = not is_flip
				else:
					count += 2
					res.append('1')
					if is_flip:
						a[right] *= -1
					else:
						a[left] *= -1
					res.append(str(j + 1))
					is_flip = not is_flip
			if is_flip:
				left += 1
			else:
				right -= 1
			j -= 1
		if is_flip:
			a_zero = 1 - a[right]
		else:
			a_zero = a[left]
		if a_zero != b[0]:
			count += 1
			res.append('1')
		print(count, ' '.join(res))


(i, r) = (input, range)
for t in r(int(i())):
	(n, s, t, f) = (int(i()), i() + '0', i() + '0', lambda s: [f'{i}' for i in r(1, n + 1) if s[i - 1] != s[i]])
	(v, w) = (f(s), f(t))
	print(len(v) + len(w), *v + w[::-1])


t = int(input())
while t:
	n = int(input())
	u = input()
	v = input()
	a = []
	b = []
	for i in range(n):
		a.append(int(u[i]))
		b.append(int(v[i]))
	k = n - 1
	c = []
	f = 0
	i = 0
	j = n - 1
	d = 0
	while k >= 0:
		d += 1
		if a[f] == b[k]:
			c.append(1)
			c.append(k + 1)
			k -= 1
		else:
			c.append(k + 1)
			k -= 1
		if f == i:
			i += 1
			f = j
		else:
			j -= 1
			f = i
		if d % 2 == 1:
			if a[f] == 1:
				a[f] = 0
			else:
				a[f] = 1
	if len(c) == 0:
		print(0)
	else:
		print(len(c), end=' ')
		for i in range(len(c) - 1):
			print(c[i], end=' ')
		print(c[len(c) - 1])
	t -= 1


from fractions import Fraction
from collections import defaultdict
import math
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))

def input():
	return sys.stdin.readline().rstrip('\r\n')
krish = 1000
result = krish - 900
t = int(input())
for _ in range(t):
	n = int(input())
	s1 = input()
	s2 = input()
	if s1 == s2:
		print('0')
	else:
		s1 = list(s1)
		s2 = list(s2)
		ans = []
		ends = n - 1
		revs = 0
		k = set(s1)
		k2 = set(s2)
		fch = s1[0]
		while ends >= 0:
			if revs & 1:
				fch = chr(1 - (ord(s1[n - revs // 2 - 1]) - 48) + 48)
			else:
				fch = s1[revs // 2]
			if fch == s2[ends]:
				ans.append(1)
				ans.append(ends + 1)
				revs += 1
				ends -= 1
			else:
				ans.append(ends + 1)
				revs += 1
				ends -= 1
		print(len(ans) + 5 - 5, end=' ')
		for i in ans:
			print(i, end=' ')
		print()
		result += 1


t = int(input())
while t > 0:
	n = int(input())
	a = list(str(input()))
	b = list(str(input()))
	a = list(map(int, a))
	b = list(map(int, b))
	level = []
	aclone = []
	for i in range(n):
		if a[i] == 0:
			aclone.append(1)
		else:
			aclone.append(0)
	for i in range(n - 1, -1, -1):
		if a[i] != b[i]:
			if i != 0:
				if a[0] != b[i]:
					level.append(i + 1)
					d = list(a)
					a = aclone[::-1]
					aclone = d[::-1]
				else:
					level.append(1)
					if a[0] == 1:
						a[0] = 0
						aclone[0] = 1
					else:
						a[0] = 1
						aclone[0] = 0
					level.append(i + 1)
					d = list(a)
					a = aclone[::-1]
					aclone = d[::-1]
			else:
				level.append(1)
		a.pop()
		aclone.pop()
	print(len(level), ' '.join(map(str, level)))
	t = t - 1


import sys
import math
import heapq
import collections

def inputnum():
	return int(input())

def inputnums():
	return map(int, input().split())

def inputlist():
	return list(map(int, input().split()))

def inputstring():
	return [x for x in input()]

def inputmatrixchar(rows):
	arr2d = [[j for j in input().strip()] for i in range(rows)]
	return arr2d

def inputmatrixint(rows):
	arr2d = []
	for _ in range(rows):
		arr2d.append([int(i) for i in input().split()])
	return arr2d
t = int(input())
for q in range(t):
	n = inputnum()
	a = inputstring()
	b = inputstring()
	ansa = []
	one = False
	for i in range(n):
		if not one and a[i] == '1':
			one = True
			if i != 0:
				ansa.append(i)
		if i == n - 1 and a[i] == '1':
			ansa.append(i + 1)
		if one and a[i] == '0':
			one = False
			ansa.append(i)
	ansb = []
	one = False
	for i in range(n):
		if not one and b[i] == '1':
			one = True
			if i != 0:
				ansb.append(i)
		if i == n - 1 and b[i] == '1':
			ansb.append(i + 1)
		if one and b[i] == '0':
			one = False
			ansb.append(i)
	print(len(ansa) + len(ansb), end=' ')
	for i in range(len(ansa)):
		print(ansa[i], end=' ')
	for i in reversed(range(len(ansb))):
		print(ansb[i], end=' ')
	print()


def solve(n, a, b):
	ans = [0]
	p = 0
	f = 0
	for i in range(n - 1, -1, -1):
		if f > -1:
			at = i
			z = 0
		else:
			at = re - i
			z = re
		if (a[at] + p) % 2 != b[i]:
			if (a[z] + p) % 2 != b[i]:
				ans += [i + 1]
			else:
				ans += [1, i + 1]
				a[z] = (a[z] + 1) % 2
				ans[0] += 1
			ans[0] += 1
			a = a[:i + 1][::-1] + a[i + 1:]
			f += 1
			if f % 2 == 1:
				re = i
			p += 1
	return ans

def main():
	t = int(input())
	for i in range(t):
		(n, a, b) = (int(input()), [int(i) for i in input()], [int(i) for i in input()])
		print(*solve(n, a, b))
main()


t = int(input())
cnt = 0
while cnt < t:
	cnt += 1
	n = int(input())
	a = input()
	b = input()
	res = []
	r = n - 1
	for i in range(r, 0, -1):
		if a[i] == b[i]:
			continue
		else:
			if a[0] == b[i]:
				res.append(1)
				tmp_num = (int(a[0]) + 1) % 2
				a = str(tmp_num) + a[1:]
			if a[0] != b[i]:
				res.append(i + 1)
				tmp = []
			for j in range(i + 1):
				tmp.append('0' if a[j] == '1' else '1')
			tmp.reverse()
			tmp = ''.join((str(q) for q in tmp)) + a[i + 1:]
			a = ''.join((str(q) for q in tmp))
	if a[0] != b[0]:
		res.append(1)
	print(len(res), end=' ')
	if res:
		print(' '.join((str(q) for q in res)))


import math
import collections
from sys import stdin, stdout, setrecursionlimit
from bisect import bisect_left as bsl
from bisect import bisect_right as bsr
import heapq as hq
setrecursionlimit(2 ** 20)
t = 1
t = int(stdin.readline())
for _ in range(t):
	n = int(stdin.readline())
	a = str(stdin.readline().strip('\n'))
	b = str(stdin.readline().strip('\n'))
	ans = []
	cur = a[0]
	l = []
	inv = 0
	in0 = 0
	for i in range(n - 1, -1, -1):
		if b[i] == a[0]:
			ans.append(1)
			ans.append(i + 1)
			for i in [ans[-2], ans[-1]]:
				tmp = ''
				for j in range(i):
					if a[j] == '1':
						tmp += '0'
					else:
						tmp += '1'
				tmp = tmp[::-1]
				a = tmp + a[i:]
		else:
			ans.append(i + 1)
			for i in [ans[-1]]:
				tmp = ''
				for j in range(i):
					if a[j] == '1':
						tmp += '0'
					else:
						tmp += '1'
				tmp = tmp[::-1]
				a = tmp + a[i:]
	print(len(ans), end=' ')
	for i in ans:
		print(i, end=' ')
	print()


for _ in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	a = list(a)
	b = list(b)
	if a == b:
		print(0)
	else:
		ans = []
		c = 0
		i = n - 1
		while i >= 0:
			if a[i] != b[i]:
				if a[0] == b[i]:
					ans.append(1)
					c += 1
					if a[0] == '1':
						a[0] = '0'
					else:
						a[0] = '1'
				for j in range(i + 1):
					if a[j] == '1':
						a[j] = '0'
					else:
						a[j] = '1'
				a[:i + 1] = a[:i + 1][::-1]
				if a[i] == b[i]:
					ans.append(i + 1)
					c += 1
				i -= 1
			else:
				i -= 1
		print(c, end=' ')
		for i in ans:
			print(i, end=' ')
		print()


import sys
input = lambda : sys.stdin.readline().rstrip()
from collections import deque
from queue import LifoQueue
for _ in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	a1 = []
	a2 = []
	for i in range(0, n, 2):
		if a[i:i + 2] == '01':
			a1.append(i + 1)
			a1.append(i + 2)
		elif a[i:i + 2] == '10':
			if i != 0:
				a1.append(i)
			a1.append(i + 1)
		elif a[i:i + 2] == '11':
			if i != 0:
				a1.append(i)
			a1.append(i + 2)
		elif a[i:i + 2] == '1':
			if n - 1 > 0:
				a1.append(n - 1)
			a1.append(n)
	for i in range(0, n, 2):
		if b[i:i + 2] == '01':
			a2.append(i + 1)
			a2.append(i + 2)
		elif b[i:i + 2] == '10':
			if i != 0:
				a2.append(i)
			a2.append(i + 1)
		elif b[i:i + 2] == '11':
			if i != 0:
				a2.append(i)
			a2.append(i + 2)
		elif b[i:i + 2] == '1':
			if n - 1 > 0:
				a2.append(n - 1)
			a2.append(n)
	a2.reverse()
	c = a1 + a2
	for i in range(len(c) - 1):
		if c[i] == c[i + 1]:
			c.pop(i)
			c.pop(i)
			break
	print(len(c), *c)


import sys
import math
input = sys.stdin.readline

def flip(plen):
	preffix = [1 - bit for bit in A[:plen]]
	A[:plen] = reversed(preffix)
T = int(input())
for t in range(T):
	N = int(input())
	A = list(map(int, input().strip()))
	B = list(map(int, input().strip()))
	flips = []
	i = N - 1
	while i >= 0:
		if A[i] == B[i]:
			i -= 1
			continue
		if 1 - A[0] == B[i]:
			flip(i + 1)
			flips.append(i + 1)
		else:
			flip(1)
			flip(i + 1)
			flips.append(1)
			flips.append(i + 1)
		i -= 1
	if flips:
		print(len(flips), end=' ')
		print(' '.join(map(str, flips)))
	else:
		print(0)


for i in range(int(input())):
	n = int(input())
	s = input()
	s1 = input()
	a = []
	for i in range(n):
		if s[i] != s1[i]:
			a.append(i + 1)
	print(3 * len(a), end=' ')
	for i in range(len(a)):
		print(a[i], 1, a[i], end=' ')
	print('\r')


for _ in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	ans = []
	c = 0
	for i in range(n - 1, -1, -1):
		if a[i] != b[i]:
			c += 3
			ans.append(i + 1)
			ans.append(1)
			ans.append(i + 1)
	print(c, *ans)


for _ in range(int(input())):
	n = int(input())
	s = input()
	t = input()
	arr = []
	for i in range(n):
		have = 0
		if i % 2 == 0:
			have = int(s[i // 2])
		else:
			have = 1 - int(s[n - i // 2 - 1])
		if have == int(t[n - i - 1]):
			arr.append(1)
		arr.append(n - i)
	print(len(arr), *arr)


def op(p, c):
	pref = c[:p].copy()
	for i in range(p - 1, -1, -1):
		c[i] = (pref[p - i - 1] + 1) % 2
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, list(input())))
	b = list(map(int, list(input())))
	array = []
	for i in range(n - 1, -1, -1):
		if a[i] != b[i]:
			if b[i] == a[0]:
				array.append(1)
				op(1, a)
			array.append(i + 1)
			op(i + 1, a)
	print(len(array), *array)


t = int(input())

def fun(n, a1, b1):
	a = []
	b = []
	for i in range(n):
		if a1[i] == '0':
			a.append(0)
		else:
			a.append(1)
		if b1[i] == '0':
			b.append(0)
		else:
			b.append(1)
	ans = []
	i = n - 1
	while i >= 0:
		if a[i] == b[i]:
			i -= 1
		elif a[i] == a[0]:
			ans.append(i + 1)
			k = a[0:i + 1]
			k.reverse()
			a[0:i + 1] = k
			for j in range(i + 1):
				a[j] += 1
				a[j] %= 2
			i -= 1
		else:
			a[0] += 1
			a[0] %= 2
			ans.append(1)
	print(len(ans), *ans, sep=' ')
while t:
	t -= 1
	n = int(input())
	a = input()
	b = input()
	fun(n, a, b)


from __future__ import division
import sys
input = sys.stdin.readline
import math
from math import sqrt, floor, ceil
from collections import Counter

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())

def insr2():
	s = input()
	return s.split(' ')

def prime_factorization(n):
	if n == 1:
		return [1]
	ans = []
	i = 2
	cap = sqrt(n)
	while i <= cap:
		if n % i == 0:
			ans.append(i)
			n = n // i
			cap = sqrt(n)
		else:
			i += 1
	if n > 1:
		ans.append(n)
	return ans

def binomial(n, k):
	if n == 1 or n == k:
		return 1
	if k > n:
		return 0
	else:
		a = math.factorial(n)
		b = math.factorial(k)
		c = math.factorial(n - k)
		div = a // (b * c)
		return div
for _ in range(inp()):
	n = inp()
	a = insr()
	b = insr()
	(opsa, opsb) = ([], [])
	for i in range(n - 1):
		if a[i] != a[i + 1]:
			opsa.append(i + 1)
		if b[i] != b[i + 1]:
			opsb.append(i + 1)
	if b[-1] != a[-1]:
		opsb.append(n)
	ans = opsa + opsb[::-1]
	ans.insert(0, len(ans))
	print(' '.join([str(a) for a in ans]))


import math, sys, bisect, heapq
from collections import defaultdict, Counter, deque
from itertools import groupby, accumulate
input = iter(sys.stdin.buffer.read().decode().splitlines()).__next__
ilele = lambda : map(int, input().split())
alele = lambda : list(map(int, input().split()))

def Y(c):
	print(['NO', 'YES'][c])

def y(c):
	print(['no', 'yes'][c])

def Yy(c):
	print(['No', 'Yes'][c])

def inv(A):
	B = []
	for i in range(len(A) - 1, -1, -1):
		if A[i] == '1':
			B.append('0')
		else:
			B.append('1')
	return B
for _ in range(int(input())):
	N = int(input())
	A = [*input()]
	B = [*input()]
	Ans = []
	for i in range(N - 1, -1, -1):
		if A[i] != B[i]:
			if A[0] == A[i]:
				A = inv(A[:i + 1]) + A[i + 1:]
				Ans.append(i + 1)
			else:
				if A[0] == '1':
					A[0] = '0'
				else:
					A[0] = '1'
				Ans.append(1)
				A = inv(A[:i + 1]) + A[i + 1:]
				Ans.append(i + 1)
	n = len(Ans)
	Ans = [n] + Ans
	print(*Ans)


for _ in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	z = []
	for i in range(len(a) - 1, -1, -1):
		if a[i] != b[i]:
			z.append(i + 1)
			z.append(1)
			z.append(i + 1)
	if len(z) == 0:
		print(0)
		continue
	print(len(z), *z)


def flip(arr, index):
	for i in range(index + 1):
		if arr[i] == '0':
			arr[i] = '1'
		else:
			arr[i] = '0'
	for i in range((index + 1) // 2):
		(arr[i], arr[index - i]) = (arr[index - i], arr[i])
T = int(input())
while T > 0:
	n = int(input())
	arr1 = list(input())
	arr2 = list(input())
	k = 0
	index = n - 1
	res = []
	while index >= 0:
		if arr1[index] != arr2[index]:
			k += 1
			res.append(index + 1)
			flip(arr1, index)
			if arr1[index] != arr2[index]:
				le = 1
				while True:
					equal = True
					for x in range(le):
						if arr1[x] != arr2[index - le + x + 1]:
							equal = False
							break
					if equal is True:
						le += 1
					else:
						break
				le -= 1
				flip(arr1, le - 1)
				res.append(le)
				flip(arr1, index)
				res.append(index + 1)
				k += 2
		else:
			index -= 1
	res.insert(0, k)
	print(*res)
	T -= 1


t = int(input())
for i in range(t):
	n = int(input())
	a = str(input())
	b = str(input())
	a = a + '0'
	b = b + '0'
	ans1 = []
	ans2 = []
	for j in range(1, n + 1):
		if a[j] != a[j - 1]:
			ans1.append(j)
		if b[j] != b[j - 1]:
			ans2.append(j)
	l = len(ans2)
	for j in range(1, l + 1):
		ans1.append(ans2[-j])
	k = len(ans1)
	lis = [k]
	for j in range(k):
		lis.append(ans1[j])
	print(' '.join((str(x) for x in lis)))


for _ in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	ans = []
	start = 0
	cnt = 0
	last = n - 1
	inc = 1
	bind = n - 1
	i = 0
	while i < n:
		if last == start:
			if a[last] == b[0] and cnt % 2 == 1 or (a[last] != b[0] and cnt % 2 == 0):
				ans.append(1)
		elif a[last] == b[bind] and cnt % 2 == 0 or (a[last] != b[bind] and cnt % 2 == 1):
			last -= inc
		elif a[start] != b[bind] and cnt % 2 == 0 or (a[start] == b[bind] and cnt % 2 == 1):
			ans.append(bind + 1)
			inc *= -1
			(start, last) = (last, start)
			cnt += 1
			last -= inc
		else:
			ans.append(1)
			ans.append(bind + 1)
			inc *= -1
			(start, last) = (last, start)
			last -= inc
			cnt += 1
		bind -= 1
		i += 1
	print(len(ans), *ans)


t = int(input())
while t:
	t -= 1
	n = int(input())
	a = input()
	b = input()
	ans = []
	for i in range(n):
		if a[i] != b[i]:
			if i == 0:
				ans.append(1)
			else:
				ans.append(i + 1)
				ans.append(1)
				ans.append(i + 1)
	print(len(ans), end=' ')
	for i in ans:
		print(i, end=' ')
	print()


T = int(input())
for _ in range(T):
	N = int(input())
	a = [int(x) for x in input()]
	b = [int(x) for x in input()]
	ans = []
	for i in range(N):
		x = a[N - (i + 1) // 2] ^ 1 if i % 2 else a[i // 2]
		if x == b[N - 1 - i]:
			ans.append(1)
		ans.append(N - i)
	print(len(ans), end=' ')
	print(' '.join((str(x) for x in ans)))


for _ in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	k = 0
	ans = []
	if a == b:
		print(k)
	else:
		for i in range(n - 1):
			c = ''
			if a[n - i - 1] == b[n - i - 1]:
				continue
			elif a[0] != b[n - i - 1]:
				ans.append(n - i)
				for j in range(n - i):
					if a[j] == '0':
						c += '1'
					else:
						c += '0'
				c = c[::-1]
				a = c + a[n - i:]
				k += 1
			else:
				if a[0] == '0':
					a = '1' + a[1:]
				else:
					a = '0' + a[1:]
				ans.append(1)
				ans.append(n - i)
				for j in range(n - i):
					if a[j] == '0':
						c += '1'
					else:
						c += '0'
				c = c[::-1]
				a = c + a[n - i:]
				k += 2
		if a[0] != b[0]:
			k += 1
			ans.append(1)
		print(k, *ans)


for _ in range(int(input())):
	n = int(input())
	a = [[i + 1, 1, i + 1] for (x, y, i) in zip(input(), input(), range(n)) if x != y]
	print(len(a * 3), ' '.join((str(x) for s in a for x in s)))


t = int(input())
for case in range(t):
	n = int(input())
	a = list(input())
	b = list(input())
	k = 0
	p = []
	for i in range(n):
		if int(a[n - i - 1]) != int(b[n - i - 1]):
			if int(a[n - i - 1]) != int(a[0]):
				k += 1
				p.append(1)
				a[0] = (int(a[0]) + 1) % 2
			k += 1
			p.append(n - i)
			tempA = a[:]
			for j in range(n - i):
				tempA[j] = (int(a[n - i - j - 1]) + 1) % 2
			a = tempA[:]
	print(str(k) + ' ' + ' '.join((str(j) for j in p)))


def operate(a, n):
	for i in range(n):
		a[i] = not a[i]
	newa = a[:n]
	newa.reverse()
	newa = newa[:n] + a[n:]
	return newa

def convert(a):
	temp = []
	for i in a:
		if i == '0':
			temp.append(False)
		else:
			temp.append(True)
	return temp
t = int(input())
while True:
	if t == 0:
		break
	t -= 1
	n = int(input())
	a = convert(input())
	b = convert(input())
	count = 0
	ans = []
	while n > 0:
		if a[n - 1] == b[n - 1]:
			n -= 1
		elif a[0] != b[n - 1]:
			count += 1
			ans.append(n)
			a = operate(a, n)
			n -= 1
		else:
			count += 1
			for i in range(n):
				if a[0] != a[i]:
					a = operate(a, i)
					ans.append(i)
					break
	ans = [str(i) for i in ans]
	print(count, ' '.join(ans))


def main():
	t = int(input())
	for i in range(t):
		n = int(input())
		str1 = input()
		str2 = input()
		anslist = []
		if str1 == str2:
			print(0)
		else:
			for j in range(n):
				if str1[0] == str2[-j - 1]:
					anslist.append(1)
				anslist.append(n - j)
				tmpstr = ''
				for k in range(0, n - j):
					if str1[k] == '0':
						tmpstr += '1'
					else:
						tmpstr += '0'
				if len(tmpstr) > 1:
					tmpstr = str2[-j - 1] + tmpstr[1:]
				else:
					tmpstr = str2[-1]
				str1 = tmpstr[::-1] + str1[n - j:]
			print(len(anslist), end=' ')
			for j in range(len(anslist) - 1):
				print(anslist[j], end=' ')
			print(anslist[len(anslist) - 1])
main()


import sys
input = sys.stdin.readline
for f in range(int(input())):
	n = int(input())
	x = input()
	a = [0] * n
	for i in range(n):
		a[i] = int(x[i])
	x = input()
	b = [0] * n
	for i in range(n):
		b[i] = int(x[i])
	ops = [0]
	i = n - 1
	while i >= 0:
		if a[i] != b[i]:
			if a[0] == b[i]:
				ops[0] += 1
				ops.append(1)
				a[0] = 1 - a[0]
			ops[0] += 1
			ops.append(i + 1)
			copy = [0] * (i + 1)
			for j in range(i + 1):
				copy[-j - 1] = 1 - a[j]
			for j in range(i + 1):
				a[j] = copy[j]
		i -= 1
	print(*ops)


for _ in range(int(input())):
	n = int(input())
	x = list(input())
	y = list(input())
	a = []
	for i in range(n - 1, -1, -1):
		if x[i] != y[i]:
			if i == 0:
				a.append(i + 1)
				x[i] = y[i]
			elif x[i] != x[0]:
				a.append(1)
				if x[0] == '0':
					x[0] = '1'
				else:
					x[0] = '0'
				a.append(i + 1)
				for j in range(0, i + 1):
					if x[j] == '0':
						x[j] = '1'
					else:
						x[j] = '0'
				p = x[i + 1:]
				x = x[:i + 1][::-1] + p
			else:
				a.append(i + 1)
				for j in range(i + 1):
					if x[j] == '0':
						x[j] = '1'
					else:
						x[j] = '0'
				p = x[i + 1:]
				x = x[:i + 1][::-1] + p
	print(len(a), end=' ')
	print(*a)


for t in range(int(input())):
	n = int(input())
	a = list(input())
	b = list(input())
	ans = []
	i = n - 1
	j = n - 1
	while j >= 0:
		while a[j] != b[j] and a[i] == a[j]:
			if j - 1 < 0:
				j = 0
				break
			j -= 1
		if i != j:
			ans.append(i + 1)
			ans.append(i - j)
			ans.append(i + 1)
		if i == 0 and j == 0 and (a[i] != b[i]):
			ans.append(1)
		if i == j:
			i = j - 1
			j -= 1
		else:
			i = j
	print(len(ans), end=' ')
	for i in ans:
		print(i, end=' ')
	print()


for _ in range(int(input())):
	n = int(input())
	a = list(input())
	b = list(input())
	l = []
	flip = a[0]
	for i in range(n - 1):
		if a[i] != a[i + 1]:
			l.append(i + 1)
			if flip == '0':
				flip = '1'
			else:
				flip = '0'
	for i in range(n - 1, -1, -1):
		if b[i] != flip:
			l.append(i + 1)
			if flip == '0':
				flip = '1'
			else:
				flip = '0'
	print(len(l), *l)


for _ in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	ans = []
	for i in range(n):
		last = n - i - 1
		if a[last] == b[last]:
			a = a[:last]
			continue
		if a[0] == b[last]:
			a = str(abs(int(a[0]) - 1)) + a[1:]
			ans.append(1)
		a = a.replace('0', '2')
		a = a.replace('1', '0')
		a = a.replace('2', '1')
		a = a[::-1]
		a = a[:last]
		ans.append(last + 1)
	print(len(ans), *ans)


for _ in range(int(input())):
	n = int(input())
	s = input()
	t = input()
	a = []
	for i in range(n):
		if s[i] != t[i]:
			a.extend([i + 1, 1, i + 1])
	if len(a) == 0:
		print(0)
	else:
		print(len(a), end=' ')
		print(*a)


def prefix_flip(index, arr):
	s = []
	for j in range(index - 1, -1, -1):
		s.append(str(int(arr[j]) ^ 1))
	s.extend(arr[index:])
	return s
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(input())
	b = list(input())
	answer = []
	for i in range(n - 1, -1, -1):
		if b[i] == a[i]:
			continue
		elif b[i] == a[0]:
			answer.append(1)
			answer.append(i + 1)
			a[0] = str(int(a[0]) ^ 1)
			a = prefix_flip(i + 1, a)
		else:
			answer.append(i + 1)
			a = prefix_flip(i + 1, a)
	print(len(answer), *answer)


from __future__ import division, print_function
MOD = 998244353
mod = 10 ** 9 + 7

def prepare_factorial():
	fact = [1]
	for i in range(1, 100005):
		fact.append(fact[-1] * i % mod)
	ifact = [0] * 100005
	ifact[100004] = pow(fact[100004], mod - 2, mod)
	for i in range(100004, 0, -1):
		ifact[i - 1] = i * ifact[i] % mod
	return (fact, ifact)
import sys
from bisect import bisect_left, bisect_right, insort
from math import floor, ceil, sqrt, degrees, atan, pi, log, sin, radians
from heapq import heappop, heapify, heappush
from collections import Counter, defaultdict, deque

def modinv(n, p):
	return pow(n, p - 2, p)

def ncr(n, r, fact, ifact):
	t = fact[n] * (ifact[r] * ifact[n - r]) % mod % mod
	return t

def intarray():
	return map(int, sys.stdin.readline().strip().split())

def array():
	return list(map(int, sys.stdin.readline().strip().split()))

def input():
	return sys.stdin.readline().strip()

def GCD(x, y):
	while y:
		(x, y) = (y, x % y)
	return x

def lcm(x, y):
	return x * y // GCD(x, y)

def get_xor(n):
	return [n, 1, n + 1, 0][n % 4]

def fast_expo(a, b):
	res = 1
	while b:
		if b & 1:
			res = res * a
			res %= MOD
			b -= 1
		else:
			a = a * a
			a %= MOD
			b >>= 1
	res %= MOD
	return res

def get_n(P):
	ans = (-1 + sqrt(1 + 8 * P)) // 2
	return ans

def reverse(a, r):
	l = 0
	if r & 1 == 0:
		if a[r // 2] == '0':
			a[r // 2] = '1'
		else:
			a[r // 2] = '0'
	while l < r:
		(a[l], a[r]) = (a[r], a[l])
		if a[l] == '0':
			a[l] = '1'
		elif a[l] == '1':
			a[l] = '0'
		if a[r] == '0':
			a[r] = '1'
		elif a[r] == '1':
			a[r] = '0'
		l += 1
		r -= 1
	return a

def solve():
	n = int(input())
	a = list(input())
	b = list(input())
	out = []
	for i in range(n - 1, -1, -1):
		if b[i] == a[i]:
			continue
		elif b[i] != a[0]:
			out.append(i + 1)
			a = reverse(a, i)
		else:
			out.append(1)
			if a[0] == '1':
				a[0] = '0'
			else:
				a[0] = '1'
			out.append(i + 1)
			a = reverse(a, i)
	print(len(out), *out)
	return

def main():
	T = int(input())
	while T:
		solve()
		T -= 1
py2 = round(0.5)
if py2:
	from future_builtins import ascii, filter, hex, map, oct, zip
	range = xrange
import os, sys
from io import IOBase, BytesIO
BUFSIZE = 8192

class FastIO(BytesIO):
	newlines = 0

	def __init__(self, file):
		self._file = file
		self._fd = file.fileno()
		self.writable = 'x' in file.mode or 'w' in file.mode
		self.write = super(FastIO, self).write if self.writable else None

	def _fill(self):
		s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
		self.seek((self.tell(), self.seek(0, 2), super(FastIO, self).write(s))[0])
		return s

	def read(self):
		while self._fill():
			pass
		return super(FastIO, self).read()

	def readline(self):
		while self.newlines == 0:
			s = self._fill()
			self.newlines = s.count(b'\n') + (not s)
		self.newlines -= 1
		return super(FastIO, self).readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.getvalue())
			(self.truncate(0), self.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		if py2:
			self.write = self.buffer.write
			self.read = self.buffer.read
			self.readline = self.buffer.readline
		else:
			self.write = lambda s: self.buffer.write(s.encode('ascii'))
			self.read = lambda : self.buffer.read().decode('ascii')
			self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


def solve(a, b, n):
	if a == b:
		return 0
	ans = []
	while True:
		if len(a) == 0 and len(b) == 0:
			return ans
		if a[0] != b[-1]:
			if len(a) == 1:
				ans.append(1)
				return ans
			ans.append(len(b))
			new = ''
			for i in range(1, len(a)):
				if a[i] == '1':
					new += '0'
				else:
					new += '1'
			a = new[::-1]
			b = b[:len(b) - 1]
		else:
			if len(a) == len(b) == 1:
				return ans
			if a[0] == '1':
				a = '0' + a[1:]
			else:
				a = '1' + a[1:]
			ans.append(1)
	return ans
t = int(input())
for _ in range(t):
	n = int(input())
	a = input()
	b = input()
	if a == b:
		print(0)
	else:
		ans = solve(a, b, n)
		print(len(ans), end=' ')
		print(*ans)


from collections import deque
from math import *
import heapq

def func(s1, s2):
	return

def negate(s1, ind):
	for i in range(ind + 1):
		if s1[i] == '0':
			s1[i] = '1'
		else:
			s1[i] = '0'
	return s1[:ind + 1]
t = int(input())
for i in range(t):
	n = int(input())
	S1 = input()
	S2 = input()
	s1 = list(S1)
	s2 = list(S2)
	if s1 == s2:
		print(0)
		continue
	end1 = len(s1) - 1
	end2 = len(s1) - 1
	ANSWER = []
	while end1 >= 0:
		if s1[end1] == s2[end2]:
			end1 -= 1
			end2 -= 1
		else:
			if s1[0] != s2[end2]:
				s1[:end1 + 1] = negate(s1, end1)
				s1[:end1 + 1] = s1[:end1 + 1][-1::-1]
				ANSWER.append(end1 + 1)
			else:
				ANSWER.append(1)
				s1[0] = negate(s1, 0)
				s1[:end1 + 1] = negate(s1, end1)
				s1[:end1 + 1] = s1[:end1 + 1][-1::-1]
				ANSWER.append(end1 + 1)
			end1 -= 1
			end2 -= 1
	print(len(ANSWER), *ANSWER)


def reverse_sublist(lst, start, end):
	lst[start:end] = lst[start:end][::-1]
	return lst

def found(lst, start, end):
	lst[start:end] = lst[start:end][::-1]
	return lst

def invertion(arr, prefix, dp):
	tmp = ''.join(map(str, arr[0:prefix]))
	if tmp in dp:
		arr[0:prefix] = dp[tmp]
	else:
		for i in range(prefix):
			if arr[i] == 0:
				arr[i] = 1
			else:
				arr[i] = 0
		arr = reverse_sublist(arr, 0, prefix)
		dp.update({tmp: arr[0:prefix]})

def main():
	t = int(input())
	dp = {}
	for _ in range(t):
		n = int(input())
		a = list(map(int, input()))
		b = list(map(int, input()))
		arr = []
		i = 0
		for i in range(0, n):
			if not a[i] == b[i]:
				arr.append(i + 1)
				invertion(a, i + 1, dp)
				arr.append(1)
				invertion(a, 1, dp)
				arr.append(i + 1)
				invertion(a, i + 1, dp)
		print(len(arr), *arr)
main()


for _ in range(int(input())):
	n = int(input())
	s1 = input()
	s2 = input()
	l = []
	for i in range(n):
		if s1[i] != s2[i]:
			l.extend([i + 1, 1, i + 1])
	print(len(l), end=' ')
	print(*l)


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(i) for i in input()]
	b = [int(i) for i in input()]
	pf = []
	r = n - 1
	l = 0
	while r > 0:
		if b[r] == a[0]:
			pf.append(1)
			a[0] ^= 1
		for i in range(r + 1):
			a[i] ^= 1
		a = a[:r + 1][::-1] + a[r + 1:]
		pf.append(r + 1)
		r -= 1
	if a[0] != b[0]:
		pf.append(1)
	print(len(pf), *pf)


t = int(input())
while t:
	t -= 1
	n = int(input())
	s1 = input()
	s2 = input()
	a = [int(x) for x in s1]
	b = [int(x) for x in s2]
	i = n - 1
	res = []
	while i > 0:
		if a[i] != b[i]:
			if a[i] == a[0]:
				res += [i + 1]
				temp = []
				for j in range(i + 1):
					temp += [a[j] ^ 1]
				a[:i + 1] = temp[::-1]
			else:
				res += [1]
				res += [i + 1]
				a[0] ^= 1
				temp = []
				for j in range(i + 1):
					temp += [a[j] ^ 1]
				a[:i + 1] = temp[::-1]
		i -= 1
	if a[0] != b[0]:
		res += [1]
	print(len(res), end=' ')
	print(*res)


from bisect import bisect_left as bl
from bisect import bisect_right as br
from heapq import heappush, heappop, heapify
import math
from collections import *
from functools import reduce, cmp_to_key
import sys
input = sys.stdin.readline
M = mod = 998244353

def factors(n):
	return sorted(set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0))))

def inv_mod(n):
	return pow(n, mod - 2, mod)

def li():
	return [int(i) for i in input().rstrip('\n').split()]

def st():
	return input().rstrip('\n')

def val():
	return int(input().rstrip('\n'))

def li2():
	return [i for i in input().rstrip('\n')]

def li3():
	return [int(i) for i in input().rstrip('\n')]

def do(l):
	l1 = []
	for i in range(len(l) - 1, -1, -1):
		l1.append(1 - l[i])
	return l1[:]
for _ in range(val()):
	n = val()
	a = li3()
	b = li3()
	ans = []
	for i in range(n - 1, -1, -1):
		if a[0] != b[i]:
			ans.append(i + 1)
			a = do(a[:i + 1]) + a[i + 1:]
		else:
			ans.append(1)
			a[0] = 1 - a[0]
			ans.append(i + 1)
			a = do(a[:i + 1]) + a[i + 1:]
	print(len(ans), *ans)


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(i) for i in list(input())[:n]]
	b = [int(i) for i in list(input())[:n]]
	ans = []
	for i in range(-1, -n - 1, -1):
		if a[i] != b[i]:
			if a[0] != a[i]:
				ans.append(1)
				a[0] = abs(a[0] - 1)
			ans.append(n + i + 1)
			sub = list(reversed(a[:n + i + 1]))
			for j in range(n + i + 1):
				a[j] = abs(sub[j] - 1)
	print(len(ans), ' '.join([str(i) for i in ans]))


TC = int(input())
for tc in range(TC):
	N = int(input())
	S = input()
	T = input()
	i = N - 1
	j = 0
	result = []
	rev = False
	while j <= i:
		if rev:
			while j < i and S[j] != T[i - j]:
				j += 1
			if j > i:
				break
			elif i > j:
				if S[i] != T[i - j]:
					result.append(1)
				result.append(i - j + 1)
				rev = not rev
				i -= 1
			else:
				if S[j] == T[0]:
					result.append(1)
				i -= 1
		else:
			while i >= 0 and S[i] == T[i - j]:
				i -= 1
			if j > i:
				break
			elif i > j:
				if S[j] == T[i - j]:
					result.append(1)
				result.append(i - j + 1)
				rev = not rev
				j += 1
			else:
				if S[j] != T[0]:
					result.append(1)
				j += 1
	print(len(result), end=' ')
	for r in result:
		print(r, end=' ')
	print()


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	s = [int(item) for item in list(input().rstrip())]
	t = [int(item) for item in list(input().rstrip())]
	s.reverse()
	t.reverse()
	ans = []
	for (i, ti) in enumerate(t[:-1]):
		si = s[i]
		if ti == si:
			continue
		if ti == s[-1]:
			ans.append(1)
			s[-1] = 1 - s[-1]
		ans.append(n - i)
		tmp = []
		for item in s[i:]:
			tmp.append(1 - item)
		tmp.reverse()
		s = s[:i] + tmp
	if s[-1] != t[-1]:
		ans.append(1)
	if not ans:
		print(0)
	else:
		print(len(ans), *ans)


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	a = input().strip()
	b = input().strip()
	a = list(a)
	b = list(b)
	for i in range(0, n):
		if a[i] == '1':
			a[i] = 1
		else:
			a[i] = 0
		if b[i] == '0':
			b[i] = 0
		else:
			b[i] = 1
	ind = []
	k = 0
	for i in range(n - 1, 0, -1):
		if a[i] != b[i]:
			if a[0] == b[i]:
				k += 2
				ind.append(1)
				ind.append(i + 1)
				a[0] = a[0] ^ 1
			else:
				ind.append(i + 1)
				k += 1
			l = i
			c = i // 2
			for j in range(0, c + 1):
				if j == i - j:
					a[j] = a[j] ^ 1
					break
				tmp = a[j] ^ 1
				a[j] = a[i - j] ^ 1
				a[i - j] = tmp
	if a[0] != b[0]:
		ind.append(1)
		k += 1
	print(k, end=' ')
	for i in range(0, len(ind)):
		print(ind[i], end=' ')
	print()


import sys
import os, io

def read_int():
	return map(int, input().split())

def read_list():
	return list(map(int, input().split()))

def print_list(l):
	print(' '.join(map(str, l)))
N = int(input())
for i in range(N):
	n = int(input())
	a = input().strip()
	b = input().strip()
	res = []
	for i in range(n - 1, -1, -1):
		if a[i] != b[i]:
			if a[0] == b[i]:
				res.append(1)
				res.append(i + 1)
				tmp = ['1' if a[i] == '0' else '0' for i in range(1, i + 1)]
				a = ''.join(tmp[::-1]) + b[i] + a[i + 1:]
			else:
				res.append(i + 1)
				tmp = ['1' if a[i] == '0' else '0' for i in range(i + 1)]
				a = ''.join(tmp[::-1]) + a[i + 1:]
	print_list([len(res)] + res)


for _ in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	(opa, opb) = ([], [])
	for i in range(1, n):
		if a[i] != a[i - 1]:
			opa.append(i)
	if a[-1] != b[-1]:
		opa.append(n)
	for i in range(1, n):
		if b[i] != b[i - 1]:
			opb.append(i)
	print(len(opa) + len(opb), *opa + opb[::-1])


def aa(a):
	b = []
	for i in a:
		if i == '1':
			b.append('0')
		else:
			b.append('1')
	return reversed(b)
for _ in range(int(input())):
	n = int(input())
	A = input()
	B = input()
	a = []
	b = []
	for i in A:
		a.append(i)
	for i in B:
		b.append(i)
	c = []
	for i in range(n - 1, -1, -1):
		if b[i] == '1':
			if a[0] == '1':
				c.append(1)
				c.append(i + 1)
				a[0] = '0'
				a[:i + 1] = aa(a[:i + 1])
			else:
				c.append(i + 1)
				a[:i + 1] = aa(a[:i + 1])
		elif a[0] == '0':
			c.append(1)
			c.append(i + 1)
			a[0] = '1'
			a[:i + 1] = aa(a[:i + 1])
		else:
			c.append(i + 1)
			a[:i + 1] = aa(a[:i + 1])
	print(len(c), end=' ')
	for i in c:
		print(i, end=' ')
	print()


from sys import stdin, stdout
import bisect
import math

def st():
	return list(stdin.readline().strip())

def inp():
	return int(stdin.readline())

def li():
	return list(map(int, stdin.readline().split()))

def mp():
	return map(int, stdin.readline().split())

def pr(n):
	stdout.write(str(n) + '\n')

def soe(limit):
	l = [1] * (limit + 1)
	prime = []
	for i in range(2, limit + 1):
		if l[i]:
			for j in range(i * i, limit + 1, i):
				l[j] = 0
	for i in range(2, limit + 1):
		if l[i]:
			prime.append(i)
	return prime

def segsoe(low, high):
	limit = int(high ** 0.5) + 1
	prime = soe(limit)
	n = high - low + 1
	l = [0] * (n + 1)
	for i in range(len(prime)):
		lowlimit = low // prime[i] * prime[i]
		if lowlimit < low:
			lowlimit += prime[i]
		if lowlimit == prime[i]:
			lowlimit += prime[i]
		for j in range(lowlimit, high + 1, prime[i]):
			l[j - low] = 1
	for i in range(low, high + 1):
		if not l[i - low]:
			if i != 1:
				print(i)

def power(a, n):
	r = 1
	while n:
		if n & 1:
			r = r * a
		a *= a
		n = n >> 1
	return r

def solve():
	n = inp()
	a = input()
	b = input()
	j = n - 1
	x = []
	if a == b:
		print(0)
		return
	while j >= 0:
		if a[j] == b[j]:
			j -= 1
		elif a[0] != b[j]:
			x.append(j + 1)
			p = ''
			for i in range(j + 1):
				if a[i] == '1':
					p += '0'
				else:
					p += '1'
			a = p[::-1] + a[j:]
			j -= 1
		else:
			x.append(1)
			if a[0] == '1':
				a = '0' + a[1:]
			else:
				a = '1' + a[1:]
	print(len(x), *x)
for _ in range(inp()):
	solve()


def transform(a, pre):
	b = a.copy()
	for i in range(pre):
		b[i] = 1 - a[pre - 1 - i]
	return b
for i in range(int(input())):
	n = int(input())
	a = list(map(int, list(input())))
	b = list(map(int, list(input())))
	ans = []
	lid = n - 1
	while lid > -1:
		if a[lid] == b[lid]:
			lid -= 1
		elif a[0] == b[lid]:
			a = transform(a, 1)
			ans.append(1)
		else:
			a = transform(a, lid + 1)
			ans.append(lid + 1)
	print(len(ans), ' '.join(map(str, ans)))


import os
import sys
import math
from io import BytesIO, IOBase
from fractions import Fraction
from collections import defaultdict
from itertools import permutations
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
for _ in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	r = list()
	c = list(a)
	c1 = int(c[0])
	for i in range(n - 1):
		if c[i] != c[i + 1]:
			r.append(i + 1)
	if c[-1] != b[-1]:
		r.append(n)
	for i in range(n - 2, -1, -1):
		if b[i] != b[i + 1]:
			r.append(i + 1)
	print(len(r), end=' ')
	print(*r)


from sys import stdin

def iinput():
	return int(stdin.readline())

def minput():
	return map(int, stdin.readline().split())

def linput():
	return list(map(int, stdin.readline().split()))

def flip(s):
	for i in range(len(s)):
		s[i] = '1' if s[i] == '0' else '0'
	return s
t = iinput()
while t:
	t -= 1
	n = iinput()
	a = list(input())
	b = input()
	op = []
	for i in range(n - 1, -1, -1):
		if a[i] != b[i]:
			if a[0] == b[i]:
				op.extend([1, i + 1])
				a[0] = '1' if a[0] == '0' else '0'
			else:
				op.append(i + 1)
			a[:i + 1] = flip(a[:i + 1])
			a[:i + 1] = reversed(a[:i + 1])
	print(len(op), *op)


def task():
	n = int(input())
	a = input()
	b = input()
	answer = []
	j = len(b) - 1
	for i in range(len(a)):
		if i % 2 == 0:
			i1 = i // 2
		else:
			i1 = n - 1 - i // 2
		c = a[i1]
		if i % 2 != 0:
			c = '0' if c == '1' else '1'
		if c == b[j]:
			answer.append(1)
		answer.append(j + 1)
		j -= 1
	print(len(answer), end=' ')
	print(*answer)
n = int(input())
for _ in range(n):
	task()


t = int(input())
for i in range(t):
	n = int(input())
	x = input()
	y = input()
	a = []
	b = []
	for j in x:
		a.append(j)
	for j in y:
		b.append(j)
	p = []
	j = 0
	while j < n:
		if a[j] == b[j]:
			pass
		else:
			p.append(j + 1)
			if j != 0:
				p.append(1)
				p.append(j + 1)
		j += 1
	print(len(p), end=' ')
	print(*p)


from sys import stdin, stdout
test = int(stdin.readline())
for _ in range(test):
	n = int(stdin.readline())
	a = list(stdin.readline().strip())
	a = [int(x) for x in a]
	b = list(stdin.readline().strip())
	b = [int(x) for x in b]
	ans = []
	cnt = 0
	for i in range(n - 1, 0, -1):
		if a[i] != b[i]:
			if a[i] != a[0]:
				cnt += 1
				ans.append(1)
				a[0] ^= 1
			cnt += 1
			ans.append(i + 1)
			temp = []
			for j in range(i + 1):
				temp.append(a[j] ^ 1)
			a[:i + 1] = temp[::-1]
	if a[0] != b[0]:
		cnt += 1
		ans.append(1)
	print(cnt, end=' ')
	print(*ans)


for _ in range(int(input())):
	n = int(input())
	ak = input()
	bk = input()
	a = []
	b = []
	for i in range(n):
		a.append(ak[i])
		b.append(bk[i])
	if a == b:
		print(0)
	else:
		ans = []
		nc = n
		while nc != 0:
			if a[nc - 1] == b[nc - 1]:
				nc -= 1
				continue
			elif b[nc - 1] == a[0]:
				if a[0] == '1':
					a[0] = '0'
				else:
					a[0] = '1'
				ans.append(1)
			else:
				ans.append(nc)
				for i in range(nc // 2):
					(a[i], a[nc - 1 - i]) = (a[nc - 1 - i], a[i])
				for i in range(nc):
					if a[i] == '1':
						a[i] = '0'
					else:
						a[i] = '1'
				nc -= 1
		print(len(ans), *ans)


for _ in range(int(input())):
	n = int(input())
	a = [int(__) for __ in input()]
	b = [int(___) for ___ in input()]
	l1 = []
	for i in range(n - 1, 0, -1):
		if a[i] != b[i]:
			if a[0] != a[i]:
				l1.append(1)
				a[0] = 1 - a[0]
			for j in range(i + 1):
				a[j] = 1 - a[j]
			l1.append(i + 1)
			a[:i + 1] = a[:i + 1][::-1]
	if a[0] != b[0]:
		l1.append(1)
	print(len(l1), end=' ')
	print(' '.join([str(____) for ____ in l1]))


for tt in range(int(input())):
	n = int(input())
	b = input()
	a = input()
	ans = []
	for i in range(n - 1):
		if b[i] != b[i + 1]:
			ans.append(i + 1)
	cur = int(b[-1])
	for i in range(n - 1, -1, -1):
		if cur != int(a[i]):
			ans.append(i + 1)
			cur ^= 1
	print(len(ans))
	print(*ans)


for _ in range(int(input())):
	n = int(input())
	a = str(input())
	b = str(input())
	alst = []
	count = 0
	while n:
		if b[n - 1] == a[n - 1]:
			n -= 1
			a = a[:n]
			b = b[:n]
		elif b[n - 1] == a[0]:
			alst.append(1)
			alst.append(n)
			count += 2
			t = ''
			for i in a[1:][::-1]:
				t += str(int(i) ^ 1)
			a = t
			n -= 1
			b = b[:n]
		else:
			count += 1
			alst.append(n)
			t = ''
			for i in a[1:][::-1]:
				t += str(int(i) ^ 1)
			a = t
			n -= 1
			b = b[:n]
	print(count, *alst)


import sys
fast_reader = sys.stdin.readline
fast_writer = sys.stdout.write

def input():
	return fast_reader().strip()

def print(*argv):
	fast_writer(' '.join((str(i) for i in argv)))
	fast_writer('\n')
for _ in range(int(input())):
	n = int(input())
	a = list(input())
	b = list(input())
	l = a[:]
	curr = 0
	m = []
	for i in range(n):
		if l[i] == '0':
			if curr != 0:
				if i == curr:
					m.append(i)
				else:
					m.append(i - curr)
					m.append(i)
			curr = 0
		else:
			curr += 1
	if curr != 0:
		if n == curr:
			m.append(n)
		else:
			m.append(n - curr)
			m.append(n)
	temp = ['0' for i in range(n)]
	i = n - 1
	while i >= 0:
		if temp[i] != b[i]:
			j = i
			curr = 0
			while j >= 0:
				if temp[j] != b[j]:
					curr += 1
					j -= 1
				else:
					break
			if j == -1:
				m.append(curr)
			else:
				m.append(i + 1)
				m.append(j + 1)
			i = j
		i -= 1
	print(len(m), *m)


t = int(input())
while t > 0:
	t -= 1
	n = int(input())
	a = input()
	b = input()
	a1 = list(str(a))
	b1 = list(str(b))
	l = []
	for i in range(n):
		if a[i] != b[i]:
			if i > 0:
				l.append(i + 1)
			l.append(1)
			if i > 0:
				l.append(i + 1)
	print(len(l), *l)


g = int(input())
for _ in range(g):
	n = int(input())
	A = input()
	B = input()
	count = 0
	C = []
	for i in range(n - 1, -1, -1):
		if B[i] != A[i]:
			if B[i] != A[0]:
				C.append(i + 1)
				mystr = ''
				for j in range(0, i + 1):
					if A[j] == '0':
						mystr = str(1) + mystr
					else:
						mystr = str(0) + mystr
				A = mystr + A[i + 1:]
				count += 1
			else:
				C.append(1)
				A = str(1 - int(A[0])) + A[1:]
				C.append(i + 1)
				E = list(A[:i + 1])
				mystr = ''
				for j in range(0, i + 1):
					if A[j] == '0':
						mystr = str(1) + mystr
					else:
						mystr = str(0) + mystr
				A = mystr + A[i + 1:]
				count += 2
	if count == 0:
		print(count)
	else:
		print(count, end=' ')
		for i in range(0, count):
			if i == count - 1:
				print(C[i])
			else:
				print(C[i], end=' ')


import sys, collections

def flip(num):
	for i in range(num):
		if a[i] == '0':
			a[i] = '1'
		else:
			a[i] = '0'
	a[:num].reverse()
tc = int(sys.stdin.readline())
for _ in range(tc):
	n = int(sys.stdin.readline())
	a = list(sys.stdin.readline().rstrip())
	b = list(sys.stdin.readline().rstrip())
	temp = []
	for i in range(n):
		if a[i] != b[i]:
			if i > 0:
				temp.append(i + 1)
			temp.append(1)
			if i > 0:
				temp.append(i + 1)
	print(len(temp), end=' ')
	print(' '.join(map(str, temp)))


from sys import stdin, gettrace
if gettrace():
	inputi = input
else:

	def input():
		return next(stdin)[:-1]

	def inputi():
		return stdin.buffer.readline()

def solve():
	n = int(input())
	aa = list(input())
	bb = list(input())
	res = []
	for i in range(n - 1, 0, -1):
		if aa[i] != bb[i]:
			if aa[0] == aa[i]:
				res += [i + 1]
			else:
				res += [1, i + 1]
				aa[0] = '0' if aa[0] == '1' else '1'
			an = []
			for j in range(i, -1, -1):
				an.append('1' if aa[j] == '0' else '0')
			aa[:i + 1] = an
	if aa[0] != bb[0]:
		res.append(1)
		aa[0] = '0' if aa[0] == '1' else '1'
	print(len(res), ' '.join(map(str, res)))

def main():
	t = int(input())
	for _ in range(t):
		solve()
main()


import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__

def gift():
	for _ in range(t):
		n = int(input())
		a = list(input())
		b = list(input())
		res = []
		for i in range(n):
			if a[n - 1 - i] != b[n - 1 - i]:
				if a[0] == b[n - 1 - i]:
					res.append(1)
					a[0] = '1' if b[n - 1 - i] == '0' else '1'
				res.append(n - i)
				for j in range(n - i):
					if a[j] == '0':
						a[j] = '1'
					else:
						a[j] = '0'
				for j in range((n - i) // 2):
					(a[j], a[n - i - j - 1]) = (a[n - i - j - 1], a[j])
		yield ' '.join((str(x) for x in [len(res)] + res))
t = int(input())
ans = gift()
print(*ans, sep='\n')


for i in range(int(input())):
	n = int(input())
	s = input()
	s1 = input()
	s = s + '0'
	s1 = s1 + '0'
	a = []
	b = []
	for i in range(1, n + 1):
		if s[i] != s[i - 1]:
			a.append(i)
		if s1[i] != s1[i - 1]:
			b.append(i)
	for i in range(len(b)):
		a.append(b[len(b) - i - 1])
	print(len(a), *a)


from sys import stdin
input = lambda : stdin.readline().rstrip('\r\n')
from collections import deque as que, defaultdict as vector
from heapq import *
inin = lambda : int(input())
inar = lambda : list(map(int, input().split()))
Testcase = inin()
for _ in range(Testcase):
	n = inin()
	a = list(input())
	b = list(input())
	a = [int(x) for x in a]
	b = [int(x) for x in b]
	ans = []
	while a:
		if len(a) == 1:
			if a[0] != b[0]:
				ans.append(1)
			break
		if a[-1] == b[-1]:
			a.pop()
			b.pop()
			n -= 1
			continue
		else:
			if b[-1] == 0:
				if a[0] != 1:
					ans.append(1)
					a[0] = 1
				ans.append(n)
				n -= 1
			else:
				if a[0] != 0:
					ans.append(1)
					a[0] = 1
				ans.append(n)
				n -= 1
			a = [1 - x for x in a]
			a.reverse()
			a.pop()
			b.pop()
	print(len(ans), *ans)


def inv_rev(s):
	r = ''
	for i in s[::-1]:
		if i == '1':
			r += '0'
		else:
			r += '1'
	return r
for _ in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	if a == b:
		print(0)
		continue
	res = []
	for i in range(n - 1, -1, -1):
		if a[i] != b[i]:
			if a[0] == b[i]:
				res.append(1)
				a = inv_rev(a[0]) + a[1:]
				res.append(i + 1)
				a = inv_rev(a[:i + 1]) + a[i + 1:]
			else:
				res.append(i + 1)
				a = inv_rev(a[:i + 1]) + a[i + 1:]
	print(len(res), end=' ')
	for r in res:
		print(r, end=' ')
	print()


for _ in range(int(input())):
	n = int(input())
	s = input()
	b = input()
	count = 0
	A = []
	for i in range(n):
		if s[i] != b[i]:
			A += [i + 1, 1, i + 1]
			count += 1
	print(count * 3, *A)


t = int(input())

def flip(i):
	c = [k ^ 1 for k in a[:i + 1]]
	for j in range(i + 1):
		a[j] = c[i - j]
for _ in range(t):
	n = int(input())
	a = [int(i) for i in input()]
	b = [int(i) for i in input()]
	an = []
	i = n - 1
	while i > -1:
		if b[i] == a[0]:
			an.append(1)
			flip(0)
			flip(i)
			an.append(i + 1)
		else:
			an.append(i + 1)
			flip(i)
		i -= 1
	print(len(an), *an)


def solve(a, b, n):
	i = n - 1
	res = []
	while i >= 0 and a[:i + 1] != b[:i + 1]:
		if b[i] != a[i]:
			if b[i] == a[0]:
				a = flip(a, 1)
				res.append(1)
			a = flip(a, i + 1)
			res.append(i + 1)
		i = i - 1
	print(len(res), end=' ')
	for i in res:
		print(i, end=' ')
	print()

def flip(a, n):
	part = a[:n]
	new = ''
	for i in part:
		if i == '1':
			new += '0'
		else:
			new += '1'
	return new[::-1] + a[n:]
for i in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	solve(a, b, n)


for t in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	flag = 0
	l = 0
	r = n - 1
	ans = []
	for i in range(n - 1, -1, -1):
		if flag == 0:
			if a[l] == b[i]:
				ans.append(1)
			ans.append(i + 1)
			flag = 1
			l += 1
		else:
			if a[r] != b[i]:
				ans.append(1)
			ans.append(i + 1)
			flag = 0
			r -= 1
	print(len(ans), end=' ')
	print(*ans)


def change(k):
	res = [0] * k
	for i in range(k):
		if a[i] == 1:
			res[k - i - 1] = 0
		else:
			res[k - i - 1] = 1
	a[:k] = res
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input()))
	b = list(map(int, input()))
	res = []
	for i in range(n - 1, -1, -1):
		if a[i] != b[i]:
			if b[i] == a[0]:
				change(1)
				change(i + 1)
				res.extend([0, i])
			else:
				res.append(i)
				change(i + 1)
	l = len(res)
	print(l, end=' ')
	for i in range(l):
		print(res[i] + 1, end=' ')
	print()


def multiple_input():
	return map(int, input().split())

def list_input():
	return list(map(int, input().split()))
for _ in range(int(input())):
	n = int(input())
	s = list(input().strip())
	x = input()
	i = n - 1
	ans = []
	while i > 0:
		if s[i] == x[i]:
			i -= 1
		elif s[0] == s[i]:
			ans.append(i + 1)
			end = i
			for j in range((end + 1) // 2):
				if s[j] == '0':
					a = '1'
				else:
					a = '0'
				if s[end - j] == '0':
					b = '1'
				else:
					b = '0'
				(s[j], s[end - j]) = (b, a)
			if (end + 1) % 2 != 0:
				if s[(end + 1) // 2] == '0':
					s[(end + 1) // 2] = '1'
				else:
					s[(end + 1) // 2] = '0'
			i -= 1
		else:
			ans.append(1)
			ans.append(i + 1)
			s[0] = s[i]
			end = i
			for j in range((end + 1) // 2):
				if s[j] == '0':
					a = '1'
				else:
					a = '0'
				if s[end - j] == '0':
					b = '1'
				else:
					b = '0'
				(s[j], s[end - j]) = (b, a)
			if (end + 1) % 2 != 0:
				if s[(end + 1) // 2] == '0':
					s[(end + 1) // 2] = '1'
				else:
					s[(end + 1) // 2] = '0'
			i -= 1
	if s[0] != x[0]:
		ans.append(1)
	print(len(ans), *ans)


T = int(input())
for t in range(T):
	n = int(input())
	a = input()
	b = input()
	isInv = False
	l = 0
	r = n - 1
	i = n - 1
	ans = 0
	res = []
	while i >= 0:
		if isInv:
			if b[i] == a[l]:
				if b[i] != a[r]:
					res.append(1)
					ans += 1
				ans += 1
				res.append(r - l + 1)
				r -= 1
				isInv = not isInv
			else:
				l += 1
		elif b[i] != a[r]:
			if b[i] == a[l]:
				res.append(1)
				ans += 1
			ans += 1
			res.append(r - l + 1)
			l += 1
			isInv = not isInv
		else:
			r -= 1
		i -= 1
	print(ans, end=' ')
	for i in range(ans):
		print(res[i], end=' ')
	print()


def split(word):
	return [char for char in word]

def flip(li, e):
	for i in range(e):
		if li[i] == '1':
			li[i] = '0'
		else:
			li[i] = '1'
	return li

def rev(li, e):
	liref = li[0:e]
	liref = liref[::-1]
	for i in range(len(liref)):
		li[i] = liref[i]
	return li
t = int(input())
for _ in range(t):
	n1 = int(input())
	s1 = input()
	s2 = input()
	li2 = split(s2)
	li1 = split(s1)
	i = n1 - 1
	anslist = []
	while i >= 0:
		if i == 0:
			if li1[0] != li2[0]:
				anslist.append(1)
			break
		if li1[i] != li2[i]:
			j = i
			if li1[0] != li2[i]:
				anslist.append(i + 1)
				li1 = flip(li1, i + 1)
				li1 = rev(li1, i + 1)
			else:
				j = i
				for k in range(i - 1, -1, -1):
					if li1[k] == li2[i]:
						j = k
						break
				li1 = flip(li1, j + 1)
				li1 = rev(li1, j + 1)
				anslist.append(j + 1)
				anslist.append(i + 1)
				li1 = flip(li1, i + 1)
				li1 = rev(li1, i + 1)
		i = i - 1
	print(len(anslist), end=' ')
	for i in anslist:
		print(i, end=' ')
	print()


t = int(input())
for _ in range(t):
	n = int(input())
	a = input()
	b = input()
	count = 0
	for i in range(n):
		if a[i] != b[i]:
			count += 1
	print(3 * count, end=' ')
	for i in range(n):
		if a[i] != b[i]:
			print(i + 1, 1, i + 1, sep=' ', end=' ')
	print('')


import math, sys
from sys import stdin, stdout
from collections import Counter, defaultdict, deque
input = stdin.readline
I = lambda : int(input())
li = lambda : list(map(int, input().split()))

def solve():
	n = I()
	a = list(input().strip())
	b = list(input().strip())
	ans = []
	while len(a):
		if a[-1] == b[-1]:
			a.pop()
			b.pop()
			n -= 1
		else:
			if a[0] == b[-1]:
				a[0] = a[-1]
				for i in range(len(a)):
					if a[i] == '0':
						a[i] = '1'
					else:
						a[i] = '0'
				a = a[::-1]
				ans.append(1)
				ans.append(n)
			else:
				for i in range(len(a)):
					if a[i] == '0':
						a[i] = '1'
					else:
						a[i] = '0'
				a = a[::-1]
				ans.append(n)
			n -= 1
			a.pop()
			b.pop()
	if len(ans):
		print(len(ans), *ans)
	else:
		print(0)
for _ in range(I()):
	solve()


for _ in range(int(input())):
	length = int(input())
	a = list(input())
	b = list(input())
	count = length - 1
	c_list = []
	invert = 0
	reverse = 1
	le = 0
	ri = length - 1
	while count != -1:
		if reverse:
			if int(a[ri]) != (invert + int(b[count])) % 2:
				if a[ri] == a[le]:
					c_list.append(count + 1)
				else:
					c_list.append(1)
					c_list.append(count + 1)
				reverse = 1 - reverse
				invert = 1 - invert
				le += 1
			else:
				ri -= 1
		elif int(a[le]) != (invert + int(b[count])) % 2:
			if a[ri] == a[le]:
				c_list.append(count + 1)
			else:
				c_list.append(1)
				c_list.append(count + 1)
			reverse = 1 - reverse
			invert = 1 - invert
			ri -= 1
		else:
			le += 1
		count -= 1
	print(len(c_list), *c_list)


t = int(input())
for _ in range(t):
	y = int(input())
	f = 0
	a = input()
	b = input()
	a = list(a)
	b = list(b)
	z = []
	for q in range(y):
		if a[q] != b[q] and q == 0:
			f = f + 1
			z.append(1)
		elif q != 0 and a[q] != b[q]:
			z.append(q + 1)
			f = f + 1
			z.append(1)
			f = f + 1
			z.append(q + 1)
			f = f + q + 1
	print(len(z), *z)


II = lambda : input()
JJ = lambda : int(input())
KK = lambda : list(map(int, input().split()))
for _ in range(int(input())):
	n = JJ()
	A = II()
	B = II()
	k = []
	for i in range(n - 1, -1, -1):
		if A[i] == B[i]:
			continue
		elif A[0] == B[i]:
			k.append(1)
			k.append(i + 1)
			A = A[:1].replace('1', '2').replace('0', '1').replace('2', '0')[::-1] + A[1:]
			A = A[:i + 1].replace('1', '2').replace('0', '1').replace('2', '0')[::-1] + A[i + 1:]
		else:
			k.append(i + 1)
			A = A[:i + 1].replace('1', '2').replace('0', '1').replace('2', '0')[::-1] + A[i + 1:]
	print(len(k), *k)


import string

def fun(s, n):
	ans = ''
	for i in range(n):
		w = int(s[i])
		ans = str(abs(w - 1)) + ans
	return list(ans)

def solve(a, ans):
	j = -1
	while j >= 0:
		j = -1
		for i in range(n - 1, -1, -1):
			if a[i] == '1':
				j = i
				break
		if a[0] == '0':
			ans.append(1)
			a[0] = 1
		ans.append(j)
	return ans
t = int(input())
for _ in range(t):
	n = int(input())
	a = input()
	b = input()
	if a == b:
		print(0)
		continue
	a = list(a)
	b = list(b)
	ans = []
	n1 = n
	j = n - 1
	while a.count('1') != 0:
		for i in range(len(a) - 1, -1, -1):
			if a[i] == '1':
				j = i
				break
		if a[0] == '0':
			a[0] = 1
			ans.append(1)
		ans.append(j + 1)
		a = fun(a, j + 1)
	ans1 = ans[:]
	n = n1
	a = b[:]
	j = n - 1
	ans = []
	while a.count('1') != 0:
		for i in range(len(a) - 1, -1, -1):
			if a[i] == '1':
				j = i
				break
		if a[0] == '0':
			a[0] = 1
			ans.append(1)
		ans.append(j + 1)
		a = fun(a, j + 1)
	ans.reverse()
	x = len(ans1 + ans)
	print(x, *ans1 + ans)


t = int(input())
for i in range(t):
	n = int(input())
	a = input()
	b = input()
	ans = []
	index = 0
	while index < n:
		if a[index] != b[index]:
			if index == 0:
				ans.append(index + 1)
			else:
				ans.append(index + 1)
				ans.append(1)
				ans.append(index + 1)
		index += 1
	ans.insert(0, len(ans))
	res = list(map(str, ans))
	print(' '.join(res))


import sys
import collections

def input():
	return sys.stdin.readline().rstrip()

def split_input():
	return [int(i) for i in input().split()]
tests = int(input())

def flip(s):
	if s == '1':
		return '0'
	else:
		return '1'
for _ in range(tests):
	n = int(input())
	a = list(input())
	b = list(input())
	ans = []
	for i in range(n - 1, -1, -1):
		if a[0] == b[i]:
			ans.append(1)
		a[0] = flip(a[0])
		ans.append(i + 1)
		for j in range(len(a)):
			a[j] = flip(a[j])
		a.reverse()
		a.pop()
	print('%d ' % len(ans), end='')
	print(*ans, sep=' ')


t = int(input())
for _ in range(t):
	n = int(input())
	list1 = list(map(int, input()))
	list2 = list(map(int, input()))
	ans = []
	for i in range(n - 1, 0 - 1, -1):
		aim = list2[i]
		if list1[i] == aim:
			list1.pop(i)
			continue
		if list1[0] == aim:
			ans.append(1)
			if list1[0] == 1:
				list1[0] = 0
			else:
				list1[0] = 1
			ans.append(i + 1)
		else:
			ans.append(i + 1)
		for i in range(len(list1)):
			if list1[i] == 0:
				list1[i] = 1
			else:
				list1[i] = 0
		list1.reverse()
		list1.pop(i)
	print(len(ans), end=' ')
	for val in ans:
		print(val, end=' ')
	print()


t = int(input())
for _ in range(t):
	n = int(input())
	a = str(input())
	b = str(input())
	a_pref0 = [n] * n
	a_pref1 = [n] * n
	b_pref0 = [n] * n
	b_pref1 = [n] * n
	a_ans = []
	b_ans = []
	for i in range(n - 2, -1, -1):
		if a[i + 1] == '0':
			a_pref0[i] = i + 1
		else:
			a_pref0[i] = a_pref0[i + 1]
		if a[i + 1] == '1':
			a_pref1[i] = i + 1
		else:
			a_pref1[i] = a_pref1[i + 1]
		if b[i + 1] == '0':
			b_pref0[i] = i + 1
		else:
			b_pref0[i] = b_pref0[i + 1]
		if b[i + 1] == '1':
			b_pref1[i] = i + 1
		else:
			b_pref1[i] = b_pref1[i + 1]
	a_pos = 0
	for i in range(n):
		x = a[i]
		if x == '0':
			if a_pref0[i] == i + 1:
				continue
			else:
				a_ans.append(i + 1)
		elif a_pref1[i] == i + 1:
			continue
		else:
			a_ans.append(i + 1)
	if a[-1] == '1':
		a_ans.append(n)
	for i in range(n):
		x = b[i]
		if x == '0':
			if b_pref0[i] == i + 1:
				continue
			else:
				b_ans.append(i + 1)
		elif b_pref1[i] == i + 1:
			continue
		else:
			b_ans.append(i + 1)
	if b[-1] == '1':
		b_ans.append(n)
	b_ans.reverse()
	a_ans += b_ans
	if len(a_ans) > 0:
		print(len(a_ans), *a_ans)
	else:
		print(0)


T = int(input())

def flip(el):
	if el == '1':
		return '0'
	else:
		return '1'

def oper(arr):
	return [flip(el) for el in arr[::-1]]

def solve(a, b):
	ans = ''
	opc = 0
	for i in range(0, n):
		if a[n - i - 1] != b[n - i - 1]:
			if a[0] == a[n - i - 1]:
				ans += str(n - i) + ' '
				a = oper(a[:n - i])
				opc += 1
			else:
				ans += '1' + ' ' + str(n - i) + ' '
				a = oper(a[:n - i])
				opc += 2
		else:
			continue
	return (ans, opc)
while T > 0:
	n = int(input())
	a = [el for el in input()]
	b = [el for el in input()]
	if a != b:
		(ans, opc) = solve(a, b)
		if n == 1:
			print('1 1')
		else:
			print(str(opc) + ' ' + ans)
	else:
		print(0)
	T -= 1


from collections import *
from sys import *
input = stdin.readline
for _ in range(int(input())):
	n = int(input())
	a1 = input()
	b1 = input()
	a = deque([])
	for i in a1:
		a.append(i)
	b = []
	for i in b1:
		b.append(i)
	a.pop()
	b.pop()
	ans = []
	r = 0
	for i in range(n - 1, -1, -1):
		if r == 0:
			if a[-1] == b[i]:
				a.pop()
			elif a[0] != b[i]:
				ans.append(i + 1)
				r = 1
				a.popleft()
			else:
				ans.append(1)
				ans.append(i + 1)
				r = 1
				a.popleft()
		elif a[0] != b[i]:
			a.popleft()
		elif a[-1] == b[i]:
			ans.append(i + 1)
			r = 0
			a.pop()
		else:
			ans.append(1)
			ans.append(i + 1)
			r = 0
			a.pop()
	print(len(ans), *ans)


import sys
from collections import defaultdict
for _ in range(int(input())):
	n = int(input())
	a = list(input())
	b = list(input())
	res = []
	for i in range(n - 1, -1, -1):
		if a[i] == b[i]:
			continue
		if b[i] == '0':
			if a[0] == '0':
				a[0] = '1'
				res.append(1)
			res.append(i + 1)
		else:
			if a[0] == '1':
				a[0] == '0'
				res.append(1)
			res.append(i + 1)
		for j in range(i + 1):
			if a[j] == '1':
				a[j] = '0'
			else:
				a[j] = '1'
		for j in range((i + 1) // 2):
			(a[j], a[i - j]) = (a[i - j], a[j])
	print(len(res), *res)


def funny(a, l):
	m = []
	for i in range(a):
		l[i] = 1 - l[i]
		m.append(l[i])
	m.reverse()
	l = m[:] + l[a:]
	return l
t = int(input())
final = []
for i in range(t):
	n = int(input())
	a = [int(x) for x in input()]
	b = [int(x) for x in input()]
	k = []
	juju = []
	while a != b:
		count = 0
		for j in range(len(a)):
			if a[len(a) - j - 1] == b[len(b) - j - 1]:
				count += 1
			else:
				break
		count = len(a) - count
		if a[0] == a[count - 1]:
			p = count
		else:
			p = 1
			for i in range(count - 1):
				if a[0] == a[count - 1 - i]:
					p = count - i
					break
		a = funny(p, a)
		juju.append(p)
	final.append([len(juju), *juju])
for i in range(t):
	print(*final[i])


import sys
from collections import deque
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().strip()))
	brr = list(map(int, input().strip()))
	arr = deque(arr)
	order = 1
	ans = []
	op = 0
	for i in range(n - 1, -1, -1):
		if order == 1:
			if op % 2 == 1 and brr[i] != arr[i]:
				arr.pop()
			elif op % 2 == 0 and brr[i] == arr[i]:
				arr.pop()
			elif arr[0] != arr[i]:
				ans += [1]
				arr.popleft()
				if len(arr) > 0:
					order = 0
					op += 1
					ans += [i + 1]
			else:
				ans += [i + 1]
				arr.popleft()
				order = 0
				op += 1
		else:
			if op % 2 == 0 and brr[i] == arr[0]:
				arr.popleft()
				pass
			if op % 2 == 1 and brr[i] != arr[0]:
				arr.popleft()
				pass
			elif arr[0] != arr[i]:
				ans += [1]
				arr.pop()
				if len(arr) > 0:
					order = 1
					ans += [i + 1]
					op += 1
			else:
				ans += [i + 1]
				arr.pop()
				order = 1
				op += 1
	print(len(ans), *ans)


import sys
input = sys.stdin.readline
t = int(input())
for ii in range(t):
	n = int(input())
	a = [i for i in input() if i != '\n']
	b = [i for i in input() if i != '\n']
	if n == 1:
		if a != b:
			sys.stdout.write('1' + ' ' + '1' + '\n')
		else:
			sys.stdout.write('0' + '\n')
	elif a == b:
		sys.stdout.write('0' + '\n')
	else:
		ans = []
		for i in range(1, n):
			if a[i] != a[i - 1]:
				ans.append(i)
		if a[-1] == '1':
			ok = False
			for i in range(n - 1, 0, -1):
				if b[i] == '0' and ok == False:
					ok = True
					ans.append(i + 1)
				if b[i] != b[i - 1]:
					ok = True
					ans.append(i)
		else:
			ok = False
			for i in range(n - 1, 0, -1):
				if b[i] == '1' and ok == False:
					ok = True
					ans.append(i + 1)
				if b[i] != b[i - 1]:
					ok = True
					ans.append(i)
		sys.stdout.write(str(len(ans)) + '\n')
		ans = ' '.join(map(str, ans))
		sys.stdout.write(ans + '\n')


tests = int(input())
for t in range(tests):
	n = int(input())
	str_1 = list(map(int, input()))
	str_2 = list(map(int, input()))
	res = []
	front = 0
	back = n - 1
	new_str_1 = []
	for i in range(n):
		if i % 2 == 0:
			new_str_1.append(str_1[front])
			front += 1
		else:
			new_str_1.append(str_1[back])
			back -= 1
	str_1 = new_str_1
	parity = 0
	curr_pt_2 = n - 1
	while curr_pt_2 > 0:
		target = str_2[curr_pt_2]
		curr = str_1[n - 1 - curr_pt_2]
		if parity % 2 == 0:
			if target != curr:
				res.append(curr_pt_2 + 1)
			else:
				res.append(1)
				res.append(curr_pt_2 + 1)
		elif target == curr:
			res.append(curr_pt_2 + 1)
		else:
			res.append(1)
			res.append(curr_pt_2 + 1)
		parity += 1
		curr_pt_2 -= 1
	if parity % 2 != 0:
		if str_1[n - 1] == str_2[0]:
			res.append(1)
	elif str_1[n - 1] != str_2[0]:
		res.append(1)
	res = [len(res)] + res
	print(' '.join(list(map(str, res))))


for _ in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	c = []
	for i in range(0, n):
		if a[i] != b[i]:
			if i > 0:
				c.append(i + 1)
			c.append(1)
			if i > 0:
				c.append(i + 1)
	if len(c) != 0:
		print(len(c), end=' ')
		for i in c:
			print(i, end=' ')
		print()
	else:
		print(0)


t = int(input())
for _ in range(t):
	n = int(input())
	a = input()
	b = input()
	a = list(a)
	b = list(b)
	k = 0
	i = 0
	l = []
	j = 0
	while i < n and j < n:
		if a[i] == b[j]:
			i += 1
			j += 1
		else:
			k += 3
			l.append(i + 1)
			for x in range(i + 1):
				if a[x] == '1':
					a[x] = '0'
				else:
					a[x] = '1'
			c = a[0:i + 1]
			c.reverse()
			a[0:i + 1] = c
			if a[0] == '1':
				a[0] == '0'
			else:
				a[0] = '1'
			l.append(1)
			for x in range(i + 1):
				if a[x] == '1':
					a[x] = '0'
				else:
					a[x] = '1'
			c = a[0:i + 1]
			c.reverse()
			a[0:i + 1] = c
			l.append(i + 1)
			i += 1
			j += 1
	if k == 0:
		print(0)
	else:
		print(k, end=' ')
		for x in range(len(l)):
			print(l[x], end=' ')
	print(' ')


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(input().rstrip('\n'))
	arr1 = list(input().rstrip('\n'))
	if arr == arr1:
		print(0)
		continue
	i = 0
	j = n - 1
	result = []
	while i != n:
		if arr[i] == arr1[j]:
			result.extend([1, j + 1, j])
		else:
			result.extend([j + 1, j])
		i += 1
		j -= 1
	result.pop()
	print(len(result), end=' ')
	print(*result)


def solve(a, b, n, ans):
	orig = a[:]
	moves = []
	end = n - 1
	start = 0
	visited = set()
	end1 = n - 1
	done = 0
	while end1 >= 0:
		if done % 2 == 0:
			start_val = a[start]
			end_val = a[end]
		else:
			start_val = str(int(not int(a[start])))
			end_val = str(int(not int(a[end])))
		if end_val != b[end1]:
			if start_val == end_val:
				moves.append(abs(end - start) + 1)
				done += 1
				visited.add(end)
				(end, start) = (start, end)
				end1 -= 1
				if end < start:
					end += 1
				else:
					end -= 1
			else:
				moves.append(1)
				a[start] = str(int(not int(a[start])))
		else:
			visited.add(end)
			if end < start:
				end += 1
			else:
				end -= 1
			end1 -= 1
	ans.append(moves)

def main():
	t = int(input())
	ans = []
	for i in range(t):
		n = int(input())
		a = list(input())
		b = list(input())
		solve(a, b, n, ans)
	for i in ans:
		print(len(i), end=' ')
		for j in i:
			print(j, end=' ')
		print()
main()


for _ in range(int(input())):
	l = int(input())
	a = input()
	b = input()
	lst = []
	for i in range(l):
		if a[i] != b[i]:
			lst += [i + 1, 1, i + 1]
	print(len(lst), *lst)


import sys
q = int(input())
for i in range(q):
	n = int(sys.stdin.readline())
	a = list(input())
	b = input()
	k = 0
	ops = []
	n -= 1
	while n >= 0:
		if a[n] != b[n]:
			if a[0] == a[n]:
				k += 1
				ops.append(n + 1)
				kt = a[n::-1]
				for i in range(n + 1):
					if kt[i] == '0':
						a[i] = '1'
					else:
						a[i] = '0'
			else:
				k += 2
				ops.append(1)
				ops.append(n + 1)
				if a[0] == '0':
					a[0] = '1'
				else:
					a[0] = '0'
				kt = a[n::-1]
				for i in range(n + 1):
					if kt[i] == '0':
						a[i] = '1'
					else:
						a[i] = '0'
		n -= 1
	print(k, *ops)


for _ in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	s = []
	for i in range(n):
		if a[i] != b[i]:
			if i > 0:
				s.append(i + 1)
			s.append(0 + 1)
			if i > 0:
				s.append(i + 1)
	print(len(s), *s, sep=' ')


from fractions import Fraction
from collections import defaultdict
import math
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))

def input():
	return sys.stdin.readline().rstrip('\r\n')
t = int(input())
for _ in range(t):
	n = int(input())
	s1 = input()
	s2 = input()
	if s1 == s2:
		print('0')
	else:
		s1 = list(s1)
		s2 = list(s2)
		last = n - 1
		r = []
		while last >= 0:
			if s1[last] == s2[last]:
				last -= 1
			elif s1[0] != s2[last]:
				r.append(last + 1)
				tmp = list(s1)
				for i in range(last + 1):
					tmp[i] = chr(1 - (ord(s1[last - i]) - 48) + 48)
				for i in range(last + 1):
					s1[i] = tmp[i]
			else:
				r.append(1)
				s1[0] = chr(1 - (ord(s1[0]) - 48) + 48)
		print(len(r), end=' ')
		for i in r:
			print(i, end=' ')
		print()


t = int(input())
for _ in range(t):
	n = int(input())
	a = input()
	b = input()
	new_a = []
	new_b = []
	for i in range(n):
		new_a.append(a[i])
	for i in range(n):
		new_b.append(b[i])
	a = new_a
	b = new_b
	answer = []
	flag = False
	last_i = -1
	temp = a
	counter = 0
	f = 0
	l = n
	for i in range(n):
		if flag is False:
			l -= 1
		else:
			f += 1
		if a[l] == b[n - 1 - i] if flag is False else a[f] != b[n - 1 - i]:
			continue
		if a[f] == b[n - 1 - i] if flag is False else a[l] != b[n - 1 - i]:
			answer.append(1)
		flag = not flag
		answer.append(n - 1 - i + 1)
	print(len(answer), *answer)


w = int(input())
for o in range(w):
	n = int(input())
	A = input()
	B = input()
	dp = list(map(int, list(A)))
	B = list(map(int, list(B)))
	count = 0
	ans = []
	for i in range(n - 1, -1, -1):
		if dp[i] != B[i]:
			if dp[0] != dp[i]:
				count += 1
				ans.append(1)
				dp[0] ^= 1
			count += 1
			ans.append(i + 1)
			(l, r) = (0, i)
			while l < r:
				(x, y) = (dp[l], dp[r])
				x ^= 1
				y ^= 1
				(dp[l], dp[r]) = (y, x)
				l += 1
				r -= 1
			if l == r:
				dp[l] ^= 1
	print(count, end=' ')
	for i in ans:
		print(i, end=' ')
	print()


t = int(input())
for i in range(t):
	n = int(input())
	a = input()
	b = input()
	count = []
	for i in range(len(a)):
		if a[i] != b[i]:
			count.append(i + 1)
			count.append(1)
			count.append(i + 1)
	print(len(count), *count, sep=' ')


def swap(s):
	l = []
	for i in s:
		l.append(i)
	for i in range(len(s)):
		if l[i] == '1':
			l[i] = '0'
		else:
			l[i] = '1'
	temp = ''
	for i in l:
		temp = temp + str(i)
	return temp[::-1]

def transform(n, a, b):
	if a == b:
		return []
	L = []
	for i in range(n - 1, -1, -1):
		if a[i] != b[i]:
			x = a[i]
			if a[0] == b[i]:
				L.append(1)
				a = x + a[1:]
			s = a[:i + 1]
			temp = swap(s)
			L.append(i + 1)
			a = temp + a[i + 1:]
	return L
t = int(input())
for _ in range(t):
	n = int(input())
	a = input()
	b = input()
	L = transform(n, a, b)
	l = len(L)
	s = str(l) + ' '
	for i in L:
		s = s + str(i) + ' '
	print(s)


def invert(x):
	s = ''
	for i in range(len(x)):
		if x[i] == '1':
			s += '0'
		else:
			s += '1'
	return s[::-1]
for _ in range(int(input())):
	i = int(input())
	a = input()
	b = input()
	i = i - 1
	c = 0
	l = []
	while i >= 0:
		if a[i] == b[i]:
			i -= 1
			continue
		if i >= 0:
			if b[i] == '0' and a[0] == '1' or (b[i] == '1' and a[0] == '0'):
				a = invert(a[:i + 1]) + a[i + 1:]
				l.append(i + 1)
				c += 1
				i -= 1
			else:
				a = invert(a[0]) + a[1:]
				l.append(1)
				a = invert(a[:i + 1]) + a[i + 1:]
				l.append(i + 1)
				c += 2
				i -= 1
	print(c, *l)


for _ in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	a = [int(a[i]) for i in range(n)]
	b = [int(b[i]) for i in range(n)]
	count = 0
	res = []
	for i in range(n - 1, -1, -1):
		if a[i] != b[i]:
			if a[0] != b[i]:
				count += 1
				res.append(i + 1)
				newa = [a[j] for j in range(n)]
				for j in range(i, -1, -1):
					id = i - j
					newa[j] = (1 + a[id]) % 2
				a = newa
			else:
				count += 1
				res.append(1)
				a[0] = (1 + a[0]) % 2
				count += 1
				res.append(i + 1)
				newa = [a[j] for j in range(n)]
				for j in range(i, -1, -1):
					id = i - j
					newa[j] = (1 + a[id]) % 2
				a = newa
	res = [count] + res
	print(*res)


t = int(input())
for _ in range(t):
	n = int(input())
	a = input()
	b = input()
	answer = []
	flag = False
	f = 0
	l = n
	for i in range(n):
		if flag is False:
			l -= 1
		else:
			f += 1
		if a[l] == b[n - 1 - i] if flag is False else a[f] != b[n - 1 - i]:
			continue
		if a[f] == b[n - 1 - i] if flag is False else a[l] != b[n - 1 - i]:
			answer.append(1)
		flag = not flag
		answer.append(n - 1 - i + 1)
	print(len(answer), *answer)


t = int(input())
for x in range(t):
	n = int(input())
	a = input()
	b = input()
	k = 0
	K = []
	A = [0] * n
	if n % 2 != 0:
		for y in range(0, n, 2):
			A[y] = n // 2 - y // 2
		for y in range(1, n, 2):
			A[y] = n // 2 + 1 + y // 2
	else:
		for y in range(1, n, 2):
			A[y] = n // 2 - y // 2 - 1
		for y in range(0, n, 2):
			A[y] = n // 2 + y // 2
	A[-1] = 0
	for y in range(n):
		if b[n - y - 1] == a[A[n - y - 1]]:
			if y % 2 == 0:
				k += 1
				K += [1]
		if b[n - y - 1] != a[A[n - y - 1]]:
			if y % 2 != 0:
				k += 1
				K += [1]
		k += 1
		K += [n - y]
	print(k, end=' ')
	for y in K:
		print(y, end=' ')
	print()


for _ in range(int(input())):
	n = int(input())
	ar = list(map(int, list(input())))
	br = list(map(int, list(input())))
	ar.append(0)
	br.append(0)
	ans = 0
	x1 = []
	x2 = []
	for i in range(1, n + 1):
		if ar[i] != ar[i - 1]:
			x1.append(i)
			ans += 1
		if br[i] != br[i - 1]:
			x2.append(i)
			ans += 1
	print(ans, *x1 + x2[::-1])


a = int(input())
for x in range(a):
	b = int(input())
	c = input()
	d = input()
	e = list(c)
	m = len(c) - 1
	s = []
	if c == d:
		print(0)
	else:
		while m >= 0:
			if e[m] == d[m]:
				m -= 1
			elif d[m] != e[0]:
				s.append(m + 1)
				for y in range(m + 1):
					if e[y] == '0':
						e[y] = '1'
					else:
						e[y] = '0'
				t = e[:m + 1]
				t.reverse()
				e = t + e[m + 1:]
				m -= 1
			else:
				s.append(1)
				if e[0] == '1':
					e[0] = '0'
				else:
					e[0] = '1'
				s.append(m + 1)
				for y in range(m + 1):
					if e[y] == '0':
						e[y] = '1'
					else:
						e[y] = '0'
				t = e[:m + 1]
				t.reverse()
				e = t + e[m + 1:]
				m -= 1
		print(len(s), end=' ')
		print(*s)


def reverse(end, arr):
	i = 0
	j = end
	while i < j:
		(arr[i], arr[j]) = (arr[j], arr[i])
		i += 1
		j -= 1

def flip(end, a):
	for i in range(end + 1):
		if a[i] == '0':
			a[i] = '1'
		else:
			a[i] = '0'

def solve(a, b, n, ans):
	moves = []
	end = n - 1
	while end >= 0:
		if a[end] != b[end]:
			if a[0] == a[end]:
				reverse(end, a)
				flip(end, a)
				moves.append(end + 1)
			else:
				reverse(0, a)
				flip(0, a)
				moves.append(1)
				reverse(end, a)
				flip(end, a)
				moves.append(end + 1)
		end -= 1
	ans.append(moves)

def main():
	t = int(input())
	ans = []
	for i in range(t):
		n = int(input())
		a = list(input())
		b = list(input())
		solve(a, b, n, ans)
	for i in ans:
		print(len(i), end=' ')
		for j in i:
			print(j, end=' ')
		print()
main()


def main():
	_ = input()
	a = input()
	b = input()
	ops = []
	for i in range(len(a)):
		if a[i] != b[i]:
			if i > 0:
				ops.append(i + 1)
				ops.append(1)
				ops.append(i + 1)
			else:
				ops.append(1)
	print(len(ops), end=' ')
	for op in ops:
		print(op, end=' ')
	print()
t = int(input())
for _ in range(t):
	main()


def transform(a, i):
	for i in range(i + 1):
		if a[i] == '0':
			a[i] = '1'
		else:
			a[i] = '0'
	if i != len(a) - 1:
		a = list(reversed(a[:i + 1])) + a[i + 1:]
	else:
		a = list(reversed(a[:i + 1]))
	return a
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(input())
	b = list(input())
	count = 0
	ans = []
	for i in range(n - 1, -1, -1):
		if a[i] == b[i]:
			continue
		elif b[i] == '0':
			if a[0] == '1':
				count += 1
			else:
				count += 2
				a = transform(a, 0)
				ans.append(1)
			a = transform(a, i)
			ans.append(i + 1)
		else:
			if a[0] == '0':
				count += 1
			else:
				count += 2
				a = transform(a, 0)
				ans.append(1)
			a = transform(a, i)
			ans.append(i + 1)
	print(count, *ans)


t = int(input())
for i2 in range(t):
	n = int(input())
	l1 = input()
	l2 = input()
	res = []
	pr = l1[0]
	d_pr = 1
	for i in range(1, n):
		if l1[i] != pr:
			res += [d_pr]
			pr = l1[i]
		d_pr += 1
	for i in range(n - 1, -1, -1):
		if l2[i] != pr:
			res += [d_pr]
			pr = l2[i]
		d_pr -= 1
	print(len(res), *res)


T = int(input())

def f(i, c, x, y):
	if c % 2:
		m = a[n - y - i - 1]
	else:
		m = a[x + i]
	if c % 2:
		return not m
	else:
		return m
for test in range(T):
	n = int(input())
	a = [bool(int(u)) for u in input()]
	b = [bool(int(u)) for u in input()]
	ans = []
	c = 0
	x = 0
	y = 0
	for i in range(n):
		if f(n - 1 - i, c, x, y) == b[n - 1 - i]:
			continue
		else:
			if f(0, c, x, y) == b[n - 1 - i]:
				ans.append(1)
			ans.append(n - i)
			c += 1
			if c % 2:
				y = i - x
			else:
				x = i - y
	print(len(ans), *ans)


import sys
input = lambda : sys.stdin.readline().rstrip('\n\r')

def read():
	return [int(c == '1') for c in input()]
from collections import deque
for _ in range(int(input())):
	n = int(input())
	(a, b) = (deque(read()), read())
	flip = 0
	rev = False
	moves = []
	for i in range(n - 1, -1, -1):
		first = a[0] ^ flip
		last = a[-1] ^ flip
		if rev:
			(first, last) = (last, first)
		if i == 0:
			if first != b[0]:
				moves.append(1)
			break
		if last != b[i]:
			if first == b[i]:
				moves.append(1)
			if not rev:
				a.popleft()
			else:
				a.pop()
			moves.append(i + 1)
			rev = not rev
			flip ^= 1
		elif not rev:
			a.pop()
		else:
			a.popleft()
	print(len(moves), *moves)


from __future__ import division, print_function
import itertools, os, sys, threading
from io import BytesIO, IOBase
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
from math import sqrt, floor, factorial, gcd, log, ceil
from collections import deque, Counter, defaultdict
from itertools import permutations, combinations
from math import gcd
from bisect import bisect
from random import randrange
input = lambda : sys.stdin.readline().rstrip('\r\n')
read = lambda : list(map(int, input().strip().split(' ')))

def main():
	for _ in range(int(input())):
		n = int(input())
		a = input()
		b = input()
		ans = []
		for i in range(n - 1):
			if a[i] != a[i + 1]:
				ans.append(i + 1)
		a = a[-1] * n
		f = 0
		for i in range(n - 1, -1, -1):
			if f ^ int(a[i]) != int(b[i]):
				ans.append(i + 1)
				f += 1
				f %= 2
		print(len(ans), *ans)
main()


def notS(s):
	t = ''
	for i in s:
		if i == '0':
			t += '1'
		else:
			t += '0'
	return t
t = int(input())
for l in range(t):
	n = int(input())
	a = input()
	b = input()
	ar = []
	i = len(a) - 1
	while i >= 0:
		if i == 0:
			if a[i] != b[i]:
				ar.append(1)
			break
		elif a[i] == b[i]:
			i -= 1
			continue
		else:
			j = i
			while j != -1 and a[j] != b[i]:
				j -= 1
			if j == -1:
				ar.append(1)
				a = b[i] + a[1:]
				j = 0
			x = a[:j + 1]
			x = notS(x)
			x = x[::-1]
			a = x + a[j + 1:]
			ar.append(j + 1)
			x = a[:i + 1]
			x = notS(x)
			x = x[::-1]
			a = x + a[i + 1:]
			ar.append(i + 1)
			i -= 1
	print(len(ar), end=' ')
	for i in ar:
		print(i, end=' ')
	print()


import os
import sys
import math
import heapq
from decimal import *
from io import BytesIO, IOBase
from collections import defaultdict, deque

def r():
	return int(input())

def rm():
	return map(int, input().split())

def rl():
	return list(map(int, input().split()))

def change(a, n):
	temp = []
	for i in range(n):
		temp.append(1 - a[i])
	return temp[::-1] + a[n:]
for _ in range(r()):
	n = r()
	a = list(map(int, input()))
	b = list(map(int, input()))
	ans = True
	for i in range(n):
		if a[i] != b[i]:
			ans = False
			break
	if ans:
		print(0)
		continue
	res = []
	for i in range(n - 1, -1, -1):
		if b[i] == a[i]:
			continue
		if b[i] == a[0]:
			a[0] = 1 - a[0]
			res.append(1)
			res.append(i + 1)
			a = change(a, i + 1)
		else:
			res.append(i + 1)
			a = change(a, i + 1)
	print(len(res), *res)


t = int(input(''))

def flip(i, a):
	s = ''
	for j in range(i, -1, -1):
		if a[j] == '1':
			s += '0'
		else:
			s += '1'
	if len(s) == len(a):
		return s
	else:
		s += a[i + 1:]
		return s
for k in range(t):
	n = int(input(''))
	a = input('')
	b = input('')
	c = 0
	l = []
	for i in range(len(a) - 1, -1, -1):
		if a != b and c <= 3 * n:
			if a[i] != b[i]:
				if b[i] != a[0]:
					a = flip(i, a)
					l.append(i + 1)
					c += 1
				else:
					a = flip(0, a)
					a = flip(i, a)
					l.append(1)
					l.append(i + 1)
					c += 2
	print(c, end=' ')
	for i in l:
		print(i, end=' ')
	print()


def change(a, number):
	for i in range(0, number):
		a[i] = '1' if a[i] == '0' else '0'
	reversed_a = a[0:number]
	reversed_a.reverse()
	a = reversed_a + a[number:]
	return a

def main():
	_ = input()
	a = list(input())
	b = list(input())
	ops = []
	for i in range(len(a) - 1, -1, -1):
		if b[i] != a[i]:
			if i == 0:
				ops.append(1)
				a = change(a, 1)
			else:
				if a[i] != a[0]:
					ops.append(1)
					a = change(a, 1)
				ops.append(i + 1)
				a = change(a, i + 1)
	print(len(ops), end=' ')
	for op in ops:
		print(op, end=' ')
	print()
t = int(input())
for _ in range(t):
	main()


t = int(input())
for i in range(t):
	n = int(input())
	a = input()
	b = input()
	ans = []
	for j in range(n - 1, -1, -1):
		if j == 0:
			if a[j] != b[j]:
				ans.append(1)
			else:
				continue
		elif a[j] != b[j]:
			ans.append(j + 1)
			ans.append(1)
			ans.append(j + 1)
	print(str(len(ans)) + ' ' + ' '.join([str(x) for x in ans]))


to_bool = lambda c: c == '1'

def operation_iter(l, e):
	while e >= 0:
		yield (not l[e])
		e -= 1

def operation_edit(l, e):
	l[:e + 1] = list(operation_iter(l, e))

def solve():
	n = int(input())
	a = list(map(to_bool, input()))
	b = list(map(to_bool, input()))
	history = []
	key = a[-1]
	for i in range(n - 2, -1, -1):
		if a[i] != key:
			if a[0] == key:
				z = i
				while z >= 0 and a[z] != key:
					z -= 1
				history.append(z + 1)
				operation_edit(a, z)
			history.append(i + 1)
			operation_edit(a, i)
	for i in range(n - 1, -1, -1):
		if a[i] != b[i]:
			history.append(i + 1)
			operation_edit(a, i)
		i -= 1
	print(len(history), *history)
for _ in range(int(input())):
	solve()


t = int(input())
l = []
for i in range(t):
	n = int(input())
	a = list(map(int, list(input())))
	b = list(map(int, list(input())))
	s = []
	k = n - 1
	p = 0
	d = a[0]
	count = 0
	while k != -1:
		if k == 0:
			if d == b[0]:
				break
			else:
				s.append(1)
				p = p + 1
				k = k - 1
		elif d == b[k]:
			d = (b[k] + 1) % 2
			s.append(1)
			p = p + 1
			count = count + 1
			if count % 2 == 0:
				d = (a[count // 2] + count) % 2
			else:
				d = (a[n - count // 2 - 1] + count) % 2
			s.append(k + 1)
			k = k - 1
		else:
			count = count + 1
			if count % 2 == 0:
				d = (a[count // 2] + count) % 2
			else:
				d = (a[n - count // 2 - 1] + count) % 2
			s.append(k + 1)
			k = k - 1
	s.insert(0, count + p)
	l.append(s)
for i in l:
	for j in i:
		print(j, end=' ')
	print()


t = int(input())
for _ in range(t):
	n = int(input())
	(a, b) = (list(map(int, input())), list(map(int, input())))
	(ans, res) = (0, [])
	(l, r, num) = (0, n - 1, 0)
	for j in range(n - 1, -1, -1):
		(a0, ai) = (a[l] ^ num % 2, a[r] ^ num % 2)
		if num % 2 != 0:
			(a0, ai) = (ai, a0)
		if ai == b[j]:
			if num % 2 == 0:
				r -= 1
			else:
				l += 1
			continue
		if a0 == b[j]:
			ans += 1
			res.append(1)
		res.append(j + 1)
		ans += 1
		num += 1
		if num % 2 == 0:
			r -= 1
		else:
			l += 1
	print(ans, ' '.join(map(str, res)))


import sys
RI = lambda : [int(x) for x in sys.stdin.readline().strip().split()]
rw = lambda : sys.stdin.readline().strip().split()
ri = lambda : int(sys.stdin.readline().strip())
rs = lambda : sys.stdin.readline().strip()
from collections import defaultdict as df
from math import *
t = ri()
for _ in range(t):
	n = ri()
	l = list(rs())
	l = [int(i) for i in l]
	m = list(rs())
	m = [int(i) for i in m]
	direction = 1
	ans = []
	left = 0
	right = n - 1
	bit = 0
	i = n - 1
	while i >= 0 and right >= left:
		if direction == 1:
			if l[right] ^ bit == m[i]:
				i -= 1
				right -= 1
				continue
			if l[left] != l[right]:
				ans.append(1)
				l[left] ^= 1
				ans.append(right - left + 1)
				bit ^= 1
				direction ^= 1
				left += 1
				i -= 1
			else:
				ans.append(right - left + 1)
				bit ^= 1
				direction ^= 1
				left += 1
				i -= 1
		else:
			if l[left] ^ bit == m[i]:
				i -= 1
				left += 1
				continue
			if l[left] != l[right]:
				ans.append(1)
				l[right] ^= 1
				ans.append(right - left + 1)
				bit ^= 1
				direction ^= 1
				right -= 1
				i -= 1
			else:
				ans.append(right - left + 1)
				bit ^= 1
				direction ^= 1
				right -= 1
				i -= 1
	print(len(ans), *ans)


t = int(input())
for _ in range(t):
	n = int(input())
	a = str(input())
	b = str(input())
	if a == b:
		print('0')
	elif n == 1:
		print('1 1')
	else:
		c = 0
		l = []
		for i in range(n):
			if a[i] != b[i]:
				l.append(i + 1)
				l.append(1)
				l.append(i + 1)
				c += 3
		print(c, end=' ')
		print(*l)


for _ in range(int(input())):
	n = int(input())
	ar = list(input())
	br = list(input())
	ans = 0
	li = []
	for i in range(1, n + 1):
		if ar[-i] != br[-i]:
			if ar[0] == ar[-i]:
				ans += 1
				li.append(n - i + 1)
			else:
				ans += 2
				li.append(1)
				li.append(n - i + 1)
				ar[0] = ar[-i]
			tem = []
			for j in range(n - i + 1):
				if ar[j] == '0':
					tem.append('1')
				else:
					tem.append('0')
			tem = tem[::-1]
			for j in range(n - i + 1):
				ar[j] = tem[j]
	print(ans, *li)


import sys
input = sys.stdin.readline

def ceil(x):
	if x != int(x):
		x = int(x) + 1
	return x

def swaparr(arr, a, b):
	temp = arr[a]
	arr[a] = arr[b]
	arr[b] = temp

def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)

def nCr(n, k):
	if k > n - k:
		k = n - k
	res = 1
	for i in range(k):
		res = res * (n - i)
		res = res / (i + 1)
	return int(res)

def upper_bound(a, x, lo=0, hi=None):
	if hi == None:
		hi = len(a)
	while lo < hi:
		mid = (lo + hi) // 2
		if a[mid] < x:
			lo = mid + 1
		else:
			hi = mid
	return lo

def primefs(n):
	primes = {}
	while n % 2 == 0 and n > 0:
		primes[2] = primes.get(2, 0) + 1
		n = n // 2
	for i in range(3, int(n ** 0.5) + 2, 2):
		while n % i == 0 and n > 0:
			primes[i] = primes.get(i, 0) + 1
			n = n // i
	if n > 2:
		primes[n] = primes.get(n, 0) + 1
	return primes

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def swap(a, b):
	temp = a
	a = b
	b = temp
	return (a, b)

def find(x, link):
	p = x
	while p != link[p]:
		p = link[p]
	while x != p:
		nex = link[x]
		link[x] = p
		x = nex
	return p

def union(x, y, link, size):
	x = find(x, link)
	y = find(y, link)
	if size[x] < size[y]:
		(x, y) = swap(x, y)
	if x != y:
		size[x] += size[y]
		link[y] = x

def sieve(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime
MAXN = int(100000.0 + 5)

def spf_sieve():
	spf[1] = 1
	for i in range(2, MAXN):
		spf[i] = i
	for i in range(4, MAXN, 2):
		spf[i] = 2
	for i in range(3, ceil(MAXN ** 0.5), 2):
		if spf[i] == i:
			for j in range(i * i, MAXN, i):
				if spf[j] == j:
					spf[j] = i

def factoriazation(x):
	ret = {}
	while x != 1:
		ret[spf[x]] = ret.get(spf[x], 0) + 1
		x = x // spf[x]
	return ret

def int_array():
	return list(map(int, input().strip().split()))

def str_array():
	return input().strip().split()
MOD = int(1000000000.0) + 7
CMOD = 998244353
INF = float('inf')
NINF = -float('inf')
for _ in range(int(input())):
	n = int(input())
	a = input().strip()
	b = input().strip()
	ans = []
	for i in range(n):
		if a[i] != b[i]:
			if i > 0:
				ans.append(i + 1)
			ans.append(1)
			if i > 0:
				ans.append(i + 1)
	ans = [len(ans)] + ans
	print(*ans)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(input().strip())
	b = list(input().strip())
	ans = ''
	c = 0
	if n > 1:
		c = 0
		for j in range(n - 1, 0, -1):
			if a[j] == b[j]:
				continue
			else:
				if a[j] == a[0]:
					ans = ans + str(j + 1) + ' '
					c = c + 1
				else:
					ans = ans + '1 '
					ans = ans + str(j + 1) + ' '
					c = c + 2
				te = [str(int(v) ^ 1) for v in a[:j + 1]]
				te = te[::-1]
				a = te + a[j + 1:]
	if a[0] != b[0]:
		c = c + 1
		ans = ans + '1'
	ans = str(c) + ' ' + ans.strip()
	print(ans)


def rev(s):
	t = ''.join(('1' if x == '0' else '0' for x in s))
	t = t[::-1]
	return t

def foo():
	n = int(input())
	a = input()
	b = input()
	ans = []
	if a == b:
		print(0)
		return
	moves = 0
	i = n - 1
	while i >= 0:
		if a[i] != b[i]:
			if a[0] == b[i]:
				if a[0] == '1':
					a = '0' + a[1:]
				else:
					a = '1' + a[1:]
				a = rev(a[:i + 1]) + a[i + 1:]
				moves += 2
				ans.append(1)
				ans.append(i + 1)
			else:
				a = rev(a[:i + 1]) + a[i + 1:]
				moves += 1
				ans.append(i + 1)
		i -= 1
	print(moves, end=' ')
	for i in range(moves - 1):
		print(ans[i], end=' ')
	print(ans[moves - 1])
t = int(input())
for _ in range(t):
	foo()


for _ in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	c = 0
	l = []
	for i in range(n):
		if a[i] != b[i]:
			c += 3
			l.extend([i + 1, 1, i + 1])
	print(c, *l)


I = lambda : list(map(int, input().split()))
import sys
import bisect as bi
for tc in range(int(input())):
	(n,) = I()
	s = list(input()) + ['0']
	t = list(input()) + ['0']
	ans = []
	bns = []
	for i in range(1, n):
		if s[i] != s[i - 1]:
			ans.append(i)
	if s[n - 1] != t[n - 1]:
		ans.append(n)
	for i in range(1, n):
		if t[i] != t[i - 1]:
			bns.append(i)
	bns = bns[::-1]
	print(len(ans) + len(bns), *ans, *bns)


import sys
input = sys.stdin.readline
t = int(input())
for t1 in range(t):
	n = int(input())
	a1 = input()
	a1 = a1.strip()
	b1 = input()
	b1 = b1.strip()
	c = 0
	a = []
	b = []
	for i in range(n):
		a.append(int(a1[i]))
		b.append(int(b1[i]))
	ans = []
	for i in range(n - 1, -1, -1):
		if a[i] != b[i]:
			if a[0] != b[i]:
				for j in range(i + 1):
					a[j] = a[j] ^ 1
				a = a[:i + 1][::-1] + a[i + 1:]
				ans.append(i + 1)
			else:
				a[0] = b[i] ^ 1
				for j in range(i + 1):
					a[j] = a[j] ^ 1
				z = a[:i + 1]
				z = z[::-1]
				a = z + a[i + 1:]
				ans.append(1)
				ans.append(i + 1)
	s = ''
	for i in ans:
		s += str(i) + ' '
	print(len(ans), s)


import sys

def input():
	return sys.stdin.readline().rstrip()

def input_split():
	return [int(i) for i in input().split()]
testCases = int(input())
answers = []

def update_zero(arr, diff, num_flips):
	if num_flips % 2 == 0:
		i = -diff
		arr[i] = 1 - arr[i]
	else:
		i = diff
		arr[i] = 1 - arr[i]

def fetch_val(arr, index, diff, num_flips):
	if num_flips % 2 == 0:
		i = -diff + index
		ans = arr[i]
	else:
		i = diff - index
		ans = 1 - arr[i]
	return ans

def print_arr(arr, diff, num_flips, ans_till_now):
	temp = [fetch_val(arr, i, diff, num_flips) for i in range(ans_till_now[-1])]
	temp = temp + arr[ans_till_now[-1]:]
	print('array is ')
	print(*temp, sep=' ')
for _ in range(testCases):
	n = int(input())
	arr = [int(i) for i in input()]
	barr = [int(i) for i in input()]
	ans = []
	num_flips = 0
	diff = 0
	for i in range(n):
		index = n - 1 - i
		desired = barr[index]
		if fetch_val(arr, index, diff, num_flips) == desired:
			continue
		else:
			target = fetch_val(arr, 0, diff, num_flips)
			if target == 1 - desired:
				ans.append(index + 1)
				num_flips += 1
				diff = index - diff
			else:
				ans.append(1)
				update_zero(arr, diff, num_flips)
				ans.append(index + 1)
				num_flips += 1
				diff = index - diff
	answers.append(ans)
for ans in answers:
	print(len(ans), end=' ')
	print(*ans, sep=' ')


for _ in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	idx = 0
	flip = 0
	ans = []
	for i in range(n - 1, -1, -1):
		if flip ^ (a[idx] == b[i]):
			ans.append(1)
		ans.append(i + 1)
		if flip:
			idx -= i
		else:
			idx += i
		flip = not flip
	print(len(ans), end=' ')
	for i in ans:
		print(i, end=' ')
	print()


from __future__ import division, print_function
import os, sys
from io import BytesIO, IOBase
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip

def ii():
	return int(input())

def si():
	return input()

def mi():
	return map(int, input().strip().split(' '))

def msi():
	return map(str, input().strip().split(' '))

def li():
	return list(mi())

def dmain():
	sys.setrecursionlimit(1000000)
	threading.stack_size(1024000)
	thread = threading.Thread(target=main)
	thread.start()
import sys
input = sys.stdin.readline
scanner = lambda : int(input())
string = lambda : input().rstrip()
get_list = lambda : list(read())
read = lambda : map(int, input().split())
get_float = lambda : map(float, input().split())

def ceil(x):
	if x != int(x):
		x = int(x) + 1
	return x

def factorial(x, m):
	val = 1
	while x > 0:
		val = val * x % m
		x -= 1
	return val

def fact(x):
	val = 1
	while x > 0:
		val *= x
		x -= 1
	return val

def swaparr(arr, a, b):
	temp = arr[a]
	arr[a] = arr[b]
	arr[b] = temp

def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)

def lcm(a, b):
	return a * b // math.gcd(a, b)

def is_integer(n):
	return math.ceil(n) == math.floor(n)

def nCr(n, k):
	if k > n:
		return 0
	if k > n - k:
		k = n - k
	res = 1
	for i in range(k):
		res = res * (n - i)
		res = res / (i + 1)
	return int(res)

def primefs(n):
	primes = {}
	while n % 2 == 0 and n > 0:
		primes[2] = primes.get(2, 0) + 1
		n = n // 2
	for i in range(3, int(n ** 0.5) + 2, 2):
		while n % i == 0 and n > 0:
			primes[i] = primes.get(i, 0) + 1
			n = n // i
	if n > 2:
		primes[n] = primes.get(n, 0) + 1
	return primes

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def swap(a, b):
	temp = a
	a = b
	b = temp
	return (a, b)

def find(x, link):
	p = x
	while p != link[p]:
		p = link[p]
	while x != p:
		nex = link[x]
		link[x] = p
		x = nex
	return p

def union(x, y, link, size):
	x = find(x, link)
	y = find(y, link)
	if size[x] < size[y]:
		(x, y) = swap(x, y)
	if x != y:
		size[x] += size[y]
		link[y] = x

def sieve(n):
	prime = [True for i in range(n + 1)]
	(prime[0], prime[1]) = (False, False)
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime

def phi(n):
	result = n
	p = 2
	while p * p <= n:
		if n % p == 0:
			while n % p == 0:
				n = n // p
			result = result * (1.0 - 1.0 / float(p))
		p = p + 1
	if n > 1:
		result = result * (1.0 - 1.0 / float(n))
	return int(result)

def is_prime(n):
	if n == 0:
		return False
	if n == 1:
		return True
	for i in range(2, int(n ** (1 / 2)) + 1):
		if not n % i:
			return False
	return True

def next_prime(n, primes):
	while primes[n] != True:
		n += 1
	return n
MAXN = int(100000.0 + 5)

def spf_sieve():
	spf[1] = 1
	for i in range(2, MAXN):
		spf[i] = i
	for i in range(4, MAXN, 2):
		spf[i] = 2
	for i in range(3, ceil(MAXN ** 0.5), 2):
		if spf[i] == i:
			for j in range(i * i, MAXN, i):
				if spf[j] == j:
					spf[j] = i
spf = [0 for i in range(MAXN)]

def factoriazation(x):
	res = []
	for i in range(2, int(x ** 0.5) + 1):
		while x % i == 0:
			res.append(i)
			x //= i
	if x != 1:
		res.append(x)
	return res

def factors(n):
	res = []
	for i in range(1, int(n ** 0.5) + 1):
		if n % i == 0:
			res.append(i)
			res.append(n // i)
	return list(set(res))

def int_array():
	return list(map(int, input().strip().split()))

def float_array():
	return list(map(float, input().strip().split()))

def str_array():
	return input().strip().split()

def binary_search(low, high, w, h, n):
	while low < high:
		mid = low + (high - low) // 2
		if check(mid, w, h, n):
			low = mid + 1
		else:
			high = mid
	return low

def check(moves, n):
	val = (moves + 1) // 2
	rem = moves - val
	sol = (val + 1) * (rem + 1)
	return sol < n

def sortSecond(val):
	return val[1]
MOD = int(1000000000.0) + 7
CMOD = 998244353
INF = float('inf')
NINF = -float('inf')
alphs = 'abcdefghijklmnopqrstuvwxyz'
from itertools import permutations
import math
import bisect as bis
import random
import sys
import collections as collect
import functools as fnt

def solve():
	n = scanner()
	a = string()
	b = string()
	ans = []
	for i in range(n):
		if a[i] != b[i]:
			ans.append(i + 1)
			if i == 0:
				continue
			ans.append(1)
			ans.append(i + 1)
	print(len(ans), *ans)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
t = scanner()
for i in range(t):
	solve()


from sys import stdin, stdout
from math import gcd, sqrt
from collections import deque
input = stdin.readline
R = lambda : map(int, input().split())
I = lambda : int(input())
S = lambda : input().rstrip('\n')
L = lambda : list(R())
P = lambda x: stdout.write(x)
lcm = lambda x, y: x * y // gcd(x, y)
hg = lambda x, y: (y + x - 1) // x * x
pw = lambda x: 0 if x == 1 else 1 + pw(x // 2)
chk = lambda x: chk(x // 2) if not x % 2 else True if x == 1 else False
sm = lambda x: (x ** 2 + x) // 2
N = 10 ** 9 + 7

def cng(s):
	return [i ^ 1 for i in s[::-1]]
for _ in range(I()):
	n = I()
	a = list(map(int, S()))
	b = list(map(int, S()))
	v = []
	for i in range(n - 1, -1, -1):
		if a[i] != b[i]:
			if a[0] == b[i]:
				a[0] ^= 1
				v += (1,)
			v += (i + 1,)
			a = cng(a[:i + 1]) + a[i + 1:]
	print(len(v), *v)


for _ in range(int(input())):
	n = int(input())
	v = []
	a = input()
	b = input()
	for i in range(n):
		if a[i] != b[i]:
			if i >= 1:
				v.append(i + 1)
			v.append(1)
			if i >= 1:
				v.append(i + 1)
	print(len(v), end=' ')
	print(*v)


t = int(input())
while t:
	t -= 1
	n = int(input())
	s1 = list(map(int, input()))
	s2 = list(map(int, input()))
	ans = []
	start = 0
	end = n - 1
	flipped = 0
	for i in range(n - 1, -1, -1):
		if end > start:
			if s1[end] == s2[i]:
				end -= 1
				continue
			if s1[start] == s2[i]:
				ans.append(1)
				s1[start] = 1 - s1[start]
			ans.append(i + 1)
			(end, start) = (start, end)
			end += 1
			flipped += 1
		elif end < start:
			if 1 - s1[end] == s2[i]:
				end += 1
				continue
			if 1 - s1[start] == s2[i]:
				ans.append(1)
				s1[start] = 1 - s1[start]
			ans.append(i + 1)
			(end, start) = (start, end)
			end -= 1
			flipped += 1
		elif flipped % 2 ^ s1[end] != s2[i]:
			ans.append(i + 1)
	print(len(ans), *ans)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input()))
	b = list(map(int, input()))
	(l, r) = (0, n - 1)
	f = 0
	o = []
	while l != r:
		k = abs(r - l)
		if a[r] ^ f != b[k]:
			if a[l] ^ f == b[k]:
				o += (1,)
			o += (k + 1,)
			f ^= 1
			l += 1 if r > l else -1
			(l, r) = (r, l)
		else:
			r += -1 if r > l else 1
	if a[l] ^ f != b[0]:
		o += (1,)
	print(len(o), *o)


for _ in range(int(input())):
	n = int(input())
	arr = list(input())
	arr2 = list(input())
	arr100 = []
	if arr == arr2:
		print(0)
	else:
		for k in range(n):
			if arr[k] != arr2[k]:
				if k > 0:
					arr100.append(k + 1)
				arr100.append(1)
				if k > 0:
					arr100.append(k + 1)
		print(len(arr100), end=' ')
		print(*arr100)


import os
import sys
import math
from io import BytesIO, IOBase
input = sys.stdin.readline

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())

def dijkstra(start, distance, path, n):
	visited = [False for _ in range(n)]
	distance[start] = 0
	for i in range(n):
		v = -1
		for j in range(n):
			if not visited[v] and (v == -1 or distance[j] < distance[v]):
				v = j
		if distance[v] == math.inf:
			break
		visited[v] = True
		for edge in adj[v]:
			destination = edge[0]
			weight = edge[1]
			if distance[v] + weight < distance[destination]:
				distance[destination] = distance[v] + weight
				path[destination] = v

def gcd(a, b):
	if b == 0:
		return a
	else:
		return gcd(b, a % b)

def lcm(a, b):
	return a * b // gcd(a, b)

def ncr(n, r):
	return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

def npr(n, r):
	return math.factorial(n) // math.factorial(n - r)

def seive(n):
	primes = [True] * (n + 1)
	ans = []
	for i in range(2, n):
		if not primes[i]:
			continue
		j = 2 * i
		while j <= n:
			primes[j] = False
			j += i
	for p in range(2, n + 1):
		if primes[p]:
			ans += [p]
	return ans

def factors(n):
	factors = []
	x = 1
	while x * x <= n:
		if n % x == 0:
			if n // x == x:
				factors.append(x)
			else:
				factors.append(x)
				factors.append(n // x)
		x += 1
	return factors

def main():
	try:
		for _ in range(inp()):
			n = inp()
			a = input()
			b = input()
			k = 0
			ans = []
			for i in range(n):
				if a[i] != b[i] and i == 0:
					k += 1
					ans += [1]
				elif a[i] != b[i]:
					k += 3
					ans += [i + 1, 1, i + 1]
			print(k, *ans, sep=' ')
	except Exception as e:
		print(e)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


t = int(input(''))
a = []
b = []
for i in range(t):
	input('')
	a.append(input(''))
	b.append(input(''))
for i in range(t):
	x = a[i]
	y = b[i]
	sol = []
	for j in range(len(x)):
		if x[j] != y[j]:
			sol.append(j + 1)
			sol.append(1)
			sol.append(j + 1)
	if len(sol) != 0:
		print(len(sol), *sol)
	else:
		print(len(sol))


cases = int(input())
for t in range(cases):
	n = int(input())
	a = input()
	b = input()
	s = 0
	out = []
	for i in range(n - 1, -1, -1):
		if a[i] != b[i]:
			if a[0] == b[i]:
				out.append(1)
				s += 1
				trf = int(a[0], 2) ^ 1
				a = str(trf) + a[1:]
			s += 1
			trf = int(a[:i + 1], 2) ^ int('1' * (i + 1), 2)
			v = bin(trf)[2:]
			lv = len(v)
			str1 = '0' * (i + 1 - lv) + v
			a = str1[::-1] + a[i + 1:]
			out.append(i + 1)
	print(s, *out)


for _ in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	(ans, l) = ([], 0)
	for i in range(n):
		if a[i] != b[i]:
			l += 3
			ans += [i + 1, 1, i + 1]
	print(l, *ans)


from collections import defaultdict, Counter
from math import log, ceil, floor

def recur(a, b, l):
	if a == b:
		print(len(l), *l)
		return
	if a[-1] != b[-1]:
		if b[-1] == a[0]:
			l.append(1)
			a[0] = 1
		l.append(len(a))
		for i in range(len(a)):
			a[i] ^= 1
		a = a[::-1]
	recur(a[:len(a) - 1], b[:len(b) - 1], l)

def solve():
	n = int(input())
	a = list(map(int, input()))
	b = list(map(int, input()))
	l = []
	recur(a, b, l)
t = 1
t = int(input())
for _ in range(t):
	solve()


for _ in range(int(input())):
	n = int(input())
	A = input()
	B = input()
	a = []
	b = []
	for i in A:
		a.append(i)
	for i in B:
		b.append(i)
	c = []
	cnt = 0
	x = 0
	y = n - 1
	for i in range(n - 1, -1, -1):
		if cnt % 2 == 0:
			if b[i] == '1':
				if a[x] == '1':
					c.append(1)
					c.append(i + 1)
				else:
					c.append(i + 1)
			elif a[x] == '0':
				c.append(1)
				c.append(i + 1)
			else:
				c.append(i + 1)
			x += 1
		else:
			if b[i] == '1':
				if a[y] == '0':
					c.append(1)
					c.append(i + 1)
				else:
					c.append(i + 1)
			elif a[y] == '1':
				c.append(1)
				c.append(i + 1)
			else:
				c.append(i + 1)
			y -= 1
		cnt += 1
	print(len(c), end=' ')
	for i in c:
		print(i, end=' ')
	print()


import sys
input = sys.stdin.readline

def getInt():
	return int(input())

def getVars():
	return map(int, input().split())

def getList():
	return list(map(int, input().split()))

def getStr():
	return input().strip()

def f(ch):
	if ch == '1':
		return '0'
	else:
		return '1'
t = getInt()
for _ in range(t):
	n = getInt()
	a = getStr()
	b = getStr()
	res = []
	for i in range(n - 1, 0, -1):
		if a[i] == b[i]:
			continue
		if a[i] != a[0]:
			res.append(1)
			a = f(a[0]) + a[1:]
		res.append(i + 1)
		a1 = ''
		for j in range(i + 1):
			a1 = f(a[j]) + a1
		a = a1 + b[i]
	if a[0] != b[0]:
		res.append(1)
	print(len(res), *res)


def func(l, sl):
	l = list(l)
	for k in range(sl):
		if l[k] == '0':
			l[k] = '1'
		else:
			l[k] = '0'
	return ''.join(reversed(l))
t = int(input())
while t:
	t -= 1
	n = int(input())
	a = input()
	b = input()
	ans = []
	if a == b:
		print(0)
		continue
	for j in range(n - 1, -1, -1):
		if a[0] == b[j]:
			ans.extend([1, j + 1])
			a = func(a[0], 1) + a[1:]
			a = func(a[:j + 1], j + 1) + a[j + 1:]
		else:
			a = func(a[:j + 1], j + 1) + a[j + 1:]
			ans.extend([j + 1])
	print(len(ans), *ans)


import math
value = int(input())
for T in range(value):
	n = int(input())
	sa = input()
	sb = input()
	(count, ans) = (0, [])
	for i in range(0, n):
		j = -i - 1
		k = n - 1 - 2 * i
		if k == 0 and sb[0] != sa[i]:
			count += 1
			ans.append(1)
		if k <= 0:
			break
		if sa[i] == sb[k]:
			count += 1
			ans.append(1)
		count += 1
		ans.append(k + 1)
		if sa[j] != sb[k - 1]:
			count += 1
			ans.append(1)
		count += 1
		ans.append(k)
	print(count, end=' ')
	for v in ans:
		print(v, end=' ')
	print('')


for _ in range(int(input())):
	n = int(input())
	s1 = list(input())
	s2 = list(input())
	ansLi = []
	for i in range(0, n):
		if s1[i] == s2[i]:
			continue
		else:
			ansLi.append(i + 1)
			ansLi.append(1)
			ansLi.append(i + 1)
	print(len(ansLi), end=' ')
	for j in ansLi:
		print(j, end=' ')
	print()


def modinv(n, p):
	return pow(n, p - 2, p)

def main():
	for case in range(int(input())):
		n = int(input())
		a = [int(x) for x in input()]
		b = [int(x) for x in input()]
		moves = []
		i = 0
		while True:
			if a[i] == 0:
				while i + 1 < n and a[i + 1] == 0:
					i += 1
				i += 1
				moves.append(i)
			if i >= n:
				break
			if a[i] == 1:
				while i + 1 < n and a[i + 1] == 1:
					i += 1
				i += 1
				moves.append(i)
			if i >= n:
				break
		if a[n - 1] == 0:
			moves.pop(-1)
		temp = []
		i = 0
		while True:
			if b[i] == 0:
				while i + 1 < n and b[i + 1] == 0:
					i += 1
				i += 1
				temp.append(i)
			if i >= n:
				break
			if b[i] == 1:
				while i + 1 < n and b[i + 1] == 1:
					i += 1
				i += 1
				temp.append(i)
			if i >= n:
				break
		if b[n - 1] == 0:
			temp.pop(-1)
		temp.reverse()
		print(len(temp) + len(moves), *moves, *temp)
py2 = round(0.5)
if py2:
	from future_builtins import ascii, filter, hex, map, oct, zip
	range = xrange
import os, sys
from io import IOBase, BytesIO
BUFSIZE = 8192

class FastIO(BytesIO):
	newlines = 0

	def __init__(self, file):
		self._file = file
		self._fd = file.fileno()
		self.writable = 'x' in file.mode or 'w' in file.mode
		self.write = super(FastIO, self).write if self.writable else None

	def _fill(self):
		s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
		self.seek((self.tell(), self.seek(0, 2), super(FastIO, self).write(s))[0])
		return s

	def read(self):
		while self._fill():
			pass
		return super(FastIO, self).read()

	def readline(self):
		while self.newlines == 0:
			s = self._fill()
			self.newlines = s.count(b'\n') + (not s)
		self.newlines -= 1
		return super(FastIO, self).readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.getvalue())
			(self.truncate(0), self.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		if py2:
			self.write = self.buffer.write
			self.read = self.buffer.read
			self.readline = self.buffer.readline
		else:
			self.write = lambda s: self.buffer.write(s.encode('ascii'))
			self.read = lambda : self.buffer.read().decode('ascii')
			self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


from collections import deque

def solve(n, a, b):
	ans = deque()
	for i in range(n):
		if a[n - 1 - i] != b[n - 1 - i]:
			if a[0] == b[n - 1 - i]:
				ans.append(1)
				a[0] = 1 - a[0]
				ans.append(n - i)
				st = deque([])
				for j in range(n - i):
					st.append(1 - a[j])
				for j in range(n - i):
					a[n - i - 1 - j] = st.popleft()
			else:
				ans.append(n - i)
				st = deque([])
				for j in range(n - i):
					st.append(1 - a[j])
				for j in range(n - i):
					a[n - i - 1 - j] = st.popleft()
	if len(ans) == 0:
		print(0)
		return
	else:
		print(len(ans), *ans)
		return
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, list(input())))
	b = list(map(int, list(input())))
	solve(n, a, b)


for _ in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	C = []
	A = []
	for i in range(n):
		if a[i] != b[i]:
			C.append(i + 1)
	while i >= 0:
		if a[i] != b[i]:
			if a[0] == a[i]:
				A.append(i + 1)
				s = ''
				for j in range(i + 1):
					if a[j] == '1':
						s = s + '0'
					else:
						s = s + '1'
				s = s[::-1]
				a = s + a[i + 1:]
			else:
				s = '1'
				if a[0] == '1':
					s = '0'
				for j in range(1, i + 1):
					if a[j] == '1':
						s = s + '0'
					else:
						s = s + '1'
				s = s[::-1]
				a = s + a[i + 1:]
				A.append(1)
				A.append(i + 1)
		i -= 1
		if a == b:
			break
	print(len(A), *A)


import os
import operator
import sys
from _collections import defaultdict
import bisect
from io import BytesIO, IOBase

def inar():
	return [int(k) for k in input().split()]

def power(x, p, m):
	res = 1
	while p:
		if p & 1:
			res = res * x % m
		x = x * x % m
		p >>= 1
	return res

def main():
	for _ in range(int(input())):
		n = int(input())
		a = list(input())
		b = list(input())
		res = []
		for i in range(n - 1, -1, -1):
			if b[i] == a[i]:
				continue
			else:
				n = i + 1
				if a[i] == a[n - i - 1]:
					res.append(i + 1)
					temp = []
					for j in range(i + 1):
						if a[j] == '1':
							a[j] = '0'
						else:
							a[j] = '1'
						temp.append(a[j])
					temp = temp[::-1]
					for j in range(i):
						a[j] = temp[j]
				else:
					if a[n - i - 1] == '1':
						a[n - i - 1] = '0'
					else:
						a[n - i - 1] = '1'
					res.append(n - i)
					res.append(i + 1)
					temp = []
					for j in range(i + 1):
						if a[j] == '1':
							a[j] = '0'
						else:
							a[j] = '1'
						temp.append(a[j])
					temp = temp[::-1]
					for j in range(i):
						a[j] = temp[j]
		print(len(res), end=' ')
		print(*res)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


import sys
input = sys.stdin.readline
t = int(input())
for tt in range(t):
	n = int(input())
	a1 = input().strip('\n')
	b1 = input().strip('\n')
	ans = []
	a = []
	b = []
	for i in a1:
		a.append(int(i))
	for i in b1:
		b.append(int(i))
	toggle = 0
	bit = 0
	start = 0
	end = n - 1
	for i in range(n - 1, -1, -1):
		if toggle == 0 and a[bit] == b[i]:
			a[bit] ^= 1
			ans.append(1)
		if toggle == 1 and a[bit] != b[i]:
			a[bit] ^= 1
			ans.append(1)
		ans.append(i + 1)
		if toggle == 0:
			bit = end
			start += 1
		else:
			bit = start
			end -= 1
		toggle ^= 1
	print(len(ans), end=' ')
	for i in ans:
		print(i, end=' ')
	print()


for x in range(int(input())):
	n = int(input())
	s1 = list(map(int, input()))
	s2 = list(map(int, input()))
	a = []
	for i in range(n):
		k = int(s2[n - i - 1])
		if k == s1[0]:
			a.append(1)
			if s1[0] == 1:
				s1[0] = 0
			else:
				s1[0] = 1
			l = len(s1)
			p = []
			for j in range(l - 1, -1, -1):
				if s1[j] == 1:
					p.append(0)
				else:
					p.append(1)
			a.append(l)
			p.pop(-1)
			s1 = p
		else:
			l = len(s1)
			p = []
			for j in range(l - 1, -1, -1):
				if s1[j] == 1:
					p.append(0)
				else:
					p.append(1)
			a.append(l)
			p.pop(-1)
			s1 = p
	print(len(a), *a)


import os
from sys import stdin, stdout

def solve(tc):
	n = int(stdin.readline().strip())
	seq = list(map(int, stdin.readline().strip()))
	obj = list(map(int, stdin.readline().strip()))
	hi = n - 1
	while hi >= 0 and seq[hi] == obj[hi]:
		hi -= 1
	ans = []
	l = 0
	r = hi
	toggle = False
	while hi >= 0:
		if toggle:
			if seq[r] == obj[hi]:
				if hi > 0 and seq[l] != obj[hi]:
					ans.append(1)
					seq[l] ^= 1
				ans.append(hi + 1)
				(l, r) = (r, l)
				r -= 1
				toggle = False
			else:
				r += 1
		elif seq[r] != obj[hi]:
			if hi > 0 and seq[l] == obj[hi]:
				ans.append(1)
				seq[l] ^= 1
			ans.append(hi + 1)
			(l, r) = (r, l)
			r += 1
			toggle = True
		else:
			r -= 1
		hi -= 1
	print(len(ans), *ans)
tcs = 1
tcs = int(stdin.readline().strip())
tc = 1
while tc <= tcs:
	solve(tc)
	tc += 1


for _ in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	ans = []
	arr = []
	for i in range(n - 1):
		if a[i] != a[i + 1]:
			ans.append(i + 1)
	for i in range(n - 1):
		if b[i] != b[i + 1]:
			arr.append(i + 1)
	if a[n - 1] != b[n - 1]:
		ans.append(n)
	for i in range(len(arr) - 1, -1, -1):
		ans.append(arr[i])
	print(len(ans), *ans, sep=' ')


from math import *
inf = 10 ** 10

def _in():
	return [int(x) for x in input().split(' ')]

def perform(s):
	out = ''
	for i in s:
		out = out + ('1' if i == '0' else '0')
	return out[::-1]

def main():
	n = int(input())
	a = input()
	b = input()
	ans = []
	j = n - 1
	while j >= 0 and a != b:
		while j >= 0 and a[j] == b[j]:
			j = j - 1
		if j < 0:
			break
		a = a[:j + 1]
		b = b[:j + 1]
		if len(a) == 1:
			ans.append(1)
			break
		if a[0] == b[j]:
			ans.append(1)
			a = ('0' if a[0] == '1' else '1') + a[1:]
		if len(a) > 1:
			ans.append(j + 1)
			a = perform(a)
	print(len(ans), end=' ')
	for i in ans:
		print(i, end=' ')
	print()
tt = 1
tt = int(input())
for _ in range(tt):
	main()


t = int(input())
for u in range(t):
	n = int(input())
	a = input()
	b = input()
	q = 0
	ans = list()
	for i in range(n - 1, -1, -1):
		if a[i] != b[i]:
			if a[0] != a[i]:
				a = a[i] + a[1:]
				q += 1
				ans.append(1)
			if a[i] != b[i]:
				q += 1
				ans.append(i + 1)
				c = ''
				e = a[0:i + 1]
				for j in range(i, -1, -1):
					if e[j] == '0':
						c = c + '1'
					else:
						c = c + '0'
				if i + 1 < n:
					a = c + a[i + 1:]
				else:
					a = c
	print(q, end=' ')
	for i in range(len(ans)):
		print(ans[i], end=' ')
	print()


t = int(input())
for i in range(t):
	f = lambda : map(int, input().split(' '))
	n = int(input())
	a = input()
	b = input()
	s = ''
	flag = 0
	for i in range(n):
		if a[i] != b[i]:
			flag = 1
			break
	if flag == 0:
		print(0)
	else:
		for i in range(len(b) - 1, -1, -1):
			if b[i] == '0':
				s += '1'
			else:
				s += '0'
		k = []
		for i in range(0, n - 1):
			if a[i] != a[i + 1]:
				k.append(i + 1)
		j = []
		for i in range(0, n - 1):
			if s[i] != s[i + 1]:
				j.append(i + 1)
		if s[-1] != a[-1]:
			j.append(n)
		j.reverse()
		k = k + j
		k.append(n)
		k = [len(k)] + k
		for i in k:
			print(i, end=' ')


def convert(lst, length):
	for i in range(length):
		if lst[i] == '0':
			lst[i] = '1'
		else:
			lst[i] = '0'
	return lst
case = int(input())
for _ in range(case):
	ans = []
	n = int(input())
	a = list(input())
	b = list(input())
	for i in range(n):
		if a[-i - 1] != b[-i - 1]:
			if a[0] == b[-i - 1]:
				ans.append(1)
				if a[0] == '1':
					a[0] = '0'
				else:
					a[0] = '1'
			ans.append(n - i)
			fr_a = a[:n - i]
			ba_a = a[n - i:]
			fr_a = convert(fr_a, n - i)
			a = fr_a[::-1] + ba_a
		else:
			continue
	print(len(ans), *ans)


from math import *
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(input())
	b = list(input())
	l = []
	for i in range(n):
		if a[i] != b[i]:
			l.append(i + 1)
			l.append(1)
			l.append(i + 1)
	print(len(l))
	for i in range(len(l)):
		print(l[i], end=' ')
	print()


import sys

def input():
	return sys.stdin.readline()[:-1]

def getInt():
	return int(input())

def getIntIter():
	return map(int, input().split())

def getIntList():
	return list(getIntIter())
for _ in range(getInt()):
	n = getInt()
	a = input()
	b = input()
	aLast = a[-1]
	bLast = b[-1]
	while aLast == bLast and len(a) > 1:
		a = a[:-1]
		b = b[:-1]
		aLast = a[-1]
		bLast = b[-1]
	outList = []
	while len(a) > 1:
		if a[0] == b[-1]:
			outList.append('1')
		outList.append(str(len(a)))
		b = b[:-1]
		a = a[1:]
		a = a[::-1]
		a = ''.join(('1' if x == '0' else '0' for x in a))
	if a != b:
		outList.append('1')
	print(len(outList), ' '.join(outList))


for _ in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	count = 0
	ans = []
	for i in range(n - 1, -1, -1):
		if a[i] != b[i]:
			count += 3
			ans.append(i + 1)
			ans.append(1)
			ans.append(i + 1)
	ans.insert(0, count)
	for x in ans:
		print(x, end=' ')
	print()


elem = int(input())
for _ in range(elem):
	num = int(input())
	a = input()
	b = input()
	ch = 0
	res = []
	for i in range(len(a)):
		if a[len(a) - 1 - i] != b[len(a) - 1 - i]:
			res.append(len(a) - i)
			res.append(1)
			res.append(len(a) - i)
	print(len(res), *res)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input()))
	b = list(map(int, input()))
	ops = []
	inv = False
	for i in range(n):
		if i % 2 == 0:
			first = a[i // 2]
		else:
			first = a[n - 1 - i // 2]
		if first ^ inv == b[n - 1 - i]:
			ops.append(1)
		ops.append(n - i)
		inv = 1 - inv
	print(len(ops), *ops)


import sys
input = lambda : sys.stdin.readline().rstrip()
from collections import deque
from queue import LifoQueue
for _ in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	c = []
	for i in range(n):
		if a[i] != b[i]:
			c.append(i + 1)
			c.append(1)
			c.append(i + 1)
	print(len(c), *c)


def lol(x):
	res = ''
	for xx in x:
		res += str(1 - int(xx))
	return res[::-1]

def meth(a, b, arr):
	if a == b:
		return
	while a[-1] == b[-1]:
		a = a[:-1]
		b = b[:-1]
	if a == '' and b == '':
		return
	if a == '0' and b == '1':
		arr += [1]
		return
	if a == '1' and b == '0':
		arr += [1]
		return
	if int(a[0]) + int(b[-1]) == 1:
		arr += [len(a)]
		a = lol(a)
		meth(a, b, arr)
	else:
		arr += [1]
		val = str(1 - int(a[0]))
		a = val + a[1:]
		meth(a, b, arr)
t = int(input())
for _ in range(t):
	n = int(input())
	a = input()
	b = input()
	arr = []
	meth(a, b, arr)
	print(len(arr), *arr)


def trailone(l):
	c = 0
	for i in l:
		if i == 1:
			c += 1
		else:
			break
	return c

def inp():
	return int(input())

def insr():
	s = input()
	return s

def inlt():
	return list(map(int, input().split()))

def convert(a, b):
	s1 = 0
	e1 = len(b) - 1
	rev = 0
	ans = []
	for i in range(len(a) - 1, -1, -1):
		if rev != 1:
			if a[e1] == b[i]:
				e1 -= 1
				continue
			elif a[s1] == b[i]:
				ans.append(0 + 1)
				ans.append(i + 1)
				s1 += 1
				rev = 1
			else:
				ans.append(i + 1)
				s1 += 1
				rev = 1
		elif a[s1] != b[i]:
			s1 += 1
			continue
		elif a[e1] != b[i]:
			ans.append(0 + 1)
			ans.append(i + 1)
			e1 -= 1
			rev = 0
		else:
			ans.append(i + 1)
			e1 -= 1
			rev = 0
	return ans
r = inp()
for i in range(r):
	l2 = inlt()
	a = insr()
	b = insr()
	ans = convert(a, b)
	if len(ans) == 0:
		print('0\n')
	else:
		print(str(len(ans)) + ' ' + ' '.join([str(i) for i in ans]) + '\n')


def main():
	for _ in range(int(input())):
		n = int(input())
		a = list(input())
		b = list(input())
		ans = []
		for i in range(n - 1, -1, -1):
			if a[i] == b[i]:
				continue
			if a[i] != b[i] and a[0] != b[i]:
				ans.append(i + 1)
			elif a[i] != b[i] and a[0] == b[i]:
				ans.append(1)
				ans.append(i + 1)
				if a[0] == '1':
					a[0] = '0'
				else:
					a[0] = '1'
			if i < n - 1:
				a = a[:i + 1][::-1] + a[i + 1:]
			else:
				a = a[:i + 1][::-1]
			for j in range(i + 1):
				if a[j] == '0':
					a[j] = '1'
				else:
					a[j] = '0'
		print(len(ans), end=' ')
		print(*ans)
main()


def flip(s, k):
	pre = ''.join(reversed(['1' if _ == '0' else '0' for _ in s[:k]]))
	pos = s[k:]
	return pre + pos
for _ in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	ret = []
	for i in reversed(list(range(n))):
		while a[i] != b[i]:
			if a[0] == b[i]:
				p = 1
			else:
				p = i + 1
			ret.append(p)
			a = flip(a, p)
	print(len(ret), *ret)


from collections import defaultdict as dd, deque as dq
import sys
input = sys.stdin.readline
t = int(input())
while t:
	n = int(input())
	a = list(map(int, input().strip()))
	b = list(map(int, input().strip()))
	q = dq(a)
	rev = 0
	par = 0
	last = n - 1
	i = 0
	j = n - 1
	ans = []
	while q:
		if rev == 0:
			if q:
				q[0] = q[0] ^ par
				lo = 0
				if q[0] == b[last]:
					ans.append(1)
					lo = 1
				if q:
					rev = 1 - rev
					par = 1 - par
					ans.append(len(q))
				q.popleft()
				last -= 1
		else:
			j = len(q) - 1
			if q:
				q[j] = q[j] ^ par
				lo = 0
				if q[j] == b[last]:
					j -= 1
					ans.append(1)
					lo = 1
				if q:
					rev = 1 - rev
					par = 1 - par
					ans.append(len(q))
				q.pop()
				last -= 1
	print(len(ans), *ans)
	t -= 1


for _ in range(int(input())):
	n = int(input())
	ss = input()
	sss = input()
	s1 = []
	s2 = []
	for j in ss:
		s1.append(int(j))
	for j in sss:
		s2.append(int(j))
	ans = []
	ans2 = []
	for i in range(len(s1) - 1):
		if s1[i] != s1[i + 1]:
			ans.append(i + 1)
	if s1[-1] == 1:
		ans.append(n)
	ans2 = []
	for i in range(len(s2) - 1):
		if s2[i] != s2[i + 1]:
			ans2.append(i + 1)
	if s2[-1] == 1:
		ans2.append(n)
	for i in range(len(ans2) - 1, -1, -1):
		ans.append(ans2[i])
	print(len(ans), end=' ')
	print(*ans)


from math import *
from collections import *
from random import *
from bisect import *
import sys
input = sys.stdin.readline
d = {'1': '0', '0': '1'}
t = int(input())
while t:
	t -= 1
	n = int(input())
	a = input().rstrip('\n')
	b = input().rstrip('\n')
	a = list(a)
	b = list(b)
	r = []
	lp = n - 1
	ch = n - 1
	fp = 0
	tu = 0
	while ch >= 0:
		if tu % 2 == 0:
			if a[lp] == b[ch]:
				lp -= 1
				ch -= 1
				continue
			if a[fp] == b[ch]:
				r.append(1)
				a[fp] = d[a[fp]]
				continue
		else:
			if d[a[lp]] == b[ch]:
				lp += 1
				ch -= 1
				continue
			if d[a[fp]] == b[ch]:
				r.append(1)
				a[fp] = d[a[fp]]
				continue
		if tu % 2 == 0:
			r.append(ch + 1)
			tu += 1
			(fp, lp) = (lp, fp + 1)
		else:
			r.append(ch + 1)
			tu += 1
			(fp, lp) = (lp, fp - 1)
		ch -= 1
	print(len(r), end=' ')
	print(*r)


def chg(kk):
	if kk == '0':
		return '1'
	return '0'
T = int(input())
for _ in range(0, T):
	n = int(input())
	A = input()
	B = input()
	a = []
	b = []
	for i in range(n):
		a.append(A[i])
		b.append(B[i])
	ans = []
	ok = a[-1]
	for i in range(1, len(a)):
		if a[i] != a[i - 1]:
			ans.append(i)
	for i in range(len(b) - 1, -1, -1):
		if b[i] != ok:
			ans.append(i + 1)
			ok = chg(ok)
	print(len(ans), *ans)


I = input
exec(int(I()) * "I();a=[f'{i} 1 {i}'for i,(x,y)in enumerate(zip(I(),I()),1)if x!=y];print(len(a)*3,*a);")


def inv(v):
	return 1 if v == 0 else 0

def inv_rev(idx, a):
	for j in range(idx):
		if idx - j == j:
			a[j] = inv(a[j])
		elif idx - j > j:
			tmp = inv(a[idx - j])
			a[idx - j] = inv(a[j])
			a[j] = tmp
		else:
			break
for _ in range(int(input())):
	n = int(input())
	a = [int(x) for x in input()]
	b = [int(x) for x in input()]
	moves = 0
	prefixes = []
	for i in range(n - 1, -1, -1):
		if b[i] != a[i]:
			if b[i] == a[0]:
				last_occ = i - 1 - list(reversed(a[:i])).index(a[0])
				inv_rev(last_occ, a)
				moves += 1
				prefixes.append(last_occ + 1)
			inv_rev(i, a)
			moves += 1
			prefixes.append(i + 1)
	print(f'{moves}', *prefixes)


for q in range(int(input())):
	n = int(input())
	A = list(map(int, input()))
	B = list(map(int, input()))
	ans = []
	for i in range(n - 1, 0, -1):
		if A[i] != B[i]:
			if A[i] != A[0]:
				ans.append(1)
				A[0] += 1
				A[0] %= 2
			ans.append(i + 1)
			for j in range(i + 1):
				A[j] += 1
				A[j] %= 2
			for j in range((i + 1) // 2):
				(A[j], A[i - j]) = (A[i - j], A[j])
	if B[0] != A[0]:
		ans.append(1)
	print(len(ans), *ans)


def f(a, b):
	if b & 1:
		return a ^ 1
	return a
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input()))
	b = list(map(int, input()))
	s = 0
	l = n - 1
	c = []
	i = n - 1
	while i + 1:
		if f(a[s], n - i - 1) == b[i]:
			c += (1,)
		c += (i + 1,)
		sn = l
		ln = s + [1, -1][s > n // 2]
		s = sn
		l = ln
		i -= 1
	print(len(c))
	print(*c)


class Solution:

	def __init__(self, debug=False, n=None, a=None, b=None):
		if n is None:
			(n, a, b) = Solution.input()
		self.n = n
		self.a = a
		self.b = b
		self.real = [x for x in self.a]
		self.inverse = [not x for x in self.a]
		self.curr_zero = 0
		self.curr_last = self.n - 1
		self.real_idx = self.n - 1
		self.inv = False
		self.debug = debug
		if debug:
			self.current_state = [x for x in self.a]
		self.operations = [self.n + 1000]

	@staticmethod
	def input():
		n = int(input())
		a = [x == '1' for x in input()]
		b = [x == '1' for x in input()]
		return (n, a, b)

	def iteration(self):
		if self.inv:
			if self.inverse[self.curr_last] != self.b[self.real_idx]:
				if self.inverse[self.curr_zero] != self.inverse[self.curr_last]:
					self.operation(0)
					(self.real[self.curr_zero], self.inverse[self.curr_zero]) = (self.inverse[self.curr_zero], self.real[self.curr_zero])
				self.operation(self.real_idx)
				self.inv = False
				(self.curr_zero, self.curr_last) = (self.curr_last, self.curr_zero)
		elif self.real[self.curr_last] != self.b[self.real_idx]:
			if self.real[self.curr_zero] != self.real[self.curr_last]:
				self.operation(0)
				(self.real[self.curr_zero], self.inverse[self.curr_zero]) = (self.inverse[self.curr_zero], self.real[self.curr_zero])
			self.operation(self.real_idx)
			self.inv = True
			(self.curr_zero, self.curr_last) = (self.curr_last, self.curr_zero)
		if self.inv:
			self.curr_last += 1
		else:
			self.curr_last -= 1
		self.real_idx -= 1

	def solve(self):
		while self.real_idx >= 0:
			self.iteration()
		return self.return_answer()

	def special_cases(self):
		if self.n == 1:
			if self.a[0] != self.b[0]:
				self.operation(0)
			return self.return_answer

	def operation(self, i):
		self.operations.append(i + 1)
		if self.debug:
			self.current_state[:i + 1] = [not x for x in self.current_state[:i + 1]][::-1]
		if self.n < 2:
			return

	def return_answer(self):
		if self.debug:
			assert all((x == y for (x, y) in zip(self.b, self.current_state)))
		ans = f"{len(self.operations) - 1} {' '.join((str(x) for x in self.operations[1:]))}"
		print(ans)
		return ans

def solve():
	n = int(input())
	a = [int(x) for x in input()]
	b = [int(x) for x in input()]
	real = [x for x in a]
	inverse = [(x + 1) % 2 for x in a]
	inv_idx = 0
	real_idx = n - 1
	inv = False
	operations = []
	seq = []
	while real_idx > 0:
		if not inv:
			if real[real_idx] == b[real_idx]:
				real_idx -= 1
				inv_idx += 1
				seq.append(real[real_idx])
				continue
			if real[0] != real[real_idx]:
				operations.append(1)
				(real[0], inverse[-1]) = (inverse[-1], real[0])
			operations.append(real_idx + 1)
			real[real_idx] = b[real_idx]
			seq.append(real[real_idx])
			inverse[inv_idx] = (real[real_idx] + 1) % 2
			inv = True
		else:
			if inverse[inv_idx] == b[real_idx]:
				real_idx -= 1
				inv_idx += 1
				seq.append(inverse[inv_idx])
				continue
			if inverse[-1] != inverse[inv_idx]:
				operations.append(1)
				(real[0], inverse[-1]) = (inverse[-1], real[0])
			operations.append(real_idx + 1)
			inverse[inv_idx] = b[real_idx]
			real[real_idx] = (b[real_idx] + 1) % 2
			seq.append(inverse[inv_idx])
			inv = False
		real_idx -= 1
		inv_idx += 1
	if inv and inverse[-1] != b[0] or (real[0] != b[0] and (not inv)):
		operations.append(1)
	seq.append(b[0])
	print(seq, b)
	print(len(operations), *operations)
for _ in range(int(input())):
	(n, a, b) = Solution.input()
	Solution(False, n, a, b).solve()


for i in range(int(input())):
	n = int(input())
	a = list(input())
	b = list(input())
	c = 1
	d = []
	while True:
		if int(a[-c]) == int(b[-c]):
			c += 1
		elif int(a[0]) == int(b[-c]):
			d.append(1)
			if int(a[0]) == 0:
				a[0] = '1'
			else:
				a[0] = '0'
		else:
			d.append(n - c + 1)
			f = []
			m = 0
			for i in range(n - c + 1):
				if int(a[i]) == 0:
					f.append('1')
				else:
					f.append('0')
			if len(f) == n:
				a = f[::-1]
			else:
				a = f[::-1] + a[-(c - 1):]
		if a == b:
			print(len(d), *d)
			break


def fact(x):
	if x == 0:
		return 1
	else:
		return x * fact(x - 1)

def lower_bound(li, num):
	answer = len(li) - 1
	start = 0
	end = len(li) - 1
	while start <= end:
		middle = (end + start) // 2
		if li[middle] >= num:
			answer = middle
			end = middle - 1
		else:
			start = middle + 1
	return answer

def upper_bound(li, num):
	answer = -1
	start = 0
	end = len(li) - 1
	while start <= end:
		middle = (end + start) // 2
		if li[middle] <= num:
			answer = middle
			start = middle + 1
		else:
			end = middle - 1
	return answer

def abs(x):
	return x if x >= 0 else -x

def binary_search(li, val, lb, ub):
	ans = 0
	while lb <= ub:
		mid = (lb + ub) // 2
		if li[mid] > val:
			ub = mid - 1
		elif val > li[mid]:
			lb = mid + 1
		else:
			ans = 1
			break
	return ans

def sieve_of_eratosthenes(n):
	ans = []
	arr = [1] * (n + 1)
	(arr[0], arr[1], i) = (0, 0, 2)
	while i * i <= n:
		if arr[i] == 1:
			j = i + i
			while j <= n:
				arr[j] = 0
				j += i
		i += 1
	for k in range(n):
		if arr[k] == 1:
			ans.append(k)
	return ans

def nc2(x):
	if x == 1:
		return 0
	else:
		return x * (x - 1) // 2

def kadane(x):
	sum_so_far = 0
	current_sum = 0
	for i in x:
		current_sum += i
		if current_sum < 0:
			current_sum = 0
		else:
			sum_so_far = max(sum_so_far, current_sum)
	return sum_so_far

def mex(li):
	check = [0] * 1001
	for i in li:
		check[i] += 1
	for i in range(len(check)):
		if check[i] == 0:
			return i

def sumdigits(n):
	ans = 0
	while n != 0:
		ans += n % 10
		n //= 10
	return ans

def product(li):
	ans = 1
	for i in li:
		ans *= i
	return ans

def maxpower(n, k):
	cnt = 0
	while n > 1:
		cnt += 1
		n //= k
	return cnt
from math import *
from collections import deque
max_steps = 100005
for _ in range(int(input())):
	n = int(input())
	s1 = input()
	s2 = input()
	a = [int(x) for x in list(s1)]
	b = [int(x) for x in list(s2)]
	ans = []
	for i in range(1, n):
		if a[i] != a[i - 1]:
			ans.append(i)
	if a[n - 1] != b[n - 1]:
		ans.append(n)
	temp = []
	for i in range(1, n):
		if b[i] != b[i - 1]:
			temp.append(i)
	ans += temp[::-1]
	print(len(ans), *ans)


from io import BytesIO, IOBase
import sys, os
inpl = lambda : list(map(int, input().split()))
inpm = lambda : map(int, input().split())
inpi = lambda : int(input())
inp = lambda : input()
(rev, ra, l) = (reversed, range, len)
P = print
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))

def input():
	return sys.stdin.readline().rstrip('\r\n')
import sys, operator
tc = inpi()
while tc:
	n = inpi()
	a = inp()
	b = inp()
	if a == b:
		P(0)
	else:
		(ans, li) = (0, [])
		if a[0] != b[0]:
			ans += 1
			li.append(1)
		for i in ra(1, n):
			if a[i] != b[i]:
				li.append(i + 1)
				li.append(1)
				li.append(i + 1)
				ans += 3
		P(ans, end=' ')
		P(*li)
	tc -= 1


for _ in range(int(input())):
	n = int(input())
	A = input()
	a = []
	for i in A:
		a.append(int(i))
	B = input()
	b = []
	for i in B:
		b.append(int(i))
	ans = []
	for i in range(n - 1, -1, -1):
		if b[i] != a[0]:
			ans.append(i + 1)
			for k in range(i + 1):
				a[k] = (a[k] + 1) % 2
			a[0:i + 1] = a[0:i + 1][::-1]
		else:
			ans.append(1)
			a[0] = (a[0] + 1) % 2
			for k in range(i + 1):
				a[k] = (a[k] + 1) % 2
			a[0:i + 1] = a[0:i + 1][::-1]
			ans.append(i + 1)
	print(len(ans), *ans)


t = int(input())

def change(s, n):
	d = []
	for i in range(n - 1, -1, -1):
		if s[i] == '1':
			d.append('0')
		else:
			d.append('1')
	s = ''.join(map(str, d)) + s[n:]
	return s
while t > 0:
	n = int(input())
	a = input()
	b = input()
	i = 0
	d = []
	while i < n:
		if a[n - i - 1] == b[n - i - 1]:
			i += 1
			continue
		if a[0] == b[n - i - 1]:
			a = change(a, 1)
			a = change(a, n - i)
			d.append(1)
			d.append(n - i)
			i += 1
		else:
			a = change(a, n - i)
			d.append(n - i)
			i += 1
	print(len(d), *d)
	t -= 1


from sys import stdin
input = stdin.readline
result = []
for _ in range(int(input())):
	n = int(input())
	a = input().strip()
	b = input().strip()
	bi = n - 1
	f = 0
	(l, r) = (0, n - 1)
	ans = []
	while l != r:
		if f == 0 and a[l] == b[bi] or (f != 0 and a[l] != b[bi]):
			ans.append(1)
		ans.append(abs(l - r) + 1)
		if l < r:
			l += 1
		else:
			l -= 1
		(l, r) = (r, l)
		f ^= 1
		bi -= 1
	if f == 0 and a[l] != b[0] or (f and a[l] == b[0]):
		ans.append(1)
	ans = ' '.join(map(str, [len(ans)] + ans))
	result.append(ans)
print(*result, sep='\n')


def rev(s, le):
	s = list(s)
	for i in range(le):
		if s[i] == '1':
			s[i] = '0'
		else:
			s[i] = '1'
	return ''.join(reversed(s))
for ii in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	l = []
	if a == b:
		print(0)
	else:
		for jj in range(n - 1, -1, -1):
			if a[0] == b[jj]:
				l.append(1)
				a = rev(a[0], 1) + a[1:]
				l.append(jj + 1)
				a = rev(a[:jj + 1], jj + 1) + a[jj + 1:]
			else:
				l.append(jj + 1)
				a = rev(a[:jj + 1], jj + 1) + a[jj + 1:]
		print(len(l), *l)


from itertools import groupby
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(input())
	b = list(input())
	if a == b:
		print(0)
		continue
	gr_a = groupby(a)
	index = 0
	i = 0
	ans = []
	for (key, group) in gr_a:
		index += len(list(group))
		ans.append(index)
		i += 1
	if a[0] == '1':
		if i % 2 == 0:
			p = '0'
		else:
			p = '1'
	elif i % 2 == 1:
		p = '0'
	else:
		p = '1'
	b = b[::-1]
	gr_b = groupby(b)
	indexb = n
	for (key, group) in gr_b:
		if key == p:
			ans.append(indexb)
			indexb -= len(list(group))
			if indexb > 0:
				ans.append(indexb)
		else:
			indexb -= len(list(group))
	print(len(ans), *ans)


def fun(a, b, n):
	ans = []
	(pos1, pos2) = (0, n - 1)
	flip = False
	for i in range(n - 1, -1, -1):
		if not flip:
			if a[pos1] == b[i]:
				ans.append(1)
			ans.append(i + 1)
			flip = True
			pos1 += 1
		else:
			if a[pos2] != b[i]:
				ans.append(1)
			ans.append(i + 1)
			flip = False
			pos2 -= 1
	return ans
for _ in range(int(input())):
	n = int(input())
	a = list(input())
	b = list(input())
	arr = fun(a, b, n)
	print(len(arr), end=' ')
	if len(arr) > 0:
		print(*arr)
	else:
		print()


for _ in range(int(input())):
	n = int(input())
	a = []
	b = []
	a1 = list(input())
	b1 = list(input())
	for i in a1:
		a.append(int(i))
	for i in b1:
		b.append(int(i))
	pos1 = 0
	pos2 = n - 1
	reverse = 0
	ans = []
	for i in range(n - 1, -1, -1):
		if not reverse:
			if a[pos1] == b[i]:
				ans.append(1)
			ans.append(i + 1)
			reverse = 1
			pos1 += 1
		else:
			if a[pos2] != b[i]:
				ans.append(1)
			ans.append(i + 1)
			reverse = 0
			pos2 -= 1
	print(len(ans), end=' ')
	for i in ans:
		print(i, end=' ')
	print()


import sys, math
input = sys.stdin.readline

def getInts():
	return [int(s) for s in input().split()]

def getInt():
	return int(input())

def getStrs():
	return [s for s in input().split()]

def getStr():
	return input().strip()

def listStr():
	return list(input().strip())
import collections as col
import math

def rev(arr):
	return arr[::-1]

def comp(arr):
	return [arr[j] ^ 1 for j in range(len(arr))]

def RC(arr):
	return rev(comp(arr))

def solve():
	N = getInt()
	S1 = [int(s) for s in input().strip()]
	S2 = [int(s) for s in input().strip()]
	index = N - 1
	ans = []
	parity = 1
	curr_max = N - 1
	curr_min = 0
	checker = []
	while index > -1:
		targ = S2[index]
		ref = S1[curr_min] if parity > 0 else 1 - S1[curr_max]
		if index == 0:
			if ref != S2[index]:
				ans.append(1)
				if parity > 0:
					S1[curr_min] = 1 - S1[curr_min]
				else:
					S1[curr_max] = 1 - S1[curr_max]
		elif ref != targ:
			ans.append(index + 1)
		else:
			ans.append(1)
			if parity > 0:
				S1[curr_min] = 1 - S1[curr_min]
			else:
				S1[curr_max] = 1 - S1[curr_max]
			ans.append(index + 1)
		parity *= -1
		index -= 1
		if parity < 0:
			curr_min += 1
		else:
			curr_max -= 1
	print(len(ans), *ans)
	return
for _ in range(getInt()):
	solve()


def invert(string):
	output = []
	for item in string:
		if item == '1':
			output.append('0')
		else:
			output.append('1')
	return output[::-1]
tests = int(input())
for t in range(tests):
	n = int(input())
	str_1 = list(input())
	str_2 = list(input())
	curr_pt_2 = n - 1
	res = []
	while curr_pt_2 > 0:
		target = str_2[curr_pt_2]
		curr = str_1[0]
		if curr != target:
			res.append(str(curr_pt_2 + 1))
			str_1 = invert(str_1[:curr_pt_2 + 1]) + str_1[curr_pt_2 + 1:]
		else:
			res.append('1')
			if str_1[0] == '1':
				str_1[0] = '0'
			else:
				str_1[0] = '1'
			res.append(str(curr_pt_2 + 1))
			str_1 = invert(str_1[:curr_pt_2 + 1]) + str_1[curr_pt_2 + 1:]
		curr_pt_2 -= 1
	if str_1[0] != str_2[0]:
		res.append('1')
	res = [str(len(res))] + res
	print(' '.join(res))


import sys
input = sys.stdin.buffer.readline

def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		a = list(str(input()))
		b = list(str(input()))
		a = a[2:-3]
		b = b[2:-3]

		def func(x, cnt):
			if (cnt + 1) % 2 == 0:
				return x
			else:
				return str(1 - int(x))
		ans = []
		cnt = 0
		for i in reversed(range(n)):
			if b[i] != a[0]:
				ans.append(i + 1)
				if i % 2 == (n - 1) % 2:
					a[0] = func(a[n - 1 - cnt // 2], cnt)
				else:
					a[0] = func(a[1 + cnt // 2], cnt)
			else:
				ans.append(1)
				a[0] = str(1 - int(a[0]))
				ans.append(i + 1)
				if i % 2 == (n - 1) % 2:
					a[0] = func(a[n - 1 - cnt // 2], cnt)
				else:
					a[0] = func(a[1 + cnt // 2], cnt)
			cnt += 1
		ans = [len(ans)] + ans
		print(*ans)
main()


from sys import stdin
from math import ceil, gcd

def func(x, a):
	k = []
	for ele in a:
		k.append(ele)
	for j in range(x):
		if k[j] == '0':
			k[j] = '1'
		else:
			k[j] = '0'
	k = k[:x][::-1] + k[x:]
	return ''.join(k)
for _ in range(int(stdin.readline())):
	n = int(stdin.readline())
	a = stdin.readline().strip()
	b = stdin.readline().strip()
	i = n - 1
	ans = []
	while i >= 0:
		if a[i] == b[i]:
			i -= 1
		else:
			if a[0] == b[i]:
				ans.append(1)
				a = func(1, a)
			ans.append(i + 1)
			a = func(i + 1, a)
			i -= 1
	print(len(ans), *ans)


t = int(input())

def get_zero(a):
	prefixes = ''
	for idx in range(len(a) - 1):
		if a[idx] != a[idx + 1]:
			prefixes += str(idx + 1) + ' '
	if a[-1] == '1':
		prefixes += str(len(a))
	return prefixes.strip()
for _ in range(t):
	n = int(input())
	a = input()
	b = input()
	c = 0
	prefixes = []
	for idx in range(len(a) - 1):
		if a[idx] != a[idx + 1]:
			prefixes.append(str(idx + 1))
			c += 1
	if a[-1] == '1':
		prefixes.append(str(n))
		c += 1
	rev_prefixes = []
	for idx in range(len(b) - 1):
		if b[idx] != b[idx + 1]:
			rev_prefixes.append(str(idx + 1))
			c += 1
	if b[-1] == '1':
		rev_prefixes.append(str(n))
		c += 1
	rev_prefixes.reverse()
	out = ' '.join((str(x) for x in prefixes)) + ' ' + ' '.join((str(x) for x in rev_prefixes))
	print('{} {}'.format(c, out))


t = int(input())
for i in range(t):
	n = int(input())
	a = input()
	b = input()
	j = n - 1
	while a[j] == b[j]:
		j -= 1
		if j == -1:
			break
	if j == -1:
		print(0)
		continue
	if j == 0:
		print(1, 1)
		continue
	res = []
	k = 0
	while j > 0:
		if b[j] == a[k]:
			res.append(1)
			res.append(j + 1)
		else:
			res.append(j + 1)
		k += 1
		j -= 1
		while b[j] != a[k] and j > 0:
			j -= 1
			k += 1
		if j > 0:
			res.append(j + 1)
	if b[j] == a[k]:
		res.append(1)
	print(len(res), *res)


def helper(s):
	ans = []
	for i in range(1, len(s)):
		if s[i] != s[i - 1]:
			ans.append(i - 1)
	if s[-1] == '1':
		ans.append(len(s) - 1)
	return ans
t = int(input())
for l in range(t):
	n = int(input())
	a = input()
	b = input()
	a1 = helper(a)
	a2 = helper(b)
	a = a1 + a2[::-1]
	print(len(a), end=' ')
	for i in a:
		print(i + 1, end=' ')
	print()


import os
import sys
input = sys.stdin.readline
from collections import deque
from queue import PriorityQueue
import math
import copy

class AC(object):

	def fan(self, s, pos):
		for i in range(pos + 1):
			if s[i] == '0':
				s[i] = '1'
			else:
				s[i] = '0'
		pass

	def __init__(self):
		t = int(input())
		for _ in range(t):
			n = int(input())
			a = deque(input())
			if len(a) > n:
				a.pop()
			b = list(input())
			ans = []
			now = 0
			sign = 0
			for pos in range(n)[::-1]:
				if sign == 0:
					if b[pos] == a[now]:
						ans.append(1)
				elif b[pos] != a[now]:
					ans.append(1)
				pass
				if now == 0:
					a.popleft()
				else:
					a.pop()
				ans.append(pos + 1)
				sign = 1 - sign
				now = -1 - now
			pass
			print(len(ans), end=' ')
			print(' '.join(map(str, ans)))
		pass

	def main(self):
		pass
ac = AC()
ac.main()
pass


t = int(input())
for _ in range(t):
	l = int(input())
	a = input().rstrip()
	b = input().rstrip()
	ans = []
	c = 0
	if a[0] == '1':
		c += 1
	for i in range(len(a) - 1):
		if a[i + 1] != a[i]:
			ans.append(i + 1)
			c += 1
	bns = []
	d = 0
	if b[0] == '1':
		d += 1
	for i in range(len(b) - 1):
		if b[i + 1] != b[i]:
			bns.append(i + 1)
			d += 1
	if c % 2 != d % 2:
		ans += [l]
	f = ans + bns[::-1]
	print(len(f), *f)


tc = int(input())
while tc:
	n = int(input())
	s = input()
	t = input()
	s += '0'
	t += '0'
	a = []
	b = []
	for i in range(1, n + 1):
		if s[i] != s[i - 1]:
			a.append(i)
	for i in range(1, n + 1):
		if t[i] != t[i - 1]:
			b.append(i)
	b.reverse()
	for v in b:
		a.append(v)
	print(len(a), end=' ')
	for v in a:
		print(v, end=' ')
	print()
	tc -= 1


def run():
	import sys
	sys.stdin = open('/home/punit/Documents/Cpp Programs/input.txt', 'r')
	sys.stdout = open('/home/punit/Documents/Cpp Programs/output.txt', 'w')
	from math import ceil, sqrt, floor
for _ in range(int(input())):
	n = int(input())
	f = input()
	s = input()
	ans = []
	for i in range(n):
		if f[i] != s[i] and i == 0:
			ans.append(i + 1)
			ans.append(i + 1)
			ans.append(i + 1)
		elif f[i] != s[i]:
			ans.append(i + 1)
			ans.append(1)
			ans.append(i + 1)
	print(len(ans), *ans)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, list(input())))
	b = list(map(int, list(input())))
	ans = []
	flg = a[0]
	for i in range(n - 1):
		if flg != a[i + 1]:
			flg ^= 1
			ans.append(i + 1)
	for i in range(n - 1, -1, -1):
		if flg != b[i]:
			flg ^= 1
			ans.append(i + 1)
	print(len(ans), end=' ')
	for i in ans:
		print(i, end=' ')
	print('')


t = int(input())
for _ in range(t):
	n = int(input())
	a = input()
	b = input()
	l = []
	for i in range(n):
		if a[i] != b[i]:
			l += [i + 1, 1, i + 1]
	print(len(l), *l)


def comp(val):
	return 1 if val == 0 else 0

def invert(a, p):
	temp = [comp(x) for x in a[0:p]][::-1]
	a[0:p] = temp
for _ in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().strip()]
	b = [int(x) for x in input().strip()]
	i = n - 1
	k = 0
	op = []
	while i >= 0:
		if a[i] == b[i]:
			i -= 1
			continue
		else:
			if b[i] == a[0]:
				k += 1
				op.append(1)
				a[0] = 1 if a[0] == 0 else 0
			k += 1
			op.append(i + 1)
			invert(a, i + 1)
			i -= 1
	print(k, end=' ')
	print(' '.join([str(x) for x in op]))


for _ in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	l = []
	a = list(a)
	b = list(b)
	i = 0
	if a == b:
		print(0)
		continue
	while i < n - 1:
		if a[i] != a[i + 1]:
			for j in range(i + 1):
				a[j] = '1' if a[j] == '0' else '0'
			l.append(i + 1)
			a = a[:i + 1][::-1] + a[i + 1:]
		i += 1
	if a[0] == '1':
		a = ['0' for i in range(n)]
		l.append(n)
	p = n - 1
	q = 0
	while p >= 0 and q < n:
		if a[p] != b[q]:
			p -= 1
			q += 1
		else:
			l.append(p + 1)
			for j in range(p + 1):
				a[j] = '1' if a[j] == '0' else '0'
			p -= 1
			q += 1
	l.append(n)
	if a != b:
		for j in range(n):
			a[j] = '1' if a[j] == '0' else '0'
		a = a[::-1]
	print(len(l), end=' ')
	for i in l:
		print(i, end=' ')
	print()


inp = lambda : map(int, list(input()))
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(inp())
	b = list(inp())
	ans = []
	pointer = [0, n - 1]
	p = 1
	for i in reversed(range(n)):
		if (a[pointer[p]] + p + 1) % 2 == b[i]:
			pass
		elif b[i] == (a[pointer[(p + 1) % 2]] + p) % 2:
			ans += [i + 1]
			p = (p + 1) % 2
		else:
			ans += [1, i + 1]
			p = (p + 1) % 2
		pointer[p] += [1, -1][p == 1]
	print(len(ans), end=' ')
	for i in ans:
		print(i, end=' ')
	print()


t = int(input().strip(' '))
for i in range(t):
	n = int(input().strip(' '))
	a = input().strip(' ')
	b = input().strip(' ')
	s = ''
	x = 0
	for (i, j) in enumerate(a):
		if j != b[i]:
			if i == 0:
				s = str(1) + ' '
				x = x + 1
			else:
				s = s + str(i + 1) + ' ' + str(1) + ' ' + str(i + 1) + ' '
				x = x + 3
	ans = str(x) + ' ' + s
	print(ans.strip(' '))


import os
import sys, math
from io import BytesIO, IOBase

def main():
	t = int(input())
	for case in range(t):
		n = int(input())
		a = list(map(int, list(input())))
		b = list(map(int, list(input())))
		ans = []
		up = 0
		p1 = 0
		p2 = n - 1
		for i in range(n):
			cur = a[p2]
			if up % 2 != 0:
				cur = 1 ^ cur
			if cur != b[n - i - 1]:
				f = a[p1]
				if up % 2 != 0:
					f = 1 ^ f
				if cur != f:
					ans.append(1)
				ans.append(n - i)
				if p1 <= p2:
					(p1, p2) = (p2, p1 + 1)
				else:
					(p1, p2) = (p2, p1 - 1)
				up += 1
			elif p1 <= p2:
				p2 -= 1
			else:
				p2 += 1
		print(len(ans), end=' ')
		for i in ans:
			print(i, end=' ')
		print()
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


def cal(n, flip):
	if flip & 1:
		return n ^ 1
	else:
		return n

def cal_first():
	global p1, p2
	if p1 > p2:
		return first_arr2[p2]
	else:
		return first_arr[p1]

def inc_first():
	global p1, p2
	if p1 == p2:
		p1 += 1
	elif p1 < p2:
		p1 += 1
	else:
		p2 += 1
for i in range(int(input())):
	n = int(input())
	s1 = [int(i) for i in input()]
	s2 = [int(i) for i in input()]
	flip = 0
	ans = []
	first_arr = list(range(n))
	first_arr2 = list(range(n))[::-1]
	p1 = 0
	p2 = 0
	for ind in range(len(s2) - 1, -1, -1):
		first = cal_first()
		if cal(s1[first], flip) == s2[ind]:
			ans.append(1)
			s1[first] ^= 1
			ans.append(ind + 1)
		else:
			ans.append(ind + 1)
		flip += 1
		inc_first()
	if ans:
		print(len(ans), end=' ')
		print(*ans)
	else:
		print(0)


__MULTITEST = True

def solve():
	n = int(input())
	a = [int(d) for d in str(input())]
	b = [int(d) for d in str(input())]
	order = []
	inver = []
	p1 = 0
	p2 = n - 1
	turn = 0
	while p1 <= p2:
		if turn == 0:
			order.append(p1)
			inver.append(turn)
			p1 += 1
			turn ^= 1
		else:
			order.append(p2)
			inver.append(turn)
			p2 -= 1
			turn ^= 1
	op = []
	i = 0
	while i < n:
		if a[order[i]] ^ inver[i] == b[n - i - 1]:
			op.append(1)
			a[order[i]] ^= 1
		else:
			op.append(n - i)
			i += 1
	print(len(op), end=' ')
	for x in op:
		print(x, end=' ')
	print('')
t = int(input()) if __MULTITEST else 1
for tt in range(t):
	solve()


import os
import sys
from io import BytesIO, IOBase

def main():
	pass
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def count1(s):
	c = 0
	for i in s:
		if i == '1':
			c += 1
	return c

def binary(n):
	return bin(n).replace('0b', '')

def decimal(s):
	return int(s, 2)

def pow2(n):
	p = 0
	while n > 1:
		n //= 2
		p += 1
	return p

def isPrime(n):
	if n == 1:
		return False
	else:
		root = int(n ** 0.5)
		root += 1
		for i in range(2, root):
			if n % i == 0:
				return False
		return True
for xyz in range(0, int(input())):
	n = int(input())
	s1 = input()
	s2 = input()
	s1 += '1'
	s2 += '1'
	p1 = []
	for i in range(0, n):
		if s1[i] != s1[i + 1]:
			p1.append(i + 1)
	p2 = []
	for i in range(0, n):
		if s2[i] != s2[i + 1]:
			p2.append(i + 1)
	p2.reverse()
	p1 += p2
	print(len(p1), *p1)


from collections import Counter

def fun(s, c):
	s = s[:c][::-1] + s[c:]
	ans = []
	for i in range(c):
		ans.append(str(int(s[i]) ^ 1))
	ans += s[c:]
	return ans
for _ in range(int(input())):
	n = int(input())
	A = list(input())
	B = list(input())
	t = A[:]
	p = n - 1
	l = 0
	r = n - 1
	count = 0
	ans = []
	while p >= 0:
		if count % 2 == 0:
			while p >= 0 and A[r] == B[p]:
				p -= 1
				r -= 1
			if p >= 0:
				if A[l] == B[p]:
					ans.append(1)
					A[l] = str(int(A[l]) ^ 1)
				count += 1
				ans.append(r - l + 1)
				p -= 1
				l += 1
		else:
			while p >= 0 and A[l] != B[p]:
				p -= 1
				l += 1
			if p >= 0:
				if A[r] != B[p]:
					A[r] = str(int(A[r]) ^ 1)
					ans.append(1)
				count += 1
				ans.append(r - l + 1)
				p -= 1
				r -= 1
	print(len(ans), *ans)


import math
from collections import Counter
try:
	for _ in range(int(input())):
		n = int(input())
		s = input()
		b = input()
		A = []
		for i in range(n):
			if s[i] != b[i]:
				A += [i + 1, 1, i + 1]
		print(len(A), *A)
except:
	pass


t = int(input())
for i1 in range(t):
	n = int(input())
	str1 = input()
	str2 = input()
	ans = []
	for i in range(n, 0, -1):
		if str1[i - 1] != str2[i - 1]:
			ans.append(i)
			ans.append(1)
			ans.append(i)
	print(len(ans), end=' ')
	for x in ans:
		print(x, end=' ')
	print()


import sys
import math
from collections import defaultdict
t = int(sys.stdin.readline())
for _ in range(t):
	n = int(sys.stdin.readline())
	a = list(map(int, sys.stdin.readline()[:-1]))
	b = list(map(int, sys.stdin.readline()[:-1]))
	ans = []
	for i in range(n - 1, -1, -1):
		if a[i] == b[i]:
			continue
		elif a[0] != b[i]:
			tmp = []
			for j in range(i + 1):
				tmp.append(a[j] ^ 1)
			tmp.reverse()
			for j in range(i + 1):
				a[j] = tmp[j]
			ans.append(i + 1)
		else:
			ans.append(1)
			a[0] ^= 1
			tmp = []
			for j in range(i + 1):
				tmp.append(a[j] ^ 1)
			tmp.reverse()
			for j in range(i + 1):
				a[j] = tmp[j]
			ans.append(i + 1)
	print(len(ans), *ans)


def turn(ar, k):
	for i in range(k):
		ar[i] += 1
		ar[i] %= 2
	i = 0
	while i < k:
		k -= 1
		lol = ar[i]
		ar[i] = ar[k]
		ar[k] = lol
		i += 1
	return ar
for __ in range(int(input())):
	n = int(input())
	ar1 = list(map(int, input()))
	ar2 = list(map(int, input()))
	i = n - 1
	ans = []
	while i >= 0:
		if ar1[i] == 0 and ar2[i] == 1:
			if ar1[0] == 1:
				ans.append(1)
				ar1 = turn(ar1, 1)
			ans.append(i + 1)
			ar1 = turn(ar1, i + 1)
		elif ar1[i] == 1 and ar2[i] == 0:
			if ar1[0] == 0:
				ans.append(1)
				ar1 = turn(ar1, 1)
			ans.append(i + 1)
			ar1 = turn(ar1, i + 1)
		i -= 1
	k = len(ans)
	ans = [k] + ans
	print(*ans)


for _ in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	k = 0
	ans = []
	for i in range(n):
		if a[i] != b[i]:
			k += 1
			ans.append(i + 1)
			if i - 1 >= 0:
				k += 2
				ans.append(1)
				ans.append(i + 1)
	print(k, end=' ')
	for i in ans:
		print(i, end=' ')
	print()


import math as mt

def tran(s1, s2, n):
	an = []
	for i in range(n):
		if s1[n - i - 1] == s2[n - i - 1]:
			pass
		elif s2[n - i - 1] != s1[0]:
			an.append(n - i)
			s11 = str(s1)
			ss = s1[:n - i]
			Sar = []
			for j in ss:
				if j == '1':
					Sar.append('0')
				else:
					Sar.append('1')
			Sar.reverse()
			s1 = ''
			for k in Sar:
				s1 = s1 + k
			s1 = s1 + s11[n - i:]
		else:
			an.append(1)
			s11 = str(s1)
			if s1[0] == '0':
				s1 = s1.replace('0', '1', 1)
			else:
				s1 = s1.replace('1', '0', 1)
			an.append(n - i)
			ss = s1[:n - i]
			Sar = []
			for j in ss:
				if j == '1':
					Sar.append('0')
				else:
					Sar.append('1')
			Sar.reverse()
			s1 = ''
			for k in Sar:
				s1 = s1 + k
			s1 = s1 + s11[n - i:]
	if len(an) == 0:
		return []
	else:
		return an
t = int(input())
ans = []
for i in range(t):
	n = int(input())
	s1 = input()
	s2 = input()
	r = tran(s1, s2, n)
	ans.append(r)
for i in ans:
	if len(i) == 0:
		print(0)
	else:
		p = len(i)
		i.insert(0, p)
		print(*i)


import sys
import string
import math
from collections import defaultdict
from functools import lru_cache
from collections import Counter
from fractions import Fraction

def mi(s):
	return map(int, s.strip().split())

def lmi(s):
	return list(mi(s))

def tmi(s):
	return tuple(mi(s))

def mf(f, s):
	return map(f, s)

def lmf(f, s):
	return list(mf(f, s))

def js(lst):
	return ' '.join((str(d) for d in lst))

def jsns(lst):
	return ''.join((str(d) for d in lst))

def line():
	return sys.stdin.readline().strip()

def linesp():
	return line().split()

def iline():
	return int(line())

def mat(n):
	matr = []
	for _ in range(n):
		matr.append(linesp())
	return matr

def matns(n):
	mat = []
	for _ in range(n):
		mat.append([c for c in line()])
	return mat

def mati(n):
	mat = []
	for _ in range(n):
		mat.append(lmi(line()))
	return mat

def pmat(mat):
	for row in mat:
		print(js(row))

def flip(arr, i, j):
	while i < j + 1:
		arr[i] = 1 - arr[i]
		i += 1

def reverse(arr, i, j):
	while i < j:
		(arr[i], arr[j]) = (arr[j], arr[i])
		i += 1
		j -= 1

def solve(A, B):
	A = [int(c) for c in A]
	B = [int(c) for c in B]
	moves = []
	for i in range(len(A) - 1, -1, -1):
		if i == 0:
			if A[i] == B[i]:
				break
			else:
				flip(A, 0, 0)
				moves.append(1)
		elif A[i] == B[i]:
			continue
		else:
			want = B[i]
			if A[0] == B[i]:
				moves.append(1)
			moves.append(i + 1)
			flip(A, 0, i)
			reverse(A, 0, i)
	print(len(moves), js(moves))

def main():
	t = iline()
	for _ in range(t):
		line()
		solve(line(), line())
main()


import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__

def gift():
	for _ in range(t):
		n = int(input())
		s1 = list(input())
		s2 = list(input())
		res = []
		last = s1[0]
		for i in range(1, n):
			if s1[i] != last:
				res.append(i)
				last = s1[i]
		for i in range(n - 1, -1, -1):
			if s2[i] != last:
				res.append(i + 1)
				last = s2[i]
		yield ' '.join((str(x) for x in [len(res)] + res))
t = int(input())
ans = gift()
print(*ans, sep='\n')


import sys
max_int = 1000000001
min_int = -max_int
t = int(input())
for _t in range(t):
	n = int(sys.stdin.readline())
	a = list(map(int, list(input())))
	b = list(map(int, list(input())))
	p = []
	lena = len(a)
	for i in range(lena - 1, -1, -1):
		if a[i] != b[i]:
			if a[0] == b[i]:
				p.append('1')
				a[0] = 1 - a[0]
			p.append(str(i + 1))
			for j in range((i + 2) // 2):
				(a[j], a[i - j]) = (1 - a[i - j], 1 - a[j])
	print(len(p), ' '.join(p))


f = 'First'
s = 'Second'
for _ in range(int(input())):
	n = int(input())
	a = list(input())
	b = list(input())
	ope = 0
	l = []
	for i in range(n - 1, 0, -1):
		if a[i] != b[i]:
			if a[0] == b[i]:
				ope += 2
				l.append(1)
				l.append(i + 1)
				if a[0] == '1':
					a[0] = '0'
				else:
					a[0] = '1'
				for j in range(i + 1):
					if a[j] == '1':
						a[j] = '0'
					else:
						a[j] = '1'
				nl = a[:i + 1]
				nl.reverse()
				a = nl + a[i + 1:]
			else:
				ope += 1
				l.append(i + 1)
				for j in range(i + 1):
					if a[j] == '1':
						a[j] = '0'
					else:
						a[j] = '1'
				nl = a[:i + 1]
				nl.reverse()
				a = nl + a[i + 1:]
	if a[0] != b[0]:
		ope += 1
		l.append(1)
	if ope >= 1:
		print(ope, *l)
	else:
		print(0)


t = int(input())
for _ in range(t):
	n = int(input())
	a = str(input())
	b = str(input())
	op = 0
	idx = []
	i = 0
	if not a == b:
		while i < n:
			while a[i] == '0':
				i += 1
				if i == n:
					break
			if i > 0 and i < n:
				op += 1
				idx.append(i)
			if i == n:
				break
			while a[i] == '1':
				i += 1
				if i == n:
					break
			op += 1
			idx.append(i)
		i = 0
		invidx = []
		while i < n:
			while b[i] == '0':
				i += 1
				if i == n:
					break
			if i > 0 and i < n:
				op += 1
				invidx.append(i)
			if i == n:
				break
			while b[i] == '1':
				i += 1
				if i == n:
					break
			op += 1
			invidx.append(i)
		invidx.reverse()
		idx = idx + invidx
	print(op, end=' ')
	for x in idx:
		print(x, end=' ')
	print()


import sys
t = int(input())
for _ in range(t):
	n = int(input())
	tempa = input()
	tempb = input()
	a = []
	for i in tempa:
		a.append(int(i))
	b = []
	for i in tempb:
		b.append(int(i))
	count = 0
	anslist = []
	start = 1
	rev = 0
	for i in range(n - 1, -1, -1):
		if rev == 0:
			pos = start + i
		else:
			pos = start - i
		curr = a[pos - 1]
		if rev == 1:
			curr = curr ^ 1
		if curr != b[i]:
			first = a[start - 1]
			if rev == 1:
				first = first ^ 1
			if first != b[i]:
				anslist.append(i + 1)
				count += 1
			else:
				anslist.append(1)
				anslist.append(i + 1)
				count += 2
			rev = rev ^ 1
			start = pos
	print(count, end=' ')
	for i in anslist:
		sys.stdout.write(str(i) + ' ')
	print()


import sys

def flip(a):
	return [not i for i in reversed(a)]

def prefix_flip(a, b):
	operations = []
	while True:
		if not a:
			return operations
		elif a[-1] == b[-1]:
			del a[-1]
			del b[-1]
		elif a[0] != b[-1]:
			operations.append(len(a))
			a = flip(a)
		else:
			operations.append(1)
			a[0] = not a[0]

def read_bitstring(fd):
	return [i == '1' for i in next(fd).rstrip()]

def test(fd):
	next(fd)
	a = read_bitstring(fd)
	b = read_bitstring(fd)
	operations = prefix_flip(a, b)
	print(len(operations), end=' ')
	print(*operations)

def main(fd=sys.stdin):
	t = int(next(fd))
	for _ in range(t):
		test(fd)
main()


(z, zz) = (input, lambda : list(map(int, z().split())))
zzz = lambda : [int(i) for i in stdin.readline().split()]
(szz, graph, mod, szzz) = (lambda : sorted(zz()), {}, 10 ** 9 + 7, lambda : sorted(zzz()))
from string import *
from collections import *
from queue import *
from sys import *
from collections import *
from math import *
from heapq import *
from itertools import *
from bisect import *
from collections import Counter as cc
from math import factorial as f
from bisect import bisect as bs
from bisect import bisect_left as bsl
from itertools import accumulate as ac

def lcd(xnum1, xnum2):
	return xnum1 * xnum2 // gcd(xnum1, xnum2)

def prime(x):
	p = ceil(x ** 0.5) + 1
	for i in range(2, p):
		if x % i == 0 and x != 2 or x == 0:
			return 0
	return 1

def dfs(u, visit, graph):
	visit[u] = True
	for i in graph[u]:
		if not visit[i]:
			dfs(i, visit, graph)
num = int(z())
for _ in range(num):
	n = int(z())
	l1 = z()
	l2 = z()
	ans = 0
	li = []
	k = 1
	for (i, j) in zip(l1, l2):
		if i != j:
			ans += 3
			li.append(k)
			li.append(1)
			li.append(k)
		k += 1
	print(ans, *li)


import sys
input = sys.stdin.readline
t = int(input())
while t:
	t -= 1
	n = int(input())
	a = input().strip()
	b = input().strip()
	pos = []
	(st, end) = (0, n - 1)
	rev = False
	time = 0
	for i in range(n - 1, -1, -1):
		if rev:
			if int(a[st]) ^ time % 2 == int(b[i]):
				st += 1
			else:
				if int(a[end]) ^ time % 2 == int(b[i]):
					pos.append(1)
				pos.append(i + 1)
				time += 1
				end -= 1
				rev = not rev
		elif int(a[end]) ^ time % 2 == int(b[i]):
			end -= 1
		else:
			if int(a[st]) ^ time % 2 == int(b[i]):
				pos.append(1)
			pos.append(i + 1)
			time += 1
			st += 1
			rev = not rev
	print(len(pos), *pos)


import os
import operator
import sys
from _collections import defaultdict
import bisect
from io import BytesIO, IOBase

def inar():
	return [int(k) for k in input().split()]

def power(x, p, m):
	res = 1
	while p:
		if p & 1:
			res = res * x % m
		x = x * x % m
		p >>= 1
	return res
visited = []
level = []
adj = defaultdict(list)

def bfs(node):
	global visited, level, adj
	qu = []
	visited[node] = 1
	qu.append(node)
	while qu:
		el = qu.pop(0)
		for child in adj[el]:
			if not visited[child]:
				level[child] = level[el] + 1
				visited[child] = 1
				qu.append(child)

def main():
	for _ in range(int(input())):
		n = int(input())
		s = input()
		t = input()
		s += '0'
		t += '0'
		res = []
		for i in range(n):
			if s[i] != s[i + 1]:
				res.append(i + 1)
		for i in range(n - 1, -1, -1):
			if t[i] != t[i + 1]:
				res.append(i + 1)
		print(len(res), end=' ')
		print(*res)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


for _ in range(int(input())):
	n = int(input())
	g = input()
	d = input()
	if g == d:
		print(0)
	else:
		c = 0
		g = list(g)
		d = list(d)
		pl = []
		for i in range(n):
			if g[0] == d[n - i - 1]:
				c += 1
				pl.append(1)
			pl.append(n - i)
			c += 1
			del g[0]
			for j in range(n - i - 1):
				if g[j] == '0':
					g[j] = '1'
				else:
					g[j] = '0'
			g = g[::-1]
		print(c, end=' ')
		for i in pl:
			print(i, end=' ')
		print()


for _ in range(int(input())):
	n = int(input())
	s1 = input()
	s2 = input()
	A = []
	Ans = []
	for i in range(len(s1)):
		A.append(int(s1[i]))
	for i in range(len(s1) - 1, -1, -1):
		if A[i] != int(s2[i]):
			if A[i] != A[0]:
				Ans.append(1)
				A[0] = 1 - A[0]
				Ans.append(i + 1)
				for j in range((i + 1) // 2):
					(A[j], A[i - j]) = (1 - A[i - j], 1 - A[j])
				if i % 2 == 0:
					A[i // 2] = 1 - A[i // 2]
			else:
				Ans.append(i + 1)
				for j in range((i + 1) // 2):
					(A[j], A[i - j]) = (1 - A[i - j], 1 - A[j])
				if i % 2 == 0:
					A[i // 2] = 1 - A[i // 2]
	print(len(Ans), end=' ')
	print(*Ans)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(str(input()))
	b = list(str(input()))
	ans = []
	import copy
	for i in reversed(range(n)):
		if b[i] != a[0]:
			ans.append(i + 1)
			temp = copy.copy(a)
			for j in range(i + 1):
				temp[j] = str(1 - int(a[i - j]))
			a = temp
		else:
			ans.append(1)
			a[0] = str(1 - int(a[0]))
			ans.append(i + 1)
			temp = copy.copy(a)
			for j in range(i + 1):
				temp[j] = str(1 - int(a[i - j]))
			a = temp
	ans = [len(ans)] + ans
	print(*ans)


def main(r):
	n = int(input())
	a = input()
	b = input()
	st = 0
	end = n - 1
	ln = 0
	swaps = ''
	if len(a) == 1:
		if a == b:
			print(0)
		else:
			print('1 1')
		return
	else:
		for i in range(0, n):
			if i % 2 == 0:
				if a[st] == b[-(1 + i)]:
					ln += 1
					swaps += '1 '
				st += 1
			else:
				if a[end] != b[-(1 + i)]:
					swaps += '1 '
					ln += 1
				end -= 1
			ln += 1
			swaps += str(n - i) + ' '
		print(ln, swaps)
t = int(input())
for i in range(t):
	main(i)


t = int(input())

def flip(n):
	if n == 0:
		n = 1
		return n
	if n == 1:
		n = 0
		return n
for i in range(t):
	n = int(input())
	a = list(map(int, input()))
	b = list(map(int, input()))
	lst = []
	count = 0
	j = 0
	for i in range(len(b) - 1, -1, -1):
		if b[i] == a[i]:
			continue
		elif b[i] != a[j]:
			count += 1
			lst.append(i + 1)
			for k in range(i // 2 + 1):
				(a[k], a[i - k]) = (flip(a[i - k]), flip(a[k]))
		else:
			count += 2
			lst.append(1)
			lst.append(i + 1)
			a[j] = flip(a[j])
			for k in range(i // 2 + 1):
				(a[k], a[i - k]) = (flip(a[i - k]), flip(a[k]))
	print(count, end=' ')
	for i in range(len(lst)):
		print(lst[i], end=' ')
	print()


def solve():
	n = int(input())
	a = input()
	b = input()
	if a == b:
		print(0)
		return
	prev = a[0]
	ans = 0
	ansarr = []
	for i in range(1, n):
		if a[i] != prev:
			ans += 1
			ansarr.append(i)
		prev = a[i]
	for i in reversed(range(n)):
		if b[i] != prev:
			ans += 1
			ansarr.append(i + 1)
		prev = b[i]
	print(len(ansarr), end=' ')
	for i in ansarr:
		print(i, end=' ')
	print()
t = int(input())
for _ in range(t):
	solve()


t = int(input())
for _ in range(t):
	n = int(input())
	a = input()
	a += '0'
	b = input()
	b += '0'
	ans = []
	tmp = []
	for i in range(1, n + 1):
		if a[i] != a[i - 1]:
			ans += [i]
		if b[i] != b[i - 1]:
			tmp += [i]
	print(len(ans) + len(tmp), *ans, *tmp[:][::-1])


t = int(input())
for _ in range(t):
	n = int(input())
	a = input()
	b = input()
	if a == b:
		print(0)
	else:
		ans = ''
		count = 0
		if a[0] != b[0]:
			ans += '1 '
			count = 1
		for i in range(1, n):
			if a[i] != b[i]:
				ans += str(i + 1) + ' 1 ' + str(i + 1) + ' '
				count += 3
		print(count, ans)


def doOp(a, i):
	s = ''
	s = [not x for x in a[:i]]
	s.reverse()
	return s + a[i:]
for _ in range(int(input())):
	n = int(input())
	a = [True if x == '1' else False for x in input()]
	b = [True if x == '1' else False for x in input()]
	rb = n - 1
	ct = 0
	ans = []
	while rb >= 0:
		if a[rb] != b[rb]:
			if a[0] == b[rb]:
				ct += 1
				ans.append(1)
				a[0] = ~a[0]
			ct += 1
			ans.append(rb + 1)
			a = doOp(a, rb + 1)
		rb -= 1
	if len(ans) == 0:
		print(0)
	else:
		for i in range(len(ans)):
			if i == 0:
				print(ct, end=' ')
			else:
				print(ans[i - 1], end=' ')
		print(ans[-1])


for _ in range(int(input())):
	n = int(input())
	num1 = input()
	num2 = input()
	count = 0
	li = []
	for i in range(n):
		if num1 != num2:
			if i < n - 1:
				if num1[i] == num2[-(i + 1)]:
					li.append(i + 1)
					count += 1
					print
					if i != 0:
						li.append(1)
						li.append(i + 1)
						count += 2
			else:
				count += 1
				li.append(n)
				if num1[n - 1] == num2[0]:
					count += 1
					li.append(1)
		else:
			break
	if count == 0:
		print(0)
	else:
		print(count, *li)


t = int(input())
for _ in range(t):
	n = int(input())
	a = input()
	b = input()
	ops1 = []
	ops2 = []
	for i in range(len(a) - 1):
		if a[i] != a[i + 1]:
			ops1.append(i + 1)
			if a[i] == '0':
				prefix = '1' * (i + 1)
			else:
				prefix = '0' * (i + 1)
			a = prefix + a[i + 1:]
	for i in range(len(b) - 1):
		if b[i] != b[i + 1]:
			ops2.append(i + 1)
			if b[i] == '0':
				prefix = '1' * (i + 1)
			else:
				prefix = '0' * (i + 1)
			b = prefix + b[i + 1:]
	ops2 = list(reversed(ops2))
	if a[0] != b[0]:
		ops1.append(n)
	print(len(ops1) + len(ops2), *ops1, *ops2)


for _ in range(int(input())):
	n = int(input())
	s1 = str(input())
	s2 = str(input())
	a = list(s1)
	b = list(s2)
	l = []
	for i in range(n - 1, -1, -1):
		if a[0] != b[i]:
			l.append(i + 1)
			l.append(i)
			a.pop(0)
		elif a[0] == b[i]:
			l.append(1)
			l.append(i + 1)
			l.append(i)
			a.pop(0)
	l.pop()
	print(len(l), *l)


def ans(n, a, b):
	a += '0'
	b += '0'
	aa = []
	bb = []
	for i in range(n):
		if a[i] != a[i + 1]:
			aa.append(i + 1)
	for i in range(n):
		if b[i] != b[i + 1]:
			bb.append(i + 1)
	print(len(aa) + len(bb), end=' ')
	for i in range(len(aa)):
		print(aa[i], end=' ')
	for i in range(len(bb) - 1, -1, -1):
		print(bb[i], end=' ')
	print()
t = int(input())
for i in range(t):
	n = int(input())
	a = input()
	b = input()
	ans(n, a, b)


import math
from collections import defaultdict, deque
from bisect import bisect_left, bisect_right
from sys import stdin

def mp():
	return map(int, stdin.readline().split())

def ml():
	return list(map(int, stdin.readline().split()))

def change(a, i):
	ans = ''
	for j in range(i + 1):
		ans += '1' if a[j] == '0' else '0'
	return ans[::-1] + a[i + 1:]
for _ in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	ba = a
	ans = []
	for i in range(n - 1, -1, -1):
		if a[i] != b[i]:
			if a[i] != a[0]:
				ans.extend([1, i + 1])
				a = ('1' if a[0] == '0' else '0') + a[1:]
			else:
				ans.append(i + 1)
			a = change(a, i)
	print(len(ans), *ans)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(input())
	a = list(map(int, a))
	b = list(input())
	b = list(map(int, b))
	c = []
	lst1 = []
	i = 0
	j = n - 1
	idx = 0
	while len(c) < n:
		if idx % 2 == 0:
			c.append(a[i])
			i += 1
			idx += 1
		else:
			if a[j] == 0:
				c.append(1)
			else:
				c.append(0)
			j -= 1
			idx += 1
	ans = 0
	for i in range(n):
		if c[i] == b[n - i - 1]:
			ans += 2
			lst1.append(1)
			lst1.append(n - i)
		else:
			ans += 1
			lst1.append(n - i)
	print(ans, end=' ')
	for i in range(len(lst1)):
		print(lst1[i], end=' ')
	print()


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(input())
	b = list(input())
	aans = []
	bans = []
	for i in range(n - 1):
		if a[i] != a[i + 1]:
			aans.append(i + 1)
		if b[i] != b[i + 1]:
			bans.append(i + 1)
	if a[n - 1] == '1':
		aans.append(n)
	if b[n - 1] == '1':
		bans.append(n)
	bans = list(reversed(bans))
	aans.extend(bans)
	print(len(aans), *aans)


def invert_flip(a):
	c = ['0' if x == '1' else '1' for x in a]
	return list(reversed(c))
t = int(input())
while t:
	n = int(input())
	a = list(input())
	b = list(input())
	i = n - 1
	l = []
	while i >= 0:
		if i == 0:
			if a[i] != b[i]:
				l.append(1)
		elif a[i] != b[i]:
			if a[0] == a[i]:
				a[0:i + 1] = invert_flip(a[0:i + 1])
				l.append(i + 1)
			else:
				a[1:i] = invert_flip(a[1:i])
				a[i] = b[i]
				l.append(1)
				l.append(i + 1)
		i -= 1
	print(str(len(l)) + ' ' + ' '.join([str(x) for x in l]))
	t -= 1


import sys
input = sys.stdin.readline
T = int(input())
for t in range(T):
	N = int(input())
	A = input()[:-1]
	B = input()[:-1]
	answer = []
	el0 = A[0]
	for i in range(N - 1, -1, -1):
		if el0 == B[i]:
			answer.append(1)
		answer.append(i + 1)
		if i == N - 1:
			el0 = '0' if A[i] == '1' else '1'
		elif (N - i) % 2:
			el0 = '0' if A[N - 1 - (N - i - 1) // 2] == '1' else '1'
		else:
			el0 = A[(N - i) // 2]
	print(len(answer), ' '.join(map(str, answer)))


def solve(n, a, b):
	ans = [0]
	p = 0
	for i in range(n - 1, -1, -1):
		if (a[i] + p) % 2 != b[i]:
			if (a[0] + p) % 2 != b[i]:
				ans += [i + 1]
				ans[0] += 1
			else:
				ans += [1, i + 1]
				ans[0] += 2
			a = a[:i + 1][::-1] + a[i + 1:]
			p += 1
	return ans

def main():
	t = int(input())
	for i in range(t):
		(n, a, b) = (int(input()), [int(i) for i in input()], [int(i) for i in input()])
		print(*solve(n, a, b))
main()


import os
import sys
from io import BytesIO, IOBase
import random

def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		(a, b) = (input(), input())
		if a == b:
			print(0)
			continue
		done = 0
		(numops, preflens) = (0, [])
		while done != n:
			if done == n - 1:
				if a[0] != b[0]:
					numops += 1
					preflens += [1]
				break
			if a[n - done - 2:n - done] != b[n - done - 2:n - done]:
				start = a[:2]
				end = b[n - done - 2:n - done]
				if end == '01':
					if start == '11':
						numops += 2
						preflens += [1, n - done]
					elif start == '00':
						numops += 3
						preflens += [2, 1, n - done]
					elif start == '10':
						numops += 4
						preflens += [1, 2, 1, n - done]
					else:
						numops += 1
						preflens += [n - done]
					temp = '01' + a[2:n - done]
					temp = temp.replace('0', '2').replace('1', '0').replace('2', '1')
					a = temp[::-1] + a[n - done:]
				if end == '11':
					if start == '01':
						numops += 3
						preflens += [1, 2, n - done]
					elif start == '10':
						numops += 2
						preflens += [1, n - done]
					elif start == '00':
						numops += 1
						preflens += [n - done]
					else:
						numops += 2
						preflens += [2, n - done]
					temp = '00' + a[2:n - done]
					temp = temp.replace('0', '2').replace('1', '0').replace('2', '1')
					a = temp[::-1] + a[n - done:]
				if end == '00':
					if start == '10':
						numops += 3
						preflens += [1, 2, n - done]
					elif start == '01':
						numops += 2
						preflens += [1, n - done]
					elif start == '11':
						numops += 1
						preflens += [n - done]
					else:
						numops += 2
						preflens += [2, n - done]
					temp = '11' + a[2:n - done]
					temp = temp.replace('0', '2').replace('1', '0').replace('2', '1')
					a = temp[::-1] + a[n - done:]
				if end == '10':
					if start == '00':
						numops += 2
						preflens += [1, n - done]
					elif start == '11':
						numops += 3
						preflens += [2, 1, n - done]
					elif start == '01':
						numops += 4
						preflens += [1, 2, 1, n - done]
					else:
						numops += 1
						preflens += [n - done]
					temp = '10' + a[2:n - done]
					temp = temp.replace('0', '2').replace('1', '0').replace('2', '1')
					a = temp[::-1] + a[n - done:]
			done += 2
		print(numops, *preflens)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


flipped = False
flip_index = 0

def front():
	if flipped:
		return 1 - A[flip_index]
	return A[flip_index]

def invert(k):
	global flipped, flip_index
	if not flipped:
		flip_index += k - 1
	else:
		flip_index -= k - 1
	flipped = not flipped
for TEST in range(int(input())):
	flipped = False
	flip_index = 0
	n = int(input())
	A = list(map(int, input()))
	B = list(map(int, input()))
	ans = []
	for i in range(n - 1, 0, -1):
		if front() == B[i]:
			ans.append(1)
		ans.append(i + 1)
		invert(i + 1)
	if front() != B[0]:
		ans.append(1)
	print(len(ans), *ans)


T = int(input())

def go(x, k):
	ans = []
	for i in range(k + 1):
		ans.append(x[k - i] ^ 1)
	return ans
for t in range(T):
	n = int(input())
	source = [int(i) for i in input()]
	target = [int(i) for i in input()]
	left = 0
	right = n - 1
	ans = []
	direction = -1
	flip_counter = 0
	pointer = n - 1
	while pointer > 0:
		if direction < 0:
			source_pointer = right
			oppo = left
		else:
			source_pointer = left
			oppo = right
		if source[source_pointer] ^ flip_counter != target[pointer]:
			if source[oppo] ^ flip_counter == target[pointer]:
				ans.append(1)
			ans.append(pointer + 1)
			flip_counter ^= 1
			direction *= -1
		if direction < 0:
			right -= 1
		else:
			left += 1
		pointer -= 1
	if direction < 0:
		source_pointer = right
	else:
		source_pointer = left
	if source[source_pointer] ^ flip_counter != target[0]:
		ans.append(1)
	m = len(ans)
	ans = [m] + ans
	print(*ans)


def trans1(v):
	(q, w) = (0, [])
	prev = None
	for (i, p) in enumerate(v):
		if prev is not None and prev != p:
			w.append(i)
			q += 1
		prev = p
	if v[-1] == '1':
		w.append(len(v))
		q += 1
	return (q, w)

def trans2(v):
	(q, w) = (0, [])
	c = 0
	for (i, p) in enumerate(v[::-1]):
		if c != int(p):
			w.append(len(v) - i)
			q += 1
			c = 1 - c
	return (q, w)
for t in range(int(input())):
	n = int(input())
	(a, b) = (input(), input())
	(q1, w1) = trans1(a)
	(q2, w2) = trans2(b)
	for x in [q1 + q2] + w1 + w2:
		print(x, end=' ')
	print()


import sys
t = int(input())
for _ in range(t):
	n = int(input())
	a = input()
	b = input()
	ans1 = []
	boo1 = a[0]
	for i in range(1, n):
		if a[i] != boo1:
			ans1.append(i)
			if boo1 == '1':
				boo1 = '0'
			else:
				boo1 = '1'
	ans2 = []
	boo2 = b[0]
	for i in range(1, n):
		if b[i] != boo2:
			ans2.append(i)
			if boo2 == '1':
				boo2 = '0'
			else:
				boo2 = '1'
	if boo1 == boo2:
		print(len(ans1 + ans2), end=' ')
		for i in ans1:
			print(i, end=' ')
		for i in ans2[::-1]:
			print(i, end=' ')
		print()
		continue
	print(len(ans1 + ans2) + 1, end=' ')
	for i in ans1:
		print(i, end=' ')
	print(n, end=' ')
	for i in ans2[::-1]:
		print(i, end=' ')
	print()


import sys
input = sys.stdin.readline
I = lambda : list(map(int, input().split()))
(t,) = I()

def inv(x):
	return '0' if x == '1' else '1'
for _ in range(t):
	(n,) = I()
	a = list(input().strip())
	b = input().strip()
	op = 0
	l = 0
	an = []
	for i in range(n - 1, -1, -1):
		if a[i] != b[i]:
			if b[i] == a[0]:
				an.append(1)
				a[0] = inv(a[0])
				if i != 0:
					an.append(i + 1)
					for j in range((i + 2) // 2):
						(y, x) = (a[j], a[i - j])
						(a[j], a[i - j]) = (inv(x), inv(y))
			else:
				an.append(i + 1)
				for j in range((i + 2) // 2):
					(y, x) = (a[j], a[i - j])
					(a[j], a[i - j]) = (inv(x), inv(y))
			if i != 0:
				op = i + 1
	print(len(an), *an)


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	s = [int(item) for item in list(input().rstrip())]
	t = [int(item) for item in list(input().rstrip())]
	srev = []
	for item in s:
		srev.append(1 - item)
	ans = []
	state = 1
	for_idx = n - 1
	rev_idx = 0
	for i in range(n):
		ti = t[n - 1 - i]
		if state:
			si = s[for_idx]
			if ti == si:
				for_idx -= 1
				continue
			if ti == s[rev_idx]:
				ans.append(1)
			ans.append(n - i)
			rev_idx += 1
		else:
			si = srev[rev_idx]
			if ti == si:
				rev_idx += 1
				continue
			if ti == srev[for_idx]:
				ans.append(1)
			ans.append(n - i)
			for_idx -= 1
		state = 1 - state
	if state:
		if s[rev_idx] != t[0]:
			ans.append(1)
	elif srev[for_idx] != t[0]:
		ans.append(1)
	if not ans:
		print(0)
	else:
		print(len(ans), *ans)


def solver(lst, c):
	for i in range(c + 1):
		lst[i] = '0' if lst[i] == '1' else '1'
	(lst1, lst2) = (lst[:c + 1], lst[c + 1:])
	lst1.reverse()
	return lst1 + lst2
for _ in range(int(input())):
	n = int(input())
	s = list(input())
	t = list(input())
	(c, lst) = (n - 1, list())
	while c >= 0:
		if s[c] != t[c]:
			if s[c] == s[0]:
				s = solver(s, c)
				lst += [c + 1]
			else:
				s = solver(s, 0)
				s = solver(s, c)
				lst += [1, c + 1]
			c -= 1
		else:
			c -= 1
	print(len(lst), end=' ')
	for elem in lst:
		print(elem, end=' ')
	print('')


import math

def mi():
	return map(int, input().split())

def ii():
	return int(input())

def li():
	return list(map(int, input().split()))

def si():
	return input().split()

def chnge(a):
	b = a[:]
	for i in range(len(a)):
		if a[i] == '1':
			b[-i - 1] = '0'
		else:
			b[-i - 1] = '1'
	return b
t = ii()
for _ in range(t):
	n = ii()
	a = list(input())
	b = list(input())
	i = n - 1
	ans = []
	while i >= 0:
		if a[i] == b[i]:
			i -= 1
			continue
		if a[0] != b[i]:
			a = chnge(a[:i + 1]) + a[i + 1:]
			ans.append(i + 1)
		else:
			ans.append(1)
			a = chnge(a[:1]) + a[1:]
			ans.append(i + 1)
			a = chnge(a[:i + 1]) + a[i + 1:]
	print(len(ans), end=' ')
	for i in ans:
		print(i, end=' ')
	print()


def invrev(a, index):
	b = ''
	for i in range(index):
		if a[i] == '0':
			b += '1'
		else:
			b += '0'
	return b[::-1] + a[index:]
t = int(input())
while t != 0:
	t -= 1
	n = int(input())
	a = input()
	b = input()
	output = []
	for i in range(n - 1, -1, -1):
		if a[i] != b[i]:
			if a[0] == b[i]:
				a = invrev(a, 1)
				output.append(1)
			a = invrev(a, i + 1)
			output.append(i + 1)
	if len(output) == 0:
		print(0, end='')
	else:
		print(len(output), end=' ')
		for i in range(len(output)):
			print(output[i], end=' ')
	print('\n', end='')


from sys import stdin
t = int(input())
input = stdin.readline
for _ in range(t):
	n = int(input())
	a = [int(i) for i in input().strip()]
	b = [int(i) for i in input().strip()]
	ans = []
	for i in range(n - 1, -1, -1):
		if a[i] != b[i]:
			if a[0] != a[i] or i == 0:
				ans.append(1)
				a[0] ^= 1
				if i == 0:
					continue
			ans.append(i + 1)
			for j in range((i + 1) // 2 + (1 - i % 2)):
				if a[j] == a[i - j]:
					if j != i - j:
						a[j] ^= 1
					a[i - j] ^= 1
	assert a == b
	print(len(ans), *ans)


def f(c):
	if c == '1':
		return '0'
	else:
		return '1'

def g(a, i):
	c = a[:i + 1]
	for j in range(i):
		a[j] = f(c[i - j])
for _ in range(int(input())):
	n = int(input())
	a = list(input())
	b = list(input())
	l = []
	i = n - 1
	while i >= 0:
		if a[i] != b[i]:
			if a[0] == b[i]:
				l.append(1)
				a[0] = f(a[0])
			l.append(i + 1)
			g(a, i)
		i -= 1
	l1 = [len(l)] + l
	print(*l1)


def op(st, l):
	ans = ''
	for i in range(l):
		if st[i] == '1':
			ans += '0'
		else:
			ans += '1'
	ans = ans[::-1] + st[l:]
	return ans
for i in range(int(input())):
	n = int(input())
	b = input()
	a = input()
	count = 0
	ans = []
	for i in range(n):
		if a[i] == b[n - i - 1]:
			if b[0] == a[i]:
				ans.append(n - i)
				b = op(b, n - i)
			else:
				ans.append(1)
				b = op(b, 1)
				ans.append(n - i)
				b = op(b, n - i)
	if a != b:
		ans.append(n)
	print(len(ans), *ans)


n = int(input())
ret = [0] * 3 * 1000
for i in range(n):
	l = int(input())
	a = [int(p) for p in list(input())]
	b = [int(p) for p in list(input())]
	i = l - 1
	c = 0
	while i >= 0:
		if a[i] != b[i]:
			if a[0] == 1 - b[i]:
				a[:i + 1] = list(map(lambda x: 1 - x, a[:i + 1]))[::-1]
				ret[c] = i + 1
				c += 1
			else:
				a[0] = 1 - a[0]
				ret[c] = 1
				c += 1
				a[:i + 1] = list(map(lambda x: 1 - x, a[:i + 1]))[::-1]
				ret[c] = i + 1
				c += 1
		i -= 1
	print(c, ' '.join(map(str, ret[:c])))


import sys
sys.setrecursionlimit(10000)
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(input())
	b = list(input())
	prefs = [0]
	while len(a) > 0:
		if a[-1] == b[-1]:
			a = a[:-1]
			b = b[:-1]
			continue
		if a[0] != b[-1]:
			prefs.append(len(a))
			c = [str(1 - int(i)) for i in a]
			c.reverse()
			a = c.copy()
		else:
			prefs.append(1)
			a[0] = str(1 - int(a[0]))
	prefs[0] = len(prefs) - 1
	print(*prefs)


from functools import lru_cache
from sys import stdin, stdout
import sys
from math import *
for __ in range(int(input())):
	n = int(input())
	a = list(input())
	b = list(input())
	ans = []
	x = 0
	y = n - 1
	while y >= 0:
		if a[y] == b[y]:
			y -= 1
		elif a[x] != a[y]:
			ans.append(1)
			if a[x] == '0':
				a[x] = '1'
			else:
				a[x] = '0'
		else:
			ans.append(y + 1)
			for i in range(y + 1):
				if a[i] == '1':
					a[i] = '0'
				else:
					a[i] = '1'
			a = a[:y + 1][::-1] + a[y + 1:]
	if len(ans) == 0:
		print(0)
	else:
		print(len(ans), end=' ')
		print(*ans)


from sys import stdin, stdout
import math
t = int(stdin.readline())
for _ in range(t):
	n = int(stdin.readline())
	a = stdin.readline().strip()
	b = stdin.readline().strip()
	ans = []
	i = 1
	while i < n:
		if a[i] != a[i - 1]:
			ans.append(i)
		i += 1
	if a[-1] != b[-1]:
		ans.append(n)
	bans = []
	i = 1
	while i < n:
		if b[i] != b[i - 1]:
			bans.append(i)
		i += 1
	ans = ans + bans[::-1]
	print(' '.join(map(str, [len(ans)] + ans)))


import sys
input = sys.stdin.readline

def inv(s):
	s1 = ''
	for i in s[::-1]:
		if i == '1':
			s1 += '0'
		else:
			s1 += '1'
	return s1

def main(n, a, b):
	path = []
	x = 0
	while a != '':
		if a[0] == b[-1]:
			path.append(1)
			a = inv(a[0]) + a[1:]
		a = inv(a)
		path.append(n - x)
		x += 1
		a = a[:-1]
		b = b[:-1]
	path = list(map(str, path))
	return str(len(path)) + ' ' + ' '.join(path)
for _ in range(int(input())):
	n = int(input())
	a = input()[:-1]
	b = input()[:-1]
	print(main(n, a, b))


t = int(input())
for hh in range(0, t):
	n = int(input())
	a = input()
	b = input()
	a = list(a)
	b = list(b)
	ans = []
	for i in range(n - 1, 0, -1):
		if a[i] == b[i]:
			continue
		else:
			if a[i] != a[0]:
				ans.append(1)
				a[0] = str(1 - int(a[0]))
			ans.append(i + 1)
			a[0:i + 1] = reversed(a[0:i + 1])
			for j in range(0, i + 1):
				a[j] = str(1 - int(a[j]))
	if a[0] != b[0]:
		ans.append(1)
	print(len(ans), end=' ')
	print(*ans)


for _ in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	i = 1
	s = a[0]
	ans = []
	while i < n:
		if a[i] == s[-1]:
			i += 1
			continue
		elif a[i] == '1' and s[-1] == '0':
			s = '1'
			ans.append(i)
		else:
			s = '0'
			ans.append(i)
		i += 1
	if s[-1] == '1':
		ans.append(n)
	i = 1
	s = b[0]
	ans2 = []
	while i < n:
		if b[i] == s[-1]:
			i += 1
			continue
		elif b[i] == '1' and s[-1] == '0':
			s = '1'
			ans2.append(i)
		else:
			s = '0'
			ans2.append(i)
		i += 1
	if s[-1] == '1':
		ans2.append(n)
	l = ans + ans2[::-1]
	print(len(l), *l)


cases = int(input())
for t in range(cases):
	n = int(input())
	a = list(map(int, list(input())))
	b = list(map(int, list(input())))
	c = 0
	s = []
	for i in range(n):
		if a[n - 1 - i] != b[n - 1 - i]:
			if a[0] == b[n - 1 - i]:
				c += 1
				a[0] ^= 1
				s.append(1)
			c += 1
			s.append(n - i)
			a = [j ^ 1 for j in a[:n - i]][::-1]
	if c == 0:
		print(c)
	else:
		print(c, *s)


import sys
input = sys.stdin.readline

def f(s, k):
	for j in range(k + 1):
		if s[j] == '0':
			s[j] = '1'
		else:
			s[j] = '0'
	r = s[:k + 1]
	r = r[::-1]
	return r + s[k + 1:]
for t in range(int(input())):
	n = int(input())
	a = list(input().strip())
	b = list(input().strip())
	if a == b:
		print(0)
	else:
		k = 0
		ans = []
		for i in range(n - 1, -1, -1):
			if a[i] == b[i]:
				continue
			elif a[0] != b[i]:
				k += 1
				a = f(a, i)
				ans.append(i + 1)
			else:
				k += 3
				a = f(a, i)
				ans.append(i + 1)
				a = f(a, 0)
				ans.append(1)
				a = f(a, i)
				ans.append(i + 1)
		a = ''.join(a)
		a = a.strip()
		b = ''.join(b)
		b = b.strip()
		print(k, *ans)


def solution():
	n = int(input())
	a = list(map(int, list(input())))
	b = list(map(int, list(input())))
	if a == b:
		print(0)
		return
	answer = []
	last_zero = a[0] == 0
	for (i, ai) in enumerate(a):
		if ai == 0 and (not last_zero):
			answer.append(str(i))
		elif ai == 1 and last_zero:
			answer.append(str(i))
		last_zero = ai == 0
	for i in range(n - 1, -1, -1):
		if b[i] == 0 and (not last_zero):
			answer.append(str(i + 1))
		elif b[i] == 1 and last_zero:
			answer.append(str(i + 1))
		last_zero = b[i] == 0
	print(str(len(answer)) + ' ' + ' '.join(answer))
t = int(input())
for _ in range(t):
	solution()


import sys
import collections

def input():
	return sys.stdin.readline().rstrip()

def split_input():
	return [int(i) for i in input().split()]
tests = int(input())

def flip(s):
	if s == '1':
		return '0'
	else:
		return '1'
for _ in range(tests):
	n = int(input())
	a = input()
	b = input()
	picked = 0
	ans = []
	for i in range(n - 1, -1, -1):
		if picked % 2 == 0:
			num = a[picked // 2]
		else:
			num = flip(a[n - 1 - picked // 2])
		if num == b[i]:
			ans.append(1)
		ans.append(i + 1)
		picked += 1
	print('%d ' % len(ans), end='')
	print(*ans, sep=' ')


from __future__ import division, print_function
import math
import os
import sys
from fractions import *
from sys import *
from decimal import *
from io import BytesIO, IOBase
from itertools import *
from collections import *
M = 10 ** 9 + 7
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def inp():
	return sys.stdin.readline().rstrip('\r\n')

def out(var):
	sys.stdout.write(str(var))

def lis():
	return list(map(int, inp().split()))

def stringlis():
	return list(map(str, inp().split()))

def sep():
	return map(int, inp().split())

def strsep():
	return map(str, inp().split())

def fsep():
	return map(float, inp().split())

def inpu():
	return int(inp())

def regularbracket(t):
	p = 0
	for i in t:
		if i == '(':
			p += 1
		else:
			p -= 1
		if p < 0:
			return False
	else:
		if p > 0:
			return False
		else:
			return True

def binarySearchCount(arr, n, key):
	left = 0
	right = n - 1
	count = 0
	while left <= right:
		mid = int((right + left) / 2)
		if arr[mid] <= key:
			count = mid + 1
			left = mid + 1
		else:
			right = mid - 1
	return count

def reverse1(string):
	pp = ''
	for i in string[::-1]:
		pp += i
	if pp == string:
		return True
	return False

def reverse2(list1):
	l = []
	for i in list1[::-1]:
		l.append(i)
	if l == list1:
		return True
	return False

def mex(list1):
	p = max(list1) + 1
	for i in range(len(list1)):
		if list1[i] != i:
			p = i
			break
	return p

def sumofdigits(n):
	n = str(n)
	s1 = 0
	for i in n:
		s1 += int(i)
	return s1

def perfect_square(n):
	s = math.sqrt(n)
	if s == int(s):
		return True
	return False

def roman_number(x):
	if x > 15999:
		return
	value = [5000, 4000, 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]
	symbol = ['F', 'MF', 'M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I']
	roman = ''
	i = 0
	while x > 0:
		div = x // value[i]
		x = x % value[i]
		while div:
			roman += symbol[i]
			div -= 1
		i += 1
	return roman

def soretd(s):
	for i in range(1, len(s)):
		if s[i - 1] > s[i]:
			return False
	return True

def countRhombi(h, w):
	ct = 0
	for i in range(2, h + 1, 2):
		for j in range(2, w + 1, 2):
			ct += (h - i + 1) * (w - j + 1)
	return ct

def countrhombi2(h, w):
	return h * h // 4 * (w * w // 4)

def binpow(a, b):
	if b == 0:
		return 1
	else:
		res = binpow(a, b // 2)
	if b % 2 != 0:
		return res * res * a
	else:
		return res * res

def binpowmodulus(a, b, m):
	a %= m
	res = 1
	while b > 0:
		if b & 1:
			res = res * a % m
		a = a * a % m
		b >>= 1
	return res

def coprime_to_n(n):
	result = n
	i = 2
	while i * i <= n:
		if n % i == 0:
			while n % i == 0:
				n //= i
			result -= result // i
		i += 1
	if n > 1:
		result -= result // n
	return result

def prime(x):
	if x == 1:
		return False
	else:
		for i in range(2, int(math.sqrt(x)) + 1):
			if x % i == 0:
				return False
		else:
			return True

def luckynumwithequalnumberoffourandseven(x, n, a):
	if x >= n and str(x).count('4') == str(x).count('7'):
		a.append(x)
	elif x < 1000000000000.0:
		luckynumwithequalnumberoffourandseven(x * 10 + 4, n, a)
		luckynumwithequalnumberoffourandseven(x * 10 + 7, n, a)
	return a

def luckynum(x, l, r, a):
	if x >= l and x <= r:
		a.append(x)
	if x > r:
		a.append(x)
		return a
	if x < 10000000000.0:
		luckynum(x * 10 + 4, l, r, a)
		luckynum(x * 10 + 7, l, r, a)
	return a

def luckynuber(x, n, a):
	p = set(str(x))
	if len(p) <= 2:
		a.append(x)
	if x < n:
		luckynuber(x + 1, n, a)
	return a

def interact(type, x):
	if type == 'r':
		inp = input()
		return inp.strip()
	else:
		print(x, flush=True)

def findTrailingZeros(n):
	count = 0
	while n >= 5:
		n //= 5
		count += n
	return count

def mergeSort(arr):
	if len(arr) > 1:
		mid = len(arr) // 2
		L = arr[:mid]
		R = arr[mid:]
		mergeSort(L)
		mergeSort(R)
		i = j = k = 0
		while i < len(L) and j < len(R):
			if L[i] < R[j]:
				arr[k] = L[i]
				i += 1
			else:
				arr[k] = R[j]
				j += 1
			k += 1
		while i < len(L):
			arr[k] = L[i]
			i += 1
			k += 1
		while j < len(R):
			arr[k] = R[j]
			j += 1
			k += 1
res = set()

def solven(p, l, a, b, n):
	if p > n or l > 10:
		return
	if p > 0:
		res.add(p)
	solven(p * 10 + a, l + 1, a, b, n)
	solven(p * 10 + b, l + 1, a, b, n)

def subsetsUtil(A, subset, index, d):
	print(*subset)
	s = sum(subset)
	d.append(s)
	for i in range(index, len(A)):
		subset.append(A[i])
		subsetsUtil(A, subset, i + 1, d)
		subset.pop(-1)
	return d

def subsetSums(arr, l, r, d, sum=0):
	if l > r:
		d.append(sum)
		return
	subsetSums(arr, l + 1, r, d, sum + arr[l])
	subsetSums(arr, l + 1, r, d, sum)
	return d

def print_factors(x):
	factors = []
	for i in range(1, x + 1):
		if x % i == 0:
			factors.append(i)
	return factors

def calc(X, d, ans, D):
	if len(X) == 0:
		return
	i = X.index(max(X))
	ans[D[max(X)]] = d
	Y = X[:i]
	Z = X[i + 1:]
	calc(Y, d + 1, ans, D)
	calc(Z, d + 1, ans, D)

def factorization(n, l):
	c = n
	if prime(n) == True:
		l.append(n)
		return l
	for i in range(2, c):
		if n == 1:
			break
		while n % i == 0:
			l.append(i)
			n = n // i
	return l

def good(b):
	l = []
	i = 0
	while len(b) != 0:
		if b[i] < b[len(b) - 1 - i]:
			l.append(b[i])
			b.remove(b[i])
		else:
			l.append(b[len(b) - 1 - i])
			b.remove(b[len(b) - 1 - i])
	if l == sorted(l):
		return True
	return False

def generate(st, s):
	if len(s) == 0:
		return
	if s not in st:
		st.add(s)
		for i in range(len(s)):
			t = list(s).copy()
			t.remove(s[i])
			t = ''.join(t)
			generate(st, t)
	return

def largestincreasingsubsequence(A):
	l = [1] * len(A)
	sub = []
	for i in range(1, len(l)):
		for k in range(i):
			if A[k] < A[i]:
				sub.append(l[k])
		l[i] = 1 + max(sub, default=0)
	return max(l, default=0)

def findOR(nums, N):
	prefix_sum = 0
	result = 0
	for i in range(N):
		result |= nums[i]
		prefix_sum += nums[i]
		result |= prefix_sum
	return result

def OR(a, n):
	ans = a[0]
	for i in range(1, n):
		ans |= a[i]
	return ans

def toString(List):
	return ''.join(List)
p = []

def permute(a, l, r):
	if l == r:
		p.append(toString(a))
	else:
		for i in range(l, r + 1):
			(a[l], a[i]) = (a[i], a[l])
			permute(a, l + 1, r)
			(a[l], a[i]) = (a[i], a[l])

def squareRoot(number, precision):
	start = 0
	(end, ans) = (number, 1)
	while start <= end:
		mid = int((start + end) / 2)
		if mid * mid == number:
			ans = mid
			break
		if mid * mid < number:
			start = mid + 1
		else:
			end = mid - 1
	increment = 0.1
	for i in range(0, precision):
		while ans * ans <= number:
			ans += increment
		ans = ans - increment
		increment = increment / 10
	return ans
import math

def countRectangles(l, w):
	squareSide = math.gcd(l, w)
	return int(l * w / (squareSide * squareSide))

def count_same_digit(L, R):
	tmp = 0
	ans = 0
	n = int(math.log10(R) + 1)
	for i in range(0, n):
		tmp = tmp * 10 + 1
		for j in range(1, 10):
			if L <= tmp * j and tmp * j <= R:
				ans += 1
	return ans

def main():
	for _ in range(inpu()):
		n = inpu()
		(a, b) = (inp(), inp())
		l = []
		for i in range(n):
			if a[i] != b[i]:
				l += [i + 1, 1, i + 1]
		print(len(l), *l)
main()


import os
from sys import stdin, stdout

def solve(tc):
	n = int(stdin.readline().strip())
	seq = list(map(int, stdin.readline().strip()))
	obj = list(map(int, stdin.readline().strip()))
	hi = n - 1
	while hi >= 0 and seq[hi] == obj[hi]:
		hi -= 1
	ans = []
	while hi >= 0:
		if seq[hi] != obj[hi]:
			if hi > 0 and seq[0] == obj[hi]:
				ans.append(1)
				seq[0] ^= 1
			ans.append(hi + 1)
			for i in range(hi + 1):
				seq[i] ^= 1
			l = 0
			r = hi
			while l < r:
				(seq[l], seq[r]) = (seq[r], seq[l])
				l += 1
				r -= 1
		hi -= 1
	print(len(ans), *ans)
tcs = 1
tcs = int(stdin.readline().strip())
tc = 1
while tc <= tcs:
	solve(tc)
	tc += 1


t = int(input())
for _ in range(t):
	n = int(input())
	a = input()
	b = input()
	c = 0
	v = []
	for i in range(n):
		if a[i] != b[i]:
			c += 3
			v.append(str(i + 1))
			v.append('1')
			v.append(str(i + 1))
	v = [str(c)] + v
	print(' '.join(v))


for _ in range(int(input())):
	n = int(input())
	a = [int(i) for i in input()]
	b = [int(i) for i in input()]
	operations = []
	ind = -1
	while ind != -(n + 1):
		if a[ind] == b[ind]:
			ind -= 1
		else:
			if a[ind] != a[0]:
				a[0] = a[ind]
				operations.append(1)
			ind_of_a = 0
			while ind_of_a <= n + ind:
				a[ind_of_a] = 1 if a[ind_of_a] != 1 else 0
				ind_of_a += 1
			operations.append(n + ind + 1)
			nexus = a[:n + ind + 1]
			nexus.reverse()
			for (ind_of_a, el) in enumerate(nexus):
				a[ind_of_a] = el
			ind -= 1
	print(len(operations), *operations)


for _ in range(int(input())):
	n = int(input())
	a = list(str(input()))
	b = list(str(input()))
	(l, s) = (0, '')
	for i in range(n):
		if a[i] != b[i]:
			l += 3
			s += str(i + 1) + ' 1 ' + str(i + 1) + ' '
	print(l, s)


import math

def last_char(last_index):
	char = arr1[last_index]
	reqd_first = char
	if arr1[0] == reqd_first:
		return -1
	else:
		return 1

def perform_operation(last_index):
	i = 0
	j = last_index
	while i <= j:
		(arr1[i], arr1[j]) = (1 - arr1[j], 1 - arr1[i])
		i += 1
		j -= 1
num_cases = int(input())
for vvv in range(num_cases):
	n = int(input())
	arr1 = input().strip()
	arr1 = [int(elem) for elem in arr1]
	arr2 = input().strip()
	arr2 = [int(elem) for elem in arr2]
	ans = []
	for i in range(n - 1, -1, -1):
		if arr1[i] == arr2[i]:
			continue
		else:
			idx = last_char(i)
			if idx == 1:
				ans.append(0)
				perform_operation(0)
			ans.append(i)
			perform_operation(i)
	print(len(ans), end=' ')
	for elem in ans:
		print(elem + 1, end=' ')
	print()


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input()))
	b = list(map(int, input()))
	ans = 0
	string = ''
	k = len(a) - 1
	while a != b:
		for i in range(k, 0 - 1, -1):
			if a[i] != b[i]:
				k = i
				break
		if a[0] == b[k]:
			ans += 1
			if a[0] == 0:
				a[0] = 1
			else:
				a[0] = 0
			string += '1 '
		ans += 1
		string += str(k + 1) + ' '
		for i in range(k + 1):
			if a[i] == 0:
				a[i] = 1
			else:
				a[i] = 0
		a = a[:k + 1][::-1] + a[k + 1:]
	print(str(ans) + ' ' + string)


def solution():
	for _ in range(int(input())):
		n = int(input())
		a = list(input())
		b = list(input())
		a += ['0']
		b += ['0']
		ans = []
		rev = []
		x = '0'
		for i in range(1, n + 1):
			if a[i] != a[i - 1]:
				ans.append(i)
		for i in range(1, n + 1):
			if b[i] != b[i - 1]:
				rev.append(i)
		rev.reverse()
		print(*[len(ans) + len(rev)] + ans + rev)
solution()


for i in range(int(input())):
	am = int(input())
	a = list(input())
	b = list(input())
	for i in range(am):
		a[i] = bool(int(a[i]))
		b[i] = bool(int(b[i]))
	nums = 0
	out = []
	for i in range(am - 1, 0, -1):
		if a[i] == b[i]:
			continue
		if a[i] != a[0]:
			nums += 1
			out.append(1)
			a[0] = not a[0]
		nums += 1
		out.append(i + 1)
		was = [False] * (i + 1)
		for g in range(i + 1):
			was[g] = a[g]
		for g in range(i + 1):
			a[g] = not was[i - g]
	if a[0] != b[0]:
		nums += 1
		out.append(1)
		a[0] = not a[0]
	print(nums, *out)


def readints():
	return [int(e) for e in input()]

def main():
	n = int(input())
	a = [int(e) for e in input()]
	b = [int(e) for e in input()]
	ans = []
	for i in reversed(range(n)):
		if a[i] == b[i]:
			continue
		if a[0] == b[i]:
			a[0] ^= 1
			ans.append(1)
		for j in range(i + 1):
			a[j] ^= 1
		a[:i + 1] = a[:i + 1][::-1]
		ans.append(i + 1)
	print(len(ans), end=' ')
	print(*ans, sep=' ')
tests = int(input())
for _ in range(tests):
	main()


def flip(s, x):
	k = ''
	for i in range(x):
		if s[i] == '1':
			k += '0'
		else:
			k += '1'
	return k[::-1] + s[x:]
for t in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	pos = n - 1
	moves = []
	while pos != -1:
		if a[pos] == b[pos]:
			pos -= 1
		elif a[0] != b[pos]:
			moves += [str(pos + 1)]
			a = flip(a, pos + 1)
		else:
			moves += ['1', str(pos + 1)]
			a = flip(a, 1)
			a = flip(a, pos + 1)
	print(len(moves), ' '.join(moves))


from sys import stdin

def iinput():
	return int(stdin.readline())

def minput():
	return map(int, stdin.readline().split())

def linput():
	return list(map(int, stdin.readline().split()))
t = iinput()
while t:
	t -= 1
	n = iinput()
	a = input()
	b = input()
	(op1, op2) = ([], [])
	for i in range(1, n):
		if a[i] != a[i - 1]:
			op1.append(i)
		if b[i] != b[i - 1]:
			op2.append(i)
	if a[-1] != b[-1]:
		op1.append(n)
	op1.extend(reversed(op2))
	print(len(op1), *op1)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(input())
	b = list(input())
	for i in range(n):
		(a[i], b[i]) = (int(a[i]), int(b[i]))
	asol = []
	bsol = []
	for i in range(n - 1):
		if a[i] != a[i + 1]:
			asol += [i + 1]
		if b[i] != b[i + 1]:
			bsol += [i + 1]
	if a[-1] != b[-1]:
		asol += [n]
	bsol.reverse()
	ans = asol + bsol
	print(len(ans), end=' ')
	for i in ans:
		print(i, end=' ')
	print()


t = int(input())
cnt = 0
while cnt < t:
	cnt += 1
	n = int(input())
	a = [int(i) for i in input()]
	b = [int(i) for i in input()]
	res = []
	for i in range(n):
		if i % 2 == 0:
			flag = 1
		else:
			flag = 0
		if flag:
			if a[i // 2] == b[n - 1 - i]:
				res.append(1)
			res.append(n - i)
		else:
			if (a[n - 1 - i // 2] + 1) % 2 == b[n - 1 - i]:
				res.append(1)
			res.append(n - i)
	print(len(res), end=' ')
	if res:
		print(' '.join((str(q) for q in res)))
	else:
		print()


for _ in range(int(input())):
	piles = int(input())
	string1 = list(input())
	string2 = list(input())
	rev = 0
	pos = 0
	end = piles - 1
	operations = []
	for i in range(piles - 1, -1, -1):
		if rev == 0:
			if string1[pos] == string2[i]:
				operations.append(1)
			operations.append(i + 1)
			pos += 1
			rev = 1
		else:
			if string1[end] != string2[i]:
				operations.append(1)
			operations.append(i + 1)
			end -= 1
			rev = 0
	print(len(operations), end=' ')
	print(*operations)


for tt in range(int(input())):
	n = int(input())
	s = input()
	t = input()
	if s == t:
		print(0)
	else:
		c = 0
		for i in range(n):
			if s[i] != t[i]:
				c += 1
		print(3 * c, end=' ')
		for i in range(n):
			if s[i] != t[i]:
				print(i + 1, 1, i + 1, end=' ')
		print()


import sys
input = sys.stdin.readline
t = int(input())
letters = 'abcdefghijklmnopqrst'
for _ in range(t):
	l = int(input())
	a = input().rstrip()
	b = input().rstrip()
	moves = []
	c = 0
	for i in range(l):
		if a[i] == b[i]:
			pass
		else:
			moves.append(i + 1)
			moves.append(1)
			moves.append(i + 1)
			c += 3
	moves = [c] + moves
	print(*moves)


import io, os
R = lambda : map(int, input().split())
for _ in range(int(input())):
	n = int(input())
	a = input() + '0'
	b = input() + '0'
	(ad, bd) = ([], [])
	for i in range(1, n + 1):
		if a[i] != a[i - 1]:
			ad.append(i)
		if b[i] != b[i - 1]:
			bd.append(i)
	ad += bd[::-1]
	print(len(ad), *ad)


for _ in range(int(input())):
	n = int(input())
	a = list(str(input()))
	b = list(str(input()))
	res = []
	for i in range(n):
		if a[i] != b[i]:
			res += [i + 1, 1, i + 1]
	print(len(res), *res)


import sys
from collections import defaultdict as dd
from collections import deque
from fractions import Fraction as f
from copy import *
from bisect import *
from heapq import *
from math import *
from itertools import permutations

def eprint(*args):
	print(*args, file=sys.stderr)
zz = 1
if zz:
	input = sys.stdin.readline
else:
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('all.txt', 'w')

def li():
	return [int(xx) for xx in input().split()]

def fli():
	return [float(x) for x in input().split()]

def comp(a, b):
	if a > b:
		return 2
	return 2 if a == b else 0

def gi():
	return [xx for x in input().split()]

def fi():
	return int(input())

def swap(a, i, j):
	(a[i], a[j]) = (a[j], a[i])

def si():
	return list(input().rstrip())

def mi():
	return map(int, input().split())

def gh():
	sys.stdout.flush()

def graph(n, m):
	for i in range(m):
		(x, y) = mi()
		a[x].append(y)
		a[y].append(x)

def bo(i):
	return ord(i) - ord('a')
t = fi()
while t > 0:
	t -= 1
	n = fi()
	s = si()
	l = si()
	for i in range(n):
		s[i] = int(s[i])
		l[i] = int(l[i])
	d = []
	for i in range(n):
		if i % 2 == 0:
			if l[n - i - 1] == s[i // 2]:
				d.append(1)
				s[i // 2] ^= 1
			d.append(n - i)
		else:
			s[n - (i + 1) // 2] ^= 1
			if l[n - i - 1] == s[n - (i + 1) // 2]:
				d.append(1)
				s[n - (i + 1) // 2] ^= 1
			d.append(n - i)
	print(len(d), *d)


T = int(input())
for _ in range(T):
	n = int(input())
	a = input().strip()
	b = input().strip()
	if a == b:
		print(0)
		continue
	moves = []
	(i, j) = (0, n - 1)
	curr_i = -1
	while i < n and j >= 0:
		if i % 2 == 0:
			curr_i = a[i // 2]
		else:
			curr_i = str(1 - int(a[n - 1 - i // 2]))
		if curr_i == b[j]:
			moves.append(1)
		moves.append(j + 1)
		i += 1
		j -= 1
	print(' '.join(map(str, [len(moves)] + moves)))


import sys, math

def change(a, index):
	b = ''
	for i in range(0, index + 1):
		if a[i] == '0':
			b += '1'
		else:
			b += '0'
	b = ''.join(reversed(b))
	b += a[index + 1:]
	return b
t = int(input())
for _ in range(t):
	n = int(input())
	a = input()
	b = input()
	ans = []
	for i in range(n - 1, -1, -1):
		if a[i] != b[i]:
			if a[i] == a[0]:
				ans.append(i + 1)
				a = change(a, i)
			else:
				ans.append(1)
				a = change(a, 0)
				ans.append(i + 1)
				a = change(a, i)
	print(len(ans), end=' ')
	for i in ans:
		print(i, end=' ')
	print()


test = int(input())
k = 1
while k <= test:
	k += 1
	n = int(input())
	a = input()
	b = input()
	op1 = []
	op2 = []
	for i in range(0, len(a) - 1):
		if a[i] != a[i + 1]:
			op1.append(i)
	if a[len(a) - 1] == '1':
		op1.append(len(a) - 1)
	for i in range(0, len(b) - 1):
		if b[i] != b[i + 1]:
			op2.append(i)
	if b[len(b) - 1] == '1':
		op2.append(len(b) - 1)
	print(len(op1) + len(op2), end=' ')
	for i in op1:
		print(i + 1, end=' ')
	for i in op2[::-1]:
		print(i + 1, end=' ')
	print()


def solution(s1, s2):
	n = len(s1)
	temp = s1
	ans = []
	for i in range(len(s1)):
		if s1[i] != s2[i]:
			ans.append(i + 1)
			ans.append(1)
			ans.append(i + 1)
	return ans
for _ in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	ans = solution(a, b)
	print(len(ans), end=' ')
	print(*ans)


a = int(input())
for i in range(a):
	s = int(input())
	a1 = input()
	a2 = input()
	ans1 = []
	ans2 = []
	for i in range(len(a1)):
		ans1.append(int(a1[i]))
	for i in range(len(a2)):
		ans2.append(int(a2[i]))
	fin = []
	for i in range(len(a2) - 1, -1, -1):
		if ans2[i] == ans1[i]:
			continue
		elif ans1[0] != ans2[i]:
			fin.append(i + 1)
			temp = []
			for j in range(i, -1, -1):
				temp.append(1 - ans1[j])
			for j in range(i + 1):
				ans1[j] = temp[j]
		else:
			fin.append(1)
			ans1[0] = 1 - ans1[0]
			fin.append(i + 1)
			temp = []
			for j in range(i, -1, -1):
				temp.append(1 - ans1[j])
			for j in range(i + 1):
				ans1[j] = temp[j]
	print(len(fin), end=' ')
	print(*fin)


def change(a, i, n):
	s = ''
	for j in range(i):
		s += '0' if a[j] == '1' else '1'
	s = s[::-1]
	for j in range(i, n):
		s += a[j]
	return [i for i in s]
for _ in range(int(input())):
	n = int(input())
	a = [i for i in input()]
	b = input()
	k = 0
	ans = []
	for i in range(n - 1, -1, -1):
		if a[i] == b[i]:
			continue
		elif a[0] != b[i]:
			k += 1
			a = change(a, i + 1, n)
			ans += [i + 1]
		else:
			k += 2
			a = change(a, 1, n)
			a = change(a, i + 1, n)
			ans += [1, i + 1]
	print(*[k] + ans)


import sys
INP = lambda : sys.stdin.readline().strip()
INT = lambda : int(INP())
MAP = lambda : map(int, INP().split())
ARR = lambda : [int(i) for i in INP().split()]

def JOIN(arr, x=' '):
	return x.join([str(i) for i in arr])

def EXIT(x='NO'):
	print(x)
	exit()
for _ in range(INT()):
	n = INT()
	a = [int(i) for i in list(INP())]
	b = [int(i) for i in list(INP())]
	ans = []
	for i in reversed(range(n)):
		if b[i] == a[i]:
			continue
		if b[i] == a[0]:
			ans.append(1)
		ans.append(i + 1)
		a = [1 - x for x in reversed(a[:i + 1])] + a[i + 1:]
	print(JOIN([len(ans)] + ans))


def operation(s, l):
	str1 = ''
	for j in range(l):
		if s[j] == '1':
			str1 += '0'
		else:
			str1 += '1'
	str2 = ''
	for i in range(1, l + 1):
		str2 += str1[-i]
	s = str2 + s[l:]
	return s
t = int(input())
for i in range(t):
	n = int(input())
	a = str(input())
	b = str(input())
	l = len(a)
	ans = []
	while l > 0 and b[-1] == a[-1]:
		b = b[:l - 1]
		a = a[:l - 1]
		l -= 1
	while l > 0:
		if b[-1] == '0':
			if a[0] == '1':
				a = operation(a, l)
				ans.append(l)
				while l > 0 and b[-1] == a[-1]:
					b = b[:l - 1]
					a = a[:l - 1]
					l -= 1
			else:
				a = operation(a, 1)
				ans.append(1)
				while l > 0 and b[-1] == a[-1]:
					b = b[:l - 1]
					a = a[:l - 1]
					l -= 1
		elif a[0] == '0':
			a = operation(a, l)
			ans.append(l)
			while l > 0 and b[-1] == a[-1]:
				b = b[:l - 1]
				a = a[:l - 1]
				l -= 1
		else:
			a = operation(a, 1)
			ans.append(1)
			while l > 0 and b[-1] == a[-1]:
				b = b[:l - 1]
				a = a[:l - 1]
				l -= 1
	k = len(ans)
	lis = [k]
	for j in range(k):
		lis.append(ans[j])
	print(' '.join((str(x) for x in lis)))


from sys import stdin, stdout
import math
import heapq
t = 1

def aint():
	return int(input().strip())

def lint():
	return list(map(int, input().split()))

def fint():
	return list(map(int, stdin.readline().split()))

def main():
	n = aint()
	s1 = input()
	s2 = input()
	cnt = 0
	ans = []
	(s, e) = (0, n - 1)
	for i in range(n - 1, -1, -1):
		if cnt % 2 == 0:
			(b1, b2) = (s1[s] == '1', s2[i] == '1')
			s += 1
		else:
			(b1, b2) = (s1[e] == '0', s2[i] == '1')
			e -= 1
		if b1 == b2:
			ans.append(1)
		ans.append(i + 1)
		cnt += 1
	print(len(ans), *ans)
t = int(input())
for i in range(t):
	main()


import sys
input = sys.stdin.readline

def commute(arr, n, pos):
	for i in range(pos):
		if arr[i] == '1':
			arr[i] = '0'
		else:
			arr[i] = '1'
	arr[0:pos] = arr[0:pos][::-1]
	return arr
for _ in range(int(input())):
	n = int(input())
	a = list(input())[:n]
	b = list(input())[:n]
	ans = []
	for i in range(n - 1, -1, -1):
		if b[i] != a[i]:
			if a[0] != a[i]:
				ans.append(1)
				a[0] = a[i]
			ans.append(i + 1)
			a = commute(a, n, i + 1)
	print(len(ans), ' '.join(map(str, ans)))


for _ in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	ans1 = []
	for i in range(n - 1):
		if a[i] != a[i + 1]:
			ans1.append(i + 1)
	if a[-1] == '1':
		ans1.append(n)
	ans2 = []
	for i in range(n - 1):
		if b[i] != b[i + 1]:
			ans2.append(i + 1)
	if b[-1] == '1':
		ans2.append(n)
	ans1 += ans2[::-1]
	print(len(ans1), *ans1)


from os import path
import sys
from math import ceil, floor, gcd, log
from collections import defaultdict, Counter
maxx = float('inf')
if path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
	I = lambda : int(sys.stdin.buffer.readline())
	tup = lambda : map(int, sys.stdin.buffer.readline().split())
	lint = lambda : [int(x) for x in sys.stdin.buffer.readline().split()]
	S = lambda : sys.stdin.readline().replace('\n', '').strip()
	stpr = lambda x: sys.stdout.write(f'{x}' + '\n')
	star = lambda x: print(' '.join(map(str, x)))
else:
	I = lambda : int(sys.stdin.buffer.readline())
	tup = lambda : map(int, sys.stdin.buffer.readline().split())
	lint = lambda : [int(x) for x in sys.stdin.buffer.readline().split()]
	S = lambda : sys.stdin.readline().replace('\n', '').strip()
	stpr = lambda x: sys.stdout.write(f'{x}' + '\n')
	star = lambda x: print(' '.join(map(str, x)))
for _ in range(int(input())):
	n = int(input())
	a = input().strip()
	b = input().strip()
	if a == b:
		print(0)
		continue
	pre = []
	for i in range(n):
		if a[i] != b[i]:
			if i > 0:
				pre.append(i + 1)
			pre.append(1)
			if i > 0:
				pre.append(i + 1)
	print(len(pre), *pre)


from sys import stdin
t = int(stdin.readline().strip())
for _ in range(t):
	n = int(stdin.readline().strip())
	a = stdin.readline().strip()
	b = stdin.readline().strip()
	out = []
	for i in range(n - 1):
		if a[i] != a[i + 1]:
			out.append(i + 1)
	current = a[-1]
	for i in range(n - 1, -1, -1):
		if b[i] != current:
			out.append(i + 1)
			current = '0' if current == '1' else '1'
	print(len(out), end=' ')
	print(*out)


for _ in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	res = []
	for i in range(n):
		if a[i] != b[i]:
			c = list(a)[:i + 1]
			for j in range(len(c)):
				if c[j] == '1':
					c[j] = '0'
				else:
					c[j] = '1'
			c.reverse()
			res.append(str(i + 1))
			res.append('1')
			res.append(str(i + 1))
			if c[0] == '1':
				c[0] = '0'
			else:
				c[0] = '1'
			for j in range(len(c)):
				if c[j] == '1':
					c[j] = '0'
				else:
					c[j] = '1'
			c.reverse()
			c = ''.join(c)
			a = a[i + 1:]
			a = c + a
	print(end='\n')
	print(len(res), ' '.join(res))


t = int(input())
for i2 in range(t):
	n = int(input())
	l1 = input()
	l2 = input()
	res = []
	for i in range(n):
		if l1[i] != l2[i]:
			res += [i + 1, 1, i + 1]
	print(len(res), *res)


from sys import stdin, setrecursionlimit, stdout
setrecursionlimit(100)
from collections import deque
from math import sqrt, floor, ceil, log, log2, log10, pi, gcd, sin, cos, asin
from heapq import heapify, heappop, heappush, heapreplace, heappushpop
from bisect import bisect_right, bisect_left

def ii():
	return int(stdin.readline())

def fi():
	return float(stdin.readline())

def mi():
	return map(int, stdin.readline().split())

def fmi():
	return map(float, stdin.readline().split())

def li():
	return list(mi())

def si():
	return stdin.readline().rstrip()

def lsi():
	return list(si())
mod = 1000000007
res = ['NO', 'YES']
test = 1
test_case = ii()
while test <= test_case:
	test += 1
	n = ii()
	a = lsi()
	b = lsi()
	x = k = 0
	ans = []
	i = j = n - 1
	for j in range(n - 1, -1, -1):
		if not x and a[i] == b[j] or (x and a[i] != b[j]):
			if x:
				i += 1
			else:
				i -= 1
		elif x:
			if a[k] != b[j]:
				ans.append(1)
			ans.append(j + 1)
			x = 0
			(i, k) = (k - 1, i)
		else:
			if a[k] == b[j]:
				ans.append(1)
			ans.append(j + 1)
			x = 1
			(i, k) = (k + 1, i)
	print(len(ans), *ans)


def fun(a, x):
	temp = []
	for i in range(x + 1):
		temp.append(a[i])
	for i in range(x + 1):
		a[i] = 1 - temp[x - i]
	return a
for _ in range(int(input())):
	n = int(input())
	a1 = input()
	b1 = input()
	a = [int(i) for i in a1]
	b = [int(i) for i in b1]
	mover = n - 1
	out = []
	while mover >= 0:
		if a[mover] == b[mover]:
			mover -= 1
			continue
		if a[0] != b[mover]:
			out.append(mover + 1)
			a = fun(a, mover)
			mover -= 1
			continue
		out.append(1)
		a[0] = 1 - a[0]
		out.append(mover + 1)
		a = fun(a, mover)
		mover -= 1
	print(len(out), end=' ')
	for i in out:
		print(i, end=' ')
	print()


ll = lambda : list(map(int, input().split()))
st = lambda : input()
v = lambda : map(int, input().split())
ii = lambda : int(input())
from collections import *
from math import *
from datetime import datetime
from sys import stdin, stdout
for t in range(int(input())):
	n = int(input())
	s = st()
	s1 = st()
	if s1 == s:
		print(0)
		continue
	l = []
	c = 0
	d = s[0]
	for i in range(len(s) - 1):
		if s[i] != s[i + 1]:
			c += 1
			d = s[i + 1]
			l.append(i + 1)
	l1 = []
	c1 = 0
	d1 = s1[0]
	for i in range(len(s) - 1):
		if s1[i] != s1[i + 1]:
			c1 += 1
			d1 = s1[i + 1]
			l1.append(i + 1)
	if d1 != d:
		l1.append(n)
	ans = l + l1[::-1]
	print(len(ans), *ans)


from sys import stdin, stdout

def main():
	t = int(stdin.readline())
	for case in range(t):
		n = int(stdin.readline())
		a = stdin.readline()
		b = stdin.readline()
		k = 0
		seq = []
		for i in range(1, n + 1):
			if a[i - 1] != b[i - 1]:
				k += 3
				seq.append(i)
				seq.append(1)
				seq.append(i)
		stdout.write(str(k))
		stdout.write(' ')
		stdout.write(' '.join(map(str, seq)))
		stdout.write('\n')
	return
main()


import random

def gcd(a, b):
	if a == 0:
		return b
	return gcd(b % a, a)

def lcm(a, b):
	return a * b / gcd(a, b)
for _ in range(int(input())):
	n = int(input())
	A = input()
	B = input()
	a = []
	b = []
	for i in range(n):
		if A[i] == '1':
			a.append(1)
		else:
			a.append(0)
		if B[i] == '1':
			b.append(1)
		else:
			b.append(0)
	r = n - 1
	ans = []
	while r > 0:
		if a[r] != b[r]:
			if a[0] == a[r]:
				ans.append(r + 1)
				m = []
				for i in range(r + 1):
					m.append(a[i] ^ 1)
				m.reverse()
				for i in range(r + 1):
					a[i] = m[i]
			else:
				find = 0
				while a[0] != a[find]:
					find += 1
				ans.append(find + 1)
				m = []
				for i in range(find + 1):
					m.append(a[i] ^ 1)
				m.reverse()
				for i in range(find + 1):
					a[i] = m[i]
				ans.append(r + 1)
				m = []
				for i in range(r + 1):
					m.append(a[i] ^ 1)
				m.reverse()
				for i in range(r + 1):
					a[i] = m[i]
		r -= 1
	if r == 0 and a[0] != b[0]:
		ans.append(1)
	print(len(ans), end=' ')
	for i in ans:
		print(i, end=' ')
	print('')


import bisect
import os
import gc
import sys
from io import BytesIO, IOBase
from collections import Counter
from collections import deque
import heapq
import math
import statistics

def sin():
	return input()

def ain():
	return list(map(int, sin().split()))

def sain():
	return input().split()

def iin():
	return int(sin())
MAX = float('inf')
MIN = float('-inf')
MOD = 1000000007

def sieve(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	s = set()
	for p in range(2, n + 1):
		if prime[p]:
			s.add(p)
	return s

def readTree(n, m):
	adj = [deque([]) for _ in range(n + 1)]
	for _ in range(m):
		(u, v) = ain()
		adj[u].append(v)
		adj[v].append(u)
	return adj

def invert(x):
	if x == '1':
		return '0'
	else:
		return '1'

def main():
	for i in range(iin()):
		n = iin()
		a = deque(sin())
		b = deque(sin())
		ans = deque([])
		i = n - 1
		while i >= 0:
			if b[i] == a[i]:
				i -= 1
			elif b[i] == a[0]:
				ans.append(1)
				a[0] = invert(a[0])
			else:
				ans.append(i + 1)
				j = 0
				k = i
				while j <= k:
					(a[j], a[k]) = (invert(a[k]), invert(a[j]))
					j += 1
					k -= 1
				i -= 1
		print(len(ans), *ans)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


import sys
ii = lambda : sys.stdin.readline().strip()
idata = lambda : [int(x) for x in ii().split()]

def solve():
	n = int(ii())
	data_a = ii()
	data_b = ii()
	ans = []
	for i in range(n - 1):
		if data_a[i] != data_a[i + 1]:
			ans += [i + 1]
	if data_a[-1] == '1':
		ans += [n]
	d = 0
	for i in range(n - 1, -1, -1):
		if d % 2 != int(data_b[i]):
			d = (d + 1) % 2
			ans += [i + 1]
	print(len(ans), *ans)
	return
for t in range(int(ii())):
	solve()


from collections import defaultdict as dd
import sys
input = sys.stdin.readline
t = int(input())
while t:
	n = int(input())
	a = list(map(int, input().strip()))
	b = list(map(int, input().strip()))
	j = n - 1
	ans = []
	while j >= 0:
		while j >= 0 and b[j] == a[j]:
			j -= 1
		if j >= 0 and a[0] == b[j]:
			a[0] = 1 - a[0]
			ans.append(1)
		if j % 2 == 1:
			k = j // 2 + 1
		else:
			k = j // 2
			if j >= 0 and j % 2 == 0:
				a[j // 2] = 1 - a[j // 2]
		for i in range(k):
			(a[i], a[j - i]) = (1 - a[j - i], 1 - a[i])
		if j >= 0:
			ans.append(j + 1)
		j -= 1
	print(len(ans), *ans)
	t -= 1


t = int(input())
for j in range(t):
	n = int(input())
	s = input()
	s1 = input()
	l = []
	count = 0
	if s == s1:
		print(0)
	else:
		for i in range(n):
			if s[i] != s1[i]:
				l.append(i + 1)
				l.append(1)
				l.append(i + 1)
				count += 1
		print(3 * count, end=' ')
		print(*l, sep=' ')


from __future__ import division, print_function
from sys import stdin, stdout
import bisect
import math
import heapq
i_m = 9223372036854775807

def inin():
	return int(input())

def stin():
	return input()

def spin():
	return map(int, stin().split())

def lin():
	return list(map(int, stin().split()))

def matrix(n):
	return [list(map(int, input().split())) for i in range(n)]

def count2Dmatrix(i, list):
	return sum((c.count(i) for c in list))

def modinv(n, p):
	return pow(n, p - 2, p)

def GCD(x, y):
	x = abs(x)
	y = abs(y)
	if min(x, y) == 0:
		return max(x, y)
	while y:
		(x, y) = (y, x % y)
	return x

def Divisors(n):
	l = []
	for i in range(1, int(math.sqrt(n) + 1)):
		if n % i == 0:
			if n // i == i:
				l.append(i)
			else:
				l.append(i)
				l.append(n // i)
	return l
prime = []

def SieveOfEratosthenes(n):
	global prime
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	f = []
	for p in range(2, n):
		if prime[p]:
			f.append(p)
	return f
q = []

def dfs(n, d, v, c):
	global q
	v[n] = 1
	x = d[n]
	q.append(n)
	j = c
	for i in x:
		if i not in v:
			f = dfs(i, d, v, c + 1)
			j = max(j, f)
	return j
for _ in range(inin()):
	n = inin()
	a = list(stin())
	b = list(stin())
	c = 0
	l = []
	for i in range(n):
		if a[i] != b[i]:
			l += [i + 1, 1, i + 1]
			c += 3
	print(c, *l)


for t in range(int(input())):
	n = int(input())
	a = str(input())
	b = str(input())
	steps = []
	for i in range(n):
		if a[i] != b[i]:
			steps += [i + 1, 1, i + 1]
	print(len(steps), *steps)


def fun(a, k):
	l = ''
	for i in range(k):
		if a[i] == '1':
			l = l + '0'
		else:
			l = l + '1'
	return l[::-1]
t = int(input())
for testcase in range(t):
	n = int(input())
	ans = []
	a = input()
	b = input()
	j = n - 1
	while j > 0:
		while j > 0 and a[j] == b[j]:
			j = j - 1
		if a[0] == b[j]:
			ans.append(1)
			a = fun(a, 1) + a[1:]
			ans.append(j + 1)
			a = fun(a, j + 1) + a[j + 1:]
		else:
			ans.append(j + 1)
			a = fun(a, j + 1) + a[j + 1:]
	if a[0] != b[0]:
		ans.append(1)
	print(len(ans), *ans)


from sys import stdin
import sys
tt = int(input())
for loop in range(tt):
	n = int(input())
	a = input()
	b = input()
	if n == None:
		if a[0] == b[0]:
			print(0)
		else:
			print(1, 1)
		sys.exit()
	ind = [None] * n
	tmp = 0
	for i in range(n - 1, -1, -2):
		ind[i] = tmp
		tmp += 1
	if n % 2 == 0:
		for i in range(0, n, 2):
			ind[i] = tmp
			tmp += 1
	else:
		for i in range(1, n, 2):
			ind[i] = tmp
			tmp += 1
	ans = []
	x = 0
	for i in range(n - 1, -1, -1):
		if x == 0 and b[i] == a[ind[i]]:
			ans.append(1)
		elif x == 1 and b[i] != a[ind[i]]:
			ans.append(1)
		ans.append(i + 1)
		x ^= 1
	print(len(ans), *ans)


import math

def rewrite(arr, r):
	for i in range(r // 2):
		arr[i] = (arr[i] + 1) % 2
		arr[r - i] = (arr[r - i] + 1) % 2
	if r % 2 == 0:
		arr[r // 2] = (arr[r // 2] + 1) % 2
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(str(input()))
	b = list(str(input()))
	for i in range(n):
		a[i] = int(a[i])
		b[i] = int(b[i])
	r = []
	for j in range(n - 1, -1, -1):
		if a[j] == b[j]:
			pass
		else:
			if a[j] != a[0]:
				r.append(1)
				a[0] = (a[0] + 1) % 2
			r.append(j + 1)
			rr = j * 1
			for i in range(rr // 2 + 1):
				a[i] = (a[i] + 1) % 2
				a[rr - i] = (a[rr - i] + 1) % 2
				(a[i], a[rr - i]) = (a[rr - i], a[i])
			if rr % 2 == 0:
				a[rr // 2] = (a[rr // 2] + 1) % 2
	print(len(r), end=' ')
	for i in r:
		print(i, end=' ')
	print()


for _ in range(int(input())):
	length = int(input())
	a = list(input())
	b = list(input())
	ans = 0
	count = length - 1
	c_list = []
	while count != -1:
		if b[count] != a[count]:
			if a[count] == a[0]:
				c_list.append(count)
				ans += 1
				c = []
				for n in range(count + 1):
					c.append(str(1 - int(a[count - n])))
				a = c + a[count + 1:]
			else:
				c_list.append(0)
				c_list.append(count)
				ans += 2
				c = []
				for n in range(count):
					c.append(str(1 - int(a[count - n])))
				c.append(b[count])
				a = c + a[count + 1:]
		else:
			count -= 1
	print(ans, end=' ')
	for num in range(len(c_list)):
		print(c_list[num] + 1, end=' ')
	print()


import sys
sys.setrecursionlimit(2147483647)

def calc(arr):
	(t, a, b) = arr
	ops = []
	for i in range(1, t + 1):
		if not a[t - i] == b[t - i]:
			if a[0] != b[t - i]:
				ops.append(t - i + 1)
				a[:t - i + 1] = a[:t - i + 1][::-1]
				for j in range(t - i):
					a[j] = (a[j] + 1) % 2
			else:
				ops.append(1)
				a[0] = (a[0] + 1) % 2
				ops.append(t - i + 1)
				a[:t - i + 1] = a[:t - i + 1][::-1]
				for j in range(t - i):
					a[j] = (a[j] + 1) % 2
	return [len(ops), *ops]

def main():
	n = int(input())
	arr = []
	for _ in range(n):
		t = int(input())
		a = list(map(int, list(input())))
		b = list(map(int, list(input())))
		arr.append([t, a, b])
	for i in range(n):
		print(' '.join(map(str, calc(arr[i]))))
main()


import sys
import math
import collections
import operator as op
from collections import deque
from math import gcd, inf, sqrt, pi, cos, sin, ceil, log2
from bisect import bisect_right, bisect_left
from functools import reduce
from sys import stdin, stdout, setrecursionlimit
setrecursionlimit(2 ** 20)

def factorial(n):
	if n == 0:
		return 1
	return n * factorial(n - 1)

def ncr(n, r):
	r = min(r, n - r)
	numer = reduce(op.mul, range(n, n - r, -1), 1)
	denom = reduce(op.mul, range(1, r + 1), 1)
	return numer // denom

def prime_factors(n):
	i = 2
	factors = []
	while i * i <= n:
		if n % i:
			i += 1
		else:
			n //= i
			factors.append(i)
	if n > 1:
		factors.append(n)
	return list(factors)

def isPowerOfTwo(x):
	return x and (not x & x - 1)

def factors(n):
	return list(set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0))))
MOD = 1000000007
PMOD = 998244353
T = 1
T = int(stdin.readline())
for _ in range(T):
	n = int(stdin.readline())
	a = list(stdin.readline().strip('\n'))
	b = list(stdin.readline().strip('\n'))
	c = a[0]
	ans = []
	for i in range(1, n):
		if a[i] != a[i - 1]:
			ans.append(i)
			c = a[i]
	for i in range(n - 1, -1, -1):
		if b[i] != c:
			ans.append(i + 1)
			c = b[i]
	print(len(ans), *ans)


t = int(input())

def change(s1, i):
	schange = ''
	k = len(s1)
	for j in range(i + 1):
		if s1[j] == '0':
			schange += '1'
		else:
			schange += '0'
	sresult = ''
	for j in range(i, -1, -1):
		sresult += schange[j]
	for j in range(len(schange), k, 1):
		sresult += s1[j]
	return sresult
for q in range(t):
	n = int(input())
	s1 = str(input())
	s2 = str(input())
	ans = []
	for i in range(n):
		if s1[i] != s2[i]:
			s1 = change(s1, i)
			ans.append(i + 1)
			s1 = change(s1, 0)
			ans.append(1)
			s1 = change(s1, i)
			ans.append(i + 1)
	print(len(ans), end=' ')
	for i in range(len(ans)):
		print(ans[i], end=' ')
	print()


import sys

def invertflip(lst):
	lst.reverse()
	for i in range(len(lst)):
		if lst[i] == '1':
			lst[i] = '0'
		else:
			lst[i] = '1'
	return lst

def answer(n, a, b):
	steps = 0
	rptr = n - 1
	len_ary = []
	a = list(a)
	b = list(b)
	while rptr >= 0:
		if a[rptr] == b[rptr]:
			rptr -= 1
			continue
		if a[0] != b[rptr]:
			a[0:rptr + 1] = invertflip(a[0:rptr + 1])
			len_ary.append(rptr + 1)
			steps += 1
			rptr -= 1
			continue
		a[0:1] = invertflip(a[0:1])
		len_ary.append(1)
		steps += 1
	ans = [steps]
	ans.extend(len_ary)
	return ans

def main():
	t = int(input())
	while t:
		n = int(sys.stdin.readline().rstrip())
		a = sys.stdin.readline().rstrip()
		b = sys.stdin.readline().rstrip()
		print(' '.join(map(str, answer(n, a, b))))
		t -= 1
	return
main()


from sys import stdin
input = lambda : stdin.readline().rstrip('\r\n')
from collections import deque as que, defaultdict as vector
from heapq import *
inin = lambda : int(input())
inar = lambda : list(map(int, input().split()))
Testcase = inin()
for _ in range(Testcase):
	n = inin()
	a = list(input())
	b = list(input())
	a = [int(x) for x in a] + [0]
	b = [int(x) for x in b] + [0]
	aw = []
	bw = []
	for i in range(n):
		if a[i] != a[i + 1]:
			aw.append(i + 1)
		if b[i] != b[i + 1]:
			bw.append(i + 1)
	print(len(aw) + len(bw), *aw, *bw[::-1])


for _ in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	o = []
	p = []
	l = []
	for i in range(n):
		o.append(a[i])
		p.append(b[i])
	for i in range(n - 1, -1, -1):
		if o[i] == p[i]:
			continue
		if o[0] == p[i]:
			l.append(1)
			if o[0] == '0':
				o[0] = '1'
			else:
				o[0] = '0'
		l.append(i + 1)
		for j in range(i + 1):
			if o[j] == '1':
				o[j] = '0'
			else:
				o[j] = '1'
		for j in range((i + 1) // 2):
			t = o[j]
			o[j] = o[i - j]
			o[i - j] = t
	print(len(l), end=' ')
	for k in range(len(l)):
		print(l[k], end=' ')
	print()


import sys
input = sys.stdin.readline
from collections import *

def pair(i):
	return n - 1 - i
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input()[:-1]))
	b = list(map(int, input()[:-1]))
	ope = []
	for i in range(n - 1, -1, -1):
		if a[0] ^ b[i] == 1:
			ope.append(i)
		else:
			ope.append(0)
			ope.append(i)
		c = n - 1 - i
		if c % 2 == 0:
			a[0] = 1 ^ a[n - c // 2 - 1]
		else:
			a[0] = a[(c + 1) // 2]
	print(len(ope), *list(map(lambda x: x + 1, ope)))


def chng(c):
	if c == '1':
		return '0'
	else:
		return '1'
for _ in range(int(input())):
	n = int(input().strip())
	sa = list(input().strip())
	sb = list(input().strip())
	flip = 0
	ft = 1
	bc = n
	arr = []
	while ft < bc:
		arr.append(ft)
		arr.append(bc)
		ft += 1
		bc -= 1
	if ft == bc:
		arr.append(ft)
	j = n
	ans = []
	for a in arr:
		j -= 1
		if flip % 2 == 0:
			if sa[a - 1] == sb[j]:
				if j == 0:
					break
				ans.append(1)
				ans.append(j + 1)
			else:
				ans.append(j + 1)
		elif sa[a - 1] != sb[j]:
			if j == 0:
				break
			ans.append(1)
			ans.append(j + 1)
		else:
			ans.append(j + 1)
		flip += 1
	print(len(ans), *ans)


for _ in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	ans = []
	for i in range(n - 1, -1, -1):
		if a[i] == b[i]:
			pass
		elif a[0] == b[i]:
			ans.append(1)
			if a[0] == '1':
				a = '0' + a[1:]
			else:
				a = '1' + a[1:]
			s1 = ''
			for j in range(0, i + 1):
				if a[j] == '1':
					s1 += '0'
				else:
					s1 += '1'
			if i == n - 1:
				a = s1[::-1]
			else:
				a = s1[::-1] + a[i + 1:]
			ans.append(i + 1)
		else:
			s1 = ''
			for j in range(0, i + 1):
				if a[j] == '1':
					s1 += '0'
				else:
					s1 += '1'
			if i == n - 1:
				a = s1[::-1]
			else:
				a = s1[::-1] + a[i + 1:]
			ans.append(i + 1)
	print(len(ans), *ans)


for _ in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	ans = []
	if a == b:
		print(0)
		continue
	for i in range(0, n - 1):
		if a[i] != a[i + 1]:
			ans.append(i + 1)
	if a[n - 1] != '0':
		ans.append(n)
	ansrev = []
	for i in range(0, n - 1):
		if b[i] != b[i + 1]:
			ansrev.append(i + 1)
	if b[n - 1] != '0':
		ansrev.append(n)
	ansrev = ansrev[::-1]
	ans.extend(ansrev)
	print(len(ans), *ans)


t = int(input())

def _flip(arr, prefix, prefixes):
	prefixes.append(prefix + 1)
	tmp = arr[:prefix + 1]
	tmp.reverse()
	for (z, _) in enumerate(tmp):
		tmp[z] = 1 - tmp[z]
	arr[:prefix + 1] = tmp

def main():
	n = int(input())
	a = [int(x) for x in list(input())]
	b = [int(x) for x in list(input())]
	prefixes = []
	j = len(a) - 1
	while True:
		while a[j] == b[j]:
			j -= 1
			if j == -1:
				print(len(prefixes), *prefixes)
				return
		i = 0
		if a[i] == b[j - i]:
			_flip(a, 0, prefixes)
		_flip(a, j, prefixes)
for _ in range(t):
	main()


for _ in range(int(input())):
	n = int(input())
	s1 = list(input())
	s2 = list(input())
	l = []
	for i in range(n):
		s1[i] = int(s1[i])
		s2[i] = int(s2[i])
	for i in range(n - 1, 0, -1):
		if s1[i] != s2[i]:
			if s1[0] == s2[i]:
				l.append(1)
				if s1[0] == 1:
					s1[0] = 0
				else:
					s1[0] = 1
			l.append(i + 1)
			for j in range(i + 1):
				if s1[j] == 1:
					s1[j] = 0
				else:
					s1[j] = 1
			s1 = s1[:i + 1][::-1] + s1[i + 1:]
	if s1[0] != s2[0]:
		l.append(1)
		s1[0] = s2[0]
	if l == []:
		print(0)
	else:
		print(len(l), *l)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def main():
	for t in range(int(input())):
		n = int(input())
		a = [int(j) for j in list(input())]
		b = [int(j) for j in list(input())]
		ans = []
		r = n - 1
		flag = False
		idx = 0
		for i in range(n):
			ori = idx
			for j in range(r, -1, -1):
				pdx = 0
				if flag:
					k = 1 - a[idx - j]
					pdx = idx - j
				else:
					k = a[idx + j]
					pdx = idx + j
				if k != b[j]:
					r = j - 1
					idx = pdx
					flag = not flag
					break
			else:
				break
			st = 0
			if flag:
				st = 1 - a[ori]
			else:
				st = a[ori]
			if b[j] == st:
				ans.append(j + 1)
			else:
				ans.append(1)
				ans.append(j + 1)
		if len(ans) == 0:
			print(0)
		else:
			print(len(ans), end=' ')
			for i in range(len(ans)):
				print(ans[i], end=' ')
			print()
main()


for _ in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	a1 = list(a)
	b1 = list(b)
	t = 0
	x = []
	j = n - 1
	while j >= 0:
		if a1[j] == b1[j]:
			j -= 1
		elif int(a1[0]) ^ 1 == int(b1[j]):
			t += 1
			x.append(j + 1)
			for i in range(n):
				a1[i] = str(int(a1[i]) ^ 1)
			for k in range((j + 1) // 2):
				(a1[k], a1[j - k]) = (a1[j - k], a1[k])
			j -= 1
		else:
			t += 2
			x.append(1)
			a1[0] = str(int(a1[0]) ^ 1)
			x.append(j + 1)
			for i in range(n):
				a1[i] = str(int(a1[i]) ^ 1)
			for k in range((j + 1) // 2):
				(a1[k], a1[j - k]) = (a1[j - k], a1[k])
			j -= 1
	print(t, end=' ')
	print(*x)


import sys
input = sys.stdin.readline
T = int(input())
for _ in range(T):
	n = int(input())
	a = input()
	b = input()
	scheck = []
	pos = 0
	pos1 = n - 1
	reverse = 0
	for j in range(n - 1, -1, -1):
		if not reverse:
			if a[pos] != b[j]:
				scheck.append(j + 1)
			else:
				scheck.append(1)
				scheck.append(j + 1)
			pos += 1
			reverse = 1
		else:
			if a[pos1] != b[j]:
				scheck.append(1)
				scheck.append(j + 1)
			else:
				scheck.append(j + 1)
			pos1 -= 1
			reverse = 0
	print(len(scheck), *scheck)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def RL():
	return map(int, sys.stdin.readline().rstrip().split())

def RLL():
	return list(map(int, sys.stdin.readline().rstrip().split()))

def N():
	return int(input())

def comb(n, m):
	return factorial(n) / (factorial(m) * factorial(n - m)) if n >= m else 0

def perm(n, m):
	return factorial(n) // factorial(n - m) if n >= m else 0

def mdis(x1, y1, x2, y2):
	return abs(x1 - x2) + abs(y1 - y2)
mod = 998244353
INF = float('inf')
from math import factorial
from collections import Counter, defaultdict, deque
from heapq import heapify, heappop, heappush

def main():
	for _ in range(N()):
		n = N()
		a = list(input())
		b = list(input())
		res = []

		def c(ind):
			for i in range(ind):
				a[i] = str(int(str(a[i])) ^ 1)
			a[:ind] = a[:ind][::-1]
		for i in range(n - 1, -1, -1):
			if a[i] == b[i]:
				continue
			if a[0] == b[i]:
				res.append(1)
				c(1)
				res.append(i + 1)
				c(i + 1)
			else:
				res.append(i + 1)
				c(i + 1)
		print(len(res), *res)
main()


n = int(input())
for i in range(n):
	n = int(input())
	l = list(input())
	l1 = list(input())
	c = 0
	ml = []
	ind = n - 1
	while 1:
		if l == l1:
			break
		if l[ind] != l1[ind]:
			l2 = l[:ind + 1]
			for j in range(ind + 1):
				if l2[j] == '0':
					l2[j] = '1'
				else:
					l2[j] = '0'
			l2 = l2[::-1]
			l = l2 + l[ind + 1:]
			c += 1
			ml.append(ind + 1)
			if l == l1:
				break
			if l[ind] != l1[ind]:
				if l[0] == '1':
					l[0] = '0'
				else:
					l[0] = '1'
				c += 1
				ml.append(1)
		else:
			ind -= 1
	if ml == []:
		print(0)
	else:
		print(len(ml), end=' ')
		print(' '.join((str(x) for x in ml)))


from sys import stdin
import math
inp = lambda : stdin.readline().strip()
t = int(inp())
for _ in range(t):
	n = int(inp())
	a = inp()
	b = inp()
	eq = []
	fin = []
	if n == 1:
		if a == b:
			print(0)
		else:
			print('1 1')
	else:
		for i in range(n - 1):
			if a[i] != a[i + 1]:
				eq.append(i + 1)
		for i in range(n - 2, -1, -1):
			if b[i] != b[i + 1]:
				fin.append(i + 1)
		if a[-1] != b[-1]:
			eq.append(n)
		print(len(eq) + len(fin), end=' ')
		print(*eq, end=' ')
		print(*fin)


def chg(kk):
	if kk == '0':
		return '1'
	return '0'
T = int(input())
for _ in range(0, T):
	n = int(input())
	A = input()
	B = input()
	a = []
	b = []
	for i in range(n):
		a.append(A[i])
		b.append(B[i])
	ans = []
	for i in range(len(b) - 1, -1, -1):
		if b[i] != a[i]:
			trg = b[i]
			flag = 0
			for j in range(0, i + 1):
				if a[j] == trg:
					ans.append(j + 1)
					ans.append(i + 1)
					temp = []
					for k in range(j, -1, -1):
						temp.append(chg(a[k]))
					for k in range(0, j):
						a[k] = temp[k]
					temp = []
					for k in range(i, -1, -1):
						temp.append(chg(a[k]))
					for k in range(0, i):
						a[k] = temp[k]
					flag = 1
					break
			if flag == 0:
				ans.append(i + 1)
				temp = []
				for k in range(i, -1, -1):
					temp.append(chg(a[k]))
				for k in range(0, j):
					a[k] = temp[k]
	print(len(ans), *ans)


from sys import *
input = stdin.readline
t = int(input())

def convert(a):
	x = ''
	for i in a:
		if i == '0':
			x += '1'
		else:
			x += '0'
	return x[::-1]
for _ in range(t):
	n = int(input())
	a = input()
	b = input()
	a = a.strip()
	b = b.strip()
	l = []
	for i in range(n - 1, -1, -1):
		c = 0
		if a[i] != b[i]:
			if b[i] == a[0]:
				c = 1
				l.append(c)
				if a[0] == '1':
					s = '0'
				else:
					s = '1'
				a = s + a[1:]
				c = i + 1
				l.append(c)
				s = convert(a[:i + 1])
				a = s + a[i + 1:]
			else:
				c = i + 1
				l.append(c)
				s = convert(a[:i + 1])
				a = s + a[i + 1:]
	print(len(l), end=' ')
	for i in l:
		print(i, end=' ')
	print('')


rr = lambda : input()
rri = lambda : int(input())
rrm = lambda : list(map(int, input().split()))
INF = float('inf')

def verify(steps, A, B):
	print('verify')
	for (j, s) in enumerate(steps):
		s = int(s)
		a = A[:s]
		for i in range(s):
			a[i] ^= 1
		a = a[::-1]
		for i in range(s):
			A[i] = a[i]
	for (v1, v2) in zip(A, B):
		if v1 != v2:
			return False
	return True

def solve(N, A, B):
	if A == B:
		print(0)
		return
	A = list(map(int, list(A)))
	B = list(map(int, list(B)))
	C = A.copy()
	lo = 0
	hi = len(A) - 1
	steps = []
	while lo <= hi:
		if A[hi] == B[hi]:
			pass
		else:
			steps.append(str(hi + 1))
			steps.append(str(lo + 1))
			steps.append(str(hi + 1))
		hi -= 1
	print(len(steps), ' '.join(steps))
t = rri()
for _ in range(t):
	N = rri()
	A = rr()
	B = rr()
	solve(N, A, B)


def d(x, m):
	y = x[0:m]
	for i in range(m):
		if y[i] == 0:
			y[i] = 1
			x[m - i - 1] = y[i]
		else:
			y[i] = 0
			x[m - i - 1] = y[i]
	return x
t = int(input())
l = []
for i in range(t):
	n = int(input())
	a = list(map(int, list(input())))
	b = list(map(int, list(input())))
	s = str()
	k = n - 1
	count = 0
	while k != -1:
		if a == b:
			break
		elif a[k] == b[k]:
			k = k - 1
		elif a[0] == b[k]:
			a = d(a, 1)
			s = s + ' ' + '1'
			count = count + 1
			a = d(a, k + 1)
			s = s + ' ' + f'{k + 1}'
			count = count + 1
			k = k - 1
		else:
			a = d(a, k + 1)
			s = s + ' ' + f'{k + 1}'
			k = k - 1
			count = count + 1
	s = f'{count}' + '' + s
	l.append(s)
for i in l:
	print(i)


import sys
import math, bisect
from collections import defaultdict
from itertools import groupby, accumulate
from heapq import heapify, heappop, heappush
from collections import deque, Counter, OrderedDict
input = iter(sys.stdin.buffer.read().decode().splitlines()).__next__

def neo():
	return map(int, input().split())

def Neo():
	return list(map(int, input().split()))
for _ in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	if a == b:
		print(0)
		continue
	moves = []
	(i, j) = (0, n - 1)
	curr_i = -1
	while i < n and j >= 0:
		if i % 2 == 0:
			curr_i = a[i // 2]
		else:
			curr_i = str(1 - int(a[n - 1 - i // 2]))
		if curr_i == b[j]:
			moves.append(1)
		moves.append(j + 1)
		i += 1
		j -= 1
	print(len(moves), *moves)


for _ in range(int(input())):
	n = int(input())
	l = input()
	ans = []
	s = input()
	c = 0
	for i in range(n - 1, -1, -1):
		if l[i] != s[i]:
			ans.append(i + 1)
			ans.append(1)
			ans.append(i + 1)
			c += 3
	print(c, end=' ')
	for i in ans:
		print(i, end=' ')
	print()


for _ in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	a = [bool(int(i)) for i in a]
	b = [bool(int(i)) for i in b]
	sol = []
	while True:
		if len(a) == 0:
			break
		if a[-1] == b[-1]:
			a = a[:-1]
			b = b[:-1]
		elif a[0] == a[-1]:
			sol.append(len(a))
			a = [not i for i in a]
			a.reverse()
		else:
			sol.append(1)
			a[0] = not a[0]
	print(len(sol), ' '.join([str(i) for i in sol]))


for _ in range(int(input())):
	N = int(input())
	a = input()
	b = input()
	if a == b:
		print(0)
	else:
		store = []
		for i in range(N - 1, -1, -1):
			if a[i] == b[i]:
				pass
			elif b[i] != a[0]:
				store.append(i + 1)
				k = ''
				k = a[0:i + 1]
				k = k.replace('1', '2').replace('0', '1').replace('2', '0')
				a = k[::-1] + a[i + 1:]
			else:
				store.append(1)
				if a[0] == '0':
					a = '1' + a[1:]
				else:
					a = '0' + a[1:]
				store.append(i + 1)
				k = ''
				k = a[0:i + 1]
				k = k.replace('1', '2').replace('0', '1').replace('2', '0')
				a = k[::-1] + a[i + 1:]
		print(len(store), *store)


import sys
import math
import bisect
from sys import stdin, stdout
from math import gcd, floor, sqrt, log2, ceil
from collections import defaultdict
from bisect import bisect_left as bl, bisect_right as br
from collections import Counter
from collections import deque
ip = lambda : int(stdin.readline())
inp = lambda : map(int, stdin.readline().split())
ips = lambda : stdin.readline().rstrip()
t = ip()
for _ in range(t):
	n = ip()
	a = ips()
	b = ips()
	ch1 = []
	ch2 = []
	a += '0'
	b += '0'
	for i in range(1, n + 1):
		if a[i - 1] != a[i]:
			ch1.append(i)
		if b[i - 1] != b[i]:
			ch2.append(i)
	ch2 = ch2[::-1]
	ans = ch1 + ch2
	ans = [len(ans)] + ans
	print(*ans)


for _ in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	steps = []
	i = n - 1
	while i >= 0:
		cap = i + 1
		count = 0
		single = False
		if a[i] != b[i]:
			count = 1
			j = i - 1
			while j > 0 and a[j] == a[i] and (b[j] == b[i]):
				count += 1
				j -= 1
		if count > 0:
			if not single:
				steps.append(cap)
				steps.append(count)
				steps.append(cap)
			else:
				steps.append(cap)
			i = i - count + 1
		i -= 1
	print(len(steps), end='')
	for i in range(len(steps)):
		print(' ' + str(steps[i]), end='')
	print()


rp = ''
for _ in ' ' * int(input()):
	a = int(input())
	b = list(map(int, input().replace('', ' ').split()))
	c = list(map(int, input().replace('', ' ').split()))
	z = []
	for i in range(a - 1, -1, -1):
		if b[i] != c[i]:
			r = []
			if b[0] == c[i]:
				z += [1]
				b[0] = b[0] ^ 1
			for j in range(i + 1):
				r += [b[j] ^ 1]
			r = r[::-1]
			b[:i + 1] = r.copy()
			z += [i + 1]
	rp += str(len(z)) + ' ' + ' '.join(map(str, z)) + '\n'
print(rp)


T = int(input())
for _ in range(T):
	n = int(input())
	f = 0
	a = input()
	b = input()
	a = list(a)
	b = list(b)
	z = []
	for i in range(n):
		if a[i] != b[i] and i == 0:
			f = f + 1
			z.append(1)
		elif i != 0 and a[i] != b[i]:
			z.append(i + 1)
			f = f + 1
			z.append(1)
			f = f + 1
			z.append(i + 1)
			f = f + i + 1
	print(len(z), *z)


import sys
input = sys.stdin.readline
t = int(input())
for tt in range(t):
	n = int(input())
	a1 = input().strip('\n')
	b1 = input().strip('\n')
	ans = []
	a = []
	b = []
	for i in a1:
		a.append(int(i))
	for i in b1:
		b.append(int(i))
	for i in range(n - 1, -1, -1):
		if a[0] == b[i]:
			a[0] ^= 1
			ans.append(1)
		ans.append(i + 1)
		temp = a[0:i + 1]
		temp = temp[::-1]
		length = len(temp)
		for j in range(length):
			temp[j] ^= 1
		for j in range(i + 1):
			a[j] = temp[j]
	print(len(ans), end=' ')
	for i in ans:
		print(i, end=' ')
	print()


for _ in range(int(input())):
	n = int(input())
	s = input()
	b = input()
	ans = []
	for i in range(n - 1, -1, -1):
		if s[i] != b[i] and i == 0:
			ans.append(1)
		elif i > 0 and s[i] != b[i]:
			ans.append(i + 1)
			ans.append(1)
			ans.append(i + 1)
	print(len(ans), *ans)


def get_end(t, a, end, front):
	return a[end] if t else a[front]

def shrink(t, end, front):
	if t:
		end -= 1
	else:
		front += 1
	return (end, front)
for _ in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	a = [bool(int(i)) for i in a]
	b = [bool(int(i)) for i in b]
	sol = []
	(end, front) = (n - 1, 0)
	t = True
	while end >= front:
		size = end - front + 1
		if (get_end(t, a, end, front) == b[size - 1]) == t:
			(end, front) = shrink(t, end, front)
		else:
			if a[front] != a[end]:
				sol.append(1)
				if t:
					a[front] = not a[front]
				else:
					a[end] = not a[end]
			sol.append(size)
			t = not t
	print(len(sol), ' '.join([str(i) for i in sol]))


from _collections import deque
for _ in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	c = []
	ans = 0
	for i in range(1, n):
		if a[i - 1] is not a[i]:
			c.append(i)
			ans += 1
	if a[-1] is not b[-1]:
		c.append(n)
		ans += 1
	for i in range(n - 2, -1, -1):
		if b[i] is not b[i + 1]:
			c.append(i + 1)
			ans += 1
	print(ans, end=' ')
	for i in c:
		print(i, end=' ')
	print()


for _ in range(int(input())):
	n = int(input())
	a = str(input())
	b = str(input())
	ans = []
	i = n - 1
	while i >= 0:
		if int(a[i]) != int(b[i]):
			if int(a[0]) != int(b[i]):
				ans.append(i + 1)
			else:
				ans.append(1)
				a = b[i] + a[1:]
				ans.append(i + 1)
			for j in range(i + 1):
				if a[j] == '1':
					a = a[:j] + '0' + a[j + 1:]
				else:
					a = a[:j] + '1' + a[j + 1:]
			a = a[:i + 1][::-1] + a[i + 1:]
		i = i - 1
	print(len(ans), *ans, sep=' ')


import sys, io, os, time
from collections import defaultdict
from collections import OrderedDict
from collections import deque
from itertools import combinations
from itertools import permutations
import bisect, math, heapq
alphabet = 'abcdefghijklmnopqrstuvwxyz'
input = sys.stdin.readline
for ii in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	ans1 = []
	for i in range(1, n):
		if a[i] != a[i - 1]:
			ans1.append(i)
	if a[n - 1] != b[n - 1]:
		ans1.append(n)
	ans2 = []
	for i in range(1, n):
		if b[i] != b[i - 1]:
			ans2.append(i)
	ans2 = list(reversed(ans2))
	print(len(ans1) + len(ans2), *ans1, *ans2)


from sys import stdin
for _ in range(int(stdin.readline())):
	n = int(stdin.readline())
	a = stdin.readline().strip()
	b = stdin.readline().strip()
	ans1 = []
	for i in range(n - 1):
		if a[i] != a[i + 1]:
			ans1.append(i + 1)
	if a[-1] == '1':
		ans1.append(n)
	ans2 = []
	for i in range(n - 1):
		if b[i] != b[i + 1]:
			ans2.append(i + 1)
	if b[-1] == '1':
		ans2.append(n)
	ans1 += ans2[::-1]
	print(len(ans1), *ans1)


def solve(a, b, n):
	op1 = []
	op2 = []
	a += '0'
	b += '0'
	for i in range(1, n + 1):
		if a[i] != a[i - 1]:
			op1.append(i)
		if b[i] != b[i - 1]:
			op2.append(i)
	op1 += op2[::-1]
	print(len(op1), ' '.join([str(x) for x in op1]))
	return
t = int(input())
while t:
	n = int(input())
	a = input()
	b = input()
	solve(a, b, n)
	t -= 1


for _ in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	arr = list(a)
	brr = list(b)
	ans = []
	for i in range(n):
		if i == 0:
			if arr[i] != brr[i]:
				ans.append(i + 1)
		elif arr[i] != brr[i]:
			ans.append(i + 1)
			ans.append(1)
			ans.append(i + 1)
		else:
			pass
	print(len(ans), *ans)


def flip(s):
	s1 = ''
	for i in range(len(s)):
		if s[i] == '0':
			s1 += '1'
		else:
			s1 += '0'
	return s1[::-1]
t = int(input())
l = []
for i in range(t):
	n = int(input())
	s1 = input()
	s2 = input()
	l.append([s1, s2])
for j in l:
	l1 = []
	for k in range(len(j[0]) - 1, -1, -1):
		if j[0][k] != j[1][k]:
			if j[0][0] == j[0][k]:
				l1.append(k + 1)
				if k == len(j[0]) - 1:
					j[0] = flip(j[0][0:k + 1])
				else:
					j[0] = flip(j[0][0:k + 1]) + j[0][k + 1:]
			else:
				l1.append(1)
				j[0] = flip(j[0][0]) + j[0][1:]
				if k != 0:
					l1.append(k + 1)
				if k == len(j[0]) - 1:
					j[0] = flip(j[0][0:k + 1])
				else:
					j[0] = flip(j[0][0:k + 1]) + j[0][k + 1:]
	print(len(l1), end=' ')
	for m in l1:
		print(m, end=' ')
	print()


t = int(input())
for _ in range(t):
	n = int(input())
	s = [int(i) for i in input()]
	t = [int(i) for i in input()]
	i = n - 1
	res = []
	(a, b, d) = (0, i, 1)

	def get(j):
		if d == 1:
			if j != 0:
				return s[b]
			else:
				return s[a]
		if d == -1:
			if j != 0:
				return s[b] ^ 1
			else:
				return s[a] ^ 1
	while i > 0:
		if get(i) != t[i]:
			if get(i) != get(0):
				res.append(1)
			res.append(i + 1)
			d = d * -1
			(a, b) = (b, a)
		if d == 1:
			b -= 1
		else:
			b += 1
		i -= 1
	if get(0) != t[0]:
		res.append(1)
	print(len(res), end=' ')
	for i in res:
		print(i, end=' ')
	print()


import math

def mi():
	return map(int, input().split())

def ii():
	return int(input())

def li():
	return list(map(int, input().split()))

def si():
	return input().split()
t = ii()
for _ in range(t):
	n = ii()
	a = list(input())
	b = list(input())
	i = n - 1
	ans = []
	flip = False
	cnt = 0
	si = 0
	ei = n - 1
	while i >= 0:
		if cnt % 2 == 0:
			if a[ei] == b[i]:
				ei -= 1
				i -= 1
				continue
			else:
				if a[si] == b[i]:
					ans.append(1)
				ans.append(i + 1)
				cnt += 1
				si += 1
				i -= 1
		elif a[si] != b[i]:
			si += 1
			i -= 1
			continue
		else:
			if a[ei] != b[i]:
				ans.append(1)
			ans.append(i + 1)
			cnt += 1
			ei -= 1
			i -= 1
	print(len(ans), end=' ')
	for i in ans:
		print(i, end=' ')
	print()


a = int(input())
for i in range(a):
	r = int(input())
	a1 = input()
	a2 = input()
	ans = []
	count = 1
	for i in range(1, len(a1)):
		if a1[i] == a1[i - 1]:
			count += 1
		else:
			ans.append(count)
			count = 1
	ans.append(count)
	fin = []
	for i in range(len(ans) - 1):
		if i == 0:
			fin.append(ans[0])
		else:
			fin.append(fin[-1] + ans[i])
	if a1[0] == '0':
		if len(ans) % 2 == 1:
			col = 0
		else:
			col = 1
	elif len(ans) % 2 == 1:
		col = 1
	else:
		col = 0
	for i in range(len(a2) - 1, -1, -1):
		if int(a2[i]) == col:
			continue
		else:
			fin.append(i + 1)
			col = 1 - col
	print(len(fin), end=' ')
	print(*fin)


from sys import stdin
input = lambda : stdin.readline().rstrip('\r\n')
from collections import deque as que, defaultdict as vector
from heapq import *
inin = lambda : int(input())
inar = lambda : list(map(int, input().split()))
from types import GeneratorType

def bootstrap(f, stack=[]):

	def wrappedfunc(*args, **kwargs):
		if stack:
			return f(*args, **kwargs)
		else:
			to = f(*args, **kwargs)
			while True:
				if type(to) is GeneratorType:
					stack.append(to)
					to = next(to)
				else:
					stack.pop()
					if not stack:
						break
					to = stack[-1].send(to)
			return to
	return wrappedfunc
Testcase = inin()
for _ in range(Testcase):
	n = inin()
	a = list(input())
	b = list(input())
	a = [int(x) for x in a] + [0]
	b = [int(x) for x in b] + [0]
	aw = []
	bw = []
	for i in range(n):
		if a[i] != a[i + 1]:
			aw.append(i + 1)
		if b[i] != b[i + 1]:
			bw.append(i + 1)
	print(len(aw) + len(bw), *aw, *bw[::-1])


from collections import defaultdict

def arrinp():
	return map(int, input().split(' '))

def intinp():
	return int(input())

def solution():
	n = intinp()
	a = list(map(int, list(input())))
	b = list(map(int, list(input())))
	array = []
	a0 = a[0]
	j = n - 1
	k = 1
	for i in range(n - 1, -1, -1):
		if b[i] == a0:
			array.append(1)
		array.append(i + 1)
		if (n - i) % 2 == 1:
			a0 = (a[j] + 1) % 2
			j -= 1
		else:
			a0 = a[k]
			k += 1
	print(len(array), *array)
testcases = 1
testcases = int(input())
for _ in range(testcases):
	solution()


for _ in range(int(input())):
	x = int(input())
	str1 = input()
	str2 = input()
	str3 = str1[:]
	li = []
	j = x - 1
	while j > -1:
		if str3[j] != str2[j]:
			if str3[0] == str2[j]:
				li += [1, j + 1]
			else:
				li += [j + 1]
			str1 = ''
			for i in str3[j:0:-1]:
				str1 += str(int(i) ^ 1)
		j -= 1
		str3 = str1[:]
	print(len(li), *li)


from collections import defaultdict as dd
from sys import stdin, stdout

def tog(n):
	if n == '1':
		return '0'
	else:
		return '1'
for t in range(int(input())):
	n = int(input())
	a = list(input())
	b = list(input())
	k = 0
	ans = []
	p = 0
	while n:
		if a[0] == b[n - 1]:
			ans.append(1)
			a[0] = tog(a[0])
		for i in range(n):
			a[i] = tog(a[i])
		a.reverse()
		del a[-1]
		ans.append(n)
		n -= 1
	print(len(ans), *ans)


t = int(input())
for _ in range(t):
	(n, a, b) = (int(input()), input(), input())
	ans = []
	for i in range(n):
		if a[i] != b[i]:
			ans.append(f'{i + 1}')
			ans.append('1')
			ans.append(f'{i + 1}')
	print(f'{len(ans)}' + ' ' + ' '.join(ans))


def not_func(a):
	if a == '0':
		return '1'
	elif a == '1':
		return '0'
t = int(input())
for _ in range(t):
	n = int(input())
	a = input()
	b = input()
	new_a = []
	new_b = []
	for i in range(n):
		new_a.append(a[i])
	for i in range(n):
		new_b.append(b[i])
	a = new_a
	b = new_b
	answer = []
	for i in range(n - 1, -1, -1):
		if a[i] == b[i]:
			continue
		else:
			if a[0] == b[i]:
				answer.append(1)
				a[0] = not_func(a[0])
			temp = []
			for j in range(i + 1):
				temp.append(not_func(a[j]))
			a[:i + 1] = temp[::-1]
			answer.append(i + 1)
	print(len(answer), *answer)


t = int(input())
while t:
	n = int(input())
	k = 0
	s = []
	a = input()
	b = input()
	for i in range(n):
		if a[i] != b[i]:
			k += 3
			s.append(i + 1)
			s.append(1)
			s.append(i + 1)
	print(k, *s)
	t -= 1


for _ in range(int(input())):
	n = int(input())
	a = list(input())
	b = list(input())
	k = 0
	ans = []
	for i in range(n - 1, -1, -1):
		if a[i] != b[i]:
			if a[0] != b[i]:
				ans.append(i + 1)
				k += 1
				for r in range(i + 1):
					a[r] = str((int(a[r]) + 1) % 2)
				a[:i + 1] = a[:i + 1][::-1]
			else:
				ans.append(1)
				ans.append(i + 1)
				k += 2
				for r in range(1, i + 1):
					a[r] = str((int(a[r]) + 1) % 2)
				a[:i + 1] = a[:i + 1][::-1]
	print(k, *ans)


import sys
import math
import collections
from sys import stdin, stdout, setrecursionlimit
import bisect as bs
setrecursionlimit(2 ** 20)
M = 10 ** 9 + 7

def solve():
	n = int(stdin.readline())
	s1 = stdin.readline().strip('\n')
	s2 = stdin.readline().strip('\n')
	a = [i for i in s1]
	b = [i for i in s2]
	cnt = 0
	ans = []
	for i in range(n):
		if i > 0 and a[i] != b[i]:
			ans.append(i + 1)
			ans.append(1)
			ans.append(i + 1)
			cnt += 3
		if i == 0 and a[i] != b[i]:
			cnt += 1
			ans.append(1)
	print(cnt, *ans)
t = 1
t = int(stdin.readline())
for _ in range(t):
	solve()


for _ in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	a += '0'
	b += '0'
	(l1, l2) = ([], [])
	for i in range(1, n + 1):
		if a[i] != a[i - 1]:
			l1.append(i)
		if b[i] != b[i - 1]:
			l2.append(i)
	l2.reverse()
	l3 = l1 + l2
	print(len(l3), sep=' ', end=' ')
	for i in l3:
		print(i, sep=' ', end=' ')
	print(sep='')


def reverse_no(a):
	if a == '1':
		return '0'
	else:
		return '1'

def reverse_str(listA):
	for i in range(len(listA)):
		listA[i] = reverse_no(listA[i])
	listA.reverse()
	return listA
for i in range(int(input())):
	n = int(input())
	A = list(input())
	B = list(input())
	l = []
	while n > 0:
		if A[n - 1] == B[n - 1]:
			n = n - 1
			continue
		elif A[0] == B[n - 1]:
			l.append(1)
			A[0] = reverse_no(A[0])
			l.append(n)
			A[0:n] = reverse_str(A[0:n])
			n = n - 1
		else:
			l.append(n)
			A[0:n] = reverse_str(A[0:n])
			n = n - 1
	print(len(l), end=' ')
	for x in l:
		print(x, end=' ')
	print()


t = int(input())
for i in range(t):
	lst1 = []
	n = int(input())
	a = list(input())
	a = list(map(int, a))
	b = list(input())
	b = list(map(int, b))
	i = n - 1
	ans = 0
	while i >= 0:
		if a[i] == b[i]:
			i -= 1
		else:
			break
	while a != b:
		a = a[:i + 1]
		b = b[:i + 1]
		j = 0
		k = len(b) - 1
		if a[j] == b[k]:
			ans += 2
			lst1.append(1)
			lst1.append(len(a))
			for l in range(len(a)):
				if a[l] == 0:
					a[l] = 1
				else:
					a[l] = 0
			a = list(reversed(a))
			a[-1] = b[-1]
		else:
			ans += 1
			lst1.append(len(a))
			for l in range(len(a)):
				if a[l] == 0:
					a[l] = 1
				else:
					a[l] = 0
			a = list(reversed(a))
		i = len(a) - 1
		while i >= 0:
			if a[i] == b[i]:
				i -= 1
			else:
				break
	print(ans, end=' ')
	for i in range(len(lst1)):
		print(lst1[i], end=' ')
	print()


from sys import stdin, stdout
from math import gcd, ceil, sqrt
from collections import Counter
from bisect import bisect_left, bisect_right
ii1 = lambda : int(stdin.readline().strip())
is1 = lambda : stdin.readline().strip()
iia = lambda : list(map(int, stdin.readline().strip().split()))
isa = lambda : stdin.readline().strip().split()
mod = 1000000007
tc = ii1()
for _ in range(tc):
	n = ii1()
	a = is1()
	b = is1()
	res = []
	for i in range(1, len(a)):
		if a[i] != a[i - 1]:
			res.append(i)
	if a[-1] != b[-1]:
		res.append(n)
	b1 = []
	for i in range(1, len(b)):
		if b[i] != b[i - 1]:
			b1.append(i)
	res += b1[::-1]
	print(len(res), *res)


t = int(input())
for you in range(t):
	n = int(input())
	a = input()
	ai = [i for i in a]
	bi = input()
	k = []
	z = list(ai)
	for i in range(n - 1, -1, -1):
		if ai[i] != bi[i]:
			if ai[0] == bi[i]:
				k.append(1)
				if ai[0] == '0':
					ai[0] = '1'
				else:
					ai[0] = '0'
				k.append(i + 1)
				for j in range(i + 1):
					ai[j] = z[i - j]
				for j in range(i + 1):
					if ai[j] == '0':
						ai[j] = '1'
					else:
						ai[j] = '0'
				z = list(ai)
			else:
				k.append(i + 1)
				for j in range(i + 1):
					ai[j] = z[i - j]
				for j in range(i + 1):
					if ai[j] == '0':
						ai[j] = '1'
					else:
						ai[j] = '0'
				z = list(ai)
	print(len(k), end=' ')
	for i in k:
		print(i, end=' ')
	print()


def convert_to_1(s):
	L = []
	for i in range(len(s) - 1):
		if s[i] != s[i + 1]:
			L.append(i + 1)
	if s[-1] == '0':
		L.append(len(s))
	return L
for i in ' ' * int(input()):
	n = int(input())
	s1 = input()
	s2 = input()
	L1 = convert_to_1(s1)
	L2 = convert_to_1(s2)
	L = L1 + L2[::-1]
	print(len(L), end=' ')
	for i in L:
		print(i, end=' ')
	print()


from collections import deque
import sys

def inp():
	return sys.stdin.readline().strip()
for _ in range(int(inp())):
	n = int(inp())
	a = inp()
	b = inp()
	a = [int(i) for i in a]
	b = [int(i) for i in b]
	res1 = []
	for i in range(1, len(a)):
		if a[i] != a[i - 1]:
			res1.append(i)
	if a[-1] == 1:
		res1.append(len(a))
	res2 = []
	for j in range(1, len(b)):
		if b[j] != b[j - 1]:
			res2.append(j)
	if b[-1] == 1:
		res2.append(len(b))
	res = res1 + res2[::-1]
	print(len(res), *res)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, [i for i in input()]))
	b = list(map(int, [i for i in input()]))
	arr = []
	ans = 0
	for i in range(len(a)):
		if a[i] != b[i]:
			ans += 1
			if i == 0:
				arr.append(1)
			else:
				arr.append(i + 1)
				arr.append(1)
				arr.append(i + 1)
	if a[0] != b[0]:
		ans -= 1
		ans *= 3
		ans += 1
	else:
		ans *= 3
	arr = arr[::-1]
	print(ans, end=' ')
	for i in arr:
		print(i, end=' ')
	print('\n')


from sys import stdin, stdout
import math

def solve():
	n = int(input())
	a = input()
	b = input()
	(ans, ans2) = ([], [])
	a += '0'
	b += '0'
	for i in range(1, n + 1):
		if a[i] != a[i - 1]:
			ans.append(i)
		if b[i] != b[i - 1]:
			ans2.append(i)
	ans.extend(ans2[::-1])
	print(len(ans), end=' ')
	for i in ans:
		print(i, end=' ')
	print()
tt = 1
tt = int(input())
for __ in range(tt):
	solve()


def flip(a, b):
	res = []
	n = len(a)
	for i in range(n):
		if a[i] != b[i]:
			if i == 0:
				res.append(i + 1)
			else:
				res.append(i + 1)
				res.append(1)
				res.append(i + 1)
	x = len(res)
	if x == 0:
		print(0)
	else:
		print(x, ' ', end='')
		for i in res:
			print(i, ' ', end='')
		print()
t = int(input())
for i in range(t):
	n = int(input())
	a = input()
	b = input()
	flip(a, b)


for _ in range(int(input())):
	n = int(input())
	s1 = input()
	s2 = input()
	c = 0
	l = []
	for i in range(n):
		if s1[i] != s2[i]:
			l.append(i + 1)
			l.append(1)
			l.append(i + 1)
			c += 1
	if c == 0:
		print(0, end=' ')
	else:
		print(c * 3, *l, end=' ')
	print()


for tc in range(int(input())):
	n = int(input())
	A = input()
	B = input()
	steps = []
	for i in range(n):
		if i % 2 == 0:
			if A[int(i / 2)] != B[n - 1 - i]:
				steps.append(n - i)
			else:
				steps.append(1)
				steps.append(n - i)
		elif A[n - 1 - int(i / 2)] == B[n - 1 - i]:
			steps.append(n - i)
		else:
			steps.append(1)
			steps.append(n - i)
	print(len(steps), end=' ')
	for i in steps:
		print(i, end=' ')
	print()


from __future__ import division, print_function
from fractions import Fraction
import sys
import os
from io import BytesIO, IOBase
from itertools import *
import bisect
from heapq import *
from math import ceil, floor
from copy import *
from collections import deque, defaultdict
from collections import Counter as counter
from itertools import combinations
from itertools import permutations as permutate
from bisect import bisect_left as bl
from operator import *
from bisect import bisect_right as br
from bisect import bisect
BUFSIZE = 8192
from sys import stderr

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
from types import GeneratorType

def iterative(f, stack=[]):

	def wrapped_func(*args, **kwargs):
		if stack:
			return f(*args, **kwargs)
		to = f(*args, **kwargs)
		while True:
			if type(to) is GeneratorType:
				stack.append(to)
				to = next(to)
				continue
			stack.pop()
			if not stack:
				break
			to = stack[-1].send(to)
		return to
	return wrapped_func
mod = 1000000007

def inp():
	return sys.stdin.readline().rstrip('\r\n')

def out(var):
	sys.stdout.write(str(var))

def lis():
	return list(map(int, inp().split()))

def stringlis():
	return list(map(str, inp().split()))

def sep():
	return map(int, inp().split())

def strsep():
	return map(str, inp().split())

def fsep():
	return map(float, inp().split())

def nextline():
	out('\n')

def testcase(t):
	for p in range(t):
		solve()

def pow(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res
from functools import reduce

def factors(n):
	return set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0)))

def gcd(a, b):
	if a == b:
		return a
	while b > 0:
		(a, b) = (b, a % b)
	return a

def prefix_sum(ar):
	return list(accumulate(ar))

def suffix_sum(ar):
	return list(accumulate(ar[::-1]))[::-1]

def N():
	return int(inp())
from collections import defaultdict

def numberOfSetBits(i):
	i = i - (i >> 1 & 1431655765)
	i = (i & 858993459) + (i >> 2 & 858993459)
	return ((i + (i >> 4) & 252645135) * 16843009 & 4294967295) >> 24

def solve():
	n = N()
	a = inp()
	b = inp()
	ar1 = [int(i) for i in a]
	ar2 = [int(i) for i in b]
	cf = a[0]
	c = 0
	ans = []
	for i in range(n - 1, 0, -1):
		if ar1[i] == ar2[i]:
			continue
		else:
			if ar1[0] != ar1[i]:
				c += 1
				ans.append(1)
				ar1[0] = 1 - ar1[0]
			c += 1
			ans.append(i + 1)
			for j in range(0, i // 2 + 1):
				(ar1[j], ar1[i - j]) = (1 - ar1[i - j], 1 - ar1[j])
	if ar1[0] != ar2[0]:
		c += 1
		ar1[0] = 1 - ar1[0]
		ans.append(1)
	print(c, *ans)
testcase(int(inp()))


for _ in range(int(input())):
	n = int(input())
	la = str(input())
	lb = str(input())
	ans = n
	l = []
	a = list(la)
	b = list(lb)
	left = 0
	right = n - 1
	for i in range(n - 1, -1, -1):
		temp = n - i + 1
		if temp % 2 == 0:
			if a[left] == b[i]:
				l.append(1)
			left += 1
			l.append(i + 1)
		else:
			if a[right] != b[i]:
				l.append(1)
			right -= 1
			l.append(i + 1)
	print(len(l), end=' ')
	for i in range(len(l)):
		print(l[i], end=' ')
	print()


t = int(input())
for u in range(t):
	n = int(input())
	x = input()
	y = input()
	a = []
	b = []
	for i in x:
		a.append(True if i == '1' else False)
	for j in y:
		b.append(True if j == '1' else False)
	ans = []
	for i in range(n - 1, -1, -1):
		if a[i] != b[i]:
			if a[0] != b[i]:
				ans.append(i + 1)
				temp = a[:]
				for j in range(i, -1, -1):
					a[j] = not temp[i - j]
			else:
				ans.append(1)
				a[0] = not a[0]
				temp = a[:]
				for j in range(i, -1, -1):
					a[j] = not temp[i - j]
				ans.append(i + 1)
	print(len(ans), *ans)


for _ in range(int(input())):
	n = int(input())
	la = str(input())
	lb = str(input())
	ans = 0
	l = []
	a = list(la)
	b = list(lb)
	st = a.copy()
	for i in range(n - 1, -1, -1):
		if st[i] != b[i]:
			ans += 1
			if st[0] == b[i]:
				ans += 1
				if st[0] == '1':
					st[0] = '0'
				else:
					st[0] = '1'
				l.append(1)
			st1 = []
			for j in range(i, -1, -1):
				if st[j] == '0':
					st1.append('1')
				else:
					st1.append('0')
			for j in range(i + 1, n):
				st1.append(st[j])
			st = st1.copy()
			l.append(i + 1)
	print(ans, end=' ')
	for i in range(len(l)):
		print(l[i], end=' ')
	print()


t = int(input())
while t > 0:
	t -= 1
	n = int(input())
	s1 = input()
	s2 = input()
	l = 0
	r = n - 1
	rev = False
	ans = []
	for x in s2[-1::-1]:
		if not rev:
			if s1[r] == x:
				r -= 1
				continue
			if s1[l] == x:
				ans.append(1)
			ans.append(r - l + 1)
			l += 1
			rev = True
		else:
			if s1[l] != x:
				l += 1
				continue
			if s1[r] != x:
				ans.append(1)
			ans.append(r - l + 1)
			r -= 1
			rev = False
	print(len(ans))
	for x in ans:
		print(x, end=' ')
	print()


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().strip()))
	b = list(map(int, input().strip()))
	(ans1, ans2) = ([], [])
	if a.count(1) > n // 2:
		for i in range(n):
			a[i] = 1 * (a[i] == 0)
		a.reverse()
		ans1.append(n)
	for i in range(n):
		if a[i] == 1:
			if i != 0:
				ans1.append(i)
			ans1.append(i + 1)
	if b.count(1) > n // 2:
		for i in range(n):
			b[i] = 1 * (b[i] == 0)
		b.reverse()
		ans2.append(n)
	for i in range(n):
		if b[i] == 1:
			if i != 0:
				ans2.append(i)
			ans2.append(i + 1)
	print(len(ans1) + len(ans2), *ans1, *ans2[::-1])


for _ in range(int(input())):
	n = int(input())
	a = str(input())
	b = str(input())
	arr1 = []
	arr2 = []
	for i in a:
		arr1.append(int(i))
	for i in b:
		arr2.append(int(i))
	A = []
	for i in range(n):
		if arr1[i] != arr2[i]:
			A.extend([i + 1, 1, i + 1])
	print(len(A), *A)


import os
t1 = int(input())
while t1:
	t1 -= 1
	n = int(input())
	s = input()
	t = input()
	ans = []
	for i in range(n):
		if s[i] != t[i]:
			ans.append(i + 1)
	print(len(ans) * 3, end=' ')
	for i in range(len(ans)):
		print(ans[i], 1, ans[i], end=' ')
	print()


def mod(s, x):
	s[:x + 1] = s[:x + 1][::-1]
	for n in range(x + 1):
		s[n] = int(not s[n])
	return s

def solve():
	x = int(input())
	s = list(map(int, input()))
	z = list(map(int, input()))
	res = []
	for n in range(x - 1, -1, -1):
		if s[n] != z[n]:
			if s[n] == s[0]:
				res.append(n + 1)
				s = mod(s, n)
			else:
				res += [n + 1, 1, n + 1]
				s = mod(s, n)
				s[0] = int(not s[0])
				s = mod(s, n)
	print(len(res), *res)
for _ in range(int(input())):
	solve()


a = input()
for x in range(int(a)):
	b = input()
	c = input()
	d = input()
	list1 = [int(i) for i in c]
	list2 = [int(i) for i in d]
	ans = 0
	ans_list = []
	tag = len(list1) - 1
	while list1 != list2 and tag >= 0:
		if list1[tag] != list2[tag]:
			if list1[0] != list1[tag]:
				if list1[0] == 1:
					list1[0] = 0
				else:
					list1[0] = 1
				ans += 1
				ans_list.append(1)
			ans += 1
			ans_list.append(tag + 1)
			for i in range(tag + 1):
				if list1[i] == 0:
					list1[i] = 1
				else:
					list1[i] = 0
			list1 = list1[0:tag + 1][::-1] + list1[tag + 1:]
		tag -= 1
	if ans == 0:
		print(ans)
	else:
		print(ans, end=' ')
	for i in range(len(ans_list)):
		if i != len(ans_list) - 1:
			print(ans_list[i], end=' ')
		else:
			print(ans_list[i])


cases = int(input())
for i in range(cases):
	length = int(input())
	a = input()
	b = input()
	prefix = []
	if a == b:
		print(0)
	else:
		a = list(a)
		b = list(b)
		for (index, ele) in enumerate(a):
			if b[index] != ele:
				prefix.append(index + 1)
				prefix.append(1)
				prefix.append(index + 1)
		print(len(prefix), end=' ')
		for p in prefix:
			print(p, end=' ')
		print('\n')


import sys
import os
import math
import copy
from math import gcd
from bisect import bisect
from io import BytesIO, IOBase
from math import sqrt, floor, factorial, gcd, log, ceil
from collections import deque, Counter, defaultdict
from itertools import permutations, combinations

def Int():
	return int(sys.stdin.readline())

def Mint():
	return map(int, sys.stdin.readline().split())

def Lstr():
	return list(sys.stdin.readline().strip())

def Str():
	return sys.stdin.readline().strip()

def Mstr():
	return map(str, sys.stdin.readline().strip().split())

def List():
	return list(map(int, sys.stdin.readline().split()))

def Hash():
	return dict()

def Mod():
	return 1000000007

def Ncr(n, r, p):
	return fact[n] * (ifact[r] * ifact[n - r] % p) % p

def Most_frequent(list):
	return max(set(list), key=list.count)

def Mat2x2(n):
	return [List() for _ in range(n)]

def solution():
	for _ in range(Int()):
		n = Int()
		a = Lstr()
		b = Lstr()
		ans = []
		if a[0] != b[0]:
			ans.append(1)
		for i in range(1, n):
			if a[i] != b[i]:
				ans.append(i + 1)
				ans.append(1)
				ans.append(i + 1)
		print(len(ans), *ans)
solution()


t = int(input())

def operate(lista, c):
	for i in range(c + 1):
		if lista[i] == '1':
			lista[i] = '0'
		else:
			lista[i] = '1'
	lista1 = lista[:c + 1]
	lista2 = lista[c + 1:]
	lista1.reverse()
	lista = lista1 + lista2
	return lista
for i in range(t):
	n = int(input())
	string1 = input()
	string1 = [j for j in string1]
	string2 = input()
	string2 = [j for j in string2]
	c = n - 1
	lista = []
	while c >= 0:
		if string1[c] != string2[c]:
			if string1[c] == string1[0]:
				string1 = operate(string1, c)
				lista += [c + 1]
			else:
				string1 = operate(string1, 0)
				string1 = operate(string1, c)
				lista += [1, c + 1]
			c -= 1
		else:
			c = c - 1
	print(len(lista), end=' ')
	for j in lista:
		print(j, end=' ')
	print('')


from sys import stdin, stdout
import math, queue, heapq
fastinput = stdin.readline
fastout = stdout.write
t = int(fastinput())

def changeflag(flag):
	return 0 if flag == 1 else 1

def original(c, flag):
	if flag == 1:
		return c
	else:
		return '0' if c == '1' else '1'

def ismatch(c, v, flag):
	c = original(c, flag)
	if c == v:
		return True
	else:
		return False
while t:
	t -= 1
	n = int(fastinput())
	a = input()
	b = input()
	start = 0
	end = n - 1
	flag = 1
	instruction = []
	for i in range(n - 1, -1, -1):
		if start == end:
			if ismatch(a[start], b[i], flag):
				continue
			else:
				instruction.append(1)
		elif start < end:
			if ismatch(a[end], b[i], flag):
				end -= 1
			else:
				if ismatch(a[start], b[i], flag):
					instruction.append(1)
				flag = changeflag(flag)
				instruction.append(end - start + 1)
				temp = start
				start = end
				end = temp
				end += 1
		elif ismatch(a[end], b[i], flag):
			end += 1
		else:
			if ismatch(a[start], b[i], flag):
				instruction.append(1)
			instruction.append(start - end + 1)
			flag = changeflag(flag)
			temp = start
			start = end
			end = temp
			end -= 1
	print(len(instruction), *instruction)


def do_reverse(s):
	s1 = ''
	for i in s:
		if i == '0':
			s1 = '1' + s1
		else:
			s1 = '0' + s1
	return s1
for _ in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	if a == b:
		print(0)
		continue
	l = []
	for i in range(n - 1, -1, -1):
		if a[i] == b[i]:
			continue
		elif a[i] == a[0]:
			a = do_reverse(a[:i + 1]) + a[i + 1:]
			l.append(i + 1)
		else:
			l.append(1)
			a = a[i] + a[1:]
			a = do_reverse(a[:i + 1]) + a[i + 1:]
			l.append(i + 1)
	print(len(l), *l)


def find(s1, s2):
	if len(s1) == 1:
		if s1 == s2:
			return []
		else:
			return [1]
	indices = [-1] * len(s1)
	cur = len(s1) - 1
	val = 0
	for i in range(len(s1)):
		indices[cur] = val
		cur -= 2
		val += 1
		if cur < 0:
			break
	if indices[0] == -1:
		cur = 0
		for i in range(len(s1)):
			indices[cur] = val
			val += 1
			cur += 2
			if cur > len(s1) - 1:
				break
	else:
		cur = 1
		for i in range(len(s1)):
			indices[cur] = val
			val += 1
			cur += 2
			if cur > len(s1) - 1:
				break
	ans = []
	temp = 1
	for i in range(len(s1) - 1, -1, -1):
		if (int(s1[indices[i]]) + temp) % 2 == int(s2[i]) % 2:
			ans += [i + 1]
		else:
			ans += [1, i + 1]
		temp += 1
	return ans
ANS = []
for _ in range(int(input())):
	input()
	s1 = input()
	s2 = input()
	ans = find(s1, s2)
	ans = [str(x) for x in ans]
	ans = [str(len(ans))] + ans
	ANS += [' '.join(ans)]
print('\n'.join(ANS))


def f(x):
	for i in range(int((x + 1) / 2)):
		(A[i], A[x - 1 - i]) = (1 - A[x - 1 - i], 1 - A[i])
for tc in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	A = []
	steps = []
	for i in a:
		A.append(int(i))
	for i in range(n - 1, -1, -1):
		if A[i] != int(b[i]):
			if A[0] == A[i]:
				steps.append(i + 1)
				f(i + 1)
			else:
				steps.append(1)
				steps.append(i + 1)
				f(1)
				f(i + 1)
	print(len(steps), end=' ')
	for i in steps:
		print(i, end=' ')
	print()


t = int(input())

def opp(s):
	if s == '0':
		return '1'
	return '0'
for l in range(t):
	n = int(input())
	s1 = list(input())
	s2 = list(input())
	v = []
	for i in range(n):
		if s1[n - 1 - i] != s2[n - 1 - i]:
			if s1[n - 1 - i] != s1[0]:
				v.append(1)
				s1[0] = s1[n - 1 - i]
			v.append(n - i)
			temp = s1[:]
			for j in range(n - i):
				s1[j] = str(1 - int(temp[n - 1 - i - j]))
	print(len(v), end=' ')
	for k in v:
		print(k, end=' ')
	print('')


import sys
import heapq, functools, collections
import math, random
from collections import Counter, defaultdict

def solve(arr, brr):
	console('----- solving ------')
	arr = [int(x) for x in arr]
	brr = [int(x) for x in brr]
	console(brr, '**')
	console(arr)
	ptr = len(arr) - 1
	res = []
	if arr == brr:
		return '0'

	def operate(crr):
		return [x ^ 1 for x in crr][::-1]
	while ptr >= 0:
		if arr[ptr] == brr[ptr]:
			ptr -= 1
			continue
		if arr[0] == brr[ptr]:
			arr[0] = arr[0] ^ 1
			res.append(1)
			console(arr, 1)
		res.append(ptr + 1)
		arr[:ptr + 1] = operate(arr[:ptr + 1])
		console(arr, ptr + 1)
	return str(len(res)) + ' ' + ' '.join([str(x) for x in res])

def console(*args):
	return
for case_num in range(int(input())):
	_ = int(input())
	arr = input()
	brr = input()
	res = solve(arr, brr)
	print(res)


import copy
t = int(input())
while t:
	t -= 1
	n = int(input())
	a = list(input())
	b = list(input())
	flip = []
	k = 0
	for i in range(n):
		if n - i - 1 == 0:
			if a[0] != b[0]:
				flip.append(1)
				k += 1
		elif a[n - i - 1] != b[n - i - 1]:
			flip.append(n - i)
			flip.append(1)
			flip.append(n - i)
			k += 3
	print(k, end=' ')
	print(*flip)


import collections
t1 = int(input())
for _ in range(t1):
	flipflag = 0
	bitflag = 0
	n = int(input())
	a2 = input()
	a = collections.deque([])
	for i in range(n):
		a.append(a2[i])
	b = input()
	ans = []
	for i in range(n - 1, -1, -1):
		if flipflag == 0:
			p = a[-1]
		else:
			p = a[0]
		if bitflag == 1:
			if p == '0':
				p = '1'
			else:
				p = '0'
		if flipflag == 0:
			q = a[0]
		else:
			q = a[-1]
		if bitflag == 1:
			if q == '0':
				q = '1'
			else:
				q = '0'
		if p != b[i]:
			if q != b[i]:
				ans.append(str(i + 1))
				if flipflag == 0:
					a.popleft()
				else:
					a.pop()
				flipflag = 1 - flipflag
				bitflag = 1 - bitflag
			else:
				ans.append('1')
				ans.append(str(i + 1))
				if flipflag == 0:
					a.popleft()
				else:
					a.pop()
				flipflag = 1 - flipflag
				bitflag = 1 - bitflag
		elif flipflag == 0:
			a.pop()
		else:
			a.popleft()
	print(len(ans))
	if len(ans) > 0:
		print(' '.join(ans))


def changeString(string, index):
	outPut = ''
	for c in string[:index + 1]:
		if c == '1':
			outPut += '0'
		else:
			outPut += '1'
	outPut = outPut[::-1]
	if index != len(string) - 1:
		outPut += string[index + 1:]
	return outPut
for i in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	moves = []
	equal = False
	if a == b:
		print(0)
		equal = True
	else:
		for i in range(len(a) - 1, -1, -1):
			if a[i] != b[i]:
				if a[0] != b[i]:
					a = changeString(a, i)
					moves.append(i + 1)
				else:
					a = changeString(a, 0)
					moves.append(1)
					a = changeString(a, i)
					moves.append(i + 1)
	if not equal:
		print(f'{len(moves)} ', end='')
		print(' '.join((str(move) for move in moves)))


from collections import defaultdict
for _ in range(int(input())):
	n = int(input())
	l1 = list(input())
	l2 = list(input())
	arr = []
	for i in range(n):
		if l1[i] != l2[i]:
			arr.append(i + 1)
			arr.append(1)
			arr.append(i + 1)
	print(len(arr), *arr)


import sys
input = sys.stdin.readline

def pathLst(n, s):
	path = []
	for i in range(n - 1):
		if s[i] != s[i + 1]:
			path.append(str(i + 1))
	if s[-1] == '1':
		path.append(str(n))
	return path

def main(n, a, b):
	path = pathLst(n, a) + pathLst(n, b)[::-1]
	return str(len(path)) + ' ' + ' '.join(path)
for _ in range(int(input())):
	n = int(input())
	a = input()[:-1]
	b = input()[:-1]
	print(main(n, a, b))


def get_invert(string) -> str:
	ans1 = ''
	for e in string:
		ans1 += '1' if e == '0' else '0'
	return ans1[::-1]
for _ in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	c = 0
	p = n - 1
	ans = []
	while p > -1:
		if a[p] == b[p]:
			p -= 1
			continue
		if a[0] == b[p]:
			if b[p] == '1':
				a = '0' + a[1:]
			else:
				a = '1' + a[1:]
			c += 2
			a = get_invert(a[:p + 1]) + a[p + 1:]
			ans.append(1)
			ans.append(p + 1)
		else:
			c += 1
			a = get_invert(a[:p + 1]) + a[p + 1:]
			ans.append(p + 1)
		p -= 1
	print(c, *ans)


import sys
sys.setrecursionlimit(10000)
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(input())
	b = list(input())
	prefs = [0]
	reverse = False
	invert = False
	la = 0
	ra = n - 1
	lb = 0
	rb = n - 1
	while ra - la + 1 > 0:
		if reverse is False:
			if a[ra] == b[rb] and invert is False or (a[ra] != b[rb] and invert is True):
				ra -= 1
				rb -= 1
				continue
			if a[la] != b[rb] and invert is False or (a[la] == b[rb] and invert is True):
				prefs.append(ra - la + 1)
				invert = not invert
				reverse = not reverse
			else:
				prefs.append(1)
				prefs.append(ra - la + 1)
				reverse = not reverse
				invert = not invert
				la += 1
				rb -= 1
		else:
			if a[la] == b[rb] and invert is False or (a[la] != b[rb] and invert is True):
				la += 1
				rb -= 1
				continue
			if a[ra] != b[rb] and invert is False or (a[ra] == b[rb] and invert is True):
				prefs.append(ra - la + 1)
				invert = not invert
				reverse = not reverse
			else:
				prefs.append(1)
				prefs.append(ra - la + 1)
				reverse = not reverse
				invert = not invert
				ra -= 1
				rb -= 1
	prefs[0] = len(prefs) - 1
	print(*prefs)


for _ in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	p = []
	j1 = 0
	j2 = n - 1
	inc = -1
	flip = False
	for i in range(n - 1, -1, -1):
		val1 = a[j1]
		val2 = a[j2]
		if flip:
			if val1 == '0':
				val1 = '1'
			else:
				val1 = '0'
			if val2 == '0':
				val2 = '1'
			else:
				val2 = '0'
		if b[i] == val2:
			j2 += inc
			continue
		if val1 != val2:
			p.append(1)
		p.append(i + 1)
		j1 -= inc
		(j1, j2) = (j2, j1)
		if inc == -1:
			inc = 1
		else:
			inc = -1
		flip = not flip
	s = str(len(p))
	if len(p) > 0:
		s += ' '
		s += ' '.join(map(str, p))
	print(s)


test = False

def get_end(start, end, reversed):
	if not reversed:
		return end
	else:
		return start

def get_start(start, end, reversed):
	if not reversed:
		return start
	else:
		return end

def invert(bit, inverted):
	if not inverted:
		return bit
	else:
		return not bit
t = int(input())
for test_case in range(t):
	n = int(input())
	a = list(map(lambda x: bool(int(x)), input()))
	b = list(map(lambda x: bool(int(x)), input()))
	(start, end) = (0, n - 1)
	(reversed, inverted) = (False, False)
	instr = ''
	k = 0
	for m in range(n - 1, -1, -1):
		target = b[m]
		real_m_in_a = get_end(start, end, reversed)
		cand = invert(a[real_m_in_a], inverted)
		if cand == target:
			if not reversed:
				end -= 1
			else:
				start += 1
		else:
			first_index = get_start(start, end, reversed)
			cand = invert(a[first_index], inverted)
			if (not cand) != target:
				instr += '1 '
				k += 1
			instr += str(m + 1) + ' '
			k += 1
			reversed = not reversed
			inverted = not inverted
			if not reversed:
				end -= 1
			else:
				start += 1
	print(k, instr)


import sys
import math
input = sys.stdin.readline
T = int(input())
for t in range(T):
	N = int(input())
	A = list(map(int, input().strip()))
	B = list(map(int, input().strip()))
	flips = []
	left = 0
	right = N - 1
	i = N - 1
	big_flips = 0
	while i >= 0:
		if big_flips % 2 == 0:
			bit = A[right]
			opposite_bit = A[left]
		else:
			bit = 1 - A[left]
			opposite_bit = 1 - A[right]
		if bit == B[i]:
			i -= 1
			if big_flips % 2 == 0:
				right -= 1
			else:
				left += 1
			continue
		if 1 - opposite_bit == B[i]:
			flips.append(right - left + 1)
			if big_flips % 2 == 0:
				left += 1
			else:
				right -= 1
		else:
			flips.append(1)
			flips.append(right - left + 1)
			if big_flips % 2 == 0:
				left += 1
			else:
				right -= 1
		big_flips += 1
		i -= 1
	if flips:
		print(len(flips), end=' ')
		print(' '.join(map(str, flips)))
	else:
		print(0)


q = int(input())
for o in range(q):
	n = int(input())
	a = list(map(int, input()))
	b = list(map(int, input()))
	ans = [0]
	if a[0] != b[0]:
		ans[0] += 1
		ans.append(1)
	for i in range(1, n):
		if a[i] != b[i]:
			ans[0] += 3
			ans.append(i + 1)
			ans.append(1)
			ans.append(i + 1)
	for i in range(len(ans)):
		print(ans[i], end=' ')
	print()


import sys
t = int(input())
for _ in range(t):
	n = int(input())
	tempa = input()
	tempb = input()
	a = []
	for i in tempa:
		a.append(int(i))
	b = []
	for i in tempb:
		b.append(int(i))
	count = 0
	anslist = []
	for i in range(n - 1, -1, -1):
		if a[i] != b[i]:
			if a[0] != b[i]:
				anslist.append(i + 1)
				for j in range(i + 1):
					if a[j] == 0:
						a[j] = 1
					else:
						a[j] = 0
				a[0:i + 1] = reversed(a[0:i + 1])
				count += 1
			else:
				if a[0] == 0:
					a[0] = 1
				else:
					a[0] = 0
				anslist.append(1)
				for j in range(i + 1):
					if a[j] == 0:
						a[j] = 1
					else:
						a[j] = 0
				anslist.append(i + 1)
				a[0:i + 1] = reversed(a[0:i + 1])
				count += 2
	print(count, end=' ')
	for i in anslist:
		sys.stdout.write(str(i) + ' ')
	print()


for t in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	ind = n - 1
	ans = []
	i = 0
	k = -1
	for i in range(n - 1, -1, -1):
		if (n - 1 - i) % 2 == 0:
			if a[(n - i) // 2] == b[i]:
				ans.append(1)
			ans.append(i + 1)
		else:
			if a[n - (n - i) // 2] != b[i]:
				ans.append(1)
			ans.append(i + 1)
	print(str(len(ans)) + ' ' + ' '.join(map(str, ans)))


from functools import lru_cache
from sys import stdin, stdout
import sys
from math import *
for __ in range(int(input())):
	n = int(input())
	a = list(input())
	b = list(input())
	same = 0
	r = 0
	x = 0
	y1 = n - 1
	y2 = n - 1
	ans = []
	while y2 >= 0:
		if same == 0:
			if a[y1] == b[y2]:
				if r == 0:
					y1 -= 1
				else:
					y1 += 1
				y2 -= 1
			elif a[y1] == a[x]:
				ans.append(abs(y1 - x) + 1)
				if r == 0:
					temp = x
					x = y1
					y1 = temp + 1
				else:
					temp = y1
					y1 = x - 1
					x = temp
				y2 -= 1
				same ^= 1
				r ^= 1
			else:
				ans.append(1)
				a[x] = a[y1]
		elif a[y1] != b[y2]:
			if r == 0:
				y1 -= 1
			else:
				y1 += 1
			y2 -= 1
		elif a[y1] == a[x]:
			ans.append(abs(y1 - x) + 1)
			if r == 0:
				temp = x
				x = y1
				y1 = temp + 1
			else:
				temp = y1
				y1 = x - 1
				x = temp
			y2 -= 1
			same ^= 1
			r ^= 1
		else:
			ans.append(1)
			a[x] = a[y1]
	if len(ans) == 0:
		print(0)
	else:
		print(len(ans), end=' ')
		print(*ans)


t = int(input())
for i in range(t):
	n = int(input())
	ch1 = input()
	ch2 = input()
	L = []
	nb = 0
	s = ''
	for i in range(1, n):
		if ch1[i] != ch1[i - 1]:
			s += str(i)
			s += ' '
			nb += 1
	if ch1[n - 1] != ch2[n - 1]:
		s += str(n)
		s += ' '
		nb += 1
	for i in range(n - 2, -1, -1):
		if ch2[i] != ch2[i + 1]:
			s += str(i + 1)
			s += ' '
			nb += 1
	if ch1 != ch2:
		print(nb, s)
	else:
		print(0)


def flip(a, size):
	res = []
	for i in range(0, size):
		if a[i] == '1':
			res.append('0')
		else:
			res.append('1')
	return res
for _ in range(0, int(input())):
	n = int(input())
	a = list(input().strip())
	b = list(input().strip())
	res = []
	size = n
	for i in range(n - 1, -1, -1):
		if a[i] == b[i]:
			size -= 1
			continue
		else:
			if a[i] == '1':
				if a[0] == '1':
					r2 = flip(a, size)
					r2.reverse()
					a = [k for k in r2] + a[size:]
					res.append(size)
				else:
					res.append(1)
					a[0] = '1'
					r2 = flip(a, size)
					r2.reverse()
					a = [k for k in r2] + a[size:]
					res.append(size)
			elif a[0] == '0':
				r2 = flip(a, size)
				r2.reverse()
				a = [k for k in r2] + a[size:]
				res.append(size)
			else:
				res.append(1)
				a[0] = '0'
				r2 = flip(a, size)
				r2.reverse()
				a = [k for k in r2] + a[size:]
				res.append(size)
			size -= 1
	print(len(res), *res)


from sys import stdin
input = stdin.readline
for _ in range(int(input())):
	n = int(input())
	a = input().strip()
	b = input().strip()
	bi = n - 1
	f = 0
	(l, r) = (0, n - 1)
	ans = []
	while l != r:
		if f == 0 and a[l] == b[bi] or (f != 0 and a[l] != b[bi]):
			ans.append(1)
		ans.append(abs(l - r) + 1)
		if l < r:
			l += 1
		else:
			l -= 1
		(l, r) = (r, l)
		f ^= 1
		bi -= 1
	if f == 0 and a[l] != b[0] or (f and a[l] == b[0]):
		ans.append(1)
	print(len(ans), end=' ')
	print(*ans)


from sys import stdin, stdout
from math import gcd, ceil, sqrt
from collections import Counter
from bisect import bisect_left, bisect_right
ii1 = lambda : int(stdin.readline().strip())
is1 = lambda : stdin.readline().strip()
iia = lambda : list(map(int, stdin.readline().strip().split()))
isa = lambda : stdin.readline().strip().split()
mod = 1000000007

def flip(a, i):
	for j in range(i):
		if a[j] == '1':
			a[j] = '0'
		else:
			a[j] = '1'
	a = a[:i][::-1] + a[i:]
	return a
tc = ii1()
for _ in range(tc):
	n = ii1()
	(a, b) = (list(is1()), list(is1()))
	res = []
	i = n - 1
	while i > 0:
		if a[i] != b[i]:
			if a[0] == b[i]:
				res.append(1)
				a[0] = '0' if a[0] == '1' else '1'
			res.append(i + 1)
			a = flip(a, i + 1)
		i -= 1
	if a[0] != b[0]:
		res.append(1)
	print(len(res), *res)


import sys
inp = sys.stdin.readline

def linp(x):
	return list(map(int, x.split()))

def minp(x):
	return map(int, x.split())
from functools import *

def main():
	for _ in range(int(inp())):
		n = int(inp())
		a = input()
		a = list(a)
		a = list(map(int, a))
		b = input()
		b = list(b)
		b = list(map(int, b))
		a_ind = 0
		b_ind = n - 1
		ans = []
		while a != b:
			if a[b_ind] == b[b_ind]:
				b_ind -= 1
			elif a[b_ind] != b[b_ind]:
				if a[0] != b[b_ind]:
					ans.append(b_ind + 1)
					temp = a[:b_ind + 1]
					a = temp[::-1] + a[b_ind + 1:]
					for i in range(b_ind + 1):
						if a[i] == 0:
							a[i] = 1
						else:
							a[i] = 0
					if a == b:
						break
					b_ind -= 1
				else:
					if a[0] == 1:
						a[0] = 0
					else:
						a[0] = 1
					ans.append(1)
		print(len(ans), *ans)
main()


def operate(A, m):
	B = A[0:m]
	for i in range(m):
		if B[i] == 0:
			B[i] = 1
		else:
			B[i] = 0
	C = [B[m - 1 - i] for i in range(m)]
	return C + A[m:]
t = int(input())
for i in range(t):
	n = int(input())
	A = [int(s) for s in input()]
	B = [int(s) for s in input()]
	ans = []
	for i in range(n, 1, -1):
		if A[i - 1] != B[i - 1]:
			if A[0] != A[i - 1]:
				A[0] = 1 - A[0]
				A = operate(A, i)
				ans.append(1)
				ans.append(i)
			else:
				A = operate(A, i)
				ans.append(i)
	if A[0] != B[0]:
		ans.append(1)
	k = len(ans)
	ans = [k] + ans
	print(' '.join([str(x) for x in ans]))


for _ in range(int(input())):
	n = int(input())
	a = list(input())
	b = list(input())
	d = []
	for i in range(n):
		if a[i] != b[i]:
			d.append(i + 1)
			d.append(1)
			d.append(i + 1)
	d.insert(0, len(d))
	print(*d, sep=' ')


import sys
from math import sqrt
input = sys.stdin.readline
LI = lambda : list(map(int, input().split()))
MI = lambda : map(int, input().split())
SI = lambda : input().strip('\n')
for _ in range(int(input())):
	n = int(input())
	a = SI()
	b = SI()
	ans = []
	for i in range(n):
		if a[i] != b[i]:
			ans.append(i + 1)
			ans.append(1)
			ans.append(i + 1)
	print(len(ans), ' '.join(map(str, ans)))


from sys import stdin, stdout, setrecursionlimit

class Tail_Recursion_Optimization:

	def __init__(self, RECURSION_LIMIT, STACK_SIZE):
		setrecursionlimit(RECURSION_LIMIT)
		threading.stack_size(STACK_SIZE)
		return None

class SOLVE:

	def solve(self):
		R = stdin.readline
		W = stdout.write
		ans = []
		for i in range(int(R())):
			n = int(R())
			a = R()[:-1]
			b = R()[:-1]
			cnt = 0
			s = ''
			for j in range(n):
				if a[j] != b[j]:
					s += str(j + 1) + ' ' + '1' + ' ' + str(j + 1) + ' '
					cnt += 3
			ans.append(str(cnt) + ' ' + s)
		W('\n'.join(ans))
		return 0

def main():
	s = SOLVE()
	s.solve()
main()


x = int(input())
for z in range(x):
	n = int(input())
	s = input()
	b = input()
	l = []
	for i in range(n - 1):
		if s[i] != s[i + 1]:
			l.append(i + 1)
	if s[-1] == '1':
		c = '1'
	else:
		c = '0'
	for i in range(n):
		if b[-1 - i] != c:
			l.append(n - i)
			if c == '0':
				c = '1'
			else:
				c = '0'
	print(len(l), *l)


list_ans = []
for _ in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	list1 = []
	for i in range(n):
		if i % 2 == 0:
			x = a[i // 2]
		else:
			x = str(1 - int(a[n - 1 - i // 2]))
		if x == b[-1 - i]:
			list1.append(1)
			list1.append(n - i)
		else:
			list1.append(n - i)
	s = [len(list1)] + list1
	list_ans.append(' '.join(map(str, s)))
for item in list_ans:
	print(item)


def func(A, B):
	res = []
	i = len(B) - 1
	j = len(A) - 1
	temp = A
	while i >= 0 and j >= 0:
		if i == 0 and j == 0:
			if temp[j] == B[i]:
				return res
			else:
				res.append(1)
				return res
		if temp[j] == B[i]:
			j -= 1
			i -= 1
		elif A[0] == B[i]:
			res.append(1)
			temp = str(1 - int(temp[0])) + temp[1:]
			res.append(j + 1)
			n = ''
			for k in range(j + 1):
				if A[k] == '0':
					n += '1'
				else:
					n += '0'
			n = n[::-1]
			temp = n
			A = temp
			j -= 1
			i -= 1
		else:
			n = ''
			for k in range(j + 1):
				if A[k] == '0':
					n += '1'
				else:
					n += '0'
			res.append(j + 1)
			n = n[::-1]
			temp = n
			A = temp
			j -= 1
			i -= 1
	return res
t = int(input())
for i in range(t):
	n = int(input())
	A = input()
	B = input()
	res = func(A, B)
	if not res:
		print(0)
	else:
		print(len(res), end=' ')
		for i in res:
			print(i, end=' ')
		print()


for _ in range(int(input())):
	n = int(input())
	l1 = list(map(int, input()))
	l2 = list(map(int, input()))
	flip = False
	idx = 0
	ans = []
	for i in range(n - 1, -1, -1):
		if flip ^ (l1[idx] == l2[i]):
			ans.append(1)
		ans.append(i + 1)
		if flip:
			idx -= i
		else:
			idx += i
		flip = not flip
	print(len(ans), *ans)


import sys
from collections import deque
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	a = deque(input().rstrip())
	b = input().rstrip()
	ans = []
	cnt = 0
	for i in range(1, n)[::-1]:
		if cnt % 2 == 0:
			x = a.popleft()
		else:
			x = a.pop()
		if (cnt % 2 == 0) ^ (x == b[i]) == 0:
			ans.extend((1, i + 1))
		else:
			ans.append(i + 1)
		cnt += 1
	if (cnt % 2 == 0) ^ (a[0] == b[0]):
		ans.append(1)
	ansl = len(ans)
	print(ansl, *ans)


t = int(input())
while t != 0:
	n = int(input())
	s1 = list(input())
	s2 = list(input())
	i = n - 1
	ans = []
	count = 0
	while s1 != s2:
		if count % 2 == 0:
			if s2[-1] == s1[-1]:
				s1.pop()
				s2.pop()
			else:
				if s2[-1] == s1[0]:
					ans.append(1)
					if s1[0] == '0':
						s1[0] = '1'
					else:
						s1[0] = '0'
				ans.append(len(s1))
				for i in range(len(s1)):
					if s1[i] == '0':
						s1[i] = '1'
					else:
						s1[i] = '0'
				s1 = s1[::-1]
				s1.pop()
				s2.pop()
		elif s2[-1] != s1[-1]:
			s1.pop()
			s2.pop()
		else:
			if s2[-1] == s1[0]:
				ans.append(1)
				if s1[0] == '0':
					s1[0] = '1'
				else:
					s1[0] = '0'
			ans.append(len(s1))
			for i in range(len(s1)):
				if s1[i] == '0':
					s1[i] = '1'
				else:
					s1[i] = '0'
			s1 = s1[::-1]
			s1.pop()
			s2.pop()
	print(len(ans), *ans)
	t -= 1


import sys
sys.setrecursionlimit(int(100000.0))
t = int(input())
while t:
	t -= 1
	n = int(input())
	s1 = input().strip()
	s2 = input().strip()
	cnt = 0
	for i in range(n):
		if s1[i] != s2[i]:
			cnt += 1
	if cnt:
		print(3 * cnt, end=' ')
		for i in range(n):
			if s1[i] != s2[i]:
				print(i + 1, 1, i + 1, end=' ')
		print()
	else:
		print(0)


t = int(input())
for i in range(t):
	n = int(input())
	s1 = input()
	s2 = input()
	count = 0
	arr = []
	for i in range(n - 1, -1, -1):
		if s1[i] == s2[i]:
			continue
		elif i > 0:
			count = count + 3
			arr.append(i + 1)
			arr.append(1)
			arr.append(i + 1)
		else:
			count += 1
			arr.append(1)
	print(count, end=' ')
	for j in arr:
		print(j, end=' ')
	print()


testcases = int(input())
for j in range(testcases):
	n = int(input())
	vala = [int(b) for b in input()]
	valb = [int(b) for b in input()]
	outy = []
	flips = 0
	adj = 0
	for s in range(n - 1, -1, -1):
		if flips % 2 == 0:
			ind = adj + s
			if (vala[ind] + flips) % 2 == valb[s]:
				continue
			else:
				ind0 = adj
				if (vala[ind0] + flips) % 2 != valb[s]:
					if flips % 2 == 0:
						adj += s
					else:
						adj -= s
				else:
					if flips % 2 == 0:
						adj += s
					else:
						adj -= s
					outy.append(1)
				outy.append(s + 1)
				flips += 1
		else:
			ind = adj - s
			if (vala[ind] + flips) % 2 == valb[s]:
				continue
			else:
				ind0 = adj
				if (vala[ind0] + flips) % 2 != valb[s]:
					if flips % 2 == 0:
						adj += s
					else:
						adj -= s
				else:
					if flips % 2 == 0:
						adj += s
					else:
						adj -= s
					outy.append(1)
				outy.append(s + 1)
				flips += 1
	outy = [len(outy)] + outy
	print(*outy)


def main():
	for _ in range(int(input())):
		n = int(input())
		a = list(input())
		b = list(input())
		i = n - 1
		l = []
		while i >= 0:
			if a[i] == b[i]:
				i -= 1
				pass
			elif str(1 - int(a[0])) != a[i]:
				l.append(i + 1)
				for j in range(i + 1):
					a[j] = str(1 - int(a[j]))
				a[:i + 1] = a[:i + 1][::-1]
			else:
				l.append(1)
				a[0] = str(1 - int(a[0]))
				l.append(i + 1)
				for j in range(i + 1):
					a[j] = str(1 - int(a[j]))
				a[:i + 1] = a[:i + 1][::-1]
		print(len(l), *l)
main()


from sys import stdin
g = lambda : stdin.readline().strip()
gl = lambda : g().split()
gil = lambda : [int(var) for var in gl()]
gcl = lambda : list(g())
gbs = lambda : [int(var) for var in g()]
from collections import deque
removeLast = lambda a, rev: a.popleft() if rev else a.pop()
getLast = lambda a, rev, inv: a[0] ^ inv if rev else a[-1] ^ inv
getFirst = lambda a, rev, inv: a[-1] ^ inv if rev else a[0] ^ inv
(t,) = gil()
for _ in range(t):
	(inv, rev) = (0, 0)
	(n,) = gil()
	(a, b) = (deque(gbs()), gbs())
	r = n - 1
	ans = []
	while r != 0:
		if getLast(a, rev, inv) != b[r]:
			if getLast(a, rev, inv) != getFirst(a, rev, inv):
				if rev:
					a[-1] ^= inv ^ 1
				else:
					a[0] ^= inv ^ 1
				ans.append(1)
			ans.append(r + 1)
			inv ^= 1
			rev ^= 1
		removeLast(a, rev)
		r -= 1
	if a[0] ^ inv != b[0]:
		ans.append(1)
	print(len(ans), end=' ')
	for a in ans:
		print(a, end=' ')
	print()


def ans(n, a, b):
	c = 0
	s = []
	for i in range(n):
		if a[i] != b[i]:
			if i == 0:
				c += 1
				s.append(1)
			else:
				c += 3
				s.append(i + 1)
				s.append(1)
				s.append(i + 1)
	print(c, end=' ')
	for i in s:
		print(i, end=' ')
	print()
t = int(input())
for i in range(t):
	n = int(input())
	a = input()
	b = input()
	ans(n, a, b)


t = int(input())
while t:
	n = int(input())
	a = input()
	b = input()
	k = 0
	p = []
	if a == b:
		print('0')
	elif n == 1:
		print('1 1')
	else:
		for i in range(n):
			if a[i] != b[i]:
				k += 3
				p.append(i + 1)
				p.append(1)
				p.append(i + 1)
		print(k, end=' ')
		print(*p)
	t = t - 1


for w in range(int(input())):
	n = int(input())
	s1 = str(input())
	s2 = str(input())
	arr1 = []
	arr2 = []
	for i in s1:
		arr1.append(int(i))
	for i in s2:
		arr2.append(int(i))
	A = []
	for i in range(n):
		if arr1[i] != arr2[i]:
			A.extend([i + 1, 1, i + 1])
	print(len(A), *A)


def invert_func(a, i):
	b = [0] * i
	for j in range(i):
		b[j] = ['1', '0'][int(a[i - j - 1])]
	if i <= len(a):
		b = b + a[i:]
	return b

def main():
	for _ in range(int(input())):
		n = int(input())
		arr1 = list(input().strip())
		arr2 = input().strip()
		count = 0
		ans = []
		j = n - 1
		while j >= 0:
			if arr1[j] == '0':
				j -= 1
			else:
				if arr1[0] == '0':
					count += 1
					ans.append('1')
					arr1[0] = '1'
				count += 1
				ans.append(str(j + 1))
				arr1 = invert_func(arr1, j + 1)
				j -= 1
		j = n - 1
		flag = '0'
		while j >= 0:
			if arr2[j] == flag:
				j -= 1
			else:
				count += 1
				ans.append(str(j + 1))
				flag = ['1', '0'][int(flag)]
				j -= 1
		print(count, ' '.join(ans))
main()


for _ in range(int(input())):
	n = int(input())
	s = input()
	s1 = input()
	ans = []
	for i in range(n):
		if s[i] != s1[i]:
			ans.append(i)
	print(3 * len(ans), end=' ')
	for j in ans:
		print(j + 1, 1, j + 1, end=' ')
	print()


import sys
import math
import collections
from sys import stdin, stdout, setrecursionlimit
import bisect as bs
setrecursionlimit(2 ** 20)
MOD = 998244353
T = int(stdin.readline())
for _ in range(T):
	n = int(stdin.readline())
	a = stdin.readline().strip('\n')
	b = stdin.readline().strip('\n')
	fin = []
	for i in range(n):
		if a[i] == b[i]:
			continue
		if i == 0:
			fin.append(1)
			continue
		fin.append(i + 1)
		fin.append(1)
		fin.append(i + 1)
	h = len(fin)
	print(h, end=' ')
	for i in range(h):
		print(fin[i], end=' ')
	print('')


t = int(input())
for ii in range(t):
	n = int(input())
	a = input()
	b = input()
	operations = []
	last = n - 1
	while last >= 0:
		if a[last] == b[last]:
			last -= 1
			continue
		elif int(a[0]) != int(b[last]):
			operations.append(last + 1)
			temp = []
			for i in a[:last + 1]:
				if i == '1':
					temp.append('0')
				else:
					temp.append('1')
			temp = temp[::-1]
			a = ''.join(temp) + a[last + 1:]
		else:
			operations.append(1)
			a = str(1 - int(a[0])) + a[1:]
			operations.append(last + 1)
			temp = []
			for i in a[:last + 1]:
				if i == '1':
					temp.append('0')
				else:
					temp.append('1')
			temp = temp[::-1]
			a = ''.join(temp) + a[last + 1:]
		last -= 1
	print(len(operations), end=' ')
	print(*operations)


import math as mt
import sys, string
input = sys.stdin.readline
L = lambda : list(map(int, input().split()))
Ls = lambda : list(input().split())
M = lambda : map(int, input().split())
I = lambda : int(input())
from collections import defaultdict
t = I()
for _ in range(t):
	n = I()
	s1 = input().strip()
	s2 = input().strip()
	a = []
	b = []
	for i in range(n):
		if s1[i] == '1':
			a.append(1)
		else:
			a.append(0)
	for i in range(n):
		if s2[i] == '1':
			b.append(1)
		else:
			b.append(0)
	l = []
	r = n
	while r != 0:
		if a[r - 1] != b[r - 1]:
			if a[0] == b[r - 1]:
				l.append(1)
				a[0] ^= 1
				l.append(r)
			else:
				l.append(r)
			for i in range(r // 2):
				(a[i], a[r - 1 - i]) = (a[r - 1 - i], a[i])
			for i in range(r):
				a[i] ^= 1
		r -= 1
	print(len(l), *l)


t = int(input())
while t:
	t = t - 1
	n = int(input())
	a = list(input())
	b = input()
	rev = 0
	i = 0
	j = n - 1
	n = n - 1
	l = []
	while n >= 0:
		if rev == 0:
			rev = 1
			if a[i] == b[n]:
				l.append(1)
			l.append(n + 1)
			i = i + 1
			n = n - 1
		elif rev == 1:
			rev = 0
			if a[j] != b[n]:
				l.append(1)
			l.append(n + 1)
			j = j - 1
			n = n - 1
	print(len(l), end=' ')
	for i in l:
		print(i, end=' ')
	print()


import os
import sys
from io import BytesIO, IOBase

def pre_switch(a, x):
	ac = a[:x]
	for i in range(x):
		a[i] = 1 - ac[x - i - 1]

def main():
	t = Iint()
	for _ in range(t):
		n = Iint()
		a = [1 if i == '1' else 0 for i in I()]
		b = [1 if i == '1' else 0 for i in I()]
		ans = []
		i = n
		for i in range(n, 1, -1):
			if a[i - 1] == b[i - 1]:
				pass
			elif a[0] != b[i - 1]:
				pre_switch(a, i)
				ans.append(i)
			else:
				a[0] = 1 - a[0]
				pre_switch(a, i)
				ans.append(1)
				ans.append(i)
		if a[0] != b[0]:
			ans.append(1)
		print(len(ans), end=' ')
		Plist(ans)
FIO = 1

def I():
	return input()

def Iint():
	return int(input())

def Plist(li, s=' '):
	print(s.join(map(str, li)))
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))

def input():
	return sys.stdin.readline().rstrip('\r\n')
main()


test = int(input())
for i in range(test):
	n = int(input())
	a = [i for i in input()]
	b = [i for i in input()]
	ans = []
	if a == b:
		print(0)
	else:
		for i in range(n - 1, -1, -1):
			if a[i] != b[i]:
				if a[i] != a[0]:
					ans.append(1)
					a[0] = a[i]
				for i in range(i + 1):
					if a[i] == '0':
						a[i] = '1'
					else:
						a[i] = '0'
				a[:i + 1] = a[:i + 1][::-1]
				ans.append(i + 1)
		print(len(ans), *ans, sep=' ')


def readints():
	return [int(e) for e in input()]

def main():
	n = int(input())
	a = [int(e) for e in input()]
	b = [int(e) for e in input()]
	ans = []
	(left, right, rev, flip, b_last_pos) = (0, n - 1, False, False, n)
	while b_last_pos > 0:
		b_last_pos -= 1
		if rev:
			a_last = a[left]
			a_zero = a[right]
		else:
			a_last = a[right]
			a_zero = a[left]
		if flip:
			a_last = 1 - a_last
			a_zero = 1 - a_zero
		if a_last != b[b_last_pos]:
			if a_zero == b[b_last_pos]:
				ans.append(1)
			flip = not flip
			rev = not rev
			ans.append(b_last_pos + 1)
		if rev:
			left += 1
		else:
			right -= 1
	print(len(ans), end=' ')
	print(*ans, sep=' ')
tests = int(input())
for _ in range(tests):
	main()


def fun(a, b, n):
	ans = []
	i = n - 1
	while i > -1:
		if a[i] == b[i]:
			pass
		else:
			if a[0] == b[i]:
				ans.append(1)
				if a[0] == '0':
					a[0] = '1'
				else:
					a[0] = '0'
			ans.append(i + 1)
			for j in range(i + 1):
				if a[j] == '0':
					a[j] = '1'
				else:
					a[j] = '0'
			(k, j) = (0, i)
			while k < j:
				temp = a[k]
				a[k] = a[j]
				a[j] = temp
				k += 1
				j -= 1
		i -= 1
	return ans
for _ in range(int(input())):
	n = int(input())
	a = list(input())
	b = list(input())
	arr = fun(a, b, n)
	print(len(arr), end=' ')
	if len(arr) > 0:
		print(*arr)
	else:
		print()


from collections import deque
import sys

def input():
	return sys.stdin.readline().rstrip()

def slv():
	n = int(input())
	S = list(input())
	S = list(map(int, S))
	T = list(input())
	T = list(map(int, T))
	T.reverse()
	res = []
	idx_list = deque([i for i in range(n)])
	for i in range(n):
		if i % 2 == 0:
			idx = idx_list.popleft()
			num = S[idx] ^ 1
			if num == T[i]:
				res.append(n - i)
			else:
				res.append(1)
				res.append(n - i)
		else:
			idx = idx_list.pop()
			num = S[idx]
			if num == T[i]:
				res.append(n - i)
			else:
				res.append(1)
				res.append(n - i)
	L = len(res)
	res = [L] + res
	print(*res)
	return

def main():
	t = int(input())
	for _ in range(t):
		slv()
	return
main()


import sys, math
import io, os
for t in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	ans = []
	for i in range(n):
		if a[i] == b[i]:
			continue
		ans.append(i + 1)
		ans.append(1)
		ans.append(i + 1)
	print(str(len(ans)) + ' ' + ' '.join(map(str, ans)))


def get(c, flip):
	if flip:
		return '1' if c == '0' else '0'
	else:
		return c
t = int(input())
for _ in range(t):
	l = int(input())
	src = list(input())
	dst = list(input())
	hd = 0
	last = l - 1
	idx = l - 1
	dir = -1
	flip = False
	ans = []
	while idx >= 0:
		if dst[idx] == get(src[last], flip):
			last += dir
		elif hd == last:
			ans.append(str(1))
		else:
			if get(src[hd], flip) == dst[idx]:
				ans.append(str(1))
			ans.append(str(idx + 1))
			flip = not flip
			(hd, last) = (last, hd)
			dir *= -1
			last += dir
		idx -= 1
	print(len(ans), ' '.join(ans))


t = int(input().strip(' '))
for i in range(t):
	n = int(input().strip(' '))
	a = input().strip(' ')
	b = input().strip(' ')
	if a == b:
		print(0)
		continue
	s = []
	x = 0
	for i in range(n - 1):
		if a[i] != a[i + 1]:
			s.append(str(i + 1))
			x = x + 1
	i = n - 2
	curr = a[-1]
	if a[-1] != b[-1]:
		s.append(str(n))
		x = x + 1
	while i >= 0:
		if b[i] != b[i + 1]:
			s.append(str(i + 1))
			x = x + 1
		i = i - 1
	print(str(x) + ' ' + ' '.join(s))


import sys, os, io

def rs():
	return sys.stdin.readline().rstrip()

def ri():
	return int(sys.stdin.readline())

def ria():
	return list(map(int, sys.stdin.readline().split()))

def ws(s):
	sys.stdout.write(s + '\n')

def wi(n):
	sys.stdout.write(str(n) + '\n')

def wia(a):
	sys.stdout.write(' '.join([str(x) for x in a]) + '\n')
import math, datetime, functools, itertools
from collections import deque, defaultdict, OrderedDict
import collections

def main():
	starttime = datetime.datetime.now()
	if os.path.exists('input.txt'):
		sys.stdin = open('input.txt', 'r')
		sys.stdout = open('output.txt', 'w')
	for _ in range(ri()):
		n = ri()
		a = rs()
		b = rs()
		s = ''
		if a == b:
			print(0)
			continue
		if n == 1:
			print(1, 1)
			continue
		ans1 = []
		ans2 = []
		z = 0
		c = 0
		for j in range(n):
			if a[j] == '0':
				break
			else:
				c += 1
		if c > 0:
			ans1.append(c)
		i = c
		while i < n:
			if a[i] == '1':
				if i - 1 >= 0:
					ans1.append(i)
					s = '1' * i
				if a[i:] == '1' * (n - len(s)):
					z = 1
					break
				for k in range(i, n):
					if a[k] == '0':
						break
				ans1.append(k)
				i = k + 1
			else:
				i += 1
		if z == 1:
			ans1.append(n)
		z = 0
		c = 0
		for j in range(n):
			if b[j] == '0':
				break
			else:
				c += 1
		if c > 0:
			ans2.append(c)
		i = c
		while i < n:
			if b[i] == '1':
				if i - 1 >= 0:
					ans2.append(i)
					s = '1' * i
				if b[i:] == '1' * (n - len(s)):
					z = 1
					break
				for k in range(i, n):
					if b[k] == '0':
						break
				ans2.append(k)
				i = k + 1
			else:
				i += 1
		if z == 1:
			ans2.append(n)
		print(len(ans1) + len(ans2), *ans1 + ans2[::-1])
	endtime = datetime.datetime.now()
	time = (endtime - starttime).total_seconds() * 1000
	if os.path.exists('input.txt'):
		print('Time:', time, 'ms')

class FastReader(io.IOBase):
	newlines = 0

	def __init__(self, fd, chunk_size=1024 * 8):
		self._fd = fd
		self._chunk_size = chunk_size
		self.buffer = io.BytesIO()

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self, size=-1):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size if size == -1 else size))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

class FastWriter(io.IOBase):

	def __init__(self, fd):
		self._fd = fd
		self.buffer = io.BytesIO()
		self.write = self.buffer.write

	def flush(self):
		os.write(self._fd, self.buffer.getvalue())
		(self.buffer.truncate(0), self.buffer.seek(0))

class FastStdin(io.IOBase):

	def __init__(self, fd=0):
		self.buffer = FastReader(fd)
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

class FastStdout(io.IOBase):

	def __init__(self, fd=1):
		self.buffer = FastWriter(fd)
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.flush = self.buffer.flush
sys.stdin = FastStdin()
sys.stdout = FastStdout()
main()


def rev(a, n):
	for i in range(n):
		if a[i] == '1':
			a[i] = '0'
		else:
			a[i] = '1'
	t = a[0:n][::-1]
	for i in range(n):
		a[i] = t[i]

def fun(a, b, n):
	if n == 0:
		return
	if a[n - 1] == b[n - 1]:
		fun(a, b, n - 1)
	elif a[0] != b[n - 1]:
		fun.ans.append(n)
		rev(a, n)
		fun(a, b, n - 1)
	else:
		fun.ans.append(1)
		fun.ans.append(n)
		rev(a, 1)
		rev(a, n)
		fun(a, b, n - 1)
for _ in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	aa = list(a)
	bb = list(b)
	fun.ans = []
	fun(aa, bb, n)
	print(len(fun.ans), end=' ')
	for i in fun.ans:
		print(i, end=' ')
	print()


for z in range(int(input())):
	n = int(input())
	a = [int(i) for i in list(input())]
	b = [int(i) for i in list(input())]
	k = 0
	arr = []
	for i in range(n - 1, -1, -1):
		if a[i] != b[i]:
			if a[0] != b[i]:
				k += 1
				arr.append(i + 1)
				c = [0 for i in range(n)]
				for j in range(i + 1):
					c[j] = 1 - a[i - j]
				for j in range(i + 1):
					a[j] = c[j]
			elif i == 0:
				k += 1
				a[0] = 1 - a[0]
				arr.append(1)
			else:
				k += 2
				arr.append(1)
				arr.append(i + 1)
				a[0] = 1 - a[0]
				c = [0 for i in range(n)]
				for j in range(i + 1):
					c[j] = 1 - a[i - j]
				for j in range(i + 1):
					a[j] = c[j]
	print(len(arr), end=' ')
	for i in arr:
		print(i, end=' ')
	print()


for w in range(int(input())):
	n = int(input())
	s1 = str(input())
	s2 = str(input())
	arr1 = []
	arr2 = []
	for i in s1:
		arr1.append(int(i))
	for i in s2:
		arr2.append(int(i))
	arr1.append(0)
	arr2.append(0)
	op1 = []
	op2 = []
	for i in range(1, n + 1):
		if arr1[i] != arr1[i - 1]:
			op1.append(i)
	for i in range(1, n + 1):
		if arr2[i] != arr2[i - 1]:
			op2.append(i)
	op = op1 + op2[::-1]
	print(len(op), *op)


import sys
input = sys.stdin.readline
(inp, ip) = (lambda : int(input()), lambda : [int(w) for w in input().split()])
for _ in range(inp()):
	n = inp()
	a = input().strip()
	b = input().strip()
	ans = []
	for i in range(n - 1, -1, -1):
		if a[i] != b[i]:
			ans.extend([i + 1, 1, i + 1])
	print(len(ans), *ans)


T = int(input())
for t in range(0, T):
	n = int(input())
	a = input()
	b = input()
	sol = []
	for i in range(0, n):
		if a[i] != b[i]:
			sol.append(i + 1)
			sol.append(1)
			sol.append(i + 1)
	string = ' '.join(map(str, sol))
	print('%d %s' % (len(sol), string))


def get_flipped(x, flips):
	if flips % 2 == 0:
		return x
	else:
		return '0' if x == '1' else '1'

def solve():
	n = int(input().strip())
	a = input().strip()
	b = input().strip()
	ar = []
	for i in range(n):
		this_bit = b[n - i - 1]
		first_bit = get_flipped(a[i // 2] if i % 2 == 0 else a[n - 1 - i // 2], i)
		if first_bit == this_bit:
			ar.append(1)
		ar.append(n - i)
	print(len(ar), *ar)
t = int(input().strip())
for _ in range(t):
	solve()


for i in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	a = list(a)
	b = list(b)
	d = []
	e = 0
	i = 0
	while a != b:
		if a[i] != b[i]:
			c = a[:i + 1]
			del a[:i + 1]
			e += 1
			d.append(i + 1)
			d.append(1)
			d.append(i + 1)
			for j in range(len(c)):
				if c[j] == '0':
					c[j] = '1'
				else:
					c[j] = '0'
			c.reverse()
			if c[0] == '0':
				c[0] = '1'
			else:
				c[0] = '0'
			for j in range(len(c)):
				if c[j] == '0':
					c[j] = '1'
				else:
					c[j] = '0'
			c.reverse()
			a = c + a
		else:
			i += 1
	print(3 * e, *d)


for _ in range(int(input())):
	n = int(input())
	a = input('')
	a = list(map(int, a))
	b = input('')
	b = list(map(int, b))
	if a == b:
		print(0)
		continue

	def reverse(n):
		for i in range(n // 2):
			a[i] ^= 1
			a[n - i - 1] ^= 1
			(a[i], a[n - i - 1]) = (a[n - i - 1], a[i])
		if n & 1:
			a[n // 2] ^= 1
	ans = []
	count = 0
	for i in range(n):
		if a[0] == b[-i - 1]:
			count += 1
			a[0] ^= 1
			ans.append(1)
		count += 1
		ans.append(n - i)
		reverse(n - i)
	print(count, *ans)


for _ in range(int(input())):
	n = int(input())
	l1 = list(map(int, input()))
	l2 = list(map(int, input()))
	cnt = 0
	ans = []
	idx = 0
	for i in range(n - 1, -1, -1):
		if l1[i] != l2[i]:
			if l1[0] == l2[i]:
				cnt += 2
				ans.append(1)
				l1[0] = 0 if l1[0] == 1 else 1
				ans.append(i + 1)
			else:
				cnt += 1
				ans.append(i + 1)
			for j in range(0, n - idx):
				if l1[j] == 0:
					l1[j] = 1
				else:
					l1[j] = 0
			l1 = l1[:i + 1][::-1] + l1[i + 1:]
			idx += 1
	print(cnt, *ans)


import math
from decimal import Decimal
import heapq
import copy
import heapq
from collections import deque

def na():
	n = int(input())
	b = [int(x) for x in input().split()]
	return (n, b)

def nab():
	n = int(input())
	b = [int(x) for x in input().split()]
	c = [int(x) for x in input().split()]
	return (n, b, c)

def dv():
	(n, m) = map(int, input().split())
	return (n, m)

def da():
	(n, m) = map(int, input().split())
	a = list(map(int, input().split()))
	return (n, m, a)

def dva():
	(n, m) = map(int, input().split())
	a = [int(x) for x in input().split()]
	b = [int(x) for x in input().split()]
	return (n, m, b)

def eratosthenes(n):
	sieve = list(range(n + 1))
	for i in sieve:
		if i > 1:
			for j in range(i + i, len(sieve), i):
				sieve[j] = 0
	return sorted(set(sieve))

def lol(lst, k):
	k = k % len(lst)
	ret = [0] * len(lst)
	for i in range(len(lst)):
		if i + k < len(lst) and i + k >= 0:
			ret[i] = lst[i + k]
		if i + k >= len(lst):
			ret[i] = lst[i + k - len(lst)]
		if i + k < 0:
			ret[i] = lst[i + k + len(lst)]
	return ret

def nm():
	n = int(input())
	b = [int(x) for x in input().split()]
	m = int(input())
	c = [int(x) for x in input().split()]
	return (n, b, m, c)

def dvs():
	n = int(input())
	m = int(input())
	return (n, m)

def fact(n):
	tc = []
	ans = {}
	d = 2
	while d * d <= n:
		if n % d == 0:
			tc.append(d)
			n //= d
		else:
			d += 1
	if n > 1:
		tc.append(n)
	for i in tc:
		ans[i] = ans.get(i, 0) + 1
	return ans

def to_zero(n, s):
	lst = s[0]
	ans = []
	for i in range(n):
		if lst == s[i]:
			continue
		elif lst == '0':
			lst = '1'
			ans.append(i)
		else:
			lst = '0'
			ans.append(i)
	if lst == '1':
		ans.append(n)
	return ans
for _ in range(int(input())):
	n = int(input())
	s = input()
	t = input()
	ans = to_zero(n, s) + to_zero(n, t)[::-1]
	ans = [len(ans)] + ans
	print(*ans)


t = int(input())
for _ in range(t):
	n = int(input())
	a = input()
	b = input()
	ans = []
	for i in range(n - 1, -1, -1):
		if a[i] != b[i]:
			if b[i] != a[0]:
				ans.append(i + 1)
				c = ''
				for j in range(i + 1):
					if a[j] == '1':
						c = '0' + c
					else:
						c = '1' + c
				a = c + a[len(c):]
			else:
				if a[0] == '1':
					a = '0' + a[1:]
				else:
					a = '1' + a[1:]
				ans.append(1)
				c = ''
				for j in range(i + 1):
					if a[j] == '1':
						c = '0' + c
					else:
						c = '1' + c
				a = c + a[len(c):]
				ans.append(i + 1)
	print(len(ans), *ans)


def func(n, c1, c2):
	output1 = []
	output2 = []
	for i in range(n - 1):
		if c1[i] != c1[i + 1]:
			output1.append(i + 1)
	if c1[-1] == '1':
		output1.append(n)
	for i in range(n - 1):
		if c2[i] != c2[i + 1]:
			output2.append(i + 1)
	if c2[-1] == '1':
		output2.append(n)
	output = output1 + output2[::-1]
	output.insert(0, len(output))
	return ' '.join(list(map(str, output)))

def main():
	for i in range(int(input())):
		n = int(input())
		c1 = input()
		c2 = input()
		print(func(n, c1, c2))
main()


import sys, os, io
from math import log, gcd, ceil
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop
import math

def ncr(n, r, p):
	num = den = 1
	for i in range(r):
		num = num * (n - i) % p
		den = den * (i + 1) % p
	return num * pow(den, p - 2, p) % p

def primeFactors(n):
	l = []
	while n % 2 == 0:
		l.append(2)
		n = n / 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			l.append(int(i))
			n = n / i
	if n > 2:
		l.append(n)
	return list(set(l))

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def si():
	return input()

def prefix_sum(arr):
	r = [0] * (len(arr) + 1)
	for (i, el) in enumerate(arr):
		r[i + 1] = r[i] + el
	return r

def divideCeil(n, x):
	if n % x == 0:
		return n // x
	return n // x + 1

def ii():
	return int(input())

def li():
	return list(map(int, input().split()))

def ws(s):
	sys.stdout.write(s + '\n')

def wi(n):
	sys.stdout.write(str(n) + '\n')

def wia(a):
	sys.stdout.write(' '.join([str(x) for x in a]) + '\n')
if os.path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
t = 1
t = int(input())
for _ in range(t):
	n = ii()
	a = list(si())
	b = list(si())
	i = 0
	j = n - 1
	ptr = n - 1
	rev = 0
	ans = []
	while i <= j and ptr >= 0:
		if rev == 0:
			if b[ptr] == a[i]:
				ans.append(1)
				rev = 1
				ans.append(ptr + 1)
				ptr -= 1
				i += 1
			else:
				rev = 1
				ans.append(ptr + 1)
				ptr -= 1
				i += 1
		elif b[ptr] != a[j]:
			ans.append(1)
			rev = 0
			ans.append(ptr + 1)
			ptr -= 1
			j -= 1
		else:
			rev = 0
			ans.append(ptr + 1)
			ptr -= 1
			j -= 1
	print(len(ans), *ans)


for _ in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	a = [int(a[i]) for i in range(n)]
	b = [int(b[i]) for i in range(n)]
	count = 0
	res = []
	for i in range(n - 1, 0, -1):
		id = n - 1 - i
		if id % 2 == 0:
			ID = n - 1 - id // 2
			now = (a[ID] + n - 1 - i) % 2
		else:
			ID = (1 + id) // 2
			now = (a[ID] + n - 1 - i) % 2
		if now == b[i]:
			if a[0] == now:
				count += 1
				res.append(1)
			count += 1
			res.append(i + 1)
			a[0] = (now + 1) % 2
		else:
			if a[0] != now:
				count += 1
				res.append(1)
			count += 1
			res.append(i + 1)
			a[0] = (now + 1) % 2
	if a[0] != b[0]:
		res.append(1)
		count += 1
	res = [count] + res
	print(*res)


from copy import deepcopy

def prefix_change(a, l):
	temp = deepcopy(a)
	for i in range(l):
		a[i] = 1 - temp[l - 1 - i]
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, list(input())))
	b = list(map(int, list(input())))
	temp = a.copy()
	ls = []
	cnt = 0
	one = 0
	for i in range(n - 1, -1, -1):
		if a[i] != b[i]:
			if b[i] != a[0]:
				ls.append(i + 1)
				prefix_change(a, i + 1)
			else:
				ls.append(1)
				one += 1
				a[0] = 1 - int(a[i])
				ls.append(i + 1)
				prefix_change(a, i + 1)
				cnt += 1
	print(len(ls), *ls)


import sys
t = int(input())
for i in range(t):
	n = int(input())
	a = input()
	b = input()
	j = n - 1
	while a[j] == b[j]:
		j -= 1
		if j == -1:
			break
	if j == -1:
		print(0)
		continue
	if j == 0:
		print(1, 1)
		continue
	res = []
	k = 0
	l = j
	f = 1
	try:
		while k <= l and j >= 0:
			if f:
				if b[j] == a[k]:
					res.append(1)
					res.append(l - k + 1)
				else:
					res.append(l - k + 1)
				k += 1
				j -= 1
				while b[j] != a[k] and j >= 0:
					j -= 1
					k += 1
				f = 0
			else:
				if b[j] == a[l]:
					res.append(l - k + 1)
				else:
					res.append(1)
					res.append(l - k + 1)
				l -= 1
				j -= 1
				while b[j] == a[l] and j >= 0:
					j -= 1
					l -= 1
				f = 1
	except IndexError:
		pass
	print(len(res), *res)


from collections import defaultdict
import sys, math
f = None
try:
	f = open('q1.input', 'r')
except IOError:
	f = sys.stdin
if 'xrange' in dir(__builtins__):
	range = xrange

def print_case_iterable(case_num, iterable):
	print('Case #{}: {}'.format(case_num, ' '.join(map(str, iterable))))

def print_case_number(case_num, iterable):
	print('Case #{}: {}'.format(case_num, iterable))

def print_iterable(A):
	print(' '.join(A))

def read_int():
	return int(f.readline().strip())

def read_int_array():
	return [int(x) for x in f.readline().strip().split(' ')]

def rns():
	a = [x for x in f.readline().split(' ')]
	return (int(a[0]), a[1].strip())

def read_string():
	return list(f.readline().strip())

def ri():
	return int(f.readline().strip())

def ria():
	return [int(x) for x in f.readline().strip().split(' ')]

def rns():
	a = [x for x in f.readline().split(' ')]
	return (int(a[0]), a[1].strip())

def rs():
	return list(f.readline().strip())

def bi(x):
	return bin(x)[2:]
from collections import deque
import math
NUMBER = 10 ** 9 + 7

def factorial(n):
	M = NUMBER
	f = 1
	for i in range(1, n + 1):
		f = f * i % M
	return f

def mult(a, b):
	return a * b % NUMBER

def minus(a, b):
	return (a - b) % NUMBER

def plus(a, b):
	return (a + b) % NUMBER

def egcd(a, b):
	if a == 0:
		return (b, 0, 1)
	else:
		(g, y, x) = egcd(b % a, a)
		return (g, x - b // a * y, y)

def modinv(a):
	m = NUMBER
	(g, x, y) = egcd(a, m)
	if g != 1:
		raise Exception('modular inverse does not exist')
	else:
		return x % m

def choose(n, k):
	if n < k:
		assert false
	return mult(factorial(n), modinv(mult(factorial(k), factorial(n - k)))) % NUMBER
from collections import deque, defaultdict
import heapq
from types import GeneratorType

def bootstrap(f, stack=[]):

	def wrappedfunc(*args, **kwargs):
		if stack:
			return f(*args, **kwargs)
		else:
			to = f(*args, **kwargs)
			while True:
				if type(to) is GeneratorType:
					stack.append(to)
					to = next(to)
				else:
					stack.pop()
					if not stack:
						break
					to = stack[-1].send(to)
			return to
	return wrappedfunc

def dfs(g, timeIn, timeOut, depths, parents):
	cnt = 0
	stack = [[1, 0, 0, 0]]
	while stack:
		(v, neig_i, parent, depth) = stack[-1]
		parents[v] = parent
		depths[v] = depth
		if neig_i == 0:
			timeIn[v] = cnt
			cnt += 1
		while neig_i < len(g[v]):
			u = g[v][neig_i]
			if u == parent:
				neig_i += 1
				continue
			stack[-1][1] = neig_i + 1
			stack.append([u, 0, v, depth + 1])
			break
		if neig_i == len(g[v]):
			stack.pop()
			timeOut[v] = cnt
			cnt += 1
cnt = 0

@bootstrap
def dfs(v, adj, timeIn, timeOut, depths, parents, parent=0, depth=0):
	global cnt
	parents[v] = parent
	depths[v] = depth
	timeIn[v] = cnt
	cnt += 1
	for u in adj[v]:
		if u == parent:
			continue
		yield dfs(u, adj, timeIn, timeOut, depths, parents, v, depth + 1)
	timeOut[v] = cnt
	cnt += 1
	yield

def gcd(a, b):
	if a == 0:
		return b
	return gcd(b % a, a)

def lcm(a, b):
	return a * b / gcd(a, b)

def get_num_2_5(n):
	twos = 0
	fives = 0
	while n > 0 and n % 2 == 0:
		n //= 2
		twos += 1
	while n > 0 and n % 5 == 0:
		n //= 5
		fives += 1
	return (twos, fives)

def shift(a, i, num):
	for _ in range(num):
		(a[i], a[i + 1], a[i + 2]) = (a[i + 2], a[i], a[i + 1])

def equal(x, y):
	return abs(x - y) <= 1e-09

def getAngle(a, b, c):
	ang = math.degrees(math.atan2(c[1] - b[1], c[0] - b[0]) - math.atan2(a[1] - b[1], a[0] - b[0]))
	return ang + 360 if ang < 0 else ang

def getLength(a, b):
	return math.sqrt((a[0] - b[0]) ** 2 + (a[1] - b[1]) ** 2)
from heapq import heapify, heappush, heappop

def bfs(adj):
	s = set()
	d = defaultdict(set)
	q = deque([])
	s.add(0)
	d[0].add(0)
	q.append((0, 0))
	while len(q) > 0:
		(v, depth) = q.popleft()
		for u in adj[v]:
			if u not in s:
				s.add(u)
				d[depth + 1].add(u)
				q.append((u, depth + 1))
	return d

@bootstrap
def dfs(u, top, adj, vis):
	vis[u] = 1
	for (v, d) in adj[u]:
		if vis[v] == 2 or d == 0:
			continue
		if vis[v] == 1:
			yield 'No'
		x = (yield dfs(v, top, adj, vis))
		if x == 'No':
			yield 'No'
	vis[u] = 2
	top.append(u)
	yield

def solution(a, b, n):
	res = []
	fi = 0
	se = n - 1
	bi = n
	cnt = 0
	curr = n - 1
	last = 'r'
	while fi < se:
		bi -= 1
		length = se - fi + 1
		if a[curr] ^ cnt == b[bi]:
			if last == 'r':
				se -= 1
				curr = se
			else:
				fi += 1
				curr = fi
			continue
		if last == 'r' and a[fi] ^ cnt == b[bi]:
			a[fi] ^= 1
			res.append(1)
		elif last == 'l' and a[se] ^ cnt == b[bi]:
			res.append(1)
			a[se] ^= 1
		res.append(length)
		cnt ^= 1
		if last == 'r':
			fi += 1
			curr = fi
			last = 'l'
		else:
			se -= 1
			curr = se
			last = 'r'
	if a[fi] ^ cnt != b[0]:
		res.append(1)
	res = [len(res)] + res
	return ' '.join(map(str, res))

def main():
	T = 1
	T = ri()
	for i in range(T):
		n = ri()
		a = [int(x) for x in rs()]
		b = [int(x) for x in rs()]
		x = solution(a, b, n)
		if 'xrange' not in dir(__builtins__):
			print(x)
		else:
			(print >> output, str(x))
	if 'xrange' in dir(__builtins__):
		print(output.getvalue())
		output.close()
if 'xrange' in dir(__builtins__):
	import cStringIO
	output = cStringIO.StringIO()
main()


def flip(lst):
	for i in range(len(lst)):
		lst[i] = 1 - lst[i]
	return lst[::-1]
for _ in range(int(input())):
	n = int(input())
	s = list(map(int, list(input())))
	t = list(map(int, list(input())))
	j = n - 1
	ans = []
	if t == s:
		print(0)
		continue
	while j >= 0:
		if t[j] == s[0]:
			ans.append(1)
			ans.append(j + 1)
			s[0] = 1 - s[0]
			s = flip(s[:j + 1]) + s[j + 1:]
		else:
			ans.append(j + 1)
			s = flip(s[:j + 1]) + s[j + 1:]
		j -= 1
	if t[0] != s[0]:
		ans.append(1)
	print(len(ans), *ans)


def swap(ind):
	global a
	for i in range(ind + 1):
		a[i] = 1 - a[i]
	for i in range(ind // 2 + 1):
		(a[i], a[ind - i]) = (a[ind - i], a[i])
for _ in range(int(input())):
	n = int(input())
	a = list(input())
	b = list(input())
	a = [int(i) for i in a]
	b = [int(i) for i in b]
	ans = []
	for i in range(n - 1, -1, -1):
		if a[i] == b[i]:
			continue
		if a[i] == a[0]:
			swap(i)
			ans.append(i + 1)
		else:
			swap(0)
			swap(i)
			ans.append(1)
			ans.append(i + 1)
	print(len(ans), ' ')
	print(*ans)


def func(n, c1, c2):
	output = []
	n = [i for i in range(1, n + 1)]
	zipped = zip(n, c1, c2)
	for i in zipped:
		if i[1] != i[2]:
			output.append(str(i[0]))
			output.append(str(1))
			output.append(str(i[0]))
	output.insert(0, str(len(output)))
	return ' '.join(output)

def main():
	for i in range(int(input())):
		n = int(input())
		c1 = input()
		c2 = input()
		print(func(n, c1, c2))
main()


def fr(a, n):
	s = a[:n]
	for i in range(n):
		if s[i] == '0':
			s[i] = '1'
		else:
			s[i] = '0'
	s = list(reversed(s))
	for i in range(n):
		a[i] = s[i]

def process(n, a, b):
	c = 0
	ans = []
	while a != b:
		flag = 0
		for i in range(n):
			if a[i] == b[n - 1]:
				fr(a, i + 1)
				fr(a, n)
				ans.append(i + 1)
				ans.append(n)
				flag = 1
				break
		if flag == 0:
			fr(a, n)
			ans.append(n)
		n -= 1
	print(len(ans), end=' ')
	for i in ans:
		print(i, end=' ')
	print()
	return ans
tests = int(input())
for i in range(0, tests):
	n = int(input())
	a = list(input())
	b = list(input())
	process(n, a, b)


from sys import stdin, stdout
import heapq
import cProfile
from collections import Counter, defaultdict, deque
from functools import reduce
import math

def get_int():
	return int(stdin.readline().strip())

def get_tuple():
	return map(int, stdin.readline().split())

def get_list():
	return list(map(int, stdin.readline().split()))

def flip(a, i):
	ans = ''
	s = 0
	while s <= i:
		if a[s] == '0':
			ans = '1' + ans
		else:
			ans = '0' + ans
		s += 1
	return ans + a[i + 1:]

def solve():
	n = get_int()
	a = input()
	b = input()
	j = n - 1
	ans = []
	for j in range(n - 1, -1, -1):
		if a[j] == b[j]:
			continue
		ans.append(j + 1)
		a = flip(a, j)
		if a[j] == b[j]:
			continue
		ans.append(1)
		a = flip(a, 0)
		if a[j] == b[j]:
			continue
		ans.append(j + 1)
		a = flip(a, j)
	print(len(ans), *ans)

def main():
	solve()
TestCases = True
if TestCases:
	for i in range(get_int()):
		main()
else:
	main()


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(input())
	b = list(input())
	ans = []
	l = 0
	r = n - 1
	for i in range(n - 1, -1, -1):
		if a[i] != b[i]:
			r = i
			break
	else:
		r = 0
	if l == r:
		if a[0] == b[0]:
			print(0)
		else:
			print(1, 1)
		continue
	else:
		flip = False
		count = r
		while l != r:
			if flip == False:
				if a[l] == b[count]:
					if a[l] == '0':
						a[l] = '1'
					else:
						a[l] = '0'
					ans.append(1)
					ans.append(count + 1)
				else:
					ans.append(count + 1)
				flip = True
			else:
				if a[r] != b[count]:
					if a[r] == '0':
						a[r] = '1'
					else:
						a[r] = '0'
					ans.append(1)
					ans.append(count + 1)
				else:
					ans.append(count + 1)
				flip = False
			for i in range(count, -1, -1):
				if flip == True:
					if a[l + count - i] == b[i]:
						l = l + count - i
						count = i
						break
				elif a[r - count + i] != b[i]:
					r = r - count + i
					count = i
					break
			else:
				if flip == False:
					l = r
				else:
					r = l
		if flip == False:
			if a[l] != b[0]:
				ans.append(1)
		elif a[r] == b[0]:
			ans.append(1)
		ans = [len(ans)] + ans
		print(*ans)


def task():
	n = int(input())
	a = input()
	b = input()
	answer = []
	for i in range(len(a)):
		if a[i] != b[i]:
			answer.append(i)
	print(len(answer) * 3, end=' ')
	for e in answer:
		print(e + 1, 1, e + 1, end=' ')
	print()
n = int(input())
for _ in range(n):
	task()


import sys
sys.setrecursionlimit(10 ** 5)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep='\n')

def II():
	return int(sys.stdin.readline())

def MI():
	return map(int, sys.stdin.readline().split())

def LI():
	return list(map(int, sys.stdin.readline().split()))

def LLI(rows_number):
	return [LI() for _ in range(rows_number)]

def SI():
	return sys.stdin.readline()[:-1]
for _ in range(II()):
	n = II()
	a = SI()
	b = SI()
	ans = []
	l = 0
	r = n - 1
	rev = False
	for i in range(n - 1, 0, -1):
		if rev:
			if a[l] != b[i]:
				l += 1
				continue
			if a[r] != b[i]:
				ans.append(1)
			ans.append(i + 1)
			r -= 1
			rev = False
		else:
			if a[r] == b[i]:
				r -= 1
				continue
			if a[l] == b[i]:
				ans.append(1)
			ans.append(i + 1)
			l += 1
			rev = True
	if rev ^ (a[r] != b[0]):
		ans.append(1)
	print(len(ans), *ans)


import sys
input = sys.stdin.readline
t = int(input())
for ii in range(t):
	n = int(input())
	a = [i for i in input() if i != '\n']
	b = [i for i in input() if i != '\n']
	if n == 1:
		if a != b:
			print(1, 1)
		else:
			print(0)
	elif a == b:
		print(0)
	else:
		ans = []
		for i in range(1, n):
			if a[i] != a[i - 1]:
				ans.append(i)
		if a[-1] == '1':
			ok = False
			for i in range(n - 1, 0, -1):
				if b[i] == '0' and ok == False:
					ok = True
					ans.append(i + 1)
				if b[i] != b[i - 1]:
					ok = True
					ans.append(i)
		else:
			ok = False
			for i in range(n - 1, 0, -1):
				if b[i] == '1' and ok == False:
					ok = True
					ans.append(i + 1)
				if b[i] != b[i - 1]:
					ok = True
					ans.append(i)
		print(len(ans), *ans)


import sys
input = sys.stdin.readline
from collections import deque
t = int(input())
for tests in range(t):
	n = int(input())
	a = input().strip()
	b = input().strip()
	Q = deque(a)
	L = []
	while Q:
		L.append(Q.popleft())
		if Q:
			L.append(Q.pop())
	ANS = []
	for i in range(n):
		if i % 2 == 0:
			if L[i] == b[-1 - i]:
				ANS.append(1)
		elif L[i] != b[-1 - i]:
			ANS.append(1)
		ANS.append(n - i)
	print(len(ANS), *ANS)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(input())
	b = list(input())
	res = []
	for i in range(n - 1, 0, -1):
		if a[i] != b[i]:
			if b[i] == a[0]:
				res.append(1)
				if a[0] == '1':
					a[0] = '0'
				else:
					a[0] = '1'
			pre_a = a[:i + 1][::-1]
			for i in range(i + 1):
				if pre_a[i] == '1':
					a[i] = '0'
				else:
					a[i] = '1'
			res.append(i + 1)
	if a[0] != b[0]:
		res.append(1)
	print(len(res), str(res).replace('[', '').replace(']', '').replace(',', ''))


t = int(input())
for tt in range(t):
	n = int(input())
	sa = input()
	sb = input()
	a = []
	b = []
	for i in range(n):
		a.append(int(sa[i]))
		b.append(int(sb[i]))
	a.reverse()
	b.reverse()
	anss = []
	for i in range(n):
		if a[i] == b[i]:
			continue
		else:
			if a[i] == a[-1]:
				anss.append(n - i)
			else:
				anss.append(1)
				anss.append(n - i)
				a[-1] = int(not a[-1])
			temp_right = a[i:n]
			temp_right.reverse()
			for j in range(i, n):
				a[j] = int(not temp_right[j - i])
	print(len(anss), end=' ')
	print(*anss)


for i in range(int(input())):
	n = int(input())
	x = input()
	y = input()
	ans = []
	for j in range(n - 1, 0, -1):
		if x[j] != y[j]:
			ans.append(j + 1)
			ans.append(1)
			ans.append(j + 1)
	if x[0] != y[0]:
		ans.append(1)
	if len(ans) == 0:
		print(0)
	else:
		print(len(ans), *ans)


def answer(n, A, B):
	s = 0
	e = n - 1
	l = []
	reverse = 0
	for i in range(n - 1, -1, -1):
		if reverse == 0:
			if B[i] == A[s]:
				l.append(1)
			l.append(i + 1)
			reverse = 1
			s += 1
		else:
			if B[i] != A[e]:
				l.append(1)
			l.append(i + 1)
			e -= 1
			reverse = 0
	return [len(l)] + l
t = int(input())
for i in range(t):
	n = int(input())
	a = input()
	b = input()
	print(*answer(n, a, b))


for i in range(int(input())):
	n = int(input())
	l = list(map(int, input()))
	s = list(map(int, input()))
	a = []
	c = 0
	for i in range(n):
		if l[i] != s[i]:
			if i == 0:
				c += 1
				a.append(i + 1)
			else:
				c += 3
				a.append(i + 1)
				a.append(1)
				a.append(i + 1)
	print(c, end=' ')
	print(*a)


t = int(input())
for _ in range(t):
	n = int(input())
	s1 = list(input())
	s2 = list(input())
	s12 = s1[-1::-1]
	for i in range(len(s12)):
		if s12[i] == '1':
			s12[i] = '0'
		else:
			s12[i] = '1'
	s1inv = s12
	ans = []
	ind = n
	flag = 0
	for i in range(n):
		curr = n - i - 1
		if flag == 0 and s2[curr] == s1[curr]:
			s1 = s1[:-1]
			s1inv = s1inv[1:]
		elif flag == 1 and s2[curr] == s1inv[curr]:
			s1 = s1[1:]
			s1inv = s1inv[:-1]
		elif flag == 0 and s1[0] == s1[curr]:
			flag = 1
			ans.append(curr)
			s1 = s1[1:]
			s1inv = s1inv[:-1]
		elif flag == 1 and s1inv[0] == s1inv[curr]:
			flag = 0
			ans.append(curr)
			s1 = s1[:-1]
			s1inv = s1inv[1:]
		elif flag == 0:
			flag = 1
			ans.append(0)
			if s1[0] == '0':
				s1[0] = '1'
				s1inv[-1] = '0'
			else:
				s1[0] = '0'
				s1inv[-1] = '1'
			ans.append(curr)
			s1 = s1[1:]
			s1inv = s1inv[:-1]
		else:
			flag = 0
			ans.append(0)
			if s1inv[0] == '0':
				s1inv[0] = '1'
				s1[-1] = '0'
			else:
				s1inv[0] = '0'
				s1[-1] = '1'
			ans.append(curr)
			s1 = s1[:-1]
			s1inv = s1inv[1:]
	for i in range(len(ans)):
		ans[i] += 1
		ans[i] = str(ans[i])
	if len(ans) == 0:
		print(0)
	else:
		print(len(ans), ' '.join(ans))


from sys import stdin, stdout
input = stdin.readline

def write(n, sep='\n'):
	stdout.write(str(n))

def gil():
	return list(map(int, input().split()))

def rv(a, p):
	x = a[:p + 1]
	for i in range(p + 1):
		x[i] = 1 - x[i]
	return x[::-1] + a[p + 1:]
for _t in range(int(input())):
	n = int(input())
	a = list(map(int, list(input().strip()))) + [0]
	b = list(map(int, list(input().strip()))) + [0]
	o = []
	o2 = []
	for i in range(1, n + 1):
		if a[i - 1] != a[i]:
			o.append(i)
		if b[i - 1] != b[i]:
			o2.append(i)
	o = o + o2[::-1]
	write(len(o))
	write(' ')
	for i in o:
		write(i)
		write(' ')
	write('\n')


t = int(input())
for _ in range(t):
	n = int(input())
	a = input()
	b = input()
	ans = []
	st = a[0]
	for i in range(n - 1):
		if a[i] != a[i + 1]:
			ans.append(i + 1)
	if a[-1] == '1':
		ans.append(n)
	temp = []
	for i in range(n - 1):
		if b[i] != b[i + 1]:
			temp.append(i + 1)
	if b[-1] == '1':
		temp.append(n)
	ans = ans + temp[::-1]
	print(len(ans), *ans, sep=' ')


import sys
input = lambda : sys.stdin.readline().rstrip()

def reverse(a):
	return a[::-1]
for _ in range(int(input())):
	n = int(input())
	a = [int(i) for i in input()]
	b = [int(i) for i in input()]
	end = n - 1
	ans = []
	for i in range(n):
		if a[n - 1 - i] != b[n - 1 - i]:
			if a[0] == b[n - 1 - i]:
				a[0] ^= 1
				ans.append(1)
			for j in range(n - i):
				a[j] ^= 1
			a[:n - i] = reverse(a[:n - i])
			ans.append(n - 1 - i + 1)
	print(len(ans), *ans)


for t in range(int(input())):
	n = int(input())
	a = list(map(int, list(input())))
	b = list(map(int, list(input())))
	index = n - 1
	move = 0
	hist = []
	while index > -1:
		if index == 0:
			if a[index] == b[index]:
				pass
			else:
				move = move + 1
				hist.append(1)
			index = index - 1
		elif a[index] == b[index]:
			index = index - 1
		elif a[0] == b[index]:
			move = move + 1
			hist.append(1)
			a[0] = 1 - a[0]
		else:
			for j in range(index + 1):
				a[j] = 1 - a[j]
			a[:index + 1] = a[index::-1]
			move = move + 1
			hist.append(index + 1)
			index = index - 1
	print(move, ' '.join(list(map(str, hist))))


z = input
num = int(z())
for _ in range(num):
	n = int(z())
	l1 = list(z()) + ['0']
	l2 = list(z()) + ['0']
	(lp, lx) = ([], [])
	for i in range(1, n + 1):
		if l1[i] != l1[i - 1]:
			lp.append(i)
		if l2[i] != l2[i - 1]:
			lx.append(i)
	print(len(lp) + len(lx), *lp, *lx[::-1])


from sys import stdin
for _ in range(int(stdin.readline())):
	n = int(stdin.readline())
	a = stdin.readline().strip()
	b = stdin.readline().strip()
	ans = []
	for i in range(n - 1, -1, -1):
		if a[i] != b[i] and i:
			ans.append(i + 1)
			ans.append(1)
			ans.append(i + 1)
		elif a[i] != b[i] and (not i):
			ans.append(i + 1)
	print(len(ans), *ans)


def solve():
	n = int(input())
	a = list(map(int, input()))
	b = list(map(int, input()))
	op = 0
	pref = []
	wrong = []
	for i in reversed(range(n)):
		if a[i] != b[i]:
			if b[i] != a[0]:
				op += 1
				pref.append(i + 1)
				a[:i + 1] = [int(not a[p]) for p in reversed(range(i + 1))]
			else:
				m = 0
				while b[i] != a[m] and m <= i:
					m += 1
				op += 2
				pref.append(m + 1)
				pref.append(i + 1)
				a[:m + 1] = [int(not a[p]) for p in reversed(range(m + 1))]
				a[:i + 1] = [int(not a[p]) for p in reversed(range(i + 1))]
	print(op, end=' ')
	print(*pref)
t = int(input())
for test in range(t):
	solve()


import math
from collections import deque
from sys import stdin, stdout
from string import ascii_letters
input = stdin.readline
letters = ascii_letters[:26]
for _ in range(int(input())):
	n = int(input())
	first = list(input().strip())
	second = list(input().strip())
	res = []
	for i in range(n - 1, -1, -1):
		if (n - 1 - i) % 2:
			x = '0' if first[i + (n - i) // 2] == '1' else '1'
		else:
			x = first[(n - 1 - i) // 2]
		if i == 0:
			if x != second[i]:
				res.append(1)
			continue
		if x != second[i]:
			res.append(i + 1)
		else:
			res.append(1)
			res.append(i + 1)
	print(len(res), *res)


zero_condn = 1

def k23mrk(word):
	return [char for char in word]

def rev(li, e):
	liref = li[0:e]
	liref = liref[::-1]
	for i in range(len(liref)):
		li[i] = liref[i]
	return li

def flip(li, e):
	for i in range(e):
		if li[i] == '1':
			li[i] = '0'
		else:
			li[i] = '1'
	return li
t = int(input())
for _ in range(t):
	n1 = int(input())
	s1 = input()
	s2 = input()
	list2 = k23mrk(s2)
	list1 = k23mrk(s1)
	i = n1 - 1
	ekmfke = []
	while i >= 0:
		if i == 0 and zero_condn:
			if list1[0] != list2[0]:
				ekmfke.append(1)
			break
		if list1[i] != list2[i] and zero_condn:
			j = i
			if list1[0] != list2[i]:
				ekmfke.append(i + 1)
				list1 = flip(list1, i + 1)
				list1 = rev(list1, i + 1)
			else:
				j = i
				for k in range(i - 1, -1, -1):
					if list1[k] == list2[i]:
						j = k
						break
				list1 = flip(list1, j + 1)
				list1 = rev(list1, j + 1)
				ekmfke.append(j + 1)
				ekmfke.append(i + 1)
				list1 = flip(list1, i + 1)
				list1 = rev(list1, i + 1)
		i = i - 1
	print(len(ekmfke), end=' ')
	for i in ekmfke:
		print(i, end=' ')
	print()


def f(c):
	if c == '1':
		return '0'
	else:
		return '1'
for _ in range(int(input())):
	n = int(input())
	a = list(input())
	b = list(input())
	l = []
	i = n - 1
	j = n - 1
	am = True
	while j >= 0:
		if am:
			if a[i] != b[j]:
				if a[i - j] == b[j]:
					l.append(1)
					a[i - j] = f(a[i - j])
				l.append(j + 1)
				am = False
				i -= j
				i += 1
			else:
				i -= 1
		elif a[i] == b[j]:
			if a[i + j] != b[j]:
				l.append(1)
				a[i + j] = f(a[i + j])
			l.append(j + 1)
			am = True
			i += j
			i -= 1
		else:
			i += 1
		j -= 1
	l1 = [len(l)] + l
	print(*l1)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(input())
	b = list(input())
	answer = []
	if a[0] != b[-1]:
		answer.append(1)
	for i in range(1, n):
		answer.append(i)
		answer.append(i + 1)
		if a[i] == b[-i - 1]:
			answer.append(1)
	print(len(answer), *answer)


def flip(arr_a, arr_b):
	ops = 0
	prefixlis = []
	lis_a = arr_a.copy()
	lis_b = arr_b.copy()
	while arr_a != arr_b:
		same = 0
		for i in range(-1, -len(arr_a) - 1, -1):
			if arr_a[i] == arr_b[i]:
				same += 1
			else:
				break
		if same >= 1:
			arr_a = arr_a[:len(arr_a) - same]
			arr_b = arr_b[:len(arr_b) - same]
		if arr_a == []:
			break
		if arr_a[0] == arr_b[-1]:
			prefixlis.append(1)
			prefixlis.append(len(arr_a))
			operation(arr_a, 1)
			operation(arr_a, len(arr_a))
			ops += 2
			if arr_a == arr_b:
				break
		else:
			prefixlis.append(len(arr_a))
			operation(arr_a, len(arr_a))
			ops += 1
			if arr_a == arr_b:
				break
	print(ops, end=' ')
	if ops == 0:
		return
	for i in range(ops):
		print(prefixlis[i], end=' ')

def operation(arr_a, prefix):
	for i in range(prefix):
		if arr_a[i] == '0':
			arr_a[i] = '1'
		else:
			arr_a[i] = '0'
	arr_a[:prefix] = arr_a[:prefix][::-1]
t = int(input())
for i in range(t):
	n = int(input())
	a = input()
	b = input()
	arr_a = []
	for element in a:
		arr_a.append(element)
	arr_b = []
	for element in b:
		arr_b.append(element)
	if len(arr_a) == 1:
		if arr_a == arr_b:
			print(0)
		else:
			print(1, 1)
	else:
		flip(arr_a, arr_b)
		print()


from sys import *
input = stdin.readline
for _ in range(int(input())):
	n = int(input())
	a1 = input()
	b1 = input()
	a = []
	for i in a1:
		a.append(i)
	b = []
	for i in b1:
		b.append(i)
	a.pop()
	b.pop()
	ans = []
	for i in range(n - 1, -1, -1):
		if a[i] == b[i]:
			continue
		elif a[0] != b[i]:
			k = []
			for j in range(i, -1, -1):
				k.append(a[j])
			a = k + a[i + 1:]
			for j in range(i + 1):
				if a[j] == '1':
					a[j] = '0'
				else:
					a[j] = '1'
			ans.append(i + 1)
		else:
			ans.append(1)
			ans.append(i + 1)
			if a[0] == '1':
				a[0] = '0'
			else:
				a[0] = '1'
			k = []
			for j in range(i, -1, -1):
				k.append(a[j])
			a = k + a[i + 1:]
			for j in range(i + 1):
				if a[j] == '1':
					a[j] = '0'
				else:
					a[j] = '1'
	print(len(ans), *ans)


for _ in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	ans = []
	sign = -1
	for ansi in range(n, 0, -1):
		if (a[(n + ansi * sign) // 2] == b[ansi - 1]) ^ (sign > 0):
			ans.append(1)
		ans.append(ansi)
		sign = -sign
	print(len(ans), *ans)


t = int(input())
for s in range(t):
	n = int(input())
	a = input()
	b = input()
	array = []
	add = 0
	for i in range(n):
		if a[i] != b[i]:
			array.append(i + 1)
			array.append(1)
			array.append(i + 1)
			add += 1
	print(3 * add, end=' ')
	for i in range(3 * add):
		print(array[i], end=' ')
	print()


def flip(a, size):
	res = []
	for i in range(0, size):
		if a[i] == '1':
			res.append('0')
		else:
			res.append('1')
	return res
for _ in range(0, int(input())):
	n = int(input())
	a = list(input().strip())
	b = list(input().strip())
	resa = []
	resb = []
	size = n
	for i in range(0, n - 1):
		if a[i] == '1' and a[i + 1] == '0' or (a[i] == '0' and a[i + 1] == '1'):
			resa.append(i + 1)
	if a[-1] == '1':
		resa.append(n)
	for i in range(0, n - 1):
		if b[i] == '1' and b[i + 1] == '0' or (b[i] == '0' and b[i + 1] == '1'):
			resb.append(i + 1)
	if b[-1] == '1':
		resb.append(n)
	resb.reverse()
	print(len(resa + resb), ' '.join((str(x) for x in resa + resb)))


t = int(input())
for i in range(t):
	n = int(input())
	a = input()
	b = input()
	ans = 0
	ans2 = []
	if a == b:
		print(0)
		continue
	for i in range(n):
		if a[i] != b[i]:
			if i != 0:
				ans2.append(i + 1)
				ans2.append(1)
				ans2.append(i + 1)
				ans += 3
			else:
				ans2.append(1)
				ans += 1
	print(ans, *ans2)


def flip(a, i):
	p = [0] * i
	for j in range(i):
		if a[j] == 1:
			p[j] = 0
		else:
			p[j] = 1
	p.reverse()
	for i in range(i):
		a[i] = p[i]
	return a
t = int(input())
for _ in range(t):
	n = int(input())
	X = [int(i) for i in input()]
	Y = [int(i) for i in input()]
	k = []
	ans = 0
	for i in range(n - 1, -1, -1):
		if X[i] != Y[i]:
			if X[0] != Y[i]:
				k.append(i + 1)
				X = flip(X, i + 1)
				ans += 1
			else:
				k.append(1)
				k.append(i + 1)
				X = flip(X, 1)
				X = flip(X, i + 1)
				ans += 2
	print(ans, end=' ')
	for i in k:
		print(i, end=' ')
	print()


import sys
input = sys.stdin.readline
_DEBUG = False

def flip(ch):
	return '1' if ch == '0' else '0'

def solve(n, a, b):
	if a == b:
		return [0]
	r = []
	inv_flip_count = 0
	(al_idx, ar_idx) = (0, n - 1)
	for i in range(n - 1, -1, -1):
		if inv_flip_count % 2 == 0:
			(a0, ai) = (a[al_idx], a[ar_idx])
		else:
			(a0, ai) = (flip(a[al_idx]), flip(a[ar_idx]))
		if ai != b[i]:
			if a0 != b[i]:
				r.append(i + 1)
			else:
				r.extend([1, i + 1])
			inv_flip_count += 1
			(al_idx, ar_idx) = (ar_idx, al_idx)
		if inv_flip_count % 2 == 0:
			ar_idx -= 1
		else:
			ar_idx += 1
	return [len(r)] + r
for _ in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	sys.stdout.write(' '.join(map(str, solve(n, a, b))) + '\n')