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: Wrong Answer

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import heapq

while True:
    n,r = map(int,input().split(" "))
    if n==0 and r == 0:
        break
    q = []
    for i in range(n):
        heapq.heappush(q,(i,n-i))
    for i in range(r):
        p,c = map(int,input().split(" "))
        for j in range(p-1+c):
            tp = heapq.heappop(q)
            if j < p:
                heapq.heappush(q,(j+p-1,tp[1]))
            else:
                heapq.heappush(q,(j-p,tp[1]))
    print(heapq.heappop(q)[1])

Submission status: Wrong Answer

Solution code:

while True:
	n, r = map(int, input().split())
	if (n, r) == (0, 0):
		break
	cards = list(range(1, n+1))[::-1]
	print(cards)

	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: Wrong Answer

Solution code:
while True:
    n,r = map(int, raw_input().split())
    if n==0 and r==0:
        break
    else:
        list = []
        for i in range(1,n+1):
            list.append(i)
        print list
        for i in range(0,r):
            p, c = map(int, raw_input().split())
            hoge = list[len(list)-p+1:len(list)]
            del list[len(list)-p+1:len(list)]
            # print hoge
            # print list
            # list.insert(len(list)-c, hoge)
            list[len(list)-c:len(list)-c] = hoge
            # print list
        print list[len(list)-1]

Submission status: Wrong Answer

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

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

    cards = list(range(1, n+1))[::-1]

    for _ in range(r):
        p, c = map(int, input().split())
        cards = cards[p-1:p-1+c] + cards[:p-1] + cards[p+c:]

    print(cards[0])

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
while 1:
	x, y = map(int,raw_input().split())
	lst = range(x,0,-1)
	if x == 0:
		break
	lst = range(x,0,-1)	
	for i in range(y):
	    c = []
	    a,b = map(int,raw_input().split())
	    for f in range(b):
	    	c.append(lst.pop(a-1))
	    	lst = c + lst
	print lst[0]

Submission status: Wrong Answer

Solution code:
while True:
    str = raw_input("Input n and r:")
    list = str.split()
    n = int(list[0])
    r = int(list[1])
    if(n == 0 and r == 0):
        break
    n_list = []
    for i in xrange(n):
        n_list.append(i+1)
    for i in xrange(r):
        p_list = []
        c_list = []
        str = raw_input("Input p and c:")
        list = str.split()
        p = int(list[0])
        c = int(list[1])
        p_list.extend(n_list[n-p+1:n])
        c_list.extend(n_list[n-p+1-c:n-p+1])
        for num in c_list:
            if num in n_list:
                n_list.remove(num)
        for num in p_list:
            if num in n_list:
                n_list.remove(num)
        n_list.extend(p_list)
        n_list.extend(c_list)
    print n_list[n-1]

Submission status: Wrong Answer

Solution code:
while True:
    str = raw_input("Input n and r:")
    list = str.split()
    n = int(list[0])
    r = int(list[1])
    if(n == 0 and r == 0):
        break
    n_list = []
    for i in xrange(n):
        n_list.append(i+1)
    for i in xrange(r):
        cp_list = []
        str = raw_input("Input p and c:")
        list = str.split()
        p = int(list[0])
        c = int(list[1])
        cp_list.extend(n_list[n-p+1-c:n-p+1])
        for num in cp_list:
            if num in n_list:
                n_list.remove(num)
        n_list.extend(cp_list)
    print n_list[n-1]

Submission status: Wrong Answer

Solution code:
while True:
    str = raw_input("Input n and r:")
    list = str.split()
    n = int(list[0])
    r = int(list[1])
    if(n == 0 and r == 0):
        break
    n_list = []
    for i in xrange(n):
        n_list.append(i+1)
    for i in xrange(r):
        str = raw_input("Input p and c:")
        list = str.split()
        p = int(list[0])
        c = int(list[1])
        n_list =  n_list[0:n-(p-1)-c] + n_list[n-(p-1):n] + n_list[n-(p-1)-c:n-(p-1)]
    print n_list[n-1]

Submission status: Wrong Answer

Solution code:
while True:
    n,r = map(int,raw_input().split())
    if(n == 0 and r == 0):
        break
    li = []
    for i in xrange(1,n+1):
        li.append(i)
    print li
    for i in xrange(r):
        temp_lis1 = []
        p,c = map(int,raw_input().split())
        if(c == 1):
            temp_lis1.append(li[n-p])
            del(li[n-p])
        else:
            temp_lis1.extend(li[n-p-c+1:n-p+1])
            del(li[n-p-c+1:n-p+1])
        li.extend(temp_lis1)
        print li

Submission status: Wrong Answer

Solution code:
#-*- coding: utf-8 -*-
def hanafuda(a, p, c):
    for i in range(0,c):
        b = []
        b.append(a.pop(p+i-1))
        for j in range(0, len(b)):
            a.insert(0, b.pop(j))
    return a[0]

while(1):
    n, r = (int(i) for i in input().split())
    if (n == 0 and r == 0):
        exit(0)
    else:
        a = [int(i) for i in range(1,n+1)]
        a.reverse()
        for i in range(r):
            p, c = (int(i) for i in input().split())
            c =  hanafuda(a, p, c)
        print(c)

Submission status: Wrong Answer

Solution code:
#hanafuda
N,r=(int(i) for i in input().split())
H=[int(i) for i in range(N)]
for i in range(N):
    p,c=(int(i) for i in input().split())
    T=[]
    for j in range(p,p+c-1):
        T.append(H[p])
        H.pop(p)
    H=T+H
print(H[0])

Submission status: Wrong Answer

Solution code:
#hanafuda
N,r=(int(i) for i in input().split())
while N!=0 or r!=0:
    H=[int(i+1) for i in range(N)]
    for i in range(r):
        p,c=(int(i) for i in input().split())
        T=[]
 #       print(H)
        for j in range(p-1,p+c-1):
  #          print("T")
            T.append(H[p-1])
            H.pop(p-1)
        H=T+H
    print(H[0])
    N,r=(int(i) for i in input().split())

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
while True:
    n, r = list(map(lambda x: int(x), input().split(" ")))
    if n == 0 and r == 0:
        break
    cards = [i for i in range(1,n+1)]
    cards = cards[::-1]

    for i in range(r):
        print(cards)
        p, c = list(map(lambda x: int(x), input().split(" ")))
        p -= 1
        cards = cards[p:p+c] + cards[0:p] + cards[p+c:]
    
    print(cards[0])

Submission status: Wrong Answer

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: Wrong Answer

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

    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: Wrong Answer

Solution code:
while True:
    n,r=map(int,raw_input().split())
    if n==r==0:break
    L=range(1,n+1)[::-1]
    print L
    for	i in range(r):
	p,c=map(int,raw_input().split())
	p=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 and r==0:
        break
    cut = [i for i in range(1,n+1)][::-1]
    for i in range(r):
        p,c = map(int,input().split())
        cut = cut[p-1:p+c-1] + cut[:p-1] + cut[p+c-1:]
    print(cut[0])

Submission status: Accepted

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

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

    print(card[0])

Submission status: Accepted

Solution code:
while True:
    n, r = map(int, input().split())
    if n == 0 and r == 0:
        break
    deck = [i + 1 for i in range(n)]
    for shuffle in range(r):
        p, c = map(int, input().split())
        # print("start: {}, end: {}".format(len(deck) - p - c + 1, len(deck) - p + 1))
        hoge = deck[len(deck) - p - c + 1 : len(deck) - p + 1]
        del deck[len(deck) - p - c + 1 : len(deck) - p + 1]
        deck.extend(hoge)
        # print("現在の山札: {}".format(deck))
    print(deck[-1])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,datetime
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())

while True:
    n,r = inpl()
    if n == 0:
        break
    else:
        yama = [n-i for i in range(n)]
        for _ in range(r):
            p,c = inpl()
            next_yama = yama[p-1:p-1+c] + yama[:p-1] + yama[p-1+c:]
            yama = next_yama
        print(yama[0])

Submission status: Accepted

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

Submission status: Accepted

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:p-1+c] + cards[:p-1] + cards[p-1+c:]
    print(cards[0])

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import heapq

while True:
    n,r = map(int,input().split(" "))
    if n==0 and r == 0:
        break
    q = []
    for i in range(n):
        heapq.heappush(q,(i,n-i))
    for i in range(r):
        p,c = map(int,input().split(" "))
        tmpQueue = []
        for j in range(p-1+c):
            tp = heapq.heappop(q)
            if j < p-1:
                heapq.heappush(tmpQueue,(j+c,tp[1]))
            else:
                heapq.heappush(tmpQueue,(j-p+1,tp[1]))
        for j in range(p-1+c):
            heapq.heappush(q,heapq.heappop(tmpQueue))
    print(heapq.heappop(q)[1])

Submission status: Accepted

Solution code:
#! /usr/bin/env python
# -*- coding: utf-8 -*-

(n, r) = map(int, raw_input().split())
while n!=0 and r!=0:
    list = [n-j for j in range(n)]
    for i in range(r):
        (p, c) = map(int, raw_input().split())
        p -= 1
        list = list[p:p+c] + list
        del list[p+c:p+c+c]

    print list[0]
    (n, r) = map(int, raw_input().split())

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#! /usr/bin/python3

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

Submission status: Accepted

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

    cards = [i for i in reversed(range(1, amount + 1))]

    for _ in range(count):
        p_th, length = map(int, input().split())
        swapper = cards[p_th - 1:p_th + length - 1]
        cards[p_th - 1:p_th + length - 1] = []
        cards[0:0] = swapper

    print(cards[0])

Submission status: Accepted

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

def solve(n, r):
	fuda = [i for i in xrange(1,n+1)]
	fuda = fuda[::-1]
	for i in xrange(r):
		p,c = map(int,raw_input().split())
		fuda = fuda[p-1:p+c-1] + fuda[0:p-1] + fuda[p+c-1:n]
	print fuda[0]

if __name__ == "__main__":
	while 1:
		n,r = map(int,raw_input().split())
		if n == 0 and r == 0:
			sys.exit()
		else:
			solve(n,r)

Submission status: Accepted

Solution code:

while True:
	n, r = map(int, input().split())
	if (n, r) == (0, 0):
		break
	cards = list(range(1, n+1))[::-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:]
	print(cards[0])

Submission status: Accepted

Solution code:
while True:
    n,r = map(int, raw_input().split())
    if n==0 and r==0:
        break
    else:
        list = []
        for i in range(1,n+1):
            list.append(i)
        # print list
        for i in range(0,r):
            p, c = map(int, raw_input().split())
            hoge = list[len(list)-p+1:len(list)]
            del list[len(list)-p+1:len(list)]
            # print hoge
            # print list
            # list.insert(len(list)-c, hoge)
            list[len(list)-c:len(list)-c] = hoge
            # print list
        print list[len(list)-1]

Submission status: Accepted

Solution code:
while True:
    n, r = map(int, raw_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, raw_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: Accepted

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

    l = range(n, 0, -1)

    for i in range(r):
        tmp = []
        p, c = map(int, raw_input().split())
        for j in range(c):
            tmp.append(l.pop(p - 1))
        l = tmp + l
    print l[0]

Submission status: Accepted

Solution code:
while True:
    n, r = map(int, raw_input().split())
    if n == r == 0:
        break
    old = range(n, 0, -1)
    new = range(n, 0, -1)
    for shuffle in xrange(r):
        old = list(new)
        p, c = map(int, raw_input().split())
        if p >= 2:
            for i in xrange(c):
                new[i] = old[p-1+i]
            for i in xrange(p-1):
                new[c+i] = old[i]
    print new[0]

Submission status: Accepted

Solution code:
f = True
while f:
    x = map(int, raw_input().split())
    if x == [0, 0]:
        f = False
    else:
        deck = list(range(x[0]))
        deck.reverse()
        deck = map((lambda x: x + 1), deck)
        times = x[1]
        for i in range(times):
            p, c = tuple(map(int,raw_input().split()))
            deck = deck[p-1:p-1+c] + deck[0:p-1] + deck[p-1+c:]
        print deck[0]

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import array


def operation(p, c, cards):
    return cards[p - 1: p + c - 1] + cards[:p - 1] + cards[p + c - 1:]


if __name__ == "__main__":
    while True:
        n, r = map(int, input().split())
        if n == 0 and r == 0:
            break
        else:
            cards = array.array("B", range(n, 0, -1))
            for i in range(r):
                p, c = map(int, input().split())
                cards = operation(p, c, cards)
            print(cards[0])

Submission status: Accepted

Solution code:
while True:
    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())
        block = card[p-1:p+c-1]
        del card[p-1:p+c-1]
        card = block + card
    print(card[0])

Submission status: Accepted

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

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

    cards = list(range(1, n+1))[::-1]

    for _ 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 True:
  n,r = tuple(map(int,input().split()))
  if n==0 and r==0:
    break
  l = [n-i for i in range(n)]
  for i in range(r):
    p,c = tuple(map(int,input().split()))
    l1 = [l[i] for i in range(p-1,p+c-1)]
    l2 = [l[i] for i in range(0,p-1)]
    l3 = l1+l2
    for i in range(p+c-1):
      l[i] = l3[i]
  print(l[0])

Submission status: Accepted

Solution code:
while True:
	sousa=input().split()
	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().split()
		[p,c]=[int(p),int(c)]
		yama[:p+c-1]=yama[p-1:p+c-1]+yama[:p-1]
	print(yama[0])

Submission status: Accepted

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

Submission status: Accepted

Solution code:

while True:
    num_cards, num_repeat = map(int, input().split())

    if num_cards == 0 and num_repeat == 0:
        break

    # ??±??? [n, n-1, ... , 1]
    cards = list(range(num_cards, 0, -1))

    for _ in range(num_repeat):
        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:
cond = [int(n) for n in input().split(" ")]

while True:
    case = []
    cards = [n for n in range(1,cond[0] + 1)]
    cards.sort(reverse = True)
    for c in range(cond[1]):
        case = [int(n) for n in input().split(" ")]
        case[0] -= 1
        #print(cards[case[0]:case[0]+case[1]],cards[case[1]:case[0]+case[1]],cards[:case[1]],cards[:case[1]])
        disp = cards[case[0]:case[0]+case[1]]
        cards[case[1]:case[0]+case[1]] = cards[0:case[0]]
        cards[0:case[1]] = 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: Accepted

Solution code:
cards,trial = (int(n) for n in input().split(" "))
while True:
    cards = list(reversed([int(n) for n in range(1,(cards)+1)]))
    for n in range(trial):
        num,change = (int(n) for n in input().split(" "))
        num -= 1
        disp = cards[num:num+change]
        cards[change:change+num] = cards[:num]
        cards[:change] = disp
    print(cards[0])
    cards,trial = (int(n) for n in input().split(" "))
    if cards == 0 and trial == 0:
        break

Submission status: Accepted

Solution code:
n,r = map(int,input().split())

while n!=0 and r!=0:
	card = list(range(1,n+1))[::-1]
	for _ in range(r):
		p,c = map(int,input().split())
		pcard = card[0:p-1]
		ccard = card[p-1:p-1+c]
		tmp = card[p-1+c:]
		nxt = []
		nxt.extend(ccard)
		nxt.extend(pcard)
		nxt.extend(tmp)
		card = nxt
	print(card[0])
	n,r = map(int,input().split())

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
while True:
	x, y = map(int,raw_input().split())
	lst = range(x,0,-1)
	if x == 0 and y == 0:
		break
	lst = range(x,0,-1)	
	for i in range(y):
	    c = []
	    a,b = map(int,raw_input().split())
	    for f in range(b):
	    	c.append(lst.pop(a-1))
	    lst = c + lst
	print lst[0]

Submission status: Accepted

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

Submission status: Accepted

Solution code:
while True:
    str = raw_input()
    list = str.split()
    n = int(list[0])
    r = int(list[1])
    if(n == 0 and r == 0):
        break
    n_list = []
    for i in xrange(n):
        n_list.append(i+1)
    for i in xrange(r):
        str = raw_input()
        list = str.split()
        p = int(list[0])
        c = int(list[1])
        n_list =  n_list[0:n-(p-1)-c] + n_list[n-(p-1):n] + n_list[n-(p-1)-c:n-(p-1)]
    print n_list[n-1]

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

while 1:
    n, r = map(int, sys.stdin.readline().split())

    if (n, r) == (0, 0):
        break

    yama = [int(n - i) for i in range(n)]

    for lp in range(r):
        p, c = map(int, sys.stdin.readline().split())
        p -= 1
        yama = yama[p:p + c] + yama[:p] + yama[p + c:]

    print(yama[0])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
while True:
    n,r = map(int,raw_input().split())
    if(n == 0 and r == 0):
        break
    li = []
    for i in xrange(1,n+1):
        li.append(i)
    for i in xrange(r):
        temp_lis1 = []
        p,c = map(int,raw_input().split())
        if(c == 1):
            temp_lis1.append(li[n-p])
            del(li[n-p])
        else:
            temp_lis1.extend(li[n-p-c+1:n-p+1])
            del(li[n-p-c+1:n-p+1])
        li.extend(temp_lis1)
    print li[n-1]

Submission status: Accepted

Solution code:
while True:
    n, r = map(int, input().split())
    if n == 0:
        break
    cards = [i+1 for i in range(n)][::-1]
    for _ 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:
    a=[int(num)for num in input().split(' ')]
    n=a[0]
    if n==0:break
    hanafuda=[i+1 for i in range(n)]
    for i in range(a[1]):
        b=[int(num)for num in input().split(' ')]
        for j in range(b[1]):hanafuda.append(hanafuda.pop(n-(b[0]+b[1])+1))
    print(hanafuda.pop())

Submission status: Accepted

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

Submission status: Accepted

Solution code:
while 1:
    count, times = [int(a) for a in input().split()]
    hanahuda = [i+1 for i in range(count)]
    if count == times == 0:break
    for i in [0]*times:
        p,c = [int(i) for i in input().split()]
        for i in [0]*c:hanahuda.append(hanahuda.pop(count-(p+c)+1))
    print(hanahuda.pop())

Submission status: Accepted

Solution code:
def solve_testcase():
    n, r = map(int, raw_input().split())
    if (n, r) == (0, 0):
        return 1

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

    print cards[0]
    return 0

def main():
    while not solve_testcase():
        pass

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#hanafuda
N,r=(int(i) for i in input().split())
while N!=0 or r!=0:
    H=[int(N-i) for i in range(N)]
    for i in range(r):
        p,c=(int(i) for i in input().split())
        T=[]
 #       print(H)
        for j in range(p-1,p+c-1):
  #          print("T")
            T.append(H[p-1])
            H.pop(p-1)
        H=T+H
    print(H[0])
    N,r=(int(i) for i in input().split())

Submission status: Accepted

Solution code:
if __name__ == "__main__":
    while True:
        n,r=list(map(int,input().strip().split()))
        if n == 0 and r ==0 :break
        cards = [i for i in range(1,n + 1)]
        pset = []
        cset = []
        for j in range(r):
            p,c = list(map(int,input().strip().split()))
            pset = cards[n-(p - 1):]
            cset = cards[n-c-(p-1):n-(p-1)]
            other = cards[:n-c-(p-1)]
            cards = other + pset + cset
        print(cards[n-1])

Submission status: Accepted

Solution code:
while True:
    n,r=[int(s)for s in input().split(" ")]
    if n==r==0:break
    ops=[[int(s)for s in input().split(" ")]for i in range(r)]
    cd=[n-i for i in range(n)]
    for op in ops:
        p,c=op
        l=cd[p-1:p+c-1]
        del cd[p-1:p+c-1]
        cd=l+cd
    print(cd[0])

Submission status: Accepted

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

Submission status: Accepted

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

    for i in range(r):
        p, c = list(map(lambda x: int(x), input().split(" ")))
        p -= 1
        cards = cards[p:p+c] + cards[0:p] + cards[p+c:]
    
    print(cards[0])

Submission status: Accepted

Solution code:
while True:
	l = input()
	L = l.split()
	for i in range(len(L)):
		L[i] = int(L[i])
	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])

Submission status: Accepted

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


if __name__=="__main__":
	main()