t = int(input())
for i in range(t):
    n1 = int(input())
    arr1 = list(map(int,input().split()))
    n2 = int(input())
    arr2 = list(map(int,input().split()))
    




























    # i,j = 0, 0
    # sec = 0
    # count = 0
    # last_commen = 0
    # while i < n1:
    #     if count == 0:
    #         j = 0
    #     else:
    #         j = nex
    #     while j < n2:
    #         if arr1[i] > last_commen and arr1[i] == arr2[j] :
    #             count += 1
    #             j += 1
    #             nex = j
    #             last_commen = arr1[i]
    #             print(last_commen)
    #             break
    #         else:
    #             j += 1
    #     i += 1
