include[HTML_REMOVED]
using namespace std;
define endl “\n”
define ll long long
define ull unsigned long long
define int long long
define fi first
define se second
define all(x) x.begin(), x.end()
define rall(x) x.rbegin(), x.rend()
typedef pair[HTML_REMOVED] PII;
// queue[HTML_REMOVED] q;
const int N = 2e5 + 10, mod = 998244353, p = 1e9 + 7;
int exgcd(int a, int b, int &x, int &y) {
if(!b) {
x = 1, y = 0;
return a;
}
int d = exgcd(b, a % b, y, x);
y -= a / b * x;
return d;
}
void solve(){
int n;
cin >> n;
int M = 1;
vector<int> a(n), b(n);
for(int i = 0; i < n; i++) {
cin >> a[i] >> b[i];
M *= a[i];
}
int x, y, ans = 0;
for(int i = 0; i < n; i++) {
int mi = M / a[i];
int d = exgcd(mi, a[i], x, y);
ans = (ans + b[i] * x * mi) % M;
}
cout << (ans % M + M) % M << endl;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1;
// cin >> t;
while(t--) {
solve();
}
return 0;
}