#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
using namespace std;
typedef long long LL;
LL gcd(LL a, LL b)
{
while(a) swap(a, b%=a); return b;
}
LL lcm(LL a, LL b)
{
return a/gcd(a,b)*b;
}
class IngredientProportions
{
public:
vector <int> getMasses(vector <string> proportions)
{
int N = proportions.size() + 1;
vector<LL> a(N-1),b(N-1),p(N-1),q(N-1);
for(int i=0; i<proportions.size(); ++i)
{
stringstream sin(proportions[i]);
sin.get();//#
sin>>a[i];
sin.get();sin.get();sin.get();sin.get();sin.get();sin.get();//" and #"
sin>>b[i];
sin.get();sin.get();sin.get();sin.get();//" as "
sin>>p[i];
sin.get(); //:
sin>>q[i];
}
vector<LL> g(N, -1); g[0]=1;
for(int _=0; _<N; ++_)
for(int i=0; i<N-1; ++i)
{
LL A=a[i],B=b[i],P=p[i],Q=q[i];
if( g[A]==-1 && g[B]!=-1 )
swap(A,B), swap(P,Q);
if( g[A]!=-1 && g[B]==-1 )
{
LL f = lcm(g[A],P) / g[A];
for(int j=0; j<N; ++j)
if( g[j] != -1 )
g[j] *= f;
g[B] = g[A]/P*Q;
}
}
LL gg = accumulate( g.begin(), g.end(), g.back(), gcd );
vector<int> ans(N);
for(int i=0; i<N; ++i)
ans[i] = g[i]/gg;
return ans;
}
};
// BEGIN CUT HERE
#include <ctime>
double start_time;string timer() { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }
int verify_case(const vector <int> &Expected, const vector <int> &Received) { if (Expected == Received) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; cerr << "\tExpected: " << print_array(Expected) << endl; cerr << "\tReceived: " << print_array(Received) << endl; } return 0;}
template<int N> struct Case_ { Case_(){start_time=clock();} };
char Test_(...);
int Test_(Case_<0>) {
string proportions_[] = {"#0 and #1 as 6:4"};
vector <string> proportions(proportions_, proportions_+sizeof(proportions_)/sizeof(*proportions_));
int RetVal_[] = {3, 2 };
vector <int> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_));
return verify_case(RetVal, IngredientProportions().getMasses(proportions)); }
int Test_(Case_<1>) {
string proportions_[] = {"#0 and #1 as 9:8","#1 and #2 as 9:8"}
;
vector <string> proportions(proportions_, proportions_+sizeof(proportions_)/sizeof(*proportions_));
int RetVal_[] = {81, 72, 64 };
vector <int> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_));
return verify_case(RetVal, IngredientProportions().getMasses(proportions)); }
int Test_(Case_<2>) {
string proportions_[] = {"#4 and #0 as 1:1", "#4 and #1 as 3:1", "#4 and #2 as 5:1", "#4 and #3 as 7:1"};
vector <string> proportions(proportions_, proportions_+sizeof(proportions_)/sizeof(*proportions_));
int RetVal_[] = {105, 35, 21, 15, 105 };
vector <int> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_));
return verify_case(RetVal, IngredientProportions().getMasses(proportions)); }
int Test_(Case_<3>) {
string proportions_[] = {"#2 and #3 as 6:8", "#0 and #1 as 9:3", "#3 and #0 as 7:5"};
vector <string> proportions(proportions_, proportions_+sizeof(proportions_)/sizeof(*proportions_));
int RetVal_[] = {60, 20, 63, 84 };
vector <int> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_));
return verify_case(RetVal, IngredientProportions().getMasses(proportions)); }
template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_<N>()); Run_<sizeof(Test_(Case_<N+1>()))==1 ? -1 : N+1>(); }
template<> void Run_<-1>() {}
int main() { Run_<0>(); }
// END CUT HERE