Problem:
 Problem A: Hanafuda Shuffle There are a number of ways to shuffle a deck of cards.   Hanafuda 
shuffling for Japanese card game 'Hanafuda' is one such example.
The following is how to perform Hanafuda shuffling. There is a deck of n cards. Starting from the p -th card 
from the top of the deck, c cards are pulled out
and put on the top of the deck, as shown in Figure 1.
This operation, called a cutting operation, is repeated. Write a program that simulates Hanafuda shuffling and answers which 
card will be finally placed on the top of the deck.       Figure 1: Cutting operation    Input The input consists of multiple data sets.
Each data set starts with a line containing two positive integers n (1 <= n <= 50) and r (1 <= r <= 50); n and r are the number of cards in the deck and the number of cutting 
operations, respectively. There are r more lines in the data set, each of which
 represents a cutting operation. These cutting operations 
are performed in the listed order.
Each line contains two positive integers p and c ( p + c <= n + 1).
Starting from the p -th card from the top of the deck, c cards should be pulled out and put on the top. The end of the input is indicated by a line which contains two zeros. Each input line contains exactly two integers separated by a space 
character.
There are no other characters in the line. Output For each data set in the input, your program 
should write the number of the top card after the shuffle.
Assume that at the beginning the cards 
are numbered from 1 to n , from the bottom to the top.
Each number should be written in a separate line 
without any superfluous characters such as leading or following spaces. Sample Input 5 2
3 1
3 1
10 3
1 10
10 1
8 3
0 0 Output for the Sample Input 4
4

Solution (Python):
import copy
while True :
    n, r = map(int, input().split())
    if n == 0 and r == 0 :
        break
    
    Card = [0] * n
    for i in range(n) :
        Card[i] = n - i
    Card_copy = copy.copy(Card)
    
    for i in range(r) :
        p, c = map(int, input().split())
        for j in range(c) :
            Card[j] = Card_copy[p+j-1]
        for j in range(c, p+c-1):
            Card[j] = Card_copy[j-c]
        Card_copy = copy.copy(Card)
    print(Card[0])

Solution (Python):
MOD = 10 ** 9 + 7
INF = 10 ** 10
import sys
sys.setrecursionlimit(100000000)
dy = (-1,0,1,0)
dx = (0,1,0,-1)

def main():
    while True:
        n,r = map(int,input().split())
        if n == 0:
            return
        ans = list(range(n,0,-1))
        for _ in range(r):
            p,c = map(int,input().split())
            p -= 1
            ans = ans[p:p + c] + ans[:p] + ans[p + c:]
        print(ans[0])
if __name__ == '__main__':
    main()

Solution (Python):
class Deck:

    def __init__(self, card_num):
        self.deck_list = self.make_deck(card_num)

    def make_deck(self, card_num):
        deck_list = [None] * card_num
        order = 0
        for i in reversed(range(1, card_num + 1)):
           deck_list[order] = i
           order += 1
        return deck_list

    def shuffled(self, cut_pos1, cut_pos2):
        above_card = self.deck_list[0:cut_pos1]
        below_card = self.deck_list[cut_pos1:cut_pos2]
        remain_card = self.deck_list[cut_pos2:]
        self.deck_list = below_card + above_card + remain_card


def get_two_int():
    two_int = input().split()
    for i in range(2):
        two_int[i] = int(two_int[i])
    return two_int


if __name__ == "__main__":
    while True:
        card_num, shuffle_num = get_two_int()
        if card_num == shuffle_num == 0:
            break
        deck = Deck(card_num)
        for i in range(shuffle_num):
            c, p = get_two_int()
            cut_pos1 = c - 1
            cut_pos2 = c - 1 + p
            deck.shuffled(cut_pos1, cut_pos2)
        print(deck.deck_list[0])

Solution (Python):
while (True):
    n, r = map(lambda i: int(i), input().split())
    if n == 0:
        break

    inds = [i for i in reversed(range(1, n+1))]

    for i in range(r):
        p, c = map(lambda j: int(j), input().split())
        inds = inds[p-1:p-1+c] + inds[:p-1] + inds[p-1+c:]

    print(inds[0])

Solution (Python):
import copy 
def solve():
  while(True):
    n, r = map(int, input().split())
    beforelist = [i for i in range(n, 0, -1)]
    afterlist = copy.deepcopy(beforelist)
    
    
    if n == 0 and r == 0:
        break
    for _ in range(r):
        p, c = map(int, input().split())
        j = 0
        for i in range(p-1,p-1+c):
            
            afterlist[j] = copy.deepcopy(beforelist[i])
            j+=1
            
            
        for i in range(0,p-1):
            afterlist[j] = copy.deepcopy(beforelist[i])
            j+=1
        beforelist = copy.deepcopy(afterlist)
        
    print(beforelist[0])
if __name__ == '__main__':
  solve()

Solution (Python):
while 1:
    n,r = map(int,input().split())
    if n == 0 and r == 0:
        break
      
    lists = list(range(1,n+1))
    lists.sort()
    lists.reverse()
    for i in range(r):
        p,c = map(int,input().split())
        tmp = lists[p-1:(p-1)+c]
        lists[p-1:(p-1)+c] = []
        lists = tmp + lists
    print(lists[0])

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 6)
from bisect import *
from collections import *
from heapq import *

int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def SI(): return sys.stdin.readline()[:-1]
def MI(): return map(int, sys.stdin.readline().split())
def MI1(): return map(int1, sys.stdin.readline().split())
def MF(): return map(float, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LF(): return list(map(float, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
dij = [(0, 1), (1, 0), (0, -1), (-1, 0)]

def main():
    while 1:
        n,r=MI()
        if n==0:break
        aa=list(range(n,0,-1))
        for _ in range(r):
            p,c=MI()
            aa=aa[p-1:p-1+c]+aa[:p-1]+aa[p-1+c:]
        print(aa[0])

main()

Solution (Python):
while 1:
    n, r=map(int, input().split())
    if n==0:
        break
    a=[n-i for i in range(n)]
    for _ in range(r):
        p, c=map(int, input().split())
        a=a[p-1:p-1+c] + a[0:p-1] + a[p-1+c:]
    print(a[0])

Solution (Python):
def shuffle(arr, p, c):
    p -= 1
    cut = arr[p: p + c]
    top = arr[:p]
    bot = arr[p + c:]
    return cut + top + bot

if __name__ == '__main__':
    while True:
        N, R = list(map(int, input().split()))
        if N == 0 and R == 0:
            break

        arr = [ N - x for x in range(N) ]

        for _ in range(R):
            p, c = list(map(int, input().split()))
            arr = shuffle(arr, p, c)
        
        print(arr[0])

Solution (Python):
while 1:
    n,r = map(int,input().split())
    if n==0: break
    a = [n-i for i in range(n)]
    for i in range(r):
        p,c = map(int,input().split())
        a = a[p-1:p-1+c] + a[:p-1] + a[p-1+c:]
    print(a[0])

Solution (Python):
while True:
    n, r = [int(x) for x in input().split()]
    if n == 0 and r == 0:
        break
    L = [ n-x for x in range(n)]
    for _ in range(r):
        p, c = [int(x) for x in input().split()]
        p -= 1
        L = L[p:p+c] + L[:p] + L[p+c:]
    print(L[0])

Solution (Python):
while True:
    n,r = map(int,input().split())
    if n==0:
        break
    cards = [n-i for i in range(n)]
    for i in range(r):
        p,c=map(int,input().split())
        p -= 1
        cards = cards[p:p+c]+cards[:p]+cards[p+c:]
    print(cards[0])

Solution (Python):
ans_list = []

while True:
    n,r = map(int,input().split())
    if (n,r) == (0,0):
        break
    pc = [list(map(int,input().split())) for _ in range(r)]
    mt = list(range(1,n+1)[::-1])

    def cal(p,c):
        global mt
        mt = mt[p-1:p-1+c] + mt[:p-1] + mt[p-1+c:]
    
    for p,c in pc:
        cal(p,c)
    
    ans = mt[0]
    ans_list.append(ans)

for ans in ans_list:
    print(ans)

Solution (Python):


def main():
    while 1:
        n, r = list(map(int,input().split()))
        l = list(range(n, 0, -1))
        if n == 0 and r == 0:
            return 0
        for _ in range(r):
            p, c = list(map(int, input().split()))
            l_ = l[p-1:p+c-1]
            l_.extend(l[:p-1])
            l_.extend(l[p+c-1:])
            l = l_
        print(l[0])


if __name__ == "__main__":
    main()

Solution (Python):
def solve(n, r, pc):
    deck = [n+1-i for i in range(1,n+1)]
    for p, c in pc:
        deck = deck[p-1:p+c-1] + deck[0:p-1] + deck[p+c-1:]
    return deck[0]

def main():
    ans = []
    while True:
        n, r = map(int, input().split())
        if (n==0) and (r==0):
            break
        pc = [list(map(int, input().split())) for i in range(r)]
        ans.append(solve(n, r, pc))
    
    for i in ans:
        print(i)
    
main()

Solution (Python):
ans = []
while True:
    n,r = map(int,input().split())
    if n == 0 and r == 0:
        break
    x = [i for i in range(1,n+1)]
    for i in range(r):
        p,c = map(int,input().split())
        x = x[:n-(p-1+c)] + x[n-(p-1):] + x[n-(p-1+c):n-(p-1)]
    ans.append(x[-1])
print("\n".join(map(str,ans)))

Solution (Python):
while True:
    n,r = map(int,input().split())
    if n == 0:
        break
    card = []
    for i in range(n):
        card.append(n - i)
    for i in range(r):
        p,c = map(int,input().split())
        card = card[p - 1:p + c - 1] + card[0:p - 1] + card[p + c - 1:n]
    print(card[0])

Solution (Python):
def main(n, m):
    dp = [(i + 1) for i in range(n)]
    dp = dp[::-1]
    for _ in range(m):
        p, c = map(int, input().split())
        dp = dp[p - 1:p + c - 1] + dp[:p - 1] + dp[p + c - 1:]
    print(dp[0])
while 1:
    n, r = map(int, input().split())
    if n == r == 0:
        break
    main(n,r)

Solution (Python):
res = []
while True:
    n,k = map(int,input().split())
    if n == 0 and k == 0:
        break
    card = [i for i in range(n,0,-1)]
    for i in range(k):
        p,c = map(int,input().split())
        card = card[p-1:p-1+c]+card[:p-1]+card[p+c-1:]
    res.append(card[0])
for i in range(len(res)):
    print(res[i])

Solution (Python):
while True:
  n,r=map(int,input().split())
  if n==0 and r==0:
    break
  yama=[i for i in range(1,n+1)]
  for i in range(r):
    p,c=map(int,input().split())
    yama1=[]
    for i in range(n-(p-1)-c,n-(p-1)):
      a=yama[i]
      yama1.append(a)
    
    for i in range(len(yama1)):
      if yama1[i] in yama:
        yama.remove(yama1[i])
    yama=yama+yama1
    
  print(yama[n-1])

Solution (Python):
while True:
    n, r = map(int, input().split())
    if n == 0 and r == 0:
        break
    cards = [n-i for i in range(n)]
    for i in range(r):
        p, c = map(int, input().split())
        cards = cards[p-1:p-1+c] + cards[:p-1] + cards[p-1+c:]
    print(cards[0])

Solution (Python):
while 1:
    n,r = map(int, input().split())
    if n == r == 0:
        break
    a = list(range(1,n+1))[::-1]
    for i in range(r):
        p,c = map(int, input().split())
        p -= 1
        a = a[p:p+c]+a[:p]+a[p+c:]
    print(a[0])

Solution (Python):


while 1:
    n,r = map(int,input().split())
    if n == 0 and r == 0:
        break
    card = [i+1 for i in range(n)]
    for i in range(r):
        p,c = map(int,input().split())
        x = card[n-p-c+1:n-p+1]
        for j in x:
            card.remove(j)
        card += x
    print(card[-1])

Solution (Python):
while True:
    n, r = map(int, input().split())
    if n == r == 0:
        break
    cs = list(range(n))
    for i in range(r):
        p, c = map(int, input().split())
        of = n - c - p + 1
        cs = cs[:of] + cs[of + c:] + cs[of:of+c]

    print(cs[-1] + 1)

Solution (Python):
n, r = map(int, input().split())
while n != 0:
        hanafuda = list(range(1,n+1))[::-1]
        for _ in range(r):
                p, c = map(int, input().split())
                hanafuda = hanafuda[p-1:p+c-1] + hanafuda[:p-1] + hanafuda[p+c-1:]
        print(hanafuda[0])
        n, r = map(int, input().split())

Solution (Python):
while True :
  n, r = map(int, input().split())
  if(n == 0 and r == 0) :
    break
  else :
    card = list()
    for i in range(n) :
      card.append(i + 1)
    card = card[::-1]
    for i in range(r) :
      p, c = map(int, input().split())
      card = card[p-1:p+c-1] + card[:p-1] + card[p + c - 1:]
      
    print(card[0])

Solution (Python):


ans = []

while True:
    n, r = map(int, input().split())
    if n == 0 and r == 0:
        break

    deck = [i + 1 for i in range(n)]
    deck.reverse()
    for i in range(r):
        p, c = map(int, input().split())
        p -= 1
        deck = deck[p:p+c] + deck[:p] + deck[p+c:]
    ans.append(deck[0])

for i in ans:
    print(i)

Solution (Python):
while True:
    n, r = map(int, input().split())
    if n == 0:
        break
    deck = list(range(n, 0, -1))
    for i in range(r):
        p, c = map(int, input().split())
        deck = deck[p - 1:p - 1 + c] + deck
        del deck[p - 1 + c:p - 1 + c + c]
    print(deck[0])

Solution (Python):
while True:
    n, r = map(int, input().split())
    if n == 0:
        break
    cards = list(range(n, 0, -1))
    for p, c in (map(int, input().split()) for _ in [0]*r):
        cards = cards[p-1:p-1+c] + cards[:p-1] + cards[p-1+c:]

    print(cards[0])

Solution (Python):
while True:
    n, r = map(int, input().split())
    if (n,r) == (0,0):
        break
    else:
        cards = [i for i in range (1, n+1)]
        cards.reverse()
        for i in range (r):
            p,c = map(int,input().split())
            top = cards[0 : p-1]
            mid = cards[p-1 : p+c-1]
            btm = cards[p+c-1:]
            cards = mid + top + btm
        print(cards[0])

Solution (Python):
def main():
    while True:
        n, r = map(int, input().split())
        if n == 0:
            break
        cards = [i for i in range (1, n+1)]
        cards.reverse()
        
        for i in range (r):
            p, c = map(int,input().split())
            top = cards[0 :  p-1]
            mid = cards[p-1 : p+c-1]
            btm = cards[p+c-1:]
            cards = mid + top + btm
        print(cards[0])
main()

Solution (Python):
n,r=map(int,input().split())
while n!=0 or r!=0:
    card=[i for i in range(n,0,-1)]
    for i in range(r):
        p,c=map(int,input().split())
        for j in range(c):
            card.insert(0,card.pop(c+p-2))
    print(card[0])
    n,r=map(int,input().split())

Solution (Python):
while True:
    N, R = map(int, input().split())
    if N == 0 and R == 0:
        break
    L = [i for i in range(N, 0, -1)]
    for i in range(R):
        p, c = map(int, input().split())
        p -= 1
        c = p + c
        tmp = L[p:c]
        tmp.extend(L[:p])
        tmp.extend(L[c:])
        L = tmp[:]
    print(L[0])

Solution (Python):
while 1:
    n, r = map(int, input().split())
    if n == 0:
        break

    card = list(range(n, 0, -1))
    for _ in range(r):
        p, c = map(int, input().split())
        tmp = card[p-1:p-1+c]
        del card[p-1:p-1+c]
        card[0:0] = tmp

    print(card[0])

Solution (Python):
while True:
    n,r = map(int,input().split())
    if(n==0): break
    cards = list(range(n,0,-1))
    for i in range(r):
        p,c = map(int,input().split())
        cards = cards[p-1:p+c-1] + cards[:p-1] + cards[p+c-1:]
    print(cards[0])

Solution (Python):
while True:
  n, r = map(int, input().split())
  if n==0 and r==0:
    break

  yama = [i for i in range(n, 0, -1)]
  for _ in range(r):
    p, c = map(int, input().split())
    up, bottom = [], []
    up.extend(yama[p-1:p+c-1])
    up.extend(yama[:p-1])
    bottom.extend(yama[p+c-1:])
    up.extend(bottom)
    yama = up[:]
  print(yama[0])

Solution (Python):

def q0():
    while True:
        data=[]
        n=int(input())
        if n==0:
            return
        for i in range(n):
            data.append(int(input()))
        print((sum(data)-max(data)-min(data))//(n-2))

def q1():
    while True:
        n,r=map(int,input().split())
        if n==0:
            return
        deck=[n-i for i in range(n)]
        for i in range(r):
            p,c=map(int,input().split())
            deck=deck[p-1:p-1+c]+deck[0:p-1]+deck[p-1+c:]
        print(deck[0])


question=[q0,q1]

question[1]()

Solution (Python):
while True:
    n, r = map(int, raw_input().split())
    if n == 0 and r == 0: break

    x = range(1, n+1)[::-1]
    for _ in range(r):
        p, c = map(int, raw_input().split())
        a = p-1
        b = p+c-1
        y = x[a:b] + x[:a] + x[b:]
        x = y[:]
        
    print x[0]