#include <iostream>
#include <vector>
using namespace std;

long long int gcd( long long int a, long long int b ) {
  return b ? gcd( b, a % b ) : a ;
}
long long int lcm( long long int m, long long int n ) {
  if ( m == 0 || n == 0 ) return 0;
  return ( ( m / gcd( m, n ) ) * n );
}

int main() {

  while( true ) {

    long long int n;
    cin >> n;
    if ( n == 0 ) break;

    vector< long long int > p, q, r, b;
    for ( long long int i = 0; i < n; i++ ) {
      long long int in_p, in_q, in_r, in_b;
      cin >> in_p >> in_q >> in_r >> in_b;
      p.push_back( in_p );
      q.push_back( in_q );
      r.push_back( in_r );
      b.push_back( in_b );
    }

    for ( long long int i = 0; i < n; i++ ) {
      for ( long long int j = 0; j < n; j++ ) {

	if ( r[j] <= 0 && b[j] <= 0 ) {

	  if ( r[j] == 0 ) {
	    r[j] = 1;
	  }else {
	    r[j] = -r[j];
	  }
	  long long int r2 = r[j] * p[j];
	  if ( b[j] == 0 ) {
	    b[j] = 1;
	  }else {
	    b[j] = -b[j];
	  }
	  long long int b2 = b[j] * q[j];
	  long long int h = lcm( r2, b2 ) / p[j] + lcm( r2, b2 ) / q[j];

	  r[j] = n + 2;
	  b[j] = n + 2;

	  for ( long long int k = 0; k < n; k++ ) {
	    if ( i == n - 1 ) {
	      cout << h << endl;
	      break;
	    }
	    if ( r[k] == j + 1 ) {
	      r[k] = -h;
	      break;
	    }
	    if ( b[k] == j + 1 ) {
	      b[k] = -h;
	      break;
	    }
	  }
	  break;

	}

      }
    }

  }

  return 0;

}