import java.util.*;
public class Main {
public static class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
next = null;
}
}
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
// 在这里补充代码
// ListNode ptrA = headA;
// ListNode ptrB = headB;
// // 当两个指针不相遇时继续循环
// while (ptrA != ptrB) {
// // 如果走到链表末尾,就切换到另一个链表头部
// ptrA = (ptrA == null) ? headB : ptrA.next;
// ptrB = (ptrB == null) ? headA : ptrB.next;
// }
// return ptrA; // 返回相交节点或null
ListNode nodeA = headA;
ListNode nodeB = headB;
while (nodeA != null) {
while (nodeB != null) {
if (nodeA == nodeB) {
return nodeA;
}
nodeB = nodeB.next;
}
nodeB = headB;
nodeA = nodeA.next;
}
return null;
}
//你不需要关心主函数的内容!
public static void main(String[] args) {
Main solution = new Main();
Scanner scanner = new Scanner(System.in);
// 读入数据
int lenA = scanner.nextInt();
int lenB = scanner.nextInt();
int commonLen = scanner.nextInt();
// 构建链表
ArrayList<ListNode> nodesA = new ArrayList<>();
ArrayList<ListNode> nodesB = new ArrayList<>();
ArrayList<ListNode> nodesCommon = new ArrayList<>();
// 读入并创建链表A的独立部分
for (int i = 0; i < lenA - commonLen; i++) {
int val = scanner.nextInt();
nodesA.add(new ListNode(val));
if (i > 0) nodesA.get(i - 1).next = nodesA.get(i);
}
// 读入并创建链表B的独立部分
for (int i = 0; i < lenB - commonLen; i++) {
int val = scanner.nextInt();
nodesB.add(new ListNode(val));
if (i > 0) nodesB.get(i - 1).next = nodesB.get(i);
}
// 读入并创建公共部分
for (int i = 0; i < commonLen; i++) {
int val = scanner.nextInt();
nodesCommon.add(new ListNode(val));
if (i > 0) nodesCommon.get(i - 1).next = nodesCommon.get(i);
}
// 连接链表
ListNode headA = null;
ListNode headB = null;
if (lenA - commonLen > 0) {
headA = nodesA.get(0);
if (commonLen > 0) nodesA.get(nodesA.size() - 1).next = nodesCommon.get(0);
} else if (commonLen > 0) {
headA = nodesCommon.get(0);
}
if (lenB - commonLen > 0) {
headB = nodesB.get(0);
if (commonLen > 0) nodesB.get(nodesB.size() - 1).next = nodesCommon.get(0);
} else if (commonLen > 0) {
headB = nodesCommon.get(0);
}
// 调用函数获取结果
ListNode result = solution.getIntersectionNode(headA, headB);
// 输出结果
if (result == null) {
System.out.println("null");
} else {
System.out.println(result.val);
}
scanner.close();
}
}