Catapult-Carousel | 프로그래밍의 벗 PivotOJ
PivotOJ

Catapult-Carousel

시간 제한: 1000ms메모리 제한: 1024MB출처: EIO 2020-21 openBOJ 29884

문제

Catapult-carousel is a new experimental fairground attraction that John invented: a rotating wheel where NN seats are spaced at equal distances. The seats are numbered 1N1 \ldots N in clockwise order. Each seat is equipped with a mechanism that can launch the rider up the air in a controlled manner---so that they land on a predetermined seat.

An (X,Y)(X, Y)-jump is the mode of operation of the carousel where all riders will be launched simultaneously in such a way that the rider launched from seat 11 will land on seat XX and each subsequent rider (from seats 2N2 \ldots N) will land on a seat YY positions clockwise from the previous landing position. John has designed the carousel in a way that no two riders will ever land on the same seat (for any allowed jump mode).

One ride on the carousel comprises of several jumps. The number and parameters of jumps are the same in all rides. The riders do not change seats between jumps, even when a rider takes several rides in a sequence. John is selling multiple-ride tickets that specify the initial seat and the number of rides. Now he wants to check that the rider is in the expected seat after completing all the rides.

Write a program that gets the seat number UU of a rider and computes their seat after KK rides (or the seat they were at K|K| rides ago, in case KK is negative).

입력

The first line contains NN, the number of seats on the carousel (2N<2632 \le N < 2^{63}), and SS, the number of jumps in a ride (1S1061 \le S \le 10^6).

Each of the following SS lines sepcifies the parameters XiX_i and YiY_i of one jump (0<XiN0 < X_i \le N, 0<Yi<N0 < Y_i < N, where 1iS1 \le i \le S).

The last line contains UU, the number of the seat in question (1UN1 \le U \le N), and KK, the number of rides (263K<263-2^{63} \le K < 2^{63}).

출력

The only line should contain one integer: the number of the seat the rider will be on after KK rides (or was on K|K| rides ago, if KK is negative).

예제

예제 1

입력
11 2
3 2
5 7
4 2
출력
1

예제 2

입력
911 4
3 2
5 7
133 22
11 12
1 -1
출력
825
코드를 제출하려면 로그인하세요.