import java.util.Scanner; import java.util.Stack; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int round = in.nextInt(); for (int i = 1; i <= round; i++) { int length = in.nextInt(); in.nextLine(); // 吸收换行符 String[] arr1 = in.nextLine().split(" "); String[] arr2 = in.nextLine().split(" "); boolean result = ifValid(arr1, arr2); System.out.println(result ? "Yes" : "No"); } in.close(); } public static boolean ifValid(String[] pushArr, String[] popArr) { Stack<String> stack = new Stack<>(); int popIndex = 0; // 记录出栈序列的当前位置 for (String num : pushArr) { stack.push(num); // 先将当前元素入栈 // 入栈后,检查栈顶是否与出栈序列当前元素匹配,匹配则弹出 while (!stack.isEmpty() && stack.peek().equals(popArr[popIndex])) { stack.pop(); popIndex++; // 移动出栈指针 } } // 如果栈为空,说明所有元素都按规则弹出,即匹配 return stack.isEmpty(); } }