You have two arrays A and B of size N and M respectively. You have to merge both the arrays to form a new array C of size N + M (the relative order of elements in the original arrays A and B should not change in the array C).

For e.g. if A = [{\color{blue}1}, {\color{blue}2}, {\color{blue}7} ] and B = [{\color{red}3}, {\color{red}6}, {\color{red}5}], one possible way to merge them to form C is: C = [{\color{blue}1}, {\color{red}3}, {\color{red}6}, {\color{blue}2}, {\color{red}5}, {\color{blue}7}].

Maximize the length of longest non-decreasing subsequence of the merged array C.

As a reminder, a non-decreasing subsequence of some array X is a sequence of indices i_{1}, i_{2}, ..., i_{k}, such that i_{1} < i_{2} < ... < i_{k} and X_{i_{1}} ≤ X_{i_{2}} ≤ ... ≤ X_{i_{k}}

------ Input Format ------ 

- The first line contains T - the number of test cases. Then the test cases follow.
- The first line of each test case contains two integers N and M - the size of the array A and B respectively.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A.
- The third line of each test case contains M space-separated integers B_{1}, B_{2}, \dots, B_{M} denoting the array B.

------ Output Format ------ 

For each test case, output the maximum possible length of longest non-decreasing subsequence in the merged array.

------ Constraints ------ 

$1 ≤ T ≤ 1000$
$1 ≤ N, M ≤ 10^{5}$ 
$1 ≤ A_{i}, B_{i} ≤ 10^{9}$
- It is guaranteed that the sum of $N$ over all test cases does not exceed $4 \cdot 10^{5}$.
- It is guaranteed that the sum of $M$ over all test cases does not exceed $4 \cdot 10^{5}$.

----- Sample Input 1 ------ 
2
3 2
6 4 5
1 3
2 3
1 3
2 2 4

----- Sample Output 1 ------ 
4
5

----- explanation 1 ------ 
Test Case-1: We can merge the arrays in the following way: $C = [{\color{blue}6}, {\color{red}1}, {\color{red}3}, {\color{blue}4}, {\color{blue}5}]$. The length of longest non-decreasing subsequence in this case is $4$.

Test Case-2: We can merge the arrays in the following way: $C = [{\color{blue}1}, {\color{red}2}, {\color{red}2}, {\color{blue}3}, {\color{red}4}]$. The length of longest non-decreasing subsequence in this case is $5$.

Source: codechef | Difficulty: MEDIUM_HARD


Solutions:
from bisect import bisect_right

def lis(arr):
	sub = []
	for (i, x) in enumerate(arr):
		if len(sub) == 0 or sub[-1] <= x:
			sub.append(x)
		else:
			idx = bisect_right(sub, x)
			sub[idx] = x
	return len(sub)
for _ in range(int(input())):
	(N, M) = input().split(' ')
	N = int(N)
	M = int(M)
	A = list(map(int, input().split()))
	B = list(map(int, input().split()))
	print(lis(A) + lis(B))


from bisect import bisect_right

def lis(arr):
	sub = []
	for (i, x) in enumerate(arr):
		if len(sub) == 0 or sub[-1] <= x:
			sub.append(x)
		else:
			idx = bisect_right(sub, x)
			sub[idx] = x
	return len(sub)
t = int(input())
while t > 0:
	(n, m) = map(int, input().split())
	array1 = list(map(int, input().split(' ')[:n]))
	array2 = list(map(int, input().split(' ')[:m]))
	a = lis(array1)
	b = lis(array2)
	print(a + b)
	t -= 1


from bisect import bisect_right

def LIS(a):
	L = []
	for x in a:
		i = bisect_right(L, x)
		if i == len(L):
			L.append(x)
		else:
			L[i] = x
	return len(L)
t = int(input())
for _ in range(t):
	(n, m) = map(int, input().split())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	print(LIS(a) + LIS(b))


def fit(arr, x):
	(start, end) = (0, len(arr) - 1)
	while start <= end:
		mid = (start + end) // 2
		if x < arr[mid]:
			end = mid - 1
		else:
			start = mid + 1
		if start > end:
			break
	arr[start] = x
	return

def lis(arr):
	aux = [float('inf') for i in range(len(arr))]
	for i in range(len(arr)):
		fit(aux, arr[i])
	maxLen = 0
	for i in range(len(arr)):
		if aux[i] == float('inf'):
			maxLen = i
			break
	maxLen = len(arr) if maxLen == 0 else maxLen
	return maxLen
ans = ''
for _ in range(int(input())):
	(n, m) = list(map(int, input().split()))
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	ans += str(lis(a) + lis(b)) + '\n'
print(ans)


from bisect import bisect_right as br
INF = 1000000000.0 + 1

def get_ndesc_subs_length(arr, n):
	d = [INF] * (n + 1)
	d[0] = 0
	for el in arr:
		ind = br(d, el)
		d[ind] = el
	ans = 0
	for i in range(1, n + 1):
		if d[i] != INF:
			ans = i
	return ans
t = int(input())
for _ in range(t):
	(n, m) = map(int, input().split())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	print(get_ndesc_subs_length(a, n) + get_ndesc_subs_length(b, m))


import sys

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

def bs(arr, x):
	l = 0
	h = len(arr) - 1
	while l <= h:
		m = (l + h) // 2
		if arr[m] > x:
			y = m
			h = m - 1
		else:
			l = m + 1
	return y
for _ in range(int(input())):
	(n, m) = map(int, input().split())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	lnds_a = []
	lnds_b = []
	for i in range(n):
		if len(lnds_a) == 0:
			lnds_a.append(a[i])
		elif a[i] >= lnds_a[-1]:
			lnds_a.append(a[i])
		else:
			j = bs(lnds_a, a[i])
			lnds_a[j] = a[i]
	for i in range(m):
		if len(lnds_b) == 0:
			lnds_b.append(b[i])
		elif b[i] >= lnds_b[-1]:
			lnds_b.append(b[i])
		else:
			j = bs(lnds_b, b[i])
			lnds_b[j] = b[i]
	print(len(lnds_a) + len(lnds_b))


def bs(a, x):
	l = 0
	h = len(a) - 1
	while l <= h:
		m = (l + h) // 2
		if a[m] > x:
			y = m
			h = m - 1
		else:
			l = m + 1
	return y
t = int(input())
for _ in range(t):
	(n, m) = map(int, input().split())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	t1 = []
	t2 = []
	for i in range(n):
		if len(t1) == 0:
			t1.append(a[i])
		elif a[i] >= t1[-1]:
			t1.append(a[i])
		else:
			j = bs(t1, a[i])
			t1[j] = a[i]
	for i in range(m):
		if len(t2) == 0:
			t2.append(b[i])
		elif b[i] >= t2[-1]:
			t2.append(b[i])
		else:
			j = bs(t2, b[i])
			t2[j] = b[i]
	print(len(t1) + len(t2))


def lengthOfLIS(array):
	low = [0] * len(array)
	total_size = 0
	for x in array:
		(i, j) = (0, total_size)
		while i != j:
			m = (i + j) // 2
			if low[m] <= x:
				i = m + 1
			else:
				j = m
		low[i] = x
		total_size = max(i + 1, total_size)
	return total_size
for _ in range(int(input())):
	(n, m) = map(int, input().split())
	ar1 = list(map(int, input().split()))
	ar2 = list(map(int, input().split()))
	print(lengthOfLIS(ar1) + lengthOfLIS(ar2))


import math, os, io, heapq
from functools import reduce
from sys import stdin, stdout
from collections import Counter, deque, defaultdict
from itertools import product
import bisect

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

def getInt():
	return int(input())

def lis(arr: list):
	(larr, ln) = ([arr[0]], 1)
	for v in arr[1:]:
		if v >= larr[-1]:
			ln += 1
			larr.append(v)
		else:
			ind = bisect.bisect_right(larr, v)
			larr[ind] = v
	return ln

def solve():
	(m, n) = getList()
	print(lis(getList()) + lis(getList()))
for _ in range(getInt()):
	solve()


from bisect import bisect_right

def ans(c):
	n = len(c)
	d = [c[0]]
	for i in range(1, n):
		if c[i] >= d[-1]:
			d.append(c[i])
		else:
			index = bisect_right(d, c[i])
			d[index] = min(d[index], c[i])
	return len(d)
for t in range(int(input())):
	(n, m) = map(int, input().split())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	c = []
	i = j = 0
	while i < n and j < m:
		if a[i] <= b[j]:
			c.append(a[i])
			i += 1
		else:
			c.append(b[j])
			j += 1
	while i < n:
		c.append(a[i])
		i += 1
	while j < m:
		c.append(b[j])
		j += 1
	print(ans(a) + ans(b))


def rearr(a):
	ll = [0] * len(a)
	mx = 0
	for x in a:
		(l, r) = (0, mx)
		while l != r:
			m = (l + r) // 2
			if ll[m] <= x:
				l = m + 1
			else:
				r = m
		ll[l] = x
		mx = max(mx, l + 1)
	return mx
for i in range(int(input())):
	(n, m) = map(int, input().split())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	print(rearr(a) + rearr(b))


def bs(key, arr):
	l = 0
	r = len(arr) - 1
	while l < r:
		mid = (l + r) // 2
		if arr[mid] > key:
			r = mid
		else:
			l = mid + 1
	return r

def longestNonDecreasingSubsequence(a):
	net = [a[0]]
	for i in range(1, len(a)):
		if a[i] >= net[-1]:
			net.append(a[i])
		else:
			k = bs(a[i], net)
			net[k] = a[i]
	return len(net)
t = int(input())
while t != 0:
	(n, m) = map(int, input().split())
	a = list(map(int, input().split()))[:n]
	b = list(map(int, input().split()))[:m]
	l1 = longestNonDecreasingSubsequence(a)
	l2 = longestNonDecreasingSubsequence(b)
	print(l1 + l2)
	t -= 1


from bisect import bisect

def LIS2(x):
	dp = []
	ans = []
	for i in x:
		y = bisect(dp, i)
		if y == len(dp):
			dp.append(i)
		else:
			dp[y] = i
		ans.append(y + 1)
	return max(ans)
for i in range(int(input())):
	(n, m) = map(int, input().split())
	arr1 = list(map(int, input().split()))
	arr2 = list(map(int, input().split()))
	print(LIS2(arr1) + LIS2(arr2))


def bs(tar, net):
	(low, high) = (0, len(net) - 1)
	while low < high:
		mid = (low + high) // 2
		if net[mid] > tar:
			high = mid
		else:
			low = mid + 1
	return high

def longestNonDecreasingSubsequence(a):
	net = [a[0]]
	for i in range(1, len(a)):
		if a[i] >= net[-1]:
			net.append(a[i])
		else:
			k = bs(a[i], net)
			net[k] = a[i]
	return len(net)
t = int(input())
while t != 0:
	(n, m) = map(int, input().split())
	a = list(map(int, input().split()))[:n]
	b = list(map(int, input().split()))[:m]
	l1 = longestNonDecreasingSubsequence(a)
	l2 = longestNonDecreasingSubsequence(b)
	print(l1 + l2)
	t -= 1


import bisect

def lis(A):
	arr = []
	for i in A:
		if len(arr) == 0 or i >= arr[-1]:
			arr.append(i)
		else:
			arr[bisect.bisect_right(arr, i)] = i
	return len(arr)
for _ in range(int(input())):
	(n, m) = map(int, input().split())
	A = list(map(int, input().split()))
	B = list(map(int, input().split()))
	print(lis(A) + lis(B))


def lis(nums):
	tails = [0] * len(nums)
	size = 0
	for x in nums:
		(i, j) = (0, size)
		while i != j:
			m = (i + j) // 2
			if tails[m] <= x:
				i = m + 1
			else:
				j = m
		tails[i] = x
		size = max(i + 1, size)
	return size
for _ in range(int(input())):
	(x, y) = map(int, input().split())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	print(lis(a) + lis(b))


from bisect import bisect

def lis(c, n):
	ans = [0 for i in range(n + 1)]
	l = 1
	ans[0] = c[0]
	for i in range(1, n):
		if ans[l - 1] <= c[i]:
			ans[l] = c[i]
			l += 1
		else:
			ans[bisect(ans, c[i], 0, l - 1)] = c[i]
	return l
for _ in range(int(input())):
	(n, m) = map(int, input().split())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	k1 = lis(a, n)
	k2 = lis(b, m)
	print(k1 + k2)


from bisect import bisect_left, bisect_right

def lnds(nums=[]):
	ax = []
	ax_len = 0
	for i in nums:
		if len(ax) == 0:
			ax.extend([i])
			ax_len += 1
		elif i >= ax[-1]:
			ax.extend([i])
			ax_len += 1
		else:
			inject = bisect_right(ax, i)
			ax[inject] = i
	return ax_len
t = int(input())
for _ in range(t):
	(n, m) = [int(x) for x in input().split()]
	a = [int(x) for x in input().split()]
	b = [int(x) for x in input().split()]
	la = lnds(a)
	lb = lnds(b)
	print(la + lb)


def lis(arr):
	temp = [0] * len(arr)
	res = 0
	for x in arr:
		(i, j) = (0, res)
		while i != j:
			m = (i + j) // 2
			if temp[m] <= x:
				i = m + 1
			else:
				j = m
		temp[i] = x
		res = max(i + 1, res)
	return res
t = int(input())
while t:
	(n, m) = map(int, input().split(' '))
	a = list(map(int, input().split(' ')))
	b = list(map(int, input().split(' ')))
	print(lis(a) + lis(b))
	t -= 1


import math, bisect, heapq, random, sys, itertools
ints = lambda : list(map(int, input().split()))
alpha = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
p = 10 ** 9 + 7
from bisect import bisect_right
from bisect import bisect_left

def LIS(v):
	for i in range(len(v)):
		v[i] = v[i] + i / 1000000
	if len(v) == 0:
		return 0
	tail = [0 for i in range(len(v) + 1)]
	length = 1
	tail[0] = v[0]
	for i in range(1, len(v)):
		if v[i] > tail[length - 1]:
			tail[length] = v[i]
			length += 1
		else:
			tail[bisect_left(tail, v[i], 0, length - 1)] = v[i]
	return length
for t in range(int(input())):
	(n, m) = ints()
	a = ints()
	b = ints()
	print(LIS(a) + LIS(b))


from math import inf
from bisect import bisect_right
case = int(input())
for r in range(case):
	(i, j) = list(map(int, input().split(' ')))
	a = list(map(int, input().split(' ')))
	b = list(map(int, input().split(' ')))
	l1 = [inf] * (len(a) + 1)
	l1[0] = -inf
	for z in a:
		pos = bisect_right(l1, z)
		l1[pos] = z
	i = len(a)
	while l1[i] == inf:
		i -= 1
	l2 = [inf] * (len(b) + 1)
	l2[0] = -inf
	for s in b:
		pos = bisect_right(l2, s)
		l2[pos] = s
	j = len(b)
	while l2[j] == inf:
		j -= 1
	print(i + j)


import math
from bisect import bisect_right

def lengthOfLIS(nums) -> int:
	h = len(nums) + 1
	Lis = [math.inf] * h
	Lis[0] = -math.inf
	for i in nums:
		pos = bisect_right(Lis, i, 0, h)
		Lis[pos] = i
	for i in range(h - 1, -1, -1):
		if Lis[i] != math.inf:
			return i
T = int(input())
for i in range(T):
	(n, m) = [int(x) for x in input().split()]
	arr_n = [int(x) for x in input().split()]
	arr_m = [int(x) for x in input().split()]
	print(lengthOfLIS(arr_n) + lengthOfLIS(arr_m))


def CeilIndex(A, l, r, key):
	while r - l > 1:
		m = l + (r - l) // 2
		if A[m] > key:
			r = m
		else:
			l = m
	return r

def LongestIncreasingSubsequenceLength(A, size):
	tailTable = [0 for i in range(size + 1)]
	len = 0
	tailTable[0] = A[0]
	len = 1
	for i in range(1, size):
		if A[i] < tailTable[0]:
			tailTable[0] = A[i]
		elif A[i] >= tailTable[len - 1]:
			tailTable[len] = A[i]
			len += 1
		else:
			tailTable[CeilIndex(tailTable, -1, len - 1, A[i])] = A[i]
	return len
for _ in range(int(input())):
	(n, m) = map(int, input().split())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	ca = LongestIncreasingSubsequenceLength(a, n)
	cb = LongestIncreasingSubsequenceLength(b, m)
	print(ca + cb)


def LIS(arr, m):
	size = 0
	tails = [0] * m
	for x in arr:
		(i, j) = (0, size)
		while i != j:
			mid = (i + j) // 2
			if tails[mid] <= x:
				i = mid + 1
			else:
				j = mid
		tails[i] = x
		size = max(i + 1, size)
	return size
for _ in range(int(input())):
	(n, m) = map(int, input().split())
	a1 = [int(x) for x in input().split()]
	a2 = [int(x) for x in input().split()]
	print(LIS(a1, n) + LIS(a2, m))


from bisect import bisect_right

def lis(A, size):
	arr = [A[0]]
	len = 1
	for i in range(1, size):
		ind = bisect_right(arr, A[i], 0, len)
		if ind == len:
			arr.append(A[i])
			len += 1
		else:
			arr[ind] = A[i]
	return len
try:
	for tc in range(int(input())):
		(n, m) = list(map(int, input().split()))
		arr = list(map(int, input().split()))
		barr = list(map(int, input().split()))
		print(lis(arr, n) + lis(barr, m))
except:
	pass


t = int(input())
from bisect import bisect_right

def solve(ar1, ar2):

	def lis(ar):
		ot = []
		for i in range(len(ar)):
			idx = bisect_right(ot, ar[i])
			if idx == len(ot):
				ot.append(ar[i])
			else:
				ot[idx] = ar[i]
		return len(ot)
	return lis(ar1) + lis(ar2)
for _ in range(t):
	(m, n) = list(map(int, input().split()))
	ar1 = list(map(int, input().split()))
	ar2 = list(map(int, input().split()))
	print(solve(ar1, ar2))


import bisect
T = int(input())
for t in range(T):
	(n, m) = map(int, input().split())
	a = [int(i) for i in input().split()]
	b = [int(i) for i in input().split()]

	def lis(a):
		x = []
		n = 0
		for i in a:
			j = bisect.bisect_right(x, i)
			if j == n:
				x.append(0)
				n += 1
			x[j] = i
		return len(x)
	print(lis(a) + lis(b))


from bisect import bisect_right
t = int(input())

def lis(arr):
	sub = []
	for (i, x) in enumerate(arr):
		if len(sub) == 0 or sub[-1] <= x:
			sub.append(x)
		else:
			idx = bisect_right(sub, x)
			sub[idx] = x
	return len(sub)
for _ in range(t):
	x = [int(i) for i in input().split()]
	num = [int(i) for i in input().split()]
	num2 = [int(i) for i in input().split()]
	x = lis(num)
	y = lis(num2)
	print(x + y)


from bisect import bisect
t = int(input())
for _ in range(t):
	(n, m) = map(int, input().split())
	A = list(map(int, input().split()))
	B = list(map(int, input().split()))

	def get_lis(arr):
		seq = []
		for val in arr:
			if not seq or val >= seq[-1]:
				seq.append(val)
			else:
				x = bisect(seq, val)
				seq[x] = val
		return len(seq)
	print(get_lis(A) + get_lis(B))


import os
import sys
import sys
import os.path
from io import BytesIO, IOBase
from math import cos, e, pi, ceil, pi, tan, floor, gcd, sqrt, log2, factorial, log10
from collections import Counter
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase
from statistics import mean, median, mode
from bisect import *
alpha = ascii_lowercase
beta = ascii_uppercase

def nextPowerOf2(n):
	p = 1
	if n and (not n & n - 1):
		return n
	while p < n:
		p <<= 1
	return p

def isComposite(n):
	if n <= 1:
		return False
	if n <= 3:
		return False
	if n % 2 == 0 or n % 3 == 0:
		return True
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return True
		i = i + 6
	return False

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

def isprime(n):
	if n == 1:
		return 0
	i = 2
	while i * i <= n:
		if n % i == 0:
			return 0
		i += 1
	return 1

def Sieve(n):
	prime = [True for i in range(n + 1)]
	prime[0] = False
	prime[1] = 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 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 create_matrix(n, m):
	mat = [[] for i in range(n)]
	for i in range(n):
		for j in range(m):
			mat[i].append(0)
	return mat

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

def ispoweroftwo(n):
	if n == 1:
		return True
	elif n % 2 != 0 or n == 0:
		return False
	return ispoweroftwo(n / 2)
if os.path.exists('in.txt'):
	sys.stdin = open('in.txt', 'r')
	sys.stdout = open('ot.txt', 'w')

def solve(arr):
	sub = []
	for (i, x) in enumerate(arr):
		if len(sub) == 0 or sub[-1] <= x:
			sub.append(x)
		else:
			idx = bisect_right(sub, x)
			sub[idx] = x
	return len(sub)

def main():
	for t in range(int(input())):
		(n, m) = map(int, input().split())
		a = list(map(int, input().split()))
		b = list(map(int, input().split()))
		print(solve(a) + solve(b))
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')
main()


def lis(arr):
	q = [0] * len(arr)
	s = 0
	for x in arr:
		(i, j) = (0, s)
		while i != j:
			m = (i + j) // 2
			if q[m] <= x:
				i = m + 1
			else:
				j = m
		q[i] = x
		s = max(i + 1, s)
	return s
for t in range(int(input())):
	(n, m) = map(int, input().split())
	l = list(map(int, input().split()))
	a = list(map(int, input().split()))
	print(lis(l) + lis(a))


def lengthOfLIS(nums):
	tails = [0] * len(nums)
	size = 0
	for x in nums:
		(i, j) = (0, size)
		while i != j:
			m = (i + j) // 2
			if tails[m] <= x:
				i = m + 1
			else:
				j = m
		tails[i] = x
		size = max(i + 1, size)
	return size
n = int(input())
for _ in range(n):
	(j, m) = map(int, input().split())
	A = list(map(int, input().split()))
	B = list(map(int, input().split()))
	print(lengthOfLIS(A) + lengthOfLIS(B))


from collections import defaultdict
import bisect

def LIS(A):
	ans = []
	for x in A:
		it = bisect.bisect_right(ans, x)
		if it == len(ans):
			ans.append(x)
		else:
			ans[it] = x
	return len(ans)
for _ in range(int(input())):
	(n, m) = map(int, input().split())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	a1 = LIS(a)
	a2 = LIS(b)
	ans = a1 + a2
	print(ans)


from bisect import bisect_right

def LIS(v):
	if len(v) == 0:
		return 0
	tail = [0 for i in range(len(v) + 1)]
	length = 1
	tail[0] = v[0]
	for i in range(1, len(v)):
		if v[i] >= tail[length - 1]:
			tail[length] = v[i]
			length += 1
		else:
			tail[bisect_right(tail, v[i], 0, length - 1)] = v[i]
	return length
t = int(input())
for i in range(t):
	(n, m) = map(int, input().split())
	a = [int(x) for x in input().split()]
	b = [int(x) for x in input().split()]
	print(LIS(a) + LIS(b))


import bisect

def LIS(nums):
	dp = []
	for num in nums:
		idx = bisect.bisect_right(dp, num)
		if idx != len(dp):
			dp[idx] = num
		else:
			dp.append(num)
	return len(dp)

def solve():
	(n, m) = map(int, input().split())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	print(LIS(a) + LIS(b))
for _ in range(int(input())):
	solve()


from math import inf
from bisect import *
t = int(input())

def solve(n, m, a, b):
	alis = [inf]
	for num in a:
		if num >= alis[-1]:
			alis.append(num)
		else:
			alis[bisect_right(alis, num)] = num
	blis = [inf]
	for num in b:
		if num >= blis[-1]:
			blis.append(num)
		else:
			blis[bisect_right(blis, num)] = num
	print(len(blis) + len(alis))
while t:
	(n, m) = [int(x) for x in input().split(' ')]
	a = [int(x) for x in input().split(' ')]
	b = [int(x) for x in input().split(' ')]
	t -= 1
	solve(n, m, a, b)


def merged_lis(array):
	low = [0] * len(array)
	total_size = 0
	for x in array:
		(i, j) = (0, total_size)
		while i != j:
			m = (i + j) // 2
			if low[m] <= x:
				i = m + 1
			else:
				j = m
		low[i] = x
		total_size = max(i + 1, total_size)
	return total_size
for i in range(int(input())):
	(q, p) = map(int, input().split())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	print(merged_lis(a) + merged_lis(b))


from bisect import bisect_left, bisect_right

def LongestIncreasingSubsequenceLength(v):
	if len(v) == 0:
		return 0
	tail = [0 for i in range(len(v) + 1)]
	length = 1
	tail[0] = v[0]
	for i in range(1, len(v)):
		if v[i] >= tail[length - 1]:
			tail[length] = v[i]
			length += 1
		else:
			tail[bisect_right(tail, v[i], 0, length - 1)] = v[i]
	return length
t = int(input())
for _ in range(t):
	(n, m) = map(int, input().split())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	i = 0
	j = 0
	c = []
	flag = 0
	print(LongestIncreasingSubsequenceLength(a) + LongestIncreasingSubsequenceLength(b))


def longestNonDecreasingSubSequence(a) -> int:
	from bisect import bisect_right
	sub = []
	for (idx, x) in enumerate(a):
		if len(sub) == 0 or sub[-1] <= x:
			sub.append(x)
		else:
			i = bisect_right(sub, x)
			sub[i] = x
	return len(sub)
for _ in range(int(input())):
	(n, m) = map(int, input().split())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	print(longestNonDecreasingSubSequence(a) + longestNonDecreasingSubSequence(b))


from bisect import bisect_right

def longestNonDecreasingSubsequence(arr):
	sub = []
	for (i, x) in enumerate(arr):
		if len(sub) == 0 or sub[-1] <= x:
			sub.append(x)
		else:
			idx = bisect_right(sub, x)
			sub[idx] = x
	return len(sub)
for t in range(int(input())):
	global maximum
	(N, M) = map(int, input().split())
	from math import inf
	A = list(map(int, input().split()))
	B = list(map(int, input().split()))
	print(longestNonDecreasingSubsequence(A) + longestNonDecreasingSubsequence(B))


import bisect

def ans(arr):
	t = [arr[0]]
	for i in arr[1:]:
		b = bisect.bisect_right(t, i)
		if b == len(t):
			t.append(i)
		else:
			t[b] = i
	return len(t)
for _ in range(int(input())):
	(n, m) = map(int, input().split())
	A = list(map(int, input().split()))
	B = list(map(int, input().split()))
	aa = ans(A)
	bb = ans(B)
	print(aa + bb)


from bisect import bisect_left
from typing import DefaultDict
from collections import OrderedDict, defaultdict
import math
from math import sqrt
import collections
from sys import maxsize
from itertools import combinations_with_replacement
import sys

def sieve_erasthones(n):
	cnt = 0
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p ** 2, n + 1, p):
				prime[i] = False
		p += 1
	prime[0] = False
	prime[1] = False
	for p in range(n + 1):
		if prime[p]:
			cnt += 1
	return cnt

def calculate(p, q):
	mod = 998244353
	expo = 0
	expo = mod - 2
	while expo:
		if expo & 1:
			p = p * q % mod
		q = q * q % mod
		expo >>= 1
	return p

def count_factors(n):
	i = 1
	c = 0
	while i <= math.sqrt(n):
		if n % i == 0:
			if n // i == i:
				c += 1
			else:
				c += 2
		i += 1
	return c

def ncr_modulo(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 isprime(n):
	prime_flag = 0
	if n > 1:
		for i in range(2, int(sqrt(n)) + 1):
			if n % i == 0:
				prime_flag = 1
				break
		if prime_flag == 0:
			return True
		else:
			return False
	else:
		return True

def smallestDivisor(n):
	if n % 2 == 0:
		return 2
	i = 3
	while i * i <= n:
		if n % i == 0:
			return i
		i += 2
	return n

def dict_ele_count(l):
	d = DefaultDict(lambda : 0)
	for ele in l:
		d[ele] += 1
	return d

def max_in_dict(d):
	maxi = 0
	for ele in d:
		if d[ele] > maxi:
			maxi = d[ele]
	return maxi

def element_count(s):
	l = []
	k = s[0]
	c = 0
	for ele in s:
		if ele == k:
			c += 1
		else:
			l.append([k, c])
			k = ele
			c = 1
	l.append([k, c])
	return l

def modular_exponentiation(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 != 0:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def number_of_primefactor(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(n)
	return len(set(l))
mod = 10 ** 9 + 7
ans = 0
vowels = ['a', 'e', 'i', 'o', 'u']

def CeilIndex(A, l, r, key):
	while r > l:
		m = l + (r - l) // 2
		if A[m] > key:
			r = m
		else:
			l = m + 1
	return r

def lis(A, size):
	tailTable = [0 for i in range(size + 1)]
	len = 0
	tailTable[0] = A[0]
	len = 1
	for i in range(1, size):
		if A[i] < tailTable[0]:
			tailTable[0] = A[i]
		elif A[i] >= tailTable[len - 1]:
			tailTable[len] = A[i]
			len += 1
		else:
			tailTable[CeilIndex(tailTable, -1, len - 1, A[i])] = A[i]
	return len
for _ in range(int(input())):
	(n, m) = map(int, input().split())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	print(lis(a, n) + lis(b, m))


def lis(X):
	N = len(X)
	P = [0] * N
	M = [0] * (N + 1)
	L = 0
	for i in range(N):
		lo = 1
		hi = L
		while lo <= hi:
			mid = (lo + hi) // 2
			if X[M[mid]] <= X[i]:
				lo = mid + 1
			else:
				hi = mid - 1
		newL = lo
		P[i] = M[newL - 1]
		M[newL] = i
		if newL > L:
			L = newL
	S = []
	k = M[L]
	for i in range(L - 1, -1, -1):
		S.append(X[k])
		k = P[k]
	return len(S[::-1])
for _ in range(int(input())):
	(n, m) = map(int, input().split())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	print(lis(a) + lis(b))


def fn(nums):
	n = len(nums)
	slots = [0 for _ in range(n)]
	valid_slot = 0
	for number in nums:
		(left, right) = (0, valid_slot)
		while left < right:
			mid = (left + right) // 2
			if number >= slots[mid]:
				left = mid + 1
			else:
				right = mid
		put_index = left
		slots[put_index] = number
		valid_slot = max(put_index + 1, valid_slot)
	return valid_slot

def func():
	(n, m) = map(int, input().split())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	print(fn(a) + fn(b))
t = int(input())
for i in range(t):
	func()


def CeilIndex(A, l, r, key):
	while r - l > 1:
		m = l + (r - l) // 2
		if A[m] > key:
			r = m
		else:
			l = m
	return r

def LIS(A, size):
	tailTable = [0 for i in range(size + 1)]
	len = 0
	tailTable[0] = A[0]
	len = 1
	for i in range(1, size):
		if A[i] < tailTable[0]:
			tailTable[0] = A[i]
		elif A[i] >= tailTable[len - 1]:
			tailTable[len] = A[i]
			len += 1
		else:
			tailTable[CeilIndex(tailTable, -1, len - 1, A[i])] = A[i]
	return len
for _ in range(int(input())):
	(o, m) = map(int, input().split())
	a = [int(i) for i in input().split()]
	b = [int(i) for i in input().split()]
	print(LIS(a, o) + LIS(b, m))


from bisect import bisect_right, bisect_left

def LIS(v):
	if len(v) == 0:
		return 0
	tail = [0 for i in range(len(v) + 1)]
	length = 1
	tail[0] = v[0]
	for i in range(1, len(v)):
		if v[i] >= tail[length - 1]:
			tail[length] = v[i]
			length += 1
		else:
			tail[bisect_right(tail, v[i], 0, length - 1)] = v[i]
	return length
for _ in range(int(input())):
	(n, m) = map(int, input().split())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	print(LIS(a) + LIS(b))


from bisect import bisect
import bisect

def fun(a):
	res = []
	for i in range(len(a)):
		p = bisect.bisect_right(res, a[i])
		if p == len(res):
			res.append(a[i])
		res[p] = a[i]
	return len(res)

def main():
	how_much_noob_I_am = 1
	how_much_noob_I_am = int(input())
	for _ in range(how_much_noob_I_am):
		(n, m) = map(int, input().split())
		a = list(map(int, input().split()))
		b = list(map(int, input().split()))
		print(fun(a) + fun(b))
main()


from sys import stdin
from collections import defaultdict as dd, deque as dq
from heapq import heappush as hpush, heappop as hpop
from bisect import bisect_left as bl, bisect_right as br
from random import randint
from math import sqrt, ceil, floor

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

def text():
	return stdin.readline().rstrip()

def num():
	return int(text())

def mp(dt):
	return map(dt, stdin.readline().split())

def array(dt):
	return list(mp(dt))

def search(arr, val):
	beg = 0
	end = len(arr) - 1
	ans = -1
	while beg <= end:
		mid = (beg + end) // 2
		if arr[mid] > val:
			ans = mid
			end = mid - 1
		else:
			beg = mid + 1
	return ans
t = num()
while t > 0:
	(n, m) = mp(int)
	a = array(int)
	b = array(int)
	nums = a
	dp = [nums[0]]
	n = len(nums)
	for i in range(1, len(nums)):
		if nums[i] < dp[-1]:
			idx = search(dp, nums[i])
			dp[idx] = nums[i]
		else:
			dp.append(nums[i])
	al = len(dp)
	nums = b
	dp = [nums[0]]
	n = len(nums)
	for i in range(1, len(nums)):
		if nums[i] < dp[-1]:
			idx = search(dp, nums[i])
			dp[idx] = nums[i]
		else:
			dp.append(nums[i])
	print(al + len(dp))
	t -= 1


from bisect import bisect

def LIS(x, N):
	dp = []
	ans = []
	for i in x:
		y = bisect(dp, i)
		if y == len(dp):
			dp.append(i)
		else:
			dp[y] = i
		ans.append(y + 1)
	return ans
for _ in range(int(input())):
	(n, m) = map(int, input().split())
	l1 = list(map(int, input().split()))
	l2 = list(map(int, input().split()))
	c = LIS(l1, len(l1))
	d = LIS(l2, len(l2))
	print(max(c) + max(d))


import math, sys, bisect, heapq, os
from collections import defaultdict, Counter, deque
from itertools import groupby, accumulate
from functools import lru_cache
pr = lambda x: x

def input():
	return sys.stdin.readline().rstrip('\r\n')
aj = lambda : list(map(int, input().split()))

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

def Y(c):
	print(['NO', 'YES'][c])

def y(c):
	print(['no', 'yes'][c])

def Yy(c):
	print(['No', 'Yes'][c])

def lis(A):
	arr = []
	for i in A:
		if len(arr) == 0 or i >= arr[-1]:
			arr.append(i)
		else:
			arr[bisect.bisect_right(arr, i)] = i
	return len(arr)
for _ in range(int(input())):
	(n, m) = aj()
	A = aj()
	B = aj()
	print(lis(A) + lis(B))


def longest(nums):
	tails = [0] * len(nums)
	size = 0
	for x in nums:
		(i, j) = (0, size)
		while i != j:
			m = (i + j) // 2
			if tails[int(m)] <= x:
				i = m + 1
			else:
				j = m
		tails[i] = x
		size = max(i + 1, size)
	return size
n = int(input())
for i in range(n):
	(a, b) = [int(i) for i in input().split()]
	l1 = [int(i) for i in input().split()]
	l2 = [int(i) for i in input().split()]
	print(longest(l1) + longest(l2))


from bisect import bisect_right

def LIS(v):
	if len(v) == 0:
		return 0
	tailTable = [0 for i in range(len(v) + 1)]
	length = 1
	tailTable[0] = v[0]
	for i in range(1, len(v)):
		if v[i] >= tailTable[length - 1]:
			tailTable[length] = v[i]
			length += 1
		else:
			tailTable[bisect_right(tailTable, v[i], 0, length - 1)] = v[i]
	return length
for _ in range(int(input())):
	(n, m) = map(int, input().split())
	arr1 = list(map(int, input().split()))
	arr2 = list(map(int, input().split()))
	l1 = LIS(arr1)
	l2 = LIS(arr2)
	print(l1 + l2)


from bisect import bisect
global maximum

def LIS(x):
	dp = []
	ans = []
	for i in x:
		y = bisect(dp, i)
		if y == len(dp):
			dp.append(i)
		else:
			dp[y] = i
		ans.append(y + 1)
	return max(ans)
t = int(input())
while t > 0:
	t -= 1
	(n, m) = list(map(int, input().split(' ')))
	arr1 = list(map(int, input().split(' ')))
	arr2 = list(map(int, input().split(' ')))
	print(LIS(arr1) + LIS(arr2))


import bisect

def lnds(A):
	INF = int(10000000000.0)
	dp = [INF] * (len(A) + 1)
	dp[0] = -INF
	for i in range(len(A)):
		j = bisect.bisect_right(dp, A[i])
		if dp[j - 1] <= A[i] and A[i] < dp[j]:
			dp[j] = A[i]
	ans = 0
	for i in range(1, len(A) + 1):
		if dp[i] < INF:
			ans = i
	return ans
for _ in range(int(input())):
	(N, M) = map(int, input().strip().split())
	A = list(map(int, input().strip().split()))
	B = list(map(int, input().strip().split()))
	print(lnds(A) + lnds(B))


import heapq
import math
import sys
import collections
import bisect
import time
import random
import itertools
import string
from collections import Counter, defaultdict

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

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

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

def CeilIndex(A, l, r, key):
	while r - l > 1:
		m = l + (r - l) // 2
		if A[m] > key:
			r = m
		else:
			l = m
	return r

def LongestIncreasingSubsequenceLength(A, size):
	tailTable = [0 for i in range(size + 1)]
	tailTable[0] = A[0]
	len = 1
	for i in range(1, size):
		if A[i] < tailTable[0]:
			tailTable[0] = A[i]
		elif A[i] >= tailTable[len - 1]:
			tailTable[len] = A[i]
			len += 1
		else:
			tailTable[CeilIndex(tailTable, -1, len - 1, A[i])] = A[i]
	return len
for t in range(int(input())):
	(n, m) = map(int, input().split())
	a1 = get_list()
	a2 = get_list()
	print(LongestIncreasingSubsequenceLength(a1, n) + LongestIncreasingSubsequenceLength(a2, m))


import bisect
T = int(input())
for _ in range(T):
	(N, M) = map(int, input().split())
	A = list(map(int, input().split()))
	B = list(map(int, input().split()))
	(i, j) = (0, 0)
	res = []
	count = 0
	while i < len(A):
		indx = bisect.bisect_right(res, A[i])
		if indx == count:
			res.append(A[i])
			count += 1
		else:
			res[indx] = A[i]
		i += 1
	res2 = []
	count2 = 0
	while j < len(B):
		indx = bisect.bisect_right(res2, B[j])
		if indx == count2:
			res2.append(B[j])
			count2 += 1
		else:
			res2[indx] = B[j]
		j += 1
	print(count + count2)


def lengthOfLIS(nums):
	tails = [0] * len(nums)
	size = 0
	for x in nums:
		(i, j) = (0, size)
		while i != j:
			m = (i + j) // 2
			if tails[m] <= x:
				i = m + 1
			else:
				j = m
		tails[i] = x
		size = max(i + 1, size)
	return size
for t in range(int(input())):
	(n, m) = map(int, input().split())
	arr1 = list(map(int, input().split()))
	arr2 = list(map(int, input().split()))
	l1 = lengthOfLIS(arr1)
	l2 = lengthOfLIS(arr2)
	print(l1 + l2)


def ci(a, l, r, k):
	while r - l > 1:
		m = l + (r - l) // 2
		if a[m] > k:
			r = m
		else:
			l = m
	return r

def lon(a, s):
	tail = [0 for i in range(s + 1)]
	le = 0
	tail[0] = a[0]
	le = 1
	for i in range(1, s):
		if a[i] < tail[0]:
			tail[0] = a[i]
		elif a[i] >= tail[le - 1]:
			tail[le] = a[i]
			le += 1
		else:
			tail[ci(tail, -1, le - 1, a[i])] = a[i]
	return le
for i in range(int(input())):
	(n, m) = map(int, input().split())
	l1 = list(map(int, input().split()))
	l2 = list(map(int, input().split()))
	ans = lon(l1, n) + lon(l2, m)
	print(ans)


import bisect
import sys
ip = sys.stdin.readline

def lis(arr):
	if len(arr) == 0:
		return 0
	l = [arr[0]]
	for i in range(1, len(arr)):
		element = arr[i]
		pos = bisect.bisect(l, element)
		if pos == len(l):
			if element > l[pos - 1]:
				l.append(element)
		elif element != l[pos - 1]:
			l[pos] = element
	return len(l)
for _ in range(int(ip())):
	(n, m) = map(int, ip().split())
	a = list(map(int, ip().split()))
	b = list(map(int, ip().split()))
	a1 = sorted(list(zip(a, [i for i in range(n)])))
	b1 = sorted(list(zip(b, [i for i in range(m)])))
	di1 = dict()
	di2 = dict()
	for i in range(n):
		if i == 0:
			di1[a1[i][1]] = a1[i][0]
		else:
			di1[a1[i][1]] = di1[a1[i - 1][1]] + 1
	for i in range(m):
		if i == 0:
			di2[b1[i][1]] = b1[i][0]
		else:
			di2[b1[i][1]] = di2[b1[i - 1][1]] + 1
	a2 = []
	b2 = []
	for i in range(n):
		a2.append(di1[i])
	for i in range(m):
		b2.append(di2[i])
	print(lis(a2) + lis(b2))


import math

def lis(arr):
	dp = [0] * len(arr)
	maxx = 0
	for x in arr:
		(i, j) = (0, maxx)
		while i != j:
			m = (i + j) // 2
			if dp[m] <= x:
				i = m + 1
			else:
				j = m
		dp[i] = x
		maxx = max(i + 1, maxx)
	return maxx

def func(N, M, nums, nums2):
	return lis(nums) + lis(nums2)
	pass
T = int(input())
for i in range(T):
	(N, M) = map(int, input().split())
	A = list(map(int, input().split()))
	B = list(map(int, input().split()))
	print(func(N, M, A, B))


from bisect import bisect_right

def lis(v):
	if len(v) == 0:
		return 0
	tail = [0 for i in range(len(v) + 1)]
	length = 1
	tail[0] = v[0]
	for i in range(1, len(v)):
		if v[i] >= tail[length - 1]:
			tail[length] = v[i]
			length += 1
		else:
			tail[bisect_right(tail, v[i], 0, length - 1)] = v[i]
	return length
t = int(input())
for _ in range(t):
	(n, m) = list(map(int, input().split()))
	arr = list(map(int, input().split()))
	brr = list(map(int, input().split()))
	l1 = lis(arr)
	l2 = lis(brr)
	print(l1 + l2)


from sys import stdin
input = stdin.readline
from bisect import bisect_right

def lis(v):
	if len(v) == 0:
		return 0
	tail = [0 for i in range(len(v) + 1)]
	length = 1
	tail[0] = v[0]
	for i in range(1, len(v)):
		if v[i] >= tail[length - 1]:
			tail[length] = v[i]
			length += 1
		else:
			ind = bisect_right(tail, v[i], 0, length - 1)
			tail[ind] = v[i]
	return length

def func():
	print(lis(a) + lis(b))
for _ in range(int(input())):
	(n, m) = map(int, input().split())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	func()


from bisect import bisect_left, bisect_right

def merge(N, M):
	(i, j) = (0, 0)
	tm = []
	while i < len(N) and j < len(M):
		if N[i] < M[j]:
			tm.append(N[i])
			i += 1
		else:
			tm.append(M[j])
			j += 1
	while i < len(N):
		tm.append(N[i])
		i += 1
	while j < len(M):
		tm.append(M[j])
		j += 1
	return tm

def lengthOfLIS(nums):
	tails = [0] * len(nums)
	size = 0
	for x in nums:
		(i, j) = (0, size)
		while i != j:
			m = (i + j) // 2
			if tails[m] < x:
				i = m + 1
			else:
				j = m
		tails[i] = x
		size = max(i + 1, size)
	return size

def lengthOfLIS_(nums) -> int:
	sub = []
	for x in nums:
		if len(sub) == 0 or sub[-1] <= x:
			sub.append(x)
		else:
			idx = bisect_right(sub, x)
			sub[idx] = x
	return len(sub)
for t in range(int(input())):
	(a, b) = map(int, input().split())
	N = list(map(int, input().split()))
	M = list(map(int, input().split()))
	C = merge(N, M)
	print(lengthOfLIS_(N) + lengthOfLIS_(M))


import bisect

def count(lst):
	best = []
	for v in lst:
		pos = bisect.bisect(best, v)
		if pos < len(best):
			best[pos] = v
		else:
			best.append(v)
	return len(best)
for _ in range(int(input())):
	(n, m) = map(int, input().split())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	print(count(a) + count(b))


from math import inf
from collections import *
import math, os, sys, heapq, bisect, random
from functools import lru_cache
from itertools import *

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

def out(var):
	sys.stdout.write(str(var))

def inpu():
	return int(inp())

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 main():
	how_much_noob_I_am = 1
	how_much_noob_I_am = int(input())
	for __ in range(how_much_noob_I_am):
		(n, m) = sep()
		a = lis()
		b = lis()

		def fun(a):
			res = []
			for i in range(len(a)):
				p = bisect.bisect_right(res, a[i])
				if p == len(res):
					res.append(a[i])
				res[p] = a[i]
			return len(res)
		print(fun(a) + fun(b))
main()


def CeilIndex(A, l, r, key):
	while r - l > 1:
		m = l + (r - l) // 2
		if A[m] > key:
			r = m
		else:
			l = m
	return r

def LongestIncreasingSubsequenceLength(A, size):
	tailTable = [0 for i in range(size + 1)]
	len = 0
	tailTable[0] = A[0]
	len = 1
	for i in range(1, size):
		if A[i] < tailTable[0]:
			tailTable[0] = A[i]
		elif A[i] >= tailTable[len - 1]:
			tailTable[len] = A[i]
			len += 1
		else:
			tailTable[CeilIndex(tailTable, -1, len - 1, A[i])] = A[i]
	return len
for tt in range(int(input())):
	(n, m) = map(int, input().split())
	arr1 = list(map(int, input().split()))
	arr2 = list(map(int, input().split()))
	print(LongestIncreasingSubsequenceLength(arr1, n) + LongestIncreasingSubsequenceLength(arr2, m))


def f(A, n):
	L = [0 for i in range(n)]
	count = 0
	for x in A:
		i = 0
		j = count
		while i != j:
			y = (i + j) // 2
			if L[y] <= x:
				i = y + 1
			else:
				j = y
		L[i] = x
		count = max(i + 1, count)
	return count
T = int(input())
for tc in range(T):
	(n, m) = map(int, input().split())
	A = list(map(int, input().split()))
	B = list(map(int, input().split()))
	print(f(A, n) + f(B, m))


from bisect import *

def lis(a):
	ok = [0] * (len(a) + 1)
	fk = 1
	ok[0] = a[0]
	for i in range(1, len(a)):
		if a[i] >= ok[fk - 1]:
			ok[fk] = a[i]
			fk += 1
		else:
			ok[bisect_right(ok, a[i], 0, fk - 1)] = a[i]
	return fk
t = int(input())
for _ in range(t):
	t -= 1
	(n, m) = map(int, input().split())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	print(lis(a) + lis(b))