The Ball And Cups

All submissions for this problem are available.
At the end of a busy day, The Chef and his assistants play a game together. The game is not just for fun but also used to decide who will have to clean the kitchen. The Chef is a Game Master, so his concern is how to manage the game but not how to win the game like his assistants do.
The game requires players to find the only ball under one of the N cups after their positions are changed in a special way. At the beginning of the game, The Chef places N cups in a row and put a ball under the Cth cup from the left (the cups are numbered from 1 to N). All players can see the initial position of the ball. Then Chef performs Q flip operations. Each flip operation is defined by two integers L and R such that 1 ≤ L ≤ R ≤ N and consists in reversing the segment [L, R] of cups. Namely, Chef swaps Lth and Rth cups, (L+1)th and (R−1)th cups, and so on. After performing all the operations Chef asks his assistants to choose a cup that they think the ball is under it. Who can guess the position of the ball will win the game, and of course, the others will have to clean the kitchen.
The Chef doesn't want to check all the N cups at the end of the game. He notes down the value of C and the pairs (L, R) and asked you, the mastered programmer, to determine the cup that contains the ball.
Input
The first line of the input contains a single integer T, denoting the number of test cases. The description of T test cases follows. The first line of each test case contains three spaceseparated integers N, C and Q, denoting the total number of cups, the initial position of the ball and the number of flip operations Chef will perform. Each of the following Q lines contains two spaceseparated integers L and R, denoting the ends of the segment of the current flip operation.
Output
For each test case output on a separate line the final position of the ball.
Constraints
 1 ≤ T ≤ 10
 1 ≤ N ≤ 100000 (10^{5})
 1 ≤ C ≤ N
 1 ≤ Q ≤ 10000 (10^{4})
 1 ≤ L ≤ R ≤ N
Example
Input: 1 5 2 3 1 4 3 5 1 5 Output: 1
Explanation
The row of cups at the beginning of the game and after each flip is shown below. Here '' means an empty cup and 'B' is the cup that hides the ball, the segment of flip is marked bold.
B
B
B
B
Author:  tuananh93 
Tester:  anton_lunyov 
Editorial  http://discuss.codechef.com/problems/TAVISUAL 
Tags  adhoc, cakewalk, cook32, tuananh93 
Date Added:  26012013 
Time Limit:  1 sec 
Source Limit:  50000 Bytes 
Languages:  C, JAVA, PYTH, PYTH 3.6, CS2, PAS fpc, PAS gpc, RUBY, PHP, GO, NODEJS, HASK, SCALA, D, PERL, FORT, WSPC, ADA, CAML, ICK, BF, ASM, CLPS, PRLG, ICON, SCM qobi, PIKE, ST, NICE, LUA, BASH, NEM, LISP sbcl, LISP clisp, SCM guile, JS, ERL, TCL, PERL6, TEXT, CLOJ, FS 
Comments
 Please login at the top to post a comment.
SUCCESSFUL SUBMISSIONS
Fetching successful submissions