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.
def total(t, a):
  if ans[0] + ans[1] == 0:
    ans[0] = t
    ans[1] = a
  elif t == a:
    big = max(ans[0], ans[1])
    ans[0] = big
    ans[1] = big
  elif (ans[0] >= ans[1] and t < a) or (ans[0] < ans[1] and t < a):
    while ans[0]%t != 0 or ans[1]%1 != 0:
      ans[0] += 1
      ans[1] = ans[0]*a//t
    if ans[1] < a:
      ans[1] = a
  elif (ans[0] <= ans[1] and t > a) or (ans[0] > ans[1] and t > a):
    while ans[1]%a != 0 or ans[0]%1 != 0:
      ans[1] += 1
      ans[0] = ans[1]*t//a
    if ans[0] < t:
      ans[0] = t
    
  return [ans[0], ans[1]]
    
n = int(input())
ans = [0, 0]
for _ in range(n):
  t, a = list(map(int, input().split()))
  ans = total(t, a)
  print(ans)

print(ans[0]+ans[1] )
a=b=1
for _ in range(int(input())):
  c,d=map(int,input().split())
  m=max(0--a//c,0--b//d)
  a,b=c*m,d*m
print(a+b)