// Arup Guha
// 12/12/08
// Example for programming team: Reducing a non-graph problem to a graph 
//                               problem
// Problem: Longest Common Increasing Sequence

import java.util.*;
import java.io.*;

class Node {
	
	// Value is a number from both lists.
	// indexL1 is where that number is stored in list 1.
	// indexL2 is where that number is stored in list 2.
	public int value;
	public int indexL1;
	public int indexL2;
	
	public Node(int a, int b, int c) {
		value = a;
		indexL1 = b;
		indexL2 = c;
	}
	
	// A number in this node can be placed before node n if the number in 
	// n is larger (increasing) AND it appears in both list 1 and list 2
	// AFTER this number (subsequence).
	public boolean connect(Node n) {
		return value < n.value && indexL1 < n.indexL1 && 
		       indexL2 < n.indexL2;
	}
}

public class lcis {
	

	public static void main(String[] args) throws Exception {

		Scanner fin = new Scanner(new File("lcis.in"));
		
		int numCases = fin.nextInt();
		
		for (int i=0; i<numCases; i++) {
			
			// Read in the input.
			int len1 = fin.nextInt();
			int[] list1 = new int[len1];
			for (int j=0; j<len1; j++)
				list1[j] = fin.nextInt();
				
			int len2 = fin.nextInt();
			int[] list2 = new int[len2];
			for (int j=0; j<len2; j++)
				list2[j] = fin.nextInt();
				
			// Solve the problem.
			System.out.println(solve(list1,list2));
		}
	}	


	
	
	
	public static int solve(int[] a, int[] b) {
		
		// Step 1: Create all of the vertices for our graph.
		ArrayList<Node> allNodes = createNodes(a,b);
		
		// Step 2: Create the adj. matrix for our graph from the vertices.
		int[][] adjMatrix = createMatrix(allNodes);
		
		// Step 3: Relax this graph in "reverse" for the longest path.
		return relax(adjMatrix);	
	}
	
	// Creates all of the nodes from the two lists that we will use in
	// our corresponding graph for this instance of the problem.
	public static ArrayList<Node> createNodes(int[] a, int[] b) {
		
		ArrayList<Node> list = new ArrayList<Node>();
		
		// This will be our "start" node, so to speak.
		list.add(new Node(0,-1,-1));
		
		// Add a node for each pair of values in a and b that are the same.
		// The node stores the number, where it's found in list a and where
		// it's found in list B.
		for (int i=0; i<a.length; i++) {
			for (int j=0; j<b.length; j++) {
				if (a[i] == b[j]) {
					list.add(new Node(a[i],i,j));
				}
			}
		}
		
		// We need to find the largest number in both lists.
		int max = 0;
		for (int i=0; i<a.length; i++) if (a[i] > max) max = a[i];
		for (int i=0; i<b.length; i++) if (b[i] > max) max = b[i];
		
		// Add our end node, it comes "after" all numbers in both lists.
		list.add(new Node(max+1,a.length,b.length));
		return list;
	}
	
	public static int[][] createMatrix(ArrayList<Node> list) {
		
		int[][] m = new int[list.size()][list.size()];
		
		// Initialize adjacency matrix.
		for (int i=0; i<m.length; i++)
			for (int j=0; j<m[i].length; j++)
				m[i][j] = 0;
				
		// Fill in all edges between nodes that "connect".
		for (int i=0; i<m.length; i++) {
			for (int j=0; j<m[i].length; j++) {
				// Put in an edge of weight 1 if you can put the number
				// encoded by i before the number encoded by j
				// in a common increasing sequence.
				if ((list.get(i)).connect(list.get(j)))
					m[i][j] = 1;
			}
		}
		return m;
	}
	
	public static int relax(int[][] adj) {
		
		int[] estimates = new int[adj.length];
		for (int i=0; i<estimates.length; i++) estimates[i] = 0;
		
		// Vertex we are relaxing....
		for (int i=0; i<adj.length; i++) {
			
			// Here we look for edges leaving this vertex
			for (int j=i+1; j<adj.length; j++) {
					
					// Can't relax this edge because it doesn't exist.
					if (adj[i][j] == 0) continue;
					
					// Replace if we find a GREATER length =)
					// Our graph is acyclic, so our end answer will
					// correspond to a valid acyclic path which corresponds
					// to a valid common increasing sequence.
					if (estimates[j] < estimates[i] + adj[i][j])
						estimates[j] = estimates[i] + adj[i][j];
				
			}
		}
		
		// Node 0 is our designated start node and the last node is our
		// designated end node. We subtract one because we added and extra
		// one for the edge connecting to our "end" node.
		return estimates[estimates.length-1]-1;
	}
	
}