#include <stdio.h>

typedef struct node
{
    int data;
    struct node *next;
}link_node;

/* 打印链表 */
static void print_link(link_node *head)
{
    link_node *p = head;
    while(p != NULL)
    {
        printf("%d ", p->data);
        p = p->next;
    }
    printf("\n");
}

/* 创建头结点并赋值 */
static link_node *create_link(int headVal)
{
    link_node *head = (link_node*)malloc(sizeof(link_node));
    if(head == NULL)
    {
        printf("create_link head failed!!!\n");
        return NULL;
    }
    head->data = headVal;
    head->next = NULL;    //初始化头结点
    return head;
}

/* 在链表中查找节点a, 在a后面插入节点b */
void add(link_node *head, int a, int b)
{
    link_node *p = head;
    link_node *new = (link_node *)malloc(sizeof(link_node));
    if(new == NULL)
    {
        printf("add new failed!!!\n");
        return;
    }
    while(p != NULL)
    {
        if(p->data == b)
        {
            new->data = a;
            new->next = p->next;
            p->next = new;
            break;
        }
        else
        {
            p = p->next;   
        }
    }
}

/* 删除链表中的c节点 */
static void delete(link_node *head, int c)
{
    link_node *p1 = NULL;
    link_node *p2 = NULL;
    if(head->data == c)    //头结点就是要删除的节点
    {
        p1 = head;
        head = head->next;
        free(p1);
        p1 = NULL;
        return;
    }
    else
    {
        p1 = head;
        p2 = head->next;
        while(p2 != NULL)
        {
            if(p2->data == c)
            {
                p1->next = p2->next;    //节点的下一个节点为要删除的下一个节点
                free(p2);
                p2 = NULL;
                break;
            }
            else
            {
                p1 = p2;
                p2 = p2->next;
            }
        }
    }
}


int main()
{
    int n = 0, headVal = 0;
    scanf("%d %d", &n, &headVal);
    link_node *head = create_link(headVal);
    if(head)
    {
        for(int i = 0; i < n; i++)
        {
            int a = 0;
            int b = 0;
            scanf("%d %d", &a, &b);
            add(head, a, b);
        }
        //print_link(head);
        int c;
        scanf("%d", &c);
        delete(head, c);
        print_link(head);
    }
    return 0;
}