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



Sample input:
5 2
3 1
3 1
10 3
1 10
10 1
8 3
0 0


Sample output:
4
4

Submission status: Accepted

Solution code:
while True:
    n, r = map(int, raw_input().split())

    if n == 0 and r == 0:
        break

    Cards = [i for i in range(n, 0, -1)]

    for i in range(r):
        p, c = map(int, raw_input().split())
        Cards = Cards[p-1:p-1+c] + Cards[:p-1] + Cards[p-1+c:]

    print Cards[0]

Submission status: Accepted

Solution code:
while True:
    n, r = map(int, raw_input().split())

    if n == 0 and r == 0:
        break

    Cards = range(n, 0, -1)

    for i in range(r):
        p, c = map(int, raw_input().split())
        Cards = Cards[p-1:p-1+c] + Cards[:p-1] + Cards[p-1+c:]

    print Cards[0]

Submission status: Accepted

Solution code:
temp = True
i = 0
N = []
M = []
while True:
    p,c = map(int, input().strip().split(' '))
    if p == 0 and c == 0:
        break
    if temp:
        n = p
        r = c
        for j in range(n):
            N.append(n-j)
        temp = False
    else:
        for j in range(c):
            M.append(N[p+j-1])
        for j in range(p-1):
            M.append(N[j])
        for j in range(n-c-p+1):
            M.append(N[j+c+p-1])
        N = M
        M = []
        i = i + 1
        if i == r:
            print(N[0])
            temp = True
            N = []
            i = 0

Submission status: Accepted

Solution code:
while True:
    n, r = map(int, input().split())
    if n == 0:
        break
    else:
        cards = list(reversed(range(n)))
        for _ in range(r):
            p, c = map(int, input().split())
            for i,x in enumerate(cards):
                if 0 <= x < p-1:
                    cards[i] += c
                elif p-1 <= x < p-1+c:
                    cards[i] -= p-1
                else:
                    pass
        for i,x in enumerate(cards):
            if x == 0:
                print(i+1)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    ans = []
    while True:
        n, r = map(int, input().split())
        if n == 0 and r == 0:
            break
        cards = []
        for i in range(n):
            cards.append(i+1)
        cards = cards[::-1]
        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:]
        ans.append(cards[0])

    for i in range(len(ans)):
        print(ans[i])

main()

Submission status: Accepted

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

    ope = [list(map(int, input().split())) for _ in range(r)]
    d = [n-i for i in range(n)]

    for i in range(r):
        tv = d[ope[i][0]-1:ope[i][0]-1+ope[i][1]]
        to = d[:ope[i][0]-1] + d[ope[i][0]-1+ope[i][1]:]
        d = tv + to
    print(d[0])

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**3
eps = 1.0 / 10**10
mod = 10**9+7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    rr = []
    while True:
        n,r = LI()
        if n == 0:
            break
        a = list(range(n))
        for _ in range(r):
            p,c = LI()
            a = a[p-1:p-1+c] + a[:p-1] + a[p-1+c:]
        rr.append(n - a[0])

    return '\n'.join(map(str, rr))


print(main())

Submission status: Accepted

Solution code:
while True:
    n, r = [int(s) for s in input().strip().split()]

    if n == 0 and r == 0:
        break

    cards = list(range(1, n+1))
    cards.reverse()

    for _ in range(r):
        p, c = [int(s) for s in input().strip().split()]

        part1 = cards[0:p-1]
        part2 = cards[p-1:p-1+c]

        cards[0:c] = part2
        cards[c:c+p-1] = part1

    print(cards[0])

Submission status: Accepted

Solution code:
n , r = map(int, input().split())
while(n != 0 or r != 0):
    deck = []
    for i in range(n):
        deck.append(i+1)
    deck.reverse()
    for i in range(r):
        p, c = map(int, input().split())
        deck[:p + c -1] = deck[p-1:p+c-1] + deck[:p-1]
    print(deck[0])
    n , r = map(int, input().split())

Submission status: Accepted

Solution code:
while 1:
  n,r=list(map(int,input().split()))
  if not n and not r:break
  l=list(range(1,n+1))[::-1]
  for _ in range(r):
    p,c=list(map(int,input().split()))
    l=l[p-1:p-1+c]+l[0:p-1]+l[p-1+c:]
  print(l[0])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=1
r=1
while n !=0 and r!=0:
    n,r=input().split()
    n,r=int(n),int(r)

    card= [[int(i) for i in input().split()] for i in range(r)]
    yama= list(range(n,0,-1))
    
    if n ==0 and r==0:
        break

    for i in range(r):
        p=card[i][0]
        c=card[i][1]
        x=yama[:p-1]
        y=yama[p-1:p-1+c]
        z=yama[p-1+c:] 
      
       
        yama=(y+x+z)
        
    print(yama[0])

Submission status: Accepted

Solution code:
while 1:
        n,r = map(int, raw_input().split())
        if n == 0 and r == 0: break
        lib = range(n, 0, -1)
        #print lib
        for i in xrange(r):
                temp = []
                p,c = map(int, raw_input().split())
                for j in xrange(c):
                        temp.append(lib.pop(p-1))
                lib = temp + lib
                #print lib
        print lib[0]

Submission status: Accepted

Solution code:
import sys


while True:
    n, r = map(int, sys.stdin.readline().split())
    if n == 0 and r == 0:
        break
    shuffle = []
    for i in xrange(r):
        shuffle.append(map(int, sys.stdin.readline().split()))
    shuffle.reverse()
    pile = range(n)
    for s in shuffle:
        pile[s[0] + s[1] - 1:s[0] + s[1] - 1] += pile[:s[1]]
        pile = pile[s[1]:]
#        print pile
    print len(pile) - pile.index(0)

"""10 9 8 7 6 5 4 3 2 1
1 10 9 8 7 6 5 4 3 2
4 3 2 1 10 9 8 7 6 5

0 1 2 3 4 5 6 7 8 9
3 4 5 6 7 8 9 0 1 2
4 5 6 7 8 9 0 1 2 3

5 4 3 2 1
3 5 4 2 1
4 3 5 2 1

0 1 2 3 4
1 2 0 3 4
2 0 1 3 4"""

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:

def shuffle(cards):
    index,cut_num = map(int,input().split()) 
    pop = cards[index-1 :index + cut_num-1 ]
    del cards[index-1:index + cut_num-1]
    return pop + cards 

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

  cards = list(reversed(range(1,num_card+1)))
  for i in range(challange):
      cards = shuffle(cards)
  print(cards[0])

Submission status: Accepted

Solution code:
import sys
import math
input = sys.stdin.readline
##
n, r = input().rstrip().split()
n, r = int(n), int(r)
while n + r > 0:
    yama = list(range(1, n + 1))[::-1]
    for _ in range(r):
        p, c = input().rstrip().split()
        p, c = int(p), int(c)
        top = yama[:p - 1]
        mid = yama[p - 1:p - 1 + c]
        yama[:c] = mid
        yama[c:p - 1 + c] = top
    print(yama[0])
    n, r = input().rstrip().split()
    n, r = int(n), int(r)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# encoding: utf-8
def create_deck(deck_size):
    deck = []
    for i in range(0,deck_size):
        deck.append(i+1)
    return deck

def shuffle_deck(deck, start, cut_size):
    after = []
    cut_cards = []
    b = len(deck) - cut_size - start
    for i in range(0,len(deck)):
        if b >= i:
            after.append(deck[i])
            continue
        if (b+cut_size) >= i:
            cut_cards.append(deck[i])
            continue
        after.append(deck[i])
    after.extend(cut_cards)
    return after

while True:
    data = input().split()
    deck_size, shuffle_count = int(data[0]),int(data[1])
    if deck_size == shuffle_count == 0: break
    deck = create_deck(deck_size)
    for i in range(0,shuffle_count):
        cut_info = data = input().split()
        start, cut_size = int(cut_info[0]),int(cut_info[1])
        deck = shuffle_deck(deck, start, cut_size)
    print(deck[len(deck)-1])

Submission status: Accepted

Solution code:
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])

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

Solution code:
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])

Submission status: Accepted

Solution code:
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])

Submission status: Accepted

Solution code:
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)
    #print(beforelist)
    #print(afterlist)
    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):
            #print(beforelist)
            afterlist[j] = copy.deepcopy(beforelist[i])
            j+=1
            #print(afterlist)
            #print(beforelist)
        for i in range(0,p-1):
            afterlist[j] = copy.deepcopy(beforelist[i])
            j+=1
        beforelist = copy.deepcopy(afterlist)
        #print(afterlist)
    print(beforelist[0])
if __name__ == '__main__':
  solve()

Submission status: Accepted

Solution code:
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])

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

Solution code:
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])

Submission status: Accepted

Solution code:
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])

Submission status: Accepted

Solution code:
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])

Submission status: Accepted

Solution code:
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])

Submission status: Accepted

Solution code:
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])

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

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()

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

Solution code:
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)))

Submission status: Accepted

Solution code:
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])

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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])

Submission status: Accepted

Solution code:
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)
    #print(yama1)
    for i in range(len(yama1)):
      if yama1[i] in yama:
        yama.remove(yama1[i])
    yama=yama+yama1
    #print(yama)
  print(yama[n-1])

Submission status: Accepted

Solution code:
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])

Submission status: Accepted

Solution code:
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])

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

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])

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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())

Submission status: Accepted

Solution code:
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])

Submission status: Accepted

Solution code:
# Hanafuda Shuffle

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)

Submission status: Accepted

Solution code:
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])

Submission status: Accepted

Solution code:
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])

Submission status: Accepted

Solution code:
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])

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

Solution code:
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())

Submission status: Accepted

Solution code:
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])

Submission status: Accepted

Solution code:
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])

Submission status: Accepted

Solution code:
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])

Submission status: Accepted

Solution code:
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])

Submission status: Accepted

Solution code:
# coding: utf-8
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]()

Submission status: Accepted

Solution code:
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
    print x[0]

Submission status: Runtime Error

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

Submission status: Runtime Error

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

    l = []
    for i in range(1, n + 1):
        l.append(i)

    for i in range(0, r):
        p, c = map(int, input().split())
        tmp = l[len(l) - p + 1:len(l)]
        del l[len(l) - p + 1:len(l)]
        l[len(l) - c:len(l) - c] = tmp

    print(l[len(l) - 1])

Submission status: Runtime Error

Solution code:
x=0
while True:
	sousa=input().sprit() #?????????
	sousa=[int(sousa[0]),int(sousa[1])]
	if sousa==[0,0]:break
	yama=[]
	for k in range(sousa[1]):yama.append(k+1)
	yama=yama[::-1]
	for r in range(sousa[1]):
		[p,c]=input().sprit()
		[p,c]=[int(p),int(c)]
		yama[:p+c-1]=yama[p-1:p+c-1]+yama[:p-2]
	print(yama[0])

Submission status: Runtime Error

Solution code:
while True:
	sousa=input().sprit() #?????????
	sousa=[int(sousa[0]),int(sousa[1])]
	if sousa==[0,0]:break
	yama=[]
	for k in range(sousa[0]):yama.append(k+1)
	yama=yama[::-1]
	for r in range(sousa[1]):
		[p,c]=input().sprit()
		[p,c]=[int(p),int(c)]
		yama[:p+c-1]=yama[p-1:p+c-1]+yama[:p-2]
	print(yama[0])

Submission status: Runtime Error

Solution code:
first = input().split()
n = int(first[0])
r = int(first[1])
while n != 0 or r != 0:
    i = 0
    card = list(range(n))
    while i < r:
        sh = input().split()
        p, c = list(map(int,sh))
        card = card[p-1:p+c-2:] + card[0:p-2:] + card[p+c-1:n:]
        i = i + 1
    print(n - int(card[0]))
    next = input().split()
    n, r = list(map(int,next))

Submission status: Runtime Error

Solution code:
cond = [int(n) for n in input().split(" ")]

while True:
    case = []
    cards = [n for n in range(1,cond[0] + 1)]
    for c in range(cond[1]):
        case = ([int(n) for n in input().split(" ")]
        disp = cards[case[0]:case[0]+case[1]]
        cards[case[0]:case[0]+case[1]] = cards[case[0]-case[1]:case[0]]
        cards[case[0]-case[1]:case[0]] = disp
    else:
        print(cards[0])
        cond = [int(n) for n in input().split(" ")]
        if cond[0] == 0 and cond[1] == 0:
            break
        else:
            continue

Submission status: Runtime Error

Solution code:
cond = [int(n) for n in input().split(" ")]

while True:
    case = []
    cards = [n for n in range(1,cond[0] + 1)]
    for c in range(cond[1]):
        case = [int(n) for n in input().split(" ")]
        disp = cards[case[0]:case[0]+case[1]]
        cards[case[0]:case[0]+case[1]] = cards[case[0]-case[1]:case[0]]
        cards[case[0]-case[1]:case[0]] = disp
    else:
        print(cards[0])
        cond = [int(n) for n in input().split(" ")]
        if cond[0] == 0 and cond[1] == 0:
            break
        else:
            continue

Submission status: Runtime Error

Solution code:
l = input()
L = l.split()
for i in range(len(L)):
	L[i] = int(L[i])
while True:
	if L[0] == 0:
		break
	a = [L[0]-number for number in range(L[0])]
	for j in range(L[1]):
		r = input()
		R = r.split()
		for k in range(len(R)):
			R[k] = int(R[k])
		b = []
		b += a[R[0]-1:R[0]+R[1]-1]
		b += a[:R[0]-1]
		b += a[R[0]+R[1]-1:]
		a.clear()
		a = b.copy()
	print(a[0])