Problem:
Score : 300 points

Problem Statement

AtCoDeer the deer is seeing a quick report of election results on TV.
Two candidates are standing for the election: Takahashi and Aoki.
The report shows the ratio of the current numbers of votes the two candidates have obtained, but not the actual numbers of votes.
AtCoDeer has checked the report N times, and when he checked it for the i-th (1≦i≦N) time, the ratio was T_i:A_i.
It is known that each candidate had at least one vote when he checked the report for the first time.

Find the minimum possible total number of votes obtained by the two candidates when he checked the report for the N-th time.
It can be assumed that the number of votes obtained by each candidate never decreases.

Constraints

1≦N≦1000

1≦T_i,A_i≦1000 (1≦i≦N)

T_i and A_i (1≦i≦N) are coprime.

It is guaranteed that the correct answer is at most 10^{18}.

Input

The input is given from Standard Input in the following format:

N
T_1 A_1
T_2 A_2
:
T_N A_N

Output

Print the minimum possible total number of votes obtained by Takahashi and Aoki when AtCoDeer checked the report for the N-th time.

Sample Input 1

3
2 3
1 1
3 2

Sample Output 1

10

When the numbers of votes obtained by the two candidates change as 2,3 → 3,3 → 6,4, the total number of votes at the end is 10, which is the minimum possible number.

Sample Input 2

4
1 1
1 1
1 5
1 100

Sample Output 2

101

It is possible that neither candidate obtained a vote between the moment when he checked the report, and the moment when he checked it for the next time.

Sample Input 3

5
3 10
48 17
31 199
231 23
3 2

Sample Output 3

6930

Solution in C++ (GCC 9.2.1) (judge verdict: Accepted)

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
const ll INF = 1e12;
const ll MOD = 1000000000 + 7;
#define PB push_back
#define PF push_front
#define fi first
#define se second
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vpi vector<pii>
#define vll vector<ll>
#define vpl vector<pll>
#define vvi vector<vector<int>>
#define vvl vector<vector<ll>>
#define mii map<int, int>
#define mci map<char, int>
#define MX(x) *max_element(all(x))
#define MN(x) *min_element(all(x))
#define ios ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define pr_d(x) cout << fixed << setprecision(15) << x << endl
#define ud(c, x) distance(c.begin(), upper_bound(all(c), x))
#define ld(c, x) distance(c.begin(), lower_bound(all(c), x))
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i, a, b) for (int i = (a); i < (b); ++i)
#define rep3(i, n) for (int i = (n - 1); i >= 0; --i)
#define rep4(i, a, b) for (int i = (a); i > (b); --i)
#define pb push_back
#define out(x) cout << x << "\n"
bool odd(int i)
{
    return i % 2;
}

#define all(v) v.begin(), v.end()
#define size(x) int(x.size())

int gcd(int a, int b)
{
    return __gcd(a, b);
}
int lcm(int a, int b) { return a * (b / gcd(a, b)); }
void Yes_No(bool f)
{
    if (f)
        printf("Yes\n");
    else
        printf("No\n");
}
void YES_NO(bool f)
{
    if (f)
        printf("YES\n");
    else
        printf("NO\n");
}

template <typename T>
void deb1(T x)
{
    cout << "debug: " << x << "\n";
}

template <typename T>
bool chmax(T &a, const T &b)
{
    if (a < b)
    {
        a = b;
        return true;
    }
    return false;
}
template <typename T>
bool chmin(T &a, const T &b)
{
    if (a > b)
    {
        a = b;
        return true;
    }
    return false;
}
//-------------------ここから回答する-----------------------

ll g(ll a, ll b) //b以上の最小のaの倍数
{
    return --b / a * a + a;
}
pair<ll, ll> f(ll i, ll j, int a, int b)
{
    pair<ll, ll> p;
    if (a == b)
    {
        p.first = max(i, j);
        p.second = max(i, j);
        return p;
    }
    if (a > b)
    {
        pair<ll, ll> q;
        p.second = g(b, j);
        p.first = p.second / b * a;
        q.first = g(a, i);
        q.second = q.first / a * b;
        if (p.first >= i && p.second >= j)
            return p;
        else
            return q;
    }
    if (a < b)
    {
        pair<ll, ll> q;
        p.first = g(a, i);
        p.second = p.first / a * b;
        q.second = g(b, j);
        q.first = q.second / b * a;
        if (p.first >= i && p.second >= j)
            return p;
        else
            return q;
    }
    return p;
}

void solve(void)
{
    int n;
    cin >> n;
    vector<int> t(n), a(n);
    rep(i, n) cin >> t[i] >> a[i];
    ll taka = t[0];
    ll aok = a[0];
    // cout << endl;
    for (int i = 1; i < n; i++)
    {
        // cout << "before:" << taka << " " << aok << endl;
        // cout << "compare:" << t[i] << " " << a[i] << endl;
        pair<ll, ll> p = f(taka, aok, t[i], a[i]);
        taka = p.first;
        aok = p.second;
        // cout << "after:" << taka << " " << aok << endl
        //      << endl;
    }
    cout << taka + aok << endl;
}
int main(void)
{
    solve();
}