Problem A: Hanafuda Shuffle

There are various ways to shuffle a deck of cards. Hanafuda shuffling, used in the Japanese card game 'Hanafuda', is one such method. The process involves pulling out a certain number of cards from the deck and placing them on top, known as a cutting operation.

Write a program to simulate Hanafuda shuffling and determine which card will end up on top of the deck.

Input:
The input consists of multiple data sets. Each set begins with two positive integers, n (1 <= n <= 50) and r (1 <= r <= 50), representing the number of cards in the deck and the number of cutting operations, respectively. This is followed by r lines, each representing a cutting operation. Each line contains two positive integers, p and c (p + c <= n + 1), indicating the starting position and the number of cards to be pulled out and placed on top. The input ends with a line containing two zeros.

Output:
For each data set, the program should output the number of the top card after the shuffle. Assume the cards are initially numbered from 1 to n, from bottom to top.

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

# Buggy version:
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])

# Fixed version:
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])

# Buggy version:
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))

# Fixed version:
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))

# Buggy version:
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])


# Fixed version:
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])

# Buggy version:
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])


# Fixed version:
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])

# Buggy version:

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

# Fixed version:
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])