#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cstring>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <numeric>
#include <utility>
#include <iomanip>
#include <algorithm>
#include <functional>
using namespace std;

typedef long long ll;
typedef vector<int> vint;
typedef vector<long long> vll;
typedef pair<int,int> pint;
typedef pair<long long, long long> pll;

#define MP make_pair
#define PB push_back
#define ALL(s) (s).begin(),(s).end()
#define EACH(i, s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)
#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P) 
{ return s << '<' << P.first << ", " << P.second << '>'; }
template<class T> ostream& operator << (ostream &s, vector<T> P) 
{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; }
template<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P) 
{ EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; } return s; }



long long gcd(long long a, long long b) {
    if (b == 0) return a;
    if (a < 0) a = -a; if (b < 0) b = -b;
    return gcd(b, a % b);
}
long long lcm(long long a, long long b) {
    long long g = gcd(a, b);
    return a * b / g;
}
struct frac {
    long long first, second;
    
    frac normalize() {
        if (second < 0) {first = -first; second = -second;}
        long long d = gcd(first, second);
        if (d == 0) {first = 0; second = 1;}
        else {first /= d; second /= d;}
        return *this;
    }
    frac(long long f = 0, long long s = 1) : first(f), second(s) { normalize(); }
    inline frac operator - () { (*this).first *= -1; return (*this); }
    inline const frac& operator = (long long a) { *this = frac(a, 1); return *this; }
    inline const frac& operator += (const frac& a);
    inline const frac& operator += (long long a);
    inline const frac& operator -= (const frac& a);
    inline const frac& operator -= (long long a);
    inline const frac& operator *= (const frac& a);
    inline const frac& operator *= (long long a);
    inline const frac& operator /= (const frac& a);
    inline const frac& operator /= (long long a);
    friend ostream& operator << (ostream& s, const frac& f) { 
        s << f.first; if (f.second != 1) s << "/" << f.second; return s;
    }
};
bool operator < (const frac& a, const frac& b) {
    return (long double)(a.first) * b.second < (long double)(a.second) * b.first;
}
bool operator > (const frac& a, const frac& b) { return b < a; }
bool operator <= (const frac& a, const frac& b) { return !(b < a); }
bool operator >= (const frac& a, const frac& b) { return !(a < b); }
bool operator != (const frac& a, const frac& b) { return a < b || b < a; }
bool operator == (const frac& a, const frac& b) { return !(a < b) && !(b < a); }
frac operator + (const frac& a, const frac& b) {
    frac res;
    res.first = a.first * b.second + a.second * b.first;
    res.second = a.second * b.second;
    res.normalize();
    return res;
}
frac operator - (const frac& a, const frac& b) {
    frac res;
    res.first = a.first * b.second - a.second * b.first;
    res.second = a.second * b.second;
    res.normalize();
    return res;
}
frac operator * (const frac& a, const frac& b) {
    frac res;
    res.first = a.first * b.first;
    res.second = a.second * b.second;
    res.normalize();
    return res;
}
frac operator / (const frac& a, const frac& b) {
    frac res;
    res.first = a.first * b.second;
    res.second = a.second * b.first;
    res.normalize();
    return res;
}
frac abs(const frac& a) {
    frac res; res = a; res.normalize(); 
    if (res.first < 0) res.first = res.first * (-1);
    return res;
}
inline const frac& frac::operator += (const frac& x) {*this = *this + x; return *this;}
inline const frac& frac::operator += (long long x) {*this = *this + x; return *this;}
inline const frac& frac::operator -= (const frac& x) {*this = *this - x; return *this;}
inline const frac& frac::operator -= (long long x) {*this = *this + x; return *this;}
inline const frac& frac::operator *= (const frac& x) {*this = *this * x; return *this;}
inline const frac& frac::operator *= (long long x) {*this = *this * x; return *this;}
inline const frac& frac::operator /= (const frac& x) {*this = *this / x; return *this;}
inline const frac& frac::operator /= (long long x) {*this = *this / x; return *this;}



const int MAX = 2100;
int a[MAX], b[MAX];
int L[MAX], R[MAX];

int P[MAX];

vector<frac> omori;


int n;

void rec(int v, frac f) {
    frac left = f * frac(b[v], a[v]+b[v]);
    frac right = f * frac(a[v], a[v]+b[v]);
    
    if (L[v] == -1) omori.PB(left);
    else rec(L[v], left);
    
    if (R[v] == -1) omori.PB(right);
    else rec(R[v], right);
}


int main() {
    while (cin >> n) {
        if (n == 0) break;
        omori.clear();
        
        memset(P, -1, sizeof(P));
        for (int i = 0; i < n; ++i) {
            cin >> a[i] >> b[i] >> L[i] >> R[i];
            ll g = gcd(a[i], b[i]);
            a[i] /= g; b[i] /= g;
            --L[i]; --R[i];
            
            P[L[i]] = i; P[R[i]] = i;
        }
        
        int root = 0;
        while (P[root] != -1) root = P[root];
        
        rec(root, frac(1,1));
        
//        COUT(root);
//        COUT(omori);
        
        long long res = 1;
        for (int i = 0; i < omori.size(); ++i) {
            res = lcm(res, omori[i].second);
        }
        
        cout << res << endl;
    }
    return 0;
}


Measured memory_bytes: 1236