← Home
package main

import (
	"bufio"
	"container/heap"
	"fmt"
	"os"
)

type IntHeap []int

func (h IntHeap) Len() int           { return len(h) }
func (h IntHeap) Less(i, j int) bool { return h[i] > h[j] }
func (h IntHeap) Swap(i, j int)      { h[i], h[j] = h[j], h[i] }
func (h *IntHeap) Push(x interface{}) {
	*h = append(*h, x.(int))
}
func (h *IntHeap) Pop() interface{} {
	old := *h
	n := len(old)
	x := old[n-1]
	*h = old[0 : n-1]
	return x
}

func f(x int) int {
	res := 0
	for x > 0 {
		res++
		x /= 10
	}
	return res
}

func readInt(in *bufio.Reader) (int, bool) {
	c, err := in.ReadByte()
	if err != nil {
		return 0, false
	}
	for c <= 32 {
		c, err = in.ReadByte()
		if err != nil {
			return 0, false
		}
	}
	n := 0
	for c > 32 {
		n = n*10 + int(c-'0')
		c, err = in.ReadByte()
		if err != nil {
			break
		}
	}
	return n, true
}

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	t, ok := readInt(in)
	if !ok {
		return
	}

	for tc := 0; tc < t; tc++ {
		n, _ := readInt(in)

		ha := make(IntHeap, n)
		for i := 0; i < n; i++ {
			ha[i], _ = readInt(in)
		}
		hb := make(IntHeap, n)
		for i := 0; i < n; i++ {
			hb[i], _ = readInt(in)
		}

		heap.Init(&ha)
		heap.Init(&hb)

		ans := 0
		for ha.Len() > 0 && hb.Len() > 0 {
			va := ha[0]
			vb := hb[0]

			if va == vb {
				heap.Pop(&ha)
				heap.Pop(&hb)
			} else if va > vb {
				heap.Pop(&ha)
				heap.Push(&ha, f(va))
				ans++
			} else {
				heap.Pop(&hb)
				heap.Push(&hb, f(vb))
				ans++
			}
		}

		fmt.Fprintln(out, ans)
	}
}