Let's define $f(x)$ for a positive integer $x$ as the length of the base-10 representation of $x$ without leading zeros. I like to call it a digital logarithm. Similar to a digital root, if you are familiar with that.

You are given two arrays $a$ and $b$, each containing $n$ positive integers. In one operation, you do the following:

pick some integer $i$ from $1$ to $n$;

assign either $f(a_i)$ to $a_i$ or $f(b_i)$ to $b_i$.

Two arrays are considered similar to each other if you can rearrange the elements in both of them, so that they are equal (e. g. $a_i = b_i$ for all $i$ from $1$ to $n$).

What's the smallest number of operations required to make $a$ and $b$ similar to each other?


-----Input-----

The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of testcases.

The first line of the testcase contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of elements in each of the arrays.

The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i < 10^9$).

The third line contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_j < 10^9$).

The sum of $n$ over all testcases doesn't exceed $2 \cdot 10^5$.


-----Output-----

For each testcase, print the smallest number of operations required to make $a$ and $b$ similar to each other.


-----Examples-----

Input
4
1
1
1000
4
1 2 3 4
3 1 4 2
3
2 9 3
1 100 9
10
75019 709259 5 611271314 9024533 81871864 9 3 6 4865
9503 2 371245467 6 7 37376159 8 364036498 52295554 169
Output
2
0
2
18


-----Note-----

In the first testcase, you can apply the digital logarithm to $b_1$ twice.

In the second testcase, the arrays are already similar to each other.

In the third testcase, you can first apply the digital logarithm to $a_1$, then to $b_2$.

Difficulty: MEDIUM
Tags: Sorting

Data structures

Greedy algorithms
Skill types: Sorting

Data structures

Greedy algorithms

Starter code:

Solutions:
ot = []
for _ in range(int(input())):
	n = int(input())
	l1 = list(map(int, input().split()))
	l2 = list(map(int, input().split()))
	d1_digits = {1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0}
	d2_digits = {1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0}
	d1 = {}
	d2 = {}
	out = 0
	for x in l1:
		if x == 1:
			continue
		if x not in d1:
			d1[x] = 1
		else:
			d1[x] += 1
	for x in l2:
		if x == 1:
			continue
		if x in d1:
			d1[x] -= 1
			if d1[x] == 0:
				del d1[x]
		elif x not in d2:
			d2[x] = 1
		else:
			d2[x] += 1
	for (x, y) in d1.items():
		k = len(str(x))
		if k == 1:
			d1_digits[x] += y
		else:
			d1_digits[k] += y
			out += y
	for (x, y) in d2.items():
		k = len(str(x))
		if k == 1:
			d2_digits[x] += y
		else:
			d2_digits[k] += y
			out += y
	for (x, y) in d1_digits.items():
		m = min(y, d2_digits[x])
		out += y - m + (d2_digits[x] - m)
	ot.append(str(out))
print('\n'.join(ot))


from math import log10
T = int(input())
for _ in range(T):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	marker = {1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0}
	ans = 0
	for item in a:
		if item not in marker:
			marker[item] = 1
		else:
			marker[item] += 1
	for i in range(n):
		if b[i] in marker and marker[b[i]] >= 1:
			marker[b[i]] -= 1
			b[i] = -1
	for key in marker.keys():
		if int(log10(key)) >= 1:
			ans += marker[key]
			marker[int(log10(key)) + 1] += marker[key]
	for i in range(n):
		if b[i] == -1:
			continue
		else:
			if int(log10(b[i])) >= 1:
				b[i] = int(log10(b[i])) + 1
				ans += 1
			if b[i] in marker and marker[b[i]] >= 1:
				marker[b[i]] -= 1
				b[i] = -1
	for i in range(2, 10):
		if i in marker.keys():
			ans += marker[i]
			marker[1] += marker[i]
	for item in b:
		if item == -1 or item == 1:
			continue
		else:
			ans += 1
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	a.sort()
	b.sort()
	i = 0
	j = 0
	x = [0] * 10
	y = [0] * 10
	s = 0
	while i < n or j < n:
		if i < n and j < n:
			if a[i] == b[j]:
				i += 1
				j += 1
			elif a[i] < b[j]:
				if a[i] < 10:
					x[a[i]] += 1
				else:
					x[len(str(a[i]))] += 1
					s += 1
				i += 1
			elif a[i] > b[j]:
				if b[j] < 10:
					y[b[j]] += 1
				else:
					y[len(str(b[j]))] += 1
					s += 1
				j += 1
		elif i < n:
			if a[i] < 10:
				x[a[i]] += 1
			else:
				x[len(str(a[i]))] += 1
				s += 1
			i += 1
		elif j < n:
			if b[j] < 10:
				y[b[j]] += 1
			else:
				y[len(str(b[j]))] += 1
				s += 1
			j += 1
	for i in range(2, 10):
		s += abs(x[i] - y[i])
	print(s)


from collections import Counter
for _ in range(int(input())):
	useless = input()
	a = Counter(input().split())
	a.subtract(Counter(input().split()))
	ans = 0
	for key in list(a.keys()):
		if len(key) > 1:
			ans += abs(a[key])
			a[str(len(key))] += a[key]
			a[key] = 0
	print(ans + sum((a[key] for key in a.keys() if a[key] > 0)) * 2 - abs(a['1']))


from collections import Counter
for _ in range(int(input())):
	useless = input()
	a = Counter(input().split())
	b = Counter(input().split())
	ans = 0
	for key in set(a.keys()) & set(b.keys()):
		if a[key] < b[key]:
			b[key] -= a[key]
			a.pop(key)
		elif a[key] > b[key]:
			a[key] -= b[key]
			b.pop(key)
		else:
			a.pop(key)
			b.pop(key)
	for key in list(a.keys()):
		if len(key) > 1:
			ans += a[key]
			a[str(len(key))] += a[key]
			a[key] = 0
	for key in list(b.keys()):
		if len(key) > 1:
			ans += b[key]
			b[str(len(key))] += b[key]
			b[key] = 0
	for key in set(a.keys()) & set(b.keys()):
		if a[key] < b[key]:
			b[key] -= a[key]
			a.pop(key)
		elif a[key] > b[key]:
			a[key] -= b[key]
			b.pop(key)
		else:
			a.pop(key)
			b.pop(key)
	ans += sum((a[key] for key in a.keys())) * 2
	ans -= a['1'] + b['1']
	print(ans)


import sys
from collections import defaultdict
input = sys.stdin.readline
rand = 731141807

def cntDig(n):
	cnt = 0
	tmp = n
	while tmp:
		cnt += 1
		tmp //= 10
	return cnt

def sol(a, b, n):
	res = 0
	bst = defaultdict(int)
	for i in b:
		bst[i ^ rand] += 1
	cnta = [0] * 10
	cntb = [0] * 10
	for i in a:
		if bst[i ^ rand]:
			bst[i ^ rand] -= 1
		elif i > 9:
			cnta[cntDig(i)] += 1
			res += 1
		else:
			cnta[i] += 1
	for i in b:
		if bst[i ^ rand]:
			if i > 9:
				cntb[cntDig(i)] += 1
				res += 1
			else:
				cntb[i] += 1
			bst[i ^ rand] -= 1
	for i in range(2, 10):
		res += abs(cnta[i] - cntb[i])
	return res
t = int(input())
for case in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	print(sol(a, b, n))


def get_digit(num, ups):
	for (i, upp) in enumerate(ups):
		if num < upp:
			return i + 1
t = int(input())
ups = [10 ** k for k in range(1, 10)]
for _ in range(t):
	n = int(input())
	As = input().split()
	As = [int(i) for i in As]
	Bs = input().split()
	Bs = [int(i) for i in Bs]
	adict = dict()
	for num in As:
		adict[num] = adict.get(num, 0) + 1
	bdict = dict()
	for num in Bs:
		bdict[num] = bdict.get(num, 0) + 1
	for num in adict:
		if num in bdict:
			v = min(adict[num], bdict[num])
			adict[num] -= v
			bdict[num] -= v
	_step = 0
	alist = [0 for i in range(10)]
	blist = [0 for i in range(10)]
	for num in adict:
		if adict[num] > 0:
			if num >= 10:
				d = get_digit(num, ups)
				alist[d] += adict[num]
				_step += adict[num]
			else:
				alist[num] += adict[num]
	for num in bdict:
		if bdict[num] > 0:
			if num >= 10:
				d = get_digit(num, ups)
				blist[d] += bdict[num]
				_step += bdict[num]
			else:
				blist[num] += bdict[num]
	for i in range(2, 10):
		d = abs(alist[i] - blist[i])
		_step += d
	print(_step)


def log10(x):
	if x < 10:
		ans = 1
	elif x < 100:
		ans = 2
	elif x < 10000:
		ans = 2 + log10(x // 100)
	elif x < 100000000:
		ans = 4 + log10(x // 10000)
	else:
		ans = 9
	return ans
for tcase in range(int(input())):
	n1 = int(input())
	a1 = sorted(map(int, input().split()))
	b1 = sorted(map(int, input().split()))
	(i, j, a2, b2, ans) = (0, 0, [], [], 0)
	while i < n1 or j < n1:
		if j >= n1 or (i < n1 and a1[i] < b1[j]):
			x = a1[i]
			if x > 9:
				x = log10(x)
				ans += 1
			a2.append(x)
			i += 1
		elif i >= n1 or (j < n1 and b1[j] < a1[i]):
			x = b1[j]
			if x > 9:
				x = log10(x)
				ans += 1
			b2.append(x)
			j += 1
		else:
			i += 1
			j += 1
	a2.sort()
	b2.sort()
	(n2, i, j) = (len(a2), 0, 0)
	while i < n2 or j < n2:
		if j >= n2 or (i < n2 and a2[i] < b2[j]):
			if a2[i] > 1:
				ans += 1
			i += 1
		elif i >= n2 or (j < n2 and b2[j] < a2[i]):
			if b2[j] > 1:
				ans += 1
			j += 1
		else:
			i += 1
			j += 1
	print(ans)


test_cases = int(input())
for _ in range(test_cases):
	n = int(input())
	a = [int(x) for x in input().split()]
	b = [int(x) for x in input().split()]
	a.sort()
	b.sort()
	mapa = [0 for i in range(10)]
	mapb = [0 for i in range(10)]
	count = 0
	i = 0
	j = 0
	while j < n and i < n:
		if a[i] == b[j]:
			a[i] = 1
			b[j] = 1
			i += 1
			j += 1
		elif a[i] < b[j]:
			i += 1
		elif a[i] > b[j]:
			j += 1
	for i in range(n):
		if a[i] >= 10:
			a[i] = len(str(a[i]))
			count += 1
		if b[i] >= 10:
			b[i] = len(str(b[i]))
			count += 1
		mapa[a[i]] += 1
		mapb[b[i]] += 1
	for i in range(2, 10):
		count += abs(mapa[i] - mapb[i])
	print(count)


from collections import Counter
t = int(input())
for __ in range(t):
	n = int(input())
	cnt = Counter(input().split())
	cnt.subtract(Counter(input().split()))
	ans = 0
	keys = list(cnt.keys())
	for key in keys:
		if len(key) > 1:
			cnt[str(len(key))] += cnt[key]
			ans += abs(cnt[key])
			cnt[key] = 0
	for key in cnt:
		if key != '1':
			ans += abs(cnt[key])
	print(ans)


import sys
import math
from collections import Counter

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

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

def fun(a, b):
	d1 = Counter(a)
	d2 = Counter(b)
	if d1 == d2:
		return 0
	c = 0
	(d1, d2) = (d1 - d2, d2 - d1)
	a = []
	b = []
	for i in d1:
		for j in range(d1[i]):
			a.append(i)
	for i in d2:
		for j in range(d2[i]):
			b.append(i)
	for i in range(len(a)):
		x = len(str(a[i]))
		if x > 1:
			c += 1
			a[i] = x
	for j in range(len(b)):
		x = len(str(b[j]))
		if x > 1:
			c += 1
			b[j] = x
	d1 = Counter(a)
	d2 = Counter(b)
	(d1, d2) = (d1 - d2, d2 - d1)
	a = []
	b = []
	for i in d1:
		for j in range(d1[i]):
			a.append(i)
	for i in d2:
		for j in range(d2[i]):
			b.append(i)
	return len(a) + len(b) + c - a.count(1) - b.count(1)
for _ in range(int(input())):
	x = input()
	a = gi()
	b = gi()
	print(fun(a, b))


import math as mt
from collections import defaultdict, deque
import sys
from bisect import bisect_right as b_r
from bisect import bisect_left as b_l
from os import path
from heapq import *
input = sys.stdin.readline
mod = 1000000007
INT_MAX = sys.maxsize - 1
INT_MIN = -sys.maxsize

def f(x):
	return len(str(x))

def myyy__answer():
	n = int(input())
	maxah = []
	maxbh = []
	for i in input().split():
		heappush(maxah, -int(i))
	for i in input().split():
		heappush(maxbh, -int(i))
	cnt = 0
	while len(maxah):
		a = maxah[0]
		b = maxbh[0]
		if a < b:
			heappop(maxah)
			heappush(maxah, -f(-a))
			cnt += 1
		elif b < a:
			heappop(maxbh)
			heappush(maxbh, -f(-b))
			cnt += 1
		else:
			heappop(maxah)
			heappop(maxbh)
	return cnt
for _ in range(int(input())):
	print(myyy__answer())


from collections import Counter
t = int(input())
for i in range(t):
	n = int(input())
	w = [int(k) for k in input().split()]
	z = [int(k) for k in input().split()]
	a = Counter(w)
	b = Counter(z)
	(wnew, znew) = ([], [])
	res = 0
	for j in w:
		if j in b and b[j] > 0:
			b[j] -= 1
		else:
			wnew.append(j)
	for j in z:
		if j in a and a[j] > 0:
			a[j] -= 1
		else:
			znew.append(j)
	for j in range(len(wnew)):
		if wnew[j] >= 10:
			wnew[j] = len(str(wnew[j]))
			res += 1
	for j in range(len(znew)):
		if znew[j] >= 10:
			znew[j] = len(str(znew[j]))
			res += 1
	(w, z) = ([], [])
	a = Counter(wnew)
	b = Counter(znew)
	for j in wnew:
		if j in b and b[j] > 0:
			b[j] -= 1
		else:
			w.append(j)
	for j in znew:
		if j in a and a[j] > 0:
			a[j] -= 1
		else:
			z.append(j)
	for j in range(len(w)):
		if w[j] > 1:
			res += 1
	for j in range(len(z)):
		if z[j] > 1:
			res += 1
	print(res)


import heapq

def solution(arr1, arr2):
	ans = 0
	(heap1, heap2) = ([], [])
	for num in arr1:
		heapq.heappush(heap1, -num)
	for num in arr2:
		heapq.heappush(heap2, -num)
	while heap1 and heap2:
		top1 = -heap1[0]
		top2 = -heap2[0]
		if top1 == top2:
			heapq.heappop(heap1)
			heapq.heappop(heap2)
		elif top1 > top2:
			heapq.heappop(heap1)
			heapq.heappush(heap1, -len(str(top1)))
			ans += 1
		else:
			heapq.heappop(heap2)
			heapq.heappush(heap2, -len(str(top2)))
			ans += 1
	print(ans)
t = int(input())
for _ in range(t):
	n = int(input())
	arr1 = list(map(int, input().split(' ')))
	arr2 = list(map(int, input().split(' ')))
	solution(arr1, arr2)


from heapq import heapify, heappop, heappush, heapreplace
for loop in range(int(input())):
	n = int(input())
	a = [-int(x) for x in input().split()]
	b = [-int(x) for x in input().split()]
	heapify(a)
	heapify(b)
	count = 0
	while a:
		if -a[0] > -b[0]:
			heapreplace(a, -len(str(-a[0])))
			count += 1
		elif -a[0] < -b[0]:
			heapreplace(b, -len(str(-b[0])))
			count += 1
		if a[0] == b[0]:
			heappop(a)
			heappop(b)
	print(count)


from collections import Counter

def diglog(l):
	return len(str(l))
for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	k = 0
	i = 0
	(a, b) = (list((Counter(a) - Counter(b)).elements()), list((Counter(b) - Counter(a)).elements()))
	if a != []:
		for i in range(len(a)):
			if a[i] >= 10:
				a[i] = diglog(a[i])
				k = k + 1
		for i in range(len(a)):
			if b[i] >= 10:
				b[i] = diglog(b[i])
				k = k + 1
		(a, b) = (list((Counter(a) - Counter(b)).elements()), list((Counter(b) - Counter(a)).elements()))
		z = a.count(1)
		y = b.count(1)
		print(len(a) + len(b) + k - z - y)
	else:
		print(0)


import math
import heapq
import sys
input = sys.stdin.readline
in_il = lambda : list(map(int, input().split()))
in_sl = lambda : input().split()
in_i = lambda : int(input())
MAX_INT = float('inf')

def count_digit(n):
	return len(str(n))

def solve():
	n = in_i()
	a = list(map(lambda x: -x, in_il()))
	b = list(map(lambda x: -x, in_il()))
	heapq.heapify(a)
	heapq.heapify(b)
	steps = 0
	while a or b:
		if a[0] == b[0]:
			heapq.heappop(a)
			heapq.heappop(b)
		elif a[0] < b[0]:
			v = heapq.heappop(a)
			v = count_digit(-v)
			heapq.heappush(a, -v)
			steps += 1
		else:
			v = heapq.heappop(b)
			v = count_digit(-v)
			heapq.heappush(b, -v)
			steps += 1
	print(steps)
for _ in range(in_i()):
	solve()


import sys
from functools import lru_cache
from heapq import heappush, heappop, heapify
input = sys.stdin.readline

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

def F(A, B):
	(A, B) = ([-v for v in A], [-v for v in B])
	(heapify(A), heapify(B))
	res = 0
	while A and min(A[0], B[0]) < -1:
		if A[0] == B[0]:
			heappop(A)
			heappop(B)
			continue
		if A[0] > B[0]:
			(A, B) = (B, A)
			continue
		v = -heappop(A)
		heappush(A, -len(str(v)))
		res += 1
	return res
N = int(input())
for _ in range(N):
	n = int(input())
	(A, B) = (getA(), getA())
	print(F(A, B))


import heapq

def f(arr1, arr2):
	h1 = [-1 * el for el in arr1]
	h2 = [-1 * el for el in arr2]
	heapq.heapify(h1)
	heapq.heapify(h2)
	count = 0
	while len(h1) > 0:
		m1 = h1[0]
		m2 = h2[0]
		if m1 == m2:
			heapq.heappop(h1)
			heapq.heappop(h2)
		elif m1 < m2:
			k = heapq.heappop(h1)
			heapq.heappush(h1, -1 * len(str(abs(k))))
			count += 1
		else:
			k = heapq.heappop(h2)
			heapq.heappush(h2, -1 * len(str(abs(k))))
			count += 1
	return count
t = int(input())
for i in range(t):
	n = int(input())
	arr1 = [int(el) for el in input().split(' ')]
	arr2 = [int(el) for el in input().split(' ')]
	print(f(arr1, arr2))


import sys
sys.setrecursionlimit(10 ** 6)
from collections import Counter, deque, defaultdict
import math
import heapq
ip = sys.stdin.readline
op = sys.stdout.write
t = int(ip())
res = []
for _ in range(t):
	n = int(input())
	A = [int(x) for x in ip().split()]
	B = [int(x) for x in ip().split()]
	freqa = defaultdict(int)
	freqb = defaultdict(int)
	for (num1, num2) in zip(A, B):
		freqa[num1] += 1
		freqb[num2] += 1
	for (key, val) in freqa.items():
		f = min(val, freqb[key])
		freqa[key] -= f
		freqb[key] -= f
	moves = 0
	for num in list(freqa.keys()):
		f = freqa[num]
		if f == 0 or num <= 9:
			continue
		moves += f
		new_num = int(math.log10(num)) + 1
		freqa[new_num] += f
		freqa[num] = 0
	for num in list(freqb.keys()):
		f = freqb[num]
		if f == 0 or num <= 9:
			continue
		moves += f
		new_num = int(math.log10(num)) + 1
		freqb[new_num] += f
		freqb[num] = 0
	for (key, val) in freqa.items():
		f = min(val, freqb[key])
		freqa[key] -= f
		freqb[key] -= f
	for (num, f) in freqa.items():
		if num != 1:
			moves += f
	for (num, f) in freqb.items():
		if num != 1:
			moves += f
	res.append(moves)
for val in res:
	op(str(val) + '\n')


from heapq import heappush, _heapreplace_max, _heapify_max, _heappop_max

class MaxHeap:

	def __init__(self, list):
		self.list = [item for item in list]
		_heapify_max(self.list)

	def pop(self):
		val = _heappop_max(self.list)
		return val

	def push(self, val):
		heappush(val)

	def get_top(self):
		return self.list[0]

	def pop_push(self, val):
		_heapreplace_max(self.list, val)

	def count(self):
		return len(self.list)

def get_count_operation_for_same_arrays(list_a: MaxHeap, list_b: MaxHeap) -> int:
	count_op = 0
	while list_a.count() > 0:
		top_a = list_a.get_top()
		top_b = list_b.get_top()
		if top_a == top_b:
			list_a.pop()
			list_b.pop()
			continue
		count_op += 1
		if top_a > top_b:
			list_a.pop_push(len(str(top_a)))
		else:
			list_b.pop_push(len(str(top_b)))
	return count_op
test_case_count = int(input())
for _ in range(test_case_count):
	count = int(input())
	list_a = list(map(int, input().split()))
	list_b = list(map(int, input().split()))
	first_heap = MaxHeap(sorted(list_a))
	second_heap = MaxHeap(sorted(list_b))
	print(get_count_operation_for_same_arrays(first_heap, second_heap))


import heapq
t = int(input())
for _ in range(t):
	n = int(input())
	a = [-int(x) for x in input().split()]
	b = [-int(x) for x in input().split()]
	heapq.heapify(a)
	heapq.heapify(b)
	cnt = 0
	while a and b:
		if a[0] == b[0]:
			heapq.heappop(a)
			heapq.heappop(b)
		else:
			if a[0] < b[0]:
				v = heapq.heappop(a)
				v = len(str(-v))
				heapq.heappush(a, -v)
			else:
				v = heapq.heappop(b)
				v = len(str(-v))
				heapq.heappush(b, -v)
			cnt += 1
	print(cnt)


from heapq import *
import math

class Solution:

	def minMoves(self, a, b):
		a = [-i for i in a]
		b = [-i for i in b]
		heapify(a)
		heapify(b)
		moves = 0
		while len(a) != 0 and len(b) != 0:
			if -a[0] == -b[0]:
				heappop(a)
				heappop(b)
			elif -a[0] > -b[0]:
				x = -heappop(a)
				x = math.log10(x)
				x = math.floor(x) + 1
				heappush(a, -x)
				moves += 1
			else:
				x = -heappop(b)
				x = math.log10(x)
				x = math.floor(x) + 1
				heappush(b, -x)
				moves += 1
		return moves
ob = Solution()
t = int(input())
while t:
	n = int(input())
	a = list(map(int, input().strip().split()))
	b = list(map(int, input().strip().split()))
	print(ob.minMoves(a, b))
	t -= 1


import heapq, math

def dlog(x):
	return math.floor(math.log10(x)) + 1
for _ in range(int(input())):
	n = int(input())
	a = [-int(x) for x in input().split()]
	b = [-int(x) for x in input().split()]
	heapq.heapify(a)
	c = 0
	heapq.heapify(b)
	while n:
		if a[0] == b[0]:
			heapq.heappop(a)
			heapq.heappop(b)
			n -= 1
		else:
			c += 1
			if a[0] < b[0]:
				heapq.heappushpop(a, -dlog(-a[0]))
			else:
				heapq.heappushpop(b, -dlog(-b[0]))
	print(c)


from collections import defaultdict
import math
t = int(input())
for _ in range(t):
	n = int(input())
	A = list(map(int, input().split()))
	B = list(map(int, input().split()))
	d = defaultdict(lambda : [0, 0])
	for i in A:
		d[i][0] += 1
	for i in B:
		d[i][1] += 1
	A = []
	B = []
	for i in d:
		(a, b) = d[i]
		if a == b:
			pass
		elif a > b:
			for j in range(a - b):
				A.append(i)
		else:
			for j in range(b - a):
				B.append(i)
	L = [0] * 10
	M = [0] * 10
	for i in range(1, 10):
		L[i] = A.count(i)
		M[i] = B.count(i)
	ans = 0
	for i in A:
		a = int(math.log10(i)) + 1
		if a > 1:
			if M[a] > 0:
				M[a] -= 1
			else:
				L[a] += 1
			ans += 1
	for i in B:
		a = int(math.log10(i)) + 1
		if a > 1:
			if L[a] > 0:
				L[a] -= 1
			else:
				M[a] += 1
			ans += 1
	for i in range(2, 10):
		(a, b) = (L[i], M[i])
		ans += abs(a - b)
	print(ans)


t = int(input())
from heapq import heapify, heappush, heappop
while t:
	n = int(input())
	l1 = list(map(int, input().split()))
	l2 = list(map(int, input().split()))
	heap1 = []
	heap2 = []
	heapify(heap1)
	heapify(heap2)
	for i in l1:
		heappush(heap1, -1 * i)
	for j in l2:
		heappush(heap2, -1 * j)
	ans = 0
	while len(heap1) != 0 and len(heap2) != 0:
		if heap1[0] == heap2[0]:
			heappop(heap1)
			heappop(heap2)
			continue
		ans += 1
		if heap1[0] < heap2[0]:
			a = len(str(heappop(heap1))) - 1
			heappush(heap1, -1 * a)
		else:
			b = len(str(heappop(heap2))) - 1
			heappush(heap2, -1 * b)
	print(ans)
	t -= 1


from collections import deque, Counter
from math import *
import sys
import random
from bisect import *
from functools import reduce
from sys import stdin
from heapq import *
import copy
input = lambda : sys.stdin.readline().rstrip()
negx = lambda x: -int(x)
for _ in range(int(input())):
	n = int(input())
	a = list(map(negx, input().split()))
	b = list(map(negx, input().split()))
	heapify(a)
	heapify(b)
	ans = 0
	while len(a) != 0:
		(x, y) = (a[0], b[0])
		if x == y:
			heappop(a)
			heappop(b)
		else:
			if abs(a[0]) > abs(b[0]):
				heappush(a, -(floor(log10(abs(a[0]))) + 1))
				heappop(a)
			else:
				heappush(b, -(floor(log10(abs(b[0]))) + 1))
				heappop(b)
			ans += 1
	print(ans)


import heapq
t = int(input())
while t > 0:
	n = int(input())
	a = list(map(int, input().split(' ')))
	b = list(map(int, input().split(' ')))
	pa = []
	for i in a:
		heapq.heappush(pa, -1 * i)
	pb = []
	for i in b:
		heapq.heappush(pb, -1 * i)
	ans = 0
	while pa:
		if -1 * pa[0] == -1 * pb[0]:
			heapq.heappop(pa)
			heapq.heappop(pb)
			continue
		ans += 1
		if -1 * pa[0] > -1 * pb[0]:
			heapq.heappush(pa, -1 * len(str(-1 * pa[0])))
			heapq.heappop(pa)
		else:
			heapq.heappush(pb, -1 * len(str(-1 * pb[0])))
			heapq.heappop(pb)
	print(ans)
	t -= 1


import sys
input = sys.stdin.readline
from heapq import heappush, heappop
t = int(input())
for _ in range(t):
	input()
	A = [int(i) for i in input().split()]
	B = [int(i) for i in input().split()]
	a = []
	b = []
	for i in A:
		heappush(a, -i)
	for i in B:
		heappush(b, -i)
	ans = 0
	while len(a) > 0:
		if a[0] == b[0]:
			heappop(a)
			heappop(b)
		else:
			if -a[0] < -b[0]:
				q = -heappop(b)
				r = len(str(q))
				heappush(b, -r)
			else:
				q = -heappop(a)
				r = len(str(q))
				heappush(a, -r)
			ans += 1
	print(ans)


import sys
import heapq
t = int(input())
result = []
for _ in range(t):
	n = int(input())
	(arrayOne, arrayTwo) = (list(input().split()), list(input().split()))

	def solve():
		(heapOne, heapTwo) = ([-int(num) for num in arrayOne], [-int(num) for num in arrayTwo])
		heapq.heapify(heapOne)
		heapq.heapify(heapTwo)
		count = 0
		while heapOne and heapTwo:
			(numOne, numTwo) = (-heapq.heappop(heapOne), -heapq.heappop(heapTwo))
			if numOne == numTwo:
				continue
			if numOne > numTwo:
				newNum = -len(str(numOne))
				heapq.heappush(heapOne, newNum)
				heapq.heappush(heapTwo, -numTwo)
			else:
				newNum = -len(str(numTwo))
				heapq.heappush(heapTwo, newNum)
				heapq.heappush(heapOne, -numOne)
			count += 1
		return count
	print(solve())


import heapq
for _ in range(int(input())):
	n = int(input())
	arr1 = [-int(x) for x in input().split()]
	arr2 = [-int(x) for x in input().split()]
	heapq.heapify(arr1)
	heapq.heapify(arr2)
	res = 0
	while arr1:
		(x, y) = (heapq.heappop(arr1), heapq.heappop(arr2))
		if x < y:
			heapq.heappush(arr1, -len(str(-x)))
			heapq.heappush(arr2, y)
			res += 1
		elif x > y:
			heapq.heappush(arr2, -len(str(-y)))
			heapq.heappush(arr1, x)
			res += 1
	print(res)


for _ in range(int(input())):
	n = int(input())
	a = [-int(i) for i in input().split()]
	b = [-int(i) for i in input().split()]
	from heapq import heappush, heappop, heapify
	heapify(a)
	heapify(b)
	ans = 0
	while a:
		(x, y) = (-heappop(a), -heappop(b))
		if x > y:
			x = len(str(x))
			heappush(a, -x)
			heappush(b, -y)
			ans += 1
		elif x < y:
			y = len(str(y))
			heappush(a, -x)
			heappush(b, -y)
			ans += 1
	print(ans)


import heapq
for _ in range(int(input())):
	n = int(input())
	a = [-int(x) for x in input().split()]
	b = [-int(x) for x in input().split()]
	heapq.heapify(a)
	heapq.heapify(b)
	res = 0
	while a:
		ha = heapq.heappop(a)
		hb = heapq.heappop(b)
		if ha == hb:
			continue
		res += 1
		if -ha > -hb:
			heapq.heappush(a, -len(str(-ha)))
			heapq.heappush(b, hb)
		else:
			heapq.heappush(a, ha)
			heapq.heappush(b, -len(str(-hb)))
	print(res)


import heapq
t = int(input())
for tc in range(t):
	steps = 0
	n = int(input())
	ar_a = map(int, input().split())
	ar_b = map(int, input().split())
	inv_a = list(map(lambda x: -x, ar_a))
	inv_b = list(map(lambda x: -x, ar_b))
	heapq.heapify(inv_a)
	heapq.heapify(inv_b)
	while True:
		try:
			max_a = heapq.heappop(inv_a)
			max_b = heapq.heappop(inv_b)
			reinsert = False
			if max_a < max_b:
				max_a = -(len(str(max_a)) - 1)
				reinsert = True
			elif max_b < max_a:
				max_b = -(len(str(max_b)) - 1)
				reinsert = True
			if reinsert:
				heapq.heappush(inv_a, max_a)
				heapq.heappush(inv_b, max_b)
				steps = steps + 1
		except IndexError:
			break
	print(steps)


from heapq import heappush, heappop, heapify
t = int(input())
while t:
	n = int(input())
	a = [int(i) for i in input().split()]
	b = [int(i) for i in input().split()]
	pq1 = []
	pq2 = []
	for i in range(n):
		heappush(pq1, -a[i])
		heappush(pq2, -b[i])
	cnt = 0
	while pq1 and pq2:
		x = -heappop(pq1)
		y = -heappop(pq2)
		if y > x:
			heappush(pq2, -len(str(y)))
			heappush(pq1, -x)
			cnt += 1
		elif x > y:
			heappush(pq1, -len(str(x)))
			heappush(pq2, -y)
			cnt += 1
	print(cnt)
	t -= 1


import sys
from collections import defaultdict
import heapq
input = sys.stdin.readline

def num_digits(num):
	ret = 0
	while num > 0:
		num = num // 10
		ret += 1
	return ret
t = int(input())
while t > 0:
	n = int(input())
	s = input()
	a = [-1 * int(i) for i in s.split(' ')]
	s = input()
	b = [-1 * int(i) for i in s.split(' ')]
	heapq.heapify(a)
	heapq.heapify(b)
	ans = 0
	while len(a) > 0:
		if a[0] == b[0]:
			heapq.heappop(a)
			heapq.heappop(b)
		elif a[0] < b[0]:
			ans += 1
			heapq.heappush(a, -1 * num_digits(-1 * heapq.heappop(a)))
		elif a[0] > b[0]:
			ans += 1
			heapq.heappush(b, -1 * num_digits(-1 * heapq.heappop(b)))
	print(ans)
	t -= 1


from heapq import heapify, heappop, heappush
for _ in range(int(input())):
	n = int(input())
	a = [-int(x) for x in input().split()]
	b = [-int(x) for x in input().split()]
	heapify(a)
	heapify(b)
	ans = 0
	while len(a) > 0 or len(b) > 0:
		a_i = -heappop(a)
		b_i = -heappop(b)
		if a_i > b_i:
			heappush(a, -len(str(a_i)))
			heappush(b, -b_i)
			ans += 1
		elif b_i > a_i:
			heappush(b, -len(str(b_i)))
			heappush(a, -a_i)
			ans += 1
	print(ans)


import sys
import heapq

class MaxHeap:

	def __init__(self, data=None):
		if data is None:
			self.data = []
		else:
			self.data = [-i for i in data]
			heapq.heapify(self.data)

	def push(self, item):
		heapq.heappush(self.data, -item)

	def pop(self):
		return -heapq.heappop(self.data)

	def replace(self, item):
		return heapq.heapreplace(self.data, -item)

	def top(self):
		return -self.data[0]

	def empty(self):
		if len(self.data) == 0:
			return True
		return False
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	a = MaxHeap(list(map(int, input().split())))
	b = MaxHeap(list(map(int, input().split())))
	operation = 0
	while not a.empty():
		x = a.top()
		y = b.top()
		if x > y:
			a.pop()
			a.push(len(str(x)))
			operation += 1
		elif x < y:
			b.pop()
			b.push(len(str(y)))
			operation += 1
		else:
			a.pop()
			b.pop()
	print(operation)


import sys
input = sys.stdin.readline
from random import randint
RANDOM = randint(1, 10 ** 9)

class hf(int):

	def __init__(self, x):
		int.__init__(x)

	def __hash__(self):
		return super(hf, self).__hash__() ^ RANDOM

def prune(A, B):
	n = len(A)
	bag = {}
	(nA, nB) = ([], [])
	for i in range(n):
		v = hf(A[i])
		if v not in bag:
			bag[v] = []
		bag[v].append(i)
	for i in range(n):
		v = hf(B[i])
		if v in bag and bag[v]:
			bag[v].pop()
			continue
		nB.append(B[i])
	for val in bag:
		for i in bag[val]:
			nA.append(A[i])
	return (nA, nB)

def solve():
	n = int(input())
	A = list(map(int, input().split()))
	B = list(map(int, input().split()))
	ans = 0
	(nA, nB) = prune(A, B)
	m = len(nA)
	for i in range(m):
		if nA[i] >= 10:
			nA[i] = len(str(nA[i]))
			ans += 1
		if nB[i] >= 10:
			nB[i] = len(str(nB[i]))
			ans += 1
	(nA2, nB2) = prune(nA, nB)
	k = len(nA2)
	for i in range(k):
		if nA2[i] >= 2:
			ans += 1
		if nB2[i] >= 2:
			ans += 1
	return ans
for _ in range(int(input())):
	print(solve())


import heapq
t = int(input())
for _ in range(t):
	n = int(input())
	a_l = list(map(lambda x: (-int(x), len(x)), input().split()))
	b_l = list(map(lambda x: (-int(x), len(x)), input().split()))
	val = 0
	heapq.heapify(a_l)
	heapq.heapify(b_l)
	while a_l:
		a = a_l[0]
		b = b_l[0]
		if a == b:
			heapq.heappop(a_l)
			heapq.heappop(b_l)
		elif a < b:
			(_, a2) = heapq.heappop(a_l)
			heapq.heappush(a_l, (-a2, 1))
			val += 1
		else:
			(_, b2) = heapq.heappop(b_l)
			heapq.heappush(b_l, (-b2, 1))
			val += 1
	print(val)