#include <stdio.h>
#include <stdlib.h>

typedef struct Node {
    int data;
    struct Node *next;
    struct Node *prev;
} Node;

// Function to create a new node
Node* createNode(int val) {
    Node* newNode = (Node*)malloc(sizeof(Node));
    newNode->data = val;
    newNode->next = NULL;
    newNode->prev = NULL;
    return newNode;
}

// Function to insert node at the end of the doubly linked list
Node* insertEnd(Node* head, int val) {
    Node* newNode = createNode(val);
    if (!head) return newNode;
    Node* temp = head;
    while (temp->next) temp = temp->next;
    temp->next = newNode;
    newNode->prev = temp;
    return head;
}

// Function to find the intersection node
Node* findIntersection(Node* head1, Node* head2) {
    Node* temp1 = head1;
    Node* temp2 = head2;

    // Traverse both lists to check for intersection
    while (temp1) {
        temp2 = head2;
        while (temp2) {
            if (temp1 == temp2) return temp1; // Intersection found
            temp2 = temp2->next;
        }
        temp1 = temp1->next;
    }
    return NULL; // No intersection
}

int main() {
    int n1, n2;

    // Input validation for n1
    if (scanf("%d", &n1) != 1 || n1 <= 0 || n1 > 10) {
        printf("Invalid input\n");
        return 0;
    }

    Node* head1 = NULL;
    for (int i = 0; i < n1; i++) {
        int val;
        if (scanf("%d", &val) != 1 || val < -1000 || val > 1000) {
            printf("Invalid input\n");
            return 0;
        }
        head1 = insertEnd(head1, val);
    }

    // Input validation for n2
    if (scanf("%d", &n2) != 1 || n2 <= 0 || n2 > 10) {
        printf("Invalid input\n");
        return 0;
    }

    Node* head2 = NULL;
    for (int i = 0; i < n2; i++) {
        int val;
        if (scanf("%d", &val) != 1 || val < -1000 || val > 1000) {
            printf("Invalid input\n");
            return 0;
        }
        head2 = insertEnd(head2, val);
    }

    // Find intersection
    Node* intersection = findIntersection(head1, head2);
    if (intersection) {
        printf("%d\n", intersection->data);  // Print the intersection node's data
    } else {
        printf("The two lists do not intersect\n")