Vivek was quite bored with the lockdown, so he came up with an interesting task. He successfully completed this task and now, he would like you to solve it.
You are given two strings $A$ and $B$, each with length $N$. Let's index the characters in each string from $0$ ― for each $i$ ($0 \le i < N$), the $i+1$-th characters of $A$ and $B$ are denoted by $A_i$ and $B_i$ respectively. You should convert $A$ to $B$ by performing operations of the following type:
- Choose a subset $S$ of the set $\{0, 1, \ldots, N-1\}$.
- Let $c$ be the alphabetically smallest character among $A_x$ for all $x \in S$.
- For each $x \in S$, replace $A_x$ by $c$.
You should find the smallest necessary number of operations or report that it is impossible to convert $A$ to $B$. If it is possible, you also need to find one way to convert $A$ to $B$ using this smallest number of operations. If there are multiple solutions, you may find any one.

-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains a single integer $N$.
- The second line contains a single string $A$.
- The third line contains a single string $B$.

-----Output-----
For each test case:
- If it is impossible to convert $A$ to $B$, print a single line containing the integer $-1$.
- Otherwise, first, print a line containing a single integer $K$ ― the minimum number of operations.
- Then, print $K$ lines describing the operations. Each of these lines should contain a positive integer $Z$, followed by a space and $Z$ pairwise distinct space-separated integers from the set $\{0, 1, \ldots, N-1\}$ ― the elements of $S$.

-----Constraints-----
- $1 \le T \le 20$
- $1 \le N \le 10^3$
- $|A| = |B| = N$
- $A$ and $B$ contain only lowercase English letters

-----Subtasks-----
Subtask #1 (30 points): $B$ contains only characters 'a' and 'b'
Subtask #2 (70 points): original constraints

-----Example Input-----
3
5
abcab
aabab
3
aaa
aab
2
de
cd

-----Example Output-----
2
3 1 2 4
3 0 1 3
-1
-1

-----Explanation-----
Example case 1:
- First, we can choose $S = (1, 2, 4)$, so the character $c$ is 'b' and the string $A$ after this operation is "abbab".
- Then, we choose $S = (0, 1, 3)$, so $c$ is 'a', and $A$ becomes "aabab".
- There is no way to convert $A$ to $B$ in only one operation.
Example case 2: We can see that it is impossible to convert $A$ to $B$ since $c$ is always 'a'.
Example case 3: We can see again that it is impossible to convert $A$ to $B$ since $c$ cannot be 'c'.

Difficulty: MEDIUM_HARD
Tags: String algorithms

Data structures
Skill types: Data structures

Starter code:

Solutions:
for _ in range(int(input())):
	N = int(input())
	c_str = str(input())
	target_str = str(input())
	no_match = False
	for i in range(N):
		if c_str[i] < target_str[i]:
			no_match = True
			break
		if target_str[i] not in c_str:
			no_match = True
			break
	if no_match:
		print(-1)
	else:
		indices = {char: index for (index, char) in enumerate(c_str)}
		m_match = {}
		for i in range(N):
			if c_str[i] != target_str[i]:
				if m_match.get(target_str[i]):
					m_match[target_str[i]].append(str(i))
				else:
					m_match[target_str[i]] = [str(i)]
		print(len(m_match))
		for i in sorted(m_match.keys(), reverse=True):
			print(len(m_match[i]) + 1, ' '.join(m_match[i]), indices[i])


import collections

def possible(a, b):
	setA = collections.Counter(a)
	for i in range(len(a)):
		if ord(a[i]) < ord(b[i]) or b[i] not in setA:
			return True
	return False

def solve(a, b):
	setA = collections.defaultdict(set)
	setB = collections.defaultdict(set)
	for i in range(len(a)):
		setA[a[i]].add(i)
		if a[i] != b[i]:
			setB[b[i]].add(i)
	print(len(setB))
	setBkeys = list(setB.keys())
	setBkeys.sort(reverse=True)
	for key in setBkeys:
		temp = setA[key].union(setB[key])
		print(len(temp), ' '.join(map(str, temp)))
for _ in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	if possible(a, b):
		print(-1)
		continue
	else:
		solve(a, b)


try:
	import string
	alphabets = string.ascii_lowercase
	test = int(input())

	def solve():
		ans = []
		n = int(input())
		string1 = []
		string2 = []
		present = [None] * 26
		string1 = list(input())
		string2 = list(input())
		for i in range(n):
			if string1[i] < string2[i]:
				print(-1)
				return
		for i in range(n):
			present[ord(string1[i]) - ord('a')] = 1
		for i in range(n):
			if present[ord(string2[i]) - ord('a')] != 1:
				print(-1)
				return
		for alpha in alphabets[::-1]:
			index = []
			for i in range(n):
				if string1[i] != alpha and string2[i] == alpha:
					index.append(i)
			if index:
				for i in range(n):
					if string1[i] == alpha:
						index.append(i)
			if index:
				ans.append(index)
			for i in range(len(index)):
				idx = index[i]
				string1[idx] = alpha
		print(len(ans))
		for i in ans:
			i.sort()
			print(len(i), *i)
	for _ in range(test):
		solve()
except:
	pass


from collections import defaultdict

def fn(a, b, n):
	l = []
	da = defaultdict(lambda : [])
	db = defaultdict(lambda : [])
	idx = jdx = 0
	for (i, j) in zip(a, b):
		if i < j or j not in a:
			print(-1)
			return
		da[i].append(idx)
		db[j].append(jdx)
		idx += 1
		jdx += 1
	c = 0
	while a != b:
		mx = max(db)
		tc = [da[mx][0]]
		for i in db[mx]:
			if a[i] != b[i]:
				tc.append(i)
				a[i] = mx
		del db[mx]
		l.append(tc)
		c += 1
	print(c)
	for i in range(c):
		print(len(l[i]), end=' ')
		for j in l[i]:
			print(j, end=' ')
		print()
t = int(input())
for i in range(t):
	n = int(input())
	a = input()
	a = [i for i in a]
	b = input()
	b = [i for i in b]
	fn(a, b, n)


def get_all_indexes_of_substring(string, substring):
	all_indexes = []
	start_pos = 0
	find_result = string.find(substring, start_pos)
	while find_result != -1:
		all_indexes.append(find_result)
		start_pos = find_result + 1
		find_result = string.find(substring, start_pos)
	return all_indexes

def calculate(a, b, n):
	if a == b:
		print(0)
		return
	a_indexes_that_require_change = []
	distinct_elements_in_b = []
	for i in range(n):
		if a[i] == b[i]:
			continue
		if b[i] > a[i]:
			print('-1')
			return
		if b[i] not in a:
			print(-1)
			return
		a_indexes_that_require_change.append(i)
		if b[i] not in distinct_elements_in_b:
			distinct_elements_in_b.append(b[i])
	K = len(distinct_elements_in_b)
	print(K)
	for _ in range(K):
		b_diff_set = [b[i] for i in a_indexes_that_require_change]
		element_to_change = max(b_diff_set)
		all_indices = get_all_indexes_of_substring(''.join(b), element_to_change)
		for index in all_indices:
			if index in a_indexes_that_require_change:
				a_indexes_that_require_change.remove(index)
		seed = a.index(element_to_change)
		if seed not in all_indices:
			all_indices.append(seed)
		all_indices.sort()
		print(len(all_indices), ' '.join(map(str, all_indices)))
T = int(input())
for _ in range(T):
	N = int(input())
	A = input()
	B = input()
	calculate(A, B, N)


import string
t = int(input())
for i in range(t):
	n = int(input())
	a = list(input())
	b = list(input())
	s = []
	c = []
	count = 0
	s_ = []
	if not set(b).issubset(set(a)):
		print(-1)
		continue
	abs_ = string.ascii_lowercase
	for k in range(len(abs_) - 1, -1, -1):
		flag = 0
		f = 0
		j = abs_[k]
		if a == b:
			break
		if j not in c:
			s = []
			for i in range(n):
				if a[i] != b[i] and b[i] == j and (a[i] > j):
					f = 1
					s.append(i)
				if a[i] == j:
					flag = 1
					s.append(i)
			if len(s) != 0:
				count = count + 1
				if len(s) == 1:
					s = []
					count = count - 1
					continue
				if flag == 0 and f == 1 or (flag == 1 and f == 0):
					count = count - 1
					continue
				for i in s:
					a[i] = j
				s_.append(s)
			c.append(j)
	if a != b:
		print(-1)
		continue
	print(count)
	if count != 0:
		for i in s_:
			i = list(map(str, i))
			print(len(i), ' '.join(i))


try:
	import string
	result = string.ascii_lowercase
	t = int(input())

	def solve():
		re = []
		n = int(input())
		a = list(input())
		b = list(input())
		present = [None] * 26
		for i in range(n):
			if a[i] < b[i]:
				print(-1)
				return
		for i in range(n):
			present[ord(a[i]) - ord('a')] = 1
		for i in range(n):
			if present[ord(b[i]) - ord('a')] != 1:
				print(-1)
				return
		for q in result[::-1]:
			w = []
			for i in range(n):
				if a[i] != q and b[i] == q:
					w.append(i)
			if w:
				for i in range(n):
					if a[i] == q:
						w.append(i)
			if w:
				re.append(w)
		print(len(re))
		for i in re:
			i.sort()
			print(len(i), *i)
	for z in range(t):
		solve()
except:
	pass


for _ in range(int(input())):
	n = int(input())
	a1 = input()
	b1 = input()
	a = []
	b = []
	for i in a1:
		a.append(ord(i) - 97)
	for i in b1:
		b.append(ord(i) - 97)
	ind = [-1] * 26
	for i in range(n):
		ind[a[i]] = i
	ans = []
	temp = 0
	for i in range(n):
		if b[i] > a[i]:
			temp = 1
			break
	for i in range(n):
		if ind[b[i]] == -1:
			temp = 1
	for i in range(24, -1, -1):
		ans.append([])
		ans[-1].append(ind[i])
		for j in range(n):
			if a[j] > i and b[j] == i and (ind[i] != -1):
				ans[-1].append(j)
		if temp == 1:
			break
	if temp == 1:
		print(-1)
	else:
		l = 0
		for i in ans:
			if len(i) != 1:
				l += 1
		print(l)
		for i in ans:
			if len(i) != 1:
				print(len(i), end=' ')
				print(*i)


def conv(a, b):
	stop = 0
	n = len(a)
	for i in range(0, n):
		if ord(a[i]) - ord(b[i]) < 0:
			print(-1)
			stop = 1
	if stop == 0:
		for i in range(0, n):
			flag = 0
			for j in range(0, n):
				if b[i] == a[j]:
					flag = 1
					break
			if flag == 0:
				print(-1)
				stop = 1
				break
	if stop == 0:
		distinctArr = []
		for i in range(0, n):
			dontAdd = 0
			if a[i] != b[i]:
				for j in range(0, len(distinctArr)):
					if b[i] == distinctArr[j]:
						dontAdd = 1
				if dontAdd == 0:
					distinctArr.append(b[i])
		distinctArr.sort(reverse=True)
		print(len(distinctArr))
		for i in range(0, len(distinctArr)):
			arr = []
			for j in range(0, n):
				if distinctArr[i] == b[j] and b[j] != a[j]:
					arr.append(j)
			for j in range(0, n):
				if distinctArr[i] == a[j]:
					arr.append(j)
					break
			arr.sort()
			print(len(arr), end=' ')
			for k in range(0, len(arr)):
				if k == len(arr) - 1:
					print(arr[k])
				else:
					print(arr[k], end=' ')
t = int(input())
for i in range(0, t):
	n = int(input())
	a = input()
	b = input()
	arr1 = list(a)
	arr2 = list(b)
	conv(arr1, arr2)


from collections import deque
t = int(input())
while t > 0:
	t -= 1
	n = int(input())
	a = input()
	b = input()
	amap = {}
	bmap = {}
	for (i, char) in enumerate(a):
		if char in amap:
			amap[char].append(i)
		else:
			amap[char] = [i]
	for (i, char) in enumerate(b):
		if char in bmap:
			bmap[char].append(i)
		else:
			bmap[char] = [i]
	ans = []
	flag = False
	for letter in range(25, -1, -1):
		char = chr(ord('a') + letter)
		if char in bmap:
			if char not in amap:
				flag = True
				break
			else:
				ans.append([])
				for ind in bmap[char]:
					if a[ind] < char:
						flag = True
						break
					elif a[ind] == char:
						pass
					else:
						ans[-1].append(ind)
				if len(ans[-1]):
					ans[-1].append(amap[char][0])
				else:
					ans.pop()
		if flag:
			break
	if flag:
		print(-1)
	else:
		print(len(ans))
		for item in ans:
			print(len(item), end=' ')
			print(*item)


for _ in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	diff = set()
	for (i, j) in zip(a, b):
		if i != j:
			diff.add(j)
	diff = list(diff)
	diff.sort(reverse=True)
	impossible = False
	indices = list()
	for alphabet in diff:
		if alphabet not in a:
			impossible = True
			print(-1)
			break
		else:
			present_list = list()
			for i in range(n):
				if b[i] == alphabet:
					present_list.append(i)
					if ord(a[i]) < ord(alphabet):
						print(-1)
						impossible = True
						break
			else:
				x = a.index(alphabet)
				if x not in present_list:
					present_list.append(x)
				present_list.insert(0, len(present_list))
				indices.append(present_list)
	if not impossible:
		print(len(diff))
		for operation in indices:
			print(*operation)


from collections import defaultdict
t = int(input())
for _ in range(t):
	n = int(input())
	a = input()
	b = input()
	set_a = set(a)
	set_b = set(b)
	if not set_a.issuperset(set_b):
		print(-1)
		continue
	change = defaultdict(list)
	indexes = defaultdict(list)
	f = False
	for (i, (x, y)) in enumerate(zip(a, b)):
		indexes[x].append(i)
		if ord(x) - ord(y) < 0:
			print(-1)
			f = True
			break
		if ord(x) - ord(y):
			change[y].append(i)
	if f:
		continue
	print(len(change))
	start = sorted(list(change.keys()))
	for _ in range(len(start)):
		d = start.pop()
		out = list(change[d] + indexes[d])
		print(len(out), *sorted(out))


for _ in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	l = []
	flag = 0
	for i in range(n):
		if b[i] != a[i]:
			if b[i] in a and b[i] < a[i]:
				l.append(b[i])
			else:
				flag = 1
				break
	if flag == 1:
		print(-1)
	elif l == []:
		print(0)
	else:
		l = sorted(list(set(l)), reverse=True)
		print(len(l))
		for i in range(len(l)):
			q = []
			r = []
			for j in range(len(a)):
				if l[i] == b[j]:
					q.append(j)
					r.append(a[j])
			if l[i] not in r:
				for k in range(len(a)):
					if a[k] == l[i]:
						q.append(k)
			print(len(q), *q)


for _ in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	flag = 1
	for i in range(n):
		if a[i] < b[i]:
			flag = 0
			break
		if b[i] not in set(a):
			flag = 0
			break
	if flag == 1:
		d = {}
		for i in range(n):
			if a[i] != b[i]:
				if b[i] not in d:
					d[b[i]] = [0]
				d[b[i]][0] += 1
				d[b[i]] += [i]
		cov = sorted(d, reverse=True)
		ans = []
		opr = len(cov)
		for i in cov:
			t = [d[i][0] + 1] + [a.index(i)] + d[i][1:]
			ans.append(t)
		print(opr)
		for i in ans:
			print(*i, sep=' ')
	else:
		print(-1)


from functools import lru_cache
from sys import stdin, stdout
import sys
from math import *
input = stdin.readline
for __ in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	isans = 1
	for i in range(n):
		if a[i] < b[i]:
			isans = 0
			break
		if b[i] not in a:
			isans = 0
			break
	if isans == 0:
		print(-1)
		continue
	else:
		rslt = []
		d = {}
		for i in range(n):
			if b[i] != a[i]:
				d[b[i]] = 1
		for i in range(26, -1, -1):
			ans = []
			if chr(i + 97) in d:
				for j in range(n):
					if b[j] == chr(i + 97) and b[j] != a[j]:
						ans.append(j)
					if a[j] == chr(i + 97):
						ans.append(j)
			if len(ans) != 0:
				rslt.append(ans)
		print(len(rslt))
		for i in range(len(rslt)):
			print(len(rslt[i]), end=' ')
			print(*rslt[i])


from collections import defaultdict
t = int(input())
for test in range(t):
	n = int(input())
	a = input()
	b = input()
	ast = defaultdict(set)
	bst = defaultdict(set)
	for i in range(n):
		ast[a[i]].add(i)
		bst[b[i]].add(i)
	ans = fl = 0
	out = []
	for i in range(97 + 25, 96, -1):
		ch = chr(i)
		if ch in bst:
			if ch not in ast:
				print(-1)
				fl = 1
				break
			else:
				(c, mn) = (0, 'z')
				ind = ast[ch].union(bst[ch])
				for j in ind:
					if a[j] < mn:
						mn = a[j]
					if a[j] != ch:
						c += 1
						ast[a[j]].discard(j)
				if mn < ch:
					print(-1)
					fl = 1
					break
				if c > 0:
					ans += 1
					out.append(list(ind))
	if fl == 0:
		print(ans)
		for i in out:
			print(len(i), *i)


for _ in range(int(input())):
	N = int(input())
	A = list(input())
	B = list(input())
	db = dict()
	da = dict()
	cfg = 0
	ans = []
	for i in range(97, 97 + 26):
		da[chr(i)] = set()
		db[chr(i)] = set()
	for i in range(N):
		da[A[i]].add(i)
		db[B[i]].add(i)
		if A[i] < B[i]:
			print(-1)
			cfg = 1
			break
	count = 0
	cfr = 0
	if cfg == 0:
		for i in range(97 + 25, 96, -1):
			ch = chr(i)
			if db[ch]:
				if da[ch]:
					c = 0
					ind = db[ch].union(da[ch])
					for j in ind:
						if A[j] != ch:
							c += 1
							val = A[j]
							da[val].discard(j)
							A[j] = ch
					if c > 0:
						count += 1
						ans.append(list(ind))
				else:
					print(-1)
					cfr = 1
					break
		if cfr == 0:
			print(count)
			for x in ans:
				le = len(x)
				print(le, end=' ')
				for i in range(le):
					print(x[i], end=' ')
				print()


import sys
dry_run = False
if dry_run:
	input_file = open('convert_the_string.txt')
	sys.stdin = input_file
global initial_map, final_map, result
for t in range(int(input())):
	length = int(input())
	initial = input()
	final = input()
	initial_map = dict()
	final_map = dict()
	result = list()
	possible = True
	for index in range(length):
		if initial[index] in initial_map:
			initial_map[initial[index]].add(index)
		else:
			my_set = set()
			my_set.add(index)
			initial_map[initial[index]] = my_set
		if final[index] in final_map:
			final_map[final[index]].add(index)
		else:
			my_set = set()
			my_set.add(index)
			final_map[final[index]] = my_set
		if final[index] > initial[index]:
			possible = False
			break
	if possible:
		for key in final_map:
			if key not in initial_map:
				possible = False
				break
	if possible:
		final_keys = [key for key in final_map]
		final_keys = sorted(final_keys, reverse=True)
		for char in final_keys:
			if char in initial_map and len(initial_map[char]) > 0:
				if not final_map[char].issubset(initial_map[char]):
					temp_set = set()
					temp_set.update(final_map[char])
					temp_set.update(initial_map[char])
					result.append(temp_set)
					for index in final_map[char]:
						initial_map[initial[index]].remove(index)
			else:
				possible = False
				break
	if possible:
		print(len(result))
		for res in result:
			print(len(res), end=' ')
			print(' '.join((str(r) for r in res)))
	else:
		print(-1)


t = int(input())
for i in range(t):
	n = int(input())
	A = list(input())
	B = list(input())
	flag = 0
	if A == B:
		print(0)
	else:
		for i in range(n):
			if B[i] not in A or ord(A[i]) < ord(B[i]):
				print(-1)
				flag = 1
				break
		if flag == 0:
			count = 0
			small_ele = list(set(B))
			seti = []
			for j in small_ele:
				temp = []
				ind = A.index(j)
				if A != B:
					temp.append(ind)
					for k in range(n):
						if B[k] == j and A[k] != j:
							A[k] = j
							temp.append(k)
					count = count + 1
					seti.append(temp)
				else:
					break
			print(count)
			for i in seti:
				d = len(i)
				print(d, end=' ')
				for j in range(d):
					print(i[j], end=' ')
				print(end='\n')


for _ in range(int(input())):
	n = int(input())
	a = list(input())
	b = list(input())
	if a == b:
		print(0)
	else:
		l = []
		c = 0
		for i in range(n):
			if b[i] not in l:
				if b[i] not in a:
					c = 1
					break
				l.append(b[i])
		if c:
			print(-1)
		else:
			l.sort()
			l.reverse()
			truthy = 0
			list1 = []
			for i in range(len(l)):
				current = l[i]
				posb = []
				one = None
				for j in range(n):
					if one == None:
						if a[j] == current:
							one = j
					if b[j] == current:
						posb.append(j)
				for j in range(len(posb)):
					if a[posb[j]] < current:
						truthy = 1
						break
				if truthy == 1:
					break
				else:
					if one not in posb:
						posb.append(one)
					qq = 0
					for j in range(len(posb)):
						if a[posb[j]] != current:
							qq = 1
					if qq:
						for j in range(len(posb)):
							a[posb[j]] = current
						list1.append(posb)
			if truthy:
				print(-1)
			else:
				print(len(list1))
				for i in range(len(list1)):
					print(len(list1[i]), end=' ')
					for j in range(len(list1[i])):
						print(list1[i][j], end=' ')
					print('\n')


import string
ASCII_ASC = string.ascii_lowercase
ASCII_DSC = sorted(ASCII_ASC, reverse=True)

def int_input():
	return int(input())

def array_input(data_type=int):
	if data_type != str:
		return list(map(data_type, input().split('')))
	else:
		return list(map(str, input()))

def solve():
	N = int_input()
	A = array_input(data_type=str)
	B = array_input(data_type=str)
	for i in range(N):
		if A[i] < B[i]:
			print(-1)
			return
	res = []
	for ch in ASCII_DSC:
		idx_pos = []
		chk = False
		for i in range(0, N):
			if B[i] == ch and A[i] != ch:
				idx_pos.append(i)
		if chk is False and len(idx_pos):
			for i in range(0, N):
				if A[i] == ch:
					chk = True
					idx_pos.append(i)
		if chk is False and len(idx_pos):
			print(-1)
			return
		if len(idx_pos):
			res.append(idx_pos)
		for idx in idx_pos:
			A[idx] = ch
	print(len(res))
	for arr in res:
		print(len(arr), ' '.join(map(str, arr)))
t = int_input()
while t > 0:
	t -= 1
	solve()


T = int(input())
for _ in range(T):
	N = int(input())
	A = list(input())
	B = list(input())
	if not set(B).issubset(set(A)):
		print(-1)
	else:
		l1 = []
		flag = 1
		for (i, (x, y)) in enumerate(list(zip(A, B))):
			if y > x:
				flag = 0
				break
			elif y == x:
				pass
			else:
				l1.append((i, (x, y)))
		if flag:
			s = set(list(reversed(list(sorted([y for (i, (x, y)) in l1])))))
			number_of_ops = len(s)
			print(number_of_ops)
			for char in s:
				answer = []
				for item in l1:
					if char == item[1][1]:
						answer.append(item[0])
				answer.append(list(A).index(char))
				answer.insert(0, len(answer))
				print(' '.join(list(map(str, answer))))
		else:
			print(-1)


T = int(input())
for _ in range(T):
	N = int(input())
	A = input()
	B = input()
	dupdel = list(dict.fromkeys(B))

	def notpossible(A, B, dupdel):
		for i in range(len(dupdel)):
			if dupdel[i] not in A:
				return -1
		for i in range(len(A)):
			if A[i] < B[i]:
				return -1
	if notpossible(A, B, dupdel) == -1:
		print(-1)
		continue
	else:
		L = []
		for i in range(len(A)):
			if A[i] != B[i]:
				L.append(B[i])
		P = list(dict.fromkeys(L))
		print(len(P))
		P.sort(reverse=True)
		for i in range(len(P)):
			Q = []
			R = []
			for j in range(len(A)):
				if B[j] == P[i]:
					Q.append(j)
					R.append(A[j])
			if P[i] not in R:
				for k in range(N):
					if A[k] == P[i]:
						Q.append(k)
						break
			print(len(Q), *Q)


for _ in range(int(input())):
	n = int(input())
	firstString = list(input())
	secondString = list(input())
	firstStringSet = set(firstString)
	secondStringSet = set(secondString)
	if firstString == secondString:
		print(0)
	elif secondStringSet.issubset(firstStringSet):
		d = {}
		wr = {}
		f = 0
		for i in range(n):
			if firstString[i] != secondString[i]:
				if secondString[i] in d:
					d[secondString[i]].append(i)
					wr[secondString[i]].append(firstString[i])
				else:
					d[secondString[i]] = []
					wr[secondString[i]] = []
					d[secondString[i]].append(i)
					wr[secondString[i]].append(firstString[i])
		for (k, v) in sorted(wr.items(), reverse=True):
			for i in v:
				if ord(k) > ord(i):
					f = 1
					break
			if f == 1:
				break
		if f == 1:
			print(-1)
		else:
			print(len(d))
			for (k, v) in sorted(d.items(), reverse=True):
				print(len(d[k]) + 1, end=' ')
				print(*d[k], firstString.index(k))
	else:
		print(-1)


for _ in range(int(input())):
	n = int(input())
	A = list(input())
	B = list(input())
	element = sorted(list(set(B)))
	element = element[::-1]
	(flag, flag_) = (True, False)
	count = 0
	l = []
	for b in element:
		i = B.index(b)
		if b not in A:
			print(-1)
			flag = False
			break
		if A[i] >= b:
			s = [A.index(b)]
			j = i
			while j < n:
				if B[j] == b:
					if A[j] < b:
						print(-1)
						flag = False
						break
					elif A[j] > b:
						flag_ = True
					if j not in s:
						s.append(j)
					A[j] = b
				j += 1
			if flag_:
				l.append(s)
				count += 1
				flag_ = False
		else:
			print(-1)
			flag = False
			break
	if flag:
		print(count)
		for i in range(len(l)):
			print(len(l[i]), ' '.join(list(map(str, l[i]))))


t = int(input())
for _ in range(t):
	n = int(input())
	a = input()
	b = input()
	a = list(a)
	b = list(b)
	l = []
	f = 1
	for i in range(n):
		if b[i] != a[i]:
			if b[i] in a and b[i] < a[i]:
				l.append(b[i])
			else:
				f = 0
				break
	if f == 0:
		print(-1)
	elif l == []:
		print(0)
	else:
		l = sorted(list(set(l)), reverse=True)
		print(len(l))
		for i in range(len(l)):
			Q = []
			R = []
			for j in range(len(a)):
				if b[j] == l[i]:
					Q.append(j)
					R.append(a[j])
			if l[i] not in R:
				for k in range(n):
					if a[k] == l[i]:
						Q.append(k)
						break
			print(len(Q), *Q)


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	a = input().rstrip()
	b = input().rstrip()
	curr = {}
	L = []
	for i in range(n):
		if a[i] not in L:
			L.append(a[i])
			curr[a[i]] = []
		curr[a[i]].append(i)
	L.sort()
	to = {}
	flag = 0
	for i in range(n):
		if b[i] not in L:
			flag = 1
			break
		if a[i] != b[i]:
			if a[i] > b[i]:
				if b[i] not in to:
					to[b[i]] = []
				to[b[i]].append(i)
			else:
				flag = 1
				break
	if flag == 1:
		print(-1)
	else:
		ans = []
		while len(L) > 0:
			ch = L.pop(-1)
			if ch in to:
				li = curr[ch]
				for i in to[ch]:
					li.append(i)
				li.sort()
				ans.append(li)
		print(len(ans))
		for i in ans:
			print(len(i), end=' ')
			print(*i)


from sys import stdin, stdout
from collections import *
import re
t = int(stdin.readline().strip())
while t:
	t -= 1
	n = int(stdin.readline().strip())
	s1 = stdin.readline().strip()
	s2 = stdin.readline().strip()
	flag = True
	s1_dict = defaultdict(set)
	s2_dict = defaultdict(set)
	for (i, ch1, ch2) in zip(range(n), s1, s2):
		if ord(ch1) < ord(ch2):
			flag = False
			break
		s1_dict[ch1].add(i)
		if ch1 != ch2:
			s2_dict[ch2].add(i)
	if not flag or set(s2_dict) - set(s1_dict):
		print(-1)
		continue
	start = ord('z') + 1
	end = ord('a')
	step_cnt = 0
	steps = []
	while start > end:
		start -= 1
		ch = chr(start)
		if ch not in s2_dict:
			continue
		if s1_dict[ch] == s2_dict[ch]:
			continue
		step_cnt += 1
		all_val = s1_dict[ch] | s2_dict[ch]
		steps.append(all_val)
	print(step_cnt)
	for step in steps:
		print(len(step), ' '.join(map(str, sorted(step))))


for _ in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	a1 = set(a)
	flag = 0
	for i in range(n):
		if a[i] < b[i] or b[i] not in a1:
			flag = 1
			break
	if flag == 1:
		print(-1)
	else:
		a = list(a)
		b = list(b)
		d = {}
		for i in range(26):
			d[chr(ord('a') + i)] = 0
		l2 = []
		count1 = 0
		b1 = list(set(b))
		b1.sort(reverse=True)
		for c in b1:
			for i in range(n):
				l1 = [0]
				count = 0
				if b[i] == c and a[i] != c:
					if d[b[i]] == 0:
						count1 += 1
						d[b[i]] = 1
					for j in range(n):
						if a[j] == b[i] or b[j] == b[i]:
							l1.append(j)
							count += 1
							a[j] = b[i]
					l1.sort()
					l1[0] = count
					l2.append(l1)
		print(count1)
		for i in l2:
			print(*i)


for _ in range(int(input())):
	n = int(input())
	a = list(str(input()))
	b = list(str(input()))
	arr = []
	l = []
	flag0 = 1
	flag1 = 1
	for i in range(n):
		if ord(a[i]) < ord(b[i]):
			print(-1)
			flag0 = 0
			break
	if flag0 == 1:
		for i in range(n):
			if b[i] not in a:
				print(-1)
				flag1 = 0
				break
	if flag0 == 1 and flag1 == 1:
		for i in range(122, 96, -1):
			s = []
			for j in range(n):
				if b[j] == chr(i) and a[j] != chr(i):
					s.append(j)
			if s != []:
				for j in range(n):
					if a[j] == chr(i):
						s.append(j)
			if s != []:
				arr.append(s)
		print(len(arr))
		for i in range(len(arr)):
			print(len(arr[i]), end=' ')
			for j in range(len(arr[i])):
				print(arr[i][j], end=' ')
			print()


def valid(s1, s2, n):
	for i in range(n):
		if s1[i] < s2[i]:
			print(-1)
			return
	temp = [0] * 26
	for i in range(n):
		temp[ord(s1[i]) - ord('a')] = 1
	for i in range(n):
		if temp[ord(s2[i]) - ord('a')] == 0:
			print(-1)
			return
	a = 'abcdefghijklmnopqrstuvwxyz'
	ans = []
	for i in range(25, -1, -1):
		f = 0
		temp = []
		for j in range(n):
			if s1[j] == a[i] or s2[j] == a[i]:
				if s1[j] != a[i]:
					f = 1
				temp.append(j)
		for j in temp:
			s1[j] = a[i]
		if f:
			ans.append(temp)
	print(len(ans))
	for i in ans:
		print(len(i), *i)
	return
t = int(input())
while t:
	n = int(input())
	s1 = list(input())
	s2 = list(input())
	valid(s1, s2, n)
	t -= 1