Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

var (
	sc = bufio.NewScanner(os.Stdin)
)

func main() {
	for sc.Scan() {
		if sc.Text() == "0 0" {
			break
		}
		chunks := strings.Split(sc.Text(), " ")
		n, _ := strconv.Atoi(chunks[0])
		r, _ := strconv.Atoi(chunks[1])
		cards := make([]int, n, n)
		for i := 0; i < n; i++ {
			cards[i] = n - i
		}
		for i := 0; i < r; i++ {
			sc.Scan()
			chunks := strings.Split(sc.Text(), " ")
			p, _ := strconv.Atoi(chunks[0])
			c, _ := strconv.Atoi(chunks[1])
			block := make([]int, c, c)
			for j := 0; j < c; j++ {
				block[j] = cards[p+j-1]
			}
			for j := p - 2; j >= 0; j-- {
				cards[j+c] = cards[j]
			}
			for j := 0; j < c; j++ {
				cards[j] = block[j]
			}
		}
		fmt.Println(cards[0])
	}
}

C++: #include<iostream>

using namespace std;

int input() {
	int input[51][2];
	cin >> input[0][0] >> input[0][1];								//まずnとr
	if (input[0][0] == 0 && input[0][1] == 0) {
		return 0;
	}
	/*
	input[0][0] = 0;
	input[0][1] = 0;
	*/
	for (char pos = 1; pos <= input[0][1]; pos++) {
		cin >> input[pos][0] >> input[pos][1];
	}																//ここまで入力

	/*シャッフルもどし*/
	int whtop = 1;													//1番目≠要素
	whtop = input[input[0][1]][0];
	input[0][1]--;
	while (input[0][1] != 0) {

		if (input[input[0][1]][1] >= whtop) {
			whtop = input[input[0][1]][0] + whtop - 1;
		}
		else {
			whtop = whtop - input[input[0][1]][1];
		}
		input[0][1]--;
	}
	return input[0][0]-whtop+1;
}




int main()
{
	int output[100];
	char topos = 0;
	while (1) {
		int check;
		check=input();
		if (check == 0)break;
		else {
			output[topos] = check;
			topos++;
		}
	}

	char sendpos = 0;
	while (sendpos < topos) {
		cout << output[sendpos] << endl;
		sendpos++;
	}


}

C++: #include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
#include<cmath>
#include<climits>
#include<string>
#include<set>
#include<numeric>
#include<map>
#include<iostream>
using namespace std;
#define rep(i,n) for(int i = 0;i<((int)(n));i++)
#define reg(i,a,b) for(int i = ((int)(a));i<=((int)(b));i++)
#define irep(i,n) for(int i = ((int)(n)-1);i>=0;i--)
#define ireg(i,a,b) for(int i = ((int)(b));i>=((int)(a));i--)
typedef long long ll;
typedef pair<ll, ll> mp;


int main(void){
	int ans[100],cnt=0;
	while(1){
		int n,r;
		cin>>n>>r;
		if(n==0)break;
		int p[50],c[50];
		irep(i,r)cin>>p[i]>>c[i];//?????????????????????
		int pos=0;
		rep(i,r){
			if(pos<c[i]){
				pos+=p[i]-1;
			}else if(pos<c[i]+p[i]-1){
				pos-=c[i];
			}
		}
		ans[cnt]=n-pos;
		cnt++;
	}
	rep(i,cnt)cout<<ans[i]<<endl;
	return 0;
}

C++: #include<stdio.h>
#include <iostream>
#include <math.h>
#include <numeric>
#include <vector>
#include <map>
#include <functional>
#include <stdio.h>
#include <array>
#include <algorithm>
#include <string>
#include <string.h>
#include <assert.h>
#include <stdio.h>
#include <queue>
#include<iomanip>
#include<bitset>
#include<stack>
#include<set>
#include<limits>
#include <complex>
using namespace std;

int dx[7] = { -1,0,1,0,-1,0,1 };
int dy[7] = { 0,1,0,-1,0,1,0 };

bool memo[64][16][4][64][16][4];


vector <vector<bool>>mp;

struct aa {
	int fx;
	int fy;
	int fway;
	int tx;
	int ty;
	int tway;
};
int gx, gy;

vector<aa>astack;

bool cangoal;

int gway;

bool check(int fx, int fy,int fway, int tx, int ty, int tway) {

	if (memo[fx][fy][fway][tx][ty][tway])return false;
	memo[fx][fy][fway][tx][ty][tway] =true;
	
	//else{

		int nfy = fy + dy[fway];
		int nfx = fx + dx[fway];
		while (1) {
			
			if (mp[nfy][nfx]) {
				nfx -= dx[fway];
				nfy -= dy[fway];
				break;
			}
			nfy = nfy + dy[fway];
			nfx = nfx + dx[fway];

		}

		int nty = ty;
		int ntx = tx;
		while (1) {

			if (mp[nty][ntx]) {
				ntx -= dx[tway];
				nty -= dy[tway];
				break;
			}

			if (mp[nty + dy[tway + 1]][ntx + dx[tway + 1]]) {
				int afway = (fway + 3) % 4;
				int atway = (tway + 3) % 4;
				if (nfx == gx&&nfy == gy&&ntx == gx&&nty == gy) {
					if (tway == (gway + 2) % 4)
						return true;
				}
				if (fx == gx&&fy == gy) {
					cangoal = true;
				}
				if (!memo[nfx][nfy][afway][ntx][nty][atway]) {
					astack.push_back(aa{ nfx,nfy,afway,ntx,nty,atway });
				}
			}

			if (mp[nty + dy[tway + 3]][ntx + dx[tway + 3]]) {
				int afway = (fway + 1) % 4;
				int atway = (tway + 1) % 4;
				if (nfx == gx&&nfy == gy&&ntx == gx&&nty == gy) {
					if (tway == (gway + 2) % 4)
						return true;
				}
				if (fx == gx&&fy == gy) {
					cangoal = true;
				}
				if (!memo[nfx][nfy][afway][ntx][nty][atway]) {
					astack.push_back(aa{ nfx,nfy,afway,ntx,nty,atway });
				}
			}

			nty = nty + dy[tway];
			ntx = ntx + dx[tway];
		}
		if (mp[nty + dy[tway + 1]][ntx + dx[tway + 1]]) {
			int afway = (fway + 3) % 4;
			int atway = (tway + 3) % 4;
			if (nfx == gx&&nfy == gy&&ntx == gx&&nty == gy) {
				if (tway == (gway + 2) % 4)
					return true;
			}
			if (fx == gx&&fy == gy) {
				cangoal = true;
			}
			if (!memo[nfx][nfy][afway][ntx][nty][atway]) {
				
				astack.push_back(aa{ nfx,nfy,afway,ntx,nty,atway });
			}
		}

		if (mp[nty + dy[tway + 3]][ntx + dx[tway + 3]]) {
			int atway = (tway + 1) % 4;
			int afway = (fway + 1) % 4;
			if (nfx == gx&&nfy == gy&&ntx == gx&&nty == gy) {
				if (tway == (gway + 2) % 4)
					return true;
			}
			if (fx == gx&&fy == gy) {
				cangoal = true;
			}
			if (!memo[nfx][nfy][afway][ntx][nty][atway]) {	
				astack.push_back(aa{ nfx,nfy,afway,ntx,nty,atway });

			}
		}

	//}
	return false;
}

bool memo2[64][16][4];

bool check2(int fx, int fy, int fway) {
	if (memo2[fx][fy][fway])return false;
	memo2[fx][fy][fway] = true;

	if (fx == gx&&fy == gy)return true;
	int nfy = fy + dy[fway];
	int nfx = fx + dx[fway];
	while (1) {

		if (mp[nfy][nfx]) {
			nfy -= dy[fway];
			nfx -= dx[fway];
			break;
		}
		nfy = nfy + dy[fway];
		nfx = nfx + dx[fway];
	}
	return (check2(nfx, nfy, (fway + 1) % 4) || check2(nfx, nfy, (fway + 3) % 4));
}

int main() {
	while (1) {
		int n, r; cin >> n >> r;
		if (!n)break;
		vector<int>ya(n);
		for (int i = 0; i < n; ++i) {
			ya[i] = n-(i);
		}
		for (int i = 0; i < r; ++i) {
			int p, c; cin >> p >> c;
			vector<int>nya(ya);
			for (int i = 0; i < c; ++i) {
				nya[i] = ya[p-1 + i];
			}
			for (int i =c; i < c+p-1; ++i) {
				nya[i] = ya[i-c];
			}
			ya = nya;
		}
		cout << ya[0] << endl;
	}

	return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <utility>

using namespace std;

int solve(int n,int r){
    vector<pair<int,int> >p;
    for(int i=0;i<r;i++){
        int a,b;
        cin >> a >> b;
        p.push_back(make_pair(a,b));
    }

    reverse(p.begin(),p.end());
    int x = n;
    for(vector<pair<int,int> >::iterator it=p.begin();
        it != p.end();
        ++it){
        int a = (*it).first;
        int b = (*it).second;
        if(x >= (n-b+1)){
            x = x - (a-1);
        }else if(x >= (n-b)-(a-1)+1){
            x = x + b;
        }
    }
    cout << x << endl;
}

int main(){
    int n,r;
    while(cin >> n >> r){
        if(n==0 && r==0){
            break;
        }
        solve(n,r);
    }
    return 0;
}