Follow The Prize

시간 제한7초메모리 제한2048 MB

요약
숨겨진 상품의 위치를 m번의 위치 교환에 따라 추적하고 마지막 위치를 출력한다.
난이도

쉬움10점 중 2점

유형
시뮬레이션, 구현
정답자
아직 제출이 없습니다

문제

A total of nn cups were placed upside-down on a table, a prize was placed under one of them, and then the cups were shuffled. Luckily, you know under which cup the prize began, and how the cups were shuffled. Can you determine the final position of the prize?

A cup (and the prize which it might cover) can only be in one of nn distinct positions at any given time: 1,2,…,n1, 2, \ldots, n. No two cups can be in the same position at any given time.

The cups are shuffled by performing a series of swaps. A swap is defined as exchanging the location of two cups. For example if the cups in positions 44 and 77 were swapped, the cup originally in position 44 would end in position 77, and the cup originally in position 77 would end in position 44. If the prize were originally under cup 77, this swap would have moved the prize to position 44.

입력

The first line is nn, an integer between 22 and 1,000,0001\\,000\\,000, inclusive, defining the number of cups.

The second line is an integer between 11 and nn, inclusive, which defines the initial position of the prize.

The third line is mm, a non-negative integer less than or equal to 1,000,0001\\,000\\,000, defining the number of swaps performed to shuffle the cups.

The final mm lines report the swaps that were performed, in order. Each line consists of two different space-separated integers, between 11 and nn, defining the positions of the cups that were exchanged for that swap.

출력

The output is a single integer between 11 and nn, which is the position of the prize after all of the swaps are performed.

예제1

  1. 예제 1

    입력
    3
    2
    3
    1 2
    3 2
    1 3
    
    예상 출력
    3