
#include <iostream>
using namespace std;

const int Max = 50;

int Grid[Max][Max][2];
int Table[10][Max][Max][2];
int CountTable[10][Max][Max];
int lastName, caseNo=1;
string name1, name2;
int n, m, pos1, pos2, no1, no2;
string Names[Max];
int bestCount, numerator, denominator;

void PrintTable(int k){
cout<<"Table "<<k<<endl;
 for(int i=0;i<lastName;i++){
  for(int j=0;j<lastName;j++)
    cout<<Table[k][i][j][0]<<','<<Table[k][i][j][1]<<' ';
  cout<<endl;
 }
}

void PrintCountTable(int k){
cout<<"CountTable "<<k<<endl;
 for(int i=0;i<lastName;i++){
  for(int j=0;j<lastName;j++)
    cout<<CountTable[k][i][j]<<',';
  cout<<endl;
 }
}

int GCD(int a, int b){
  if(a % b == 0)
    return b;
  else 
    return GCD(b, a % b);
}

void reduce(int& a, int & b){
  int g = GCD(a,b);
  a /= g;
  b /= g;
}

void AddToGrid(int n1, int pos1, int n2, int pos2){
//cout<<"adding to grid "<<n1<<' '<<pos1<<' '<<n2<<' '<<pos2<<endl;
// if n1/n2 is bigger in Table[i][pos1][pos2]: new entry
reduce(n1,n2);
if (Table[1][pos1][pos2][0] == 0 ||
    n1*Table[1][pos1][pos2][1] > n2*Table[1][pos1][pos2][0]){
  Table[1][pos1][pos2][0] = n1;
  Table[1][pos1][pos2][1] = n2;
  CountTable[1][pos1][pos2] = 1;
}
else if(n1*Table[1][pos1][pos2][1] == n2*Table[1][pos1][pos2][0]) //duplicate
  CountTable[1][pos1][pos2]++;
}

void CreateTables(){  //Table[*][i][j] = best rate for j->i in exactly * steps
int tab1, tab2, num, den;
for(int tab=2; tab<10; tab++) {  //create Table[tab]
  for(int i=0;i<lastName; i++)
   for(int j=0; j<lastName; j++) //calculate Table[tab][i][j]
     {
     tab1=1; tab2=tab-1;
     for(int k=0;k<lastName; k++){
      if(Table[tab1][i][k][0] != 0 && Table[tab2][k][j][0] != 0){
       num = Table[tab1][i][k][0] * Table[tab2][k][j][0];
       den = Table[tab1][i][k][1] * Table[tab2][k][j][1];
       reduce(num, den);   //put num/den in lowest terms
       //compare num/den with Table[tab][i][j] - see if new entry
       if(Table[tab][i][j][0] == 0 ||
          num*Table[tab][i][j][1] > den*Table[tab][i][j][0]){
        CountTable[tab][i][j] = CountTable[tab1][i][k] * CountTable[tab2][k][j];
        Table[tab][i][j][0] = num;
        Table[tab][i][j][1] = den;
       }
       else if(num*Table[tab][i][j][1] == den*Table[tab][i][j][0])
        //tie - add counts
        CountTable[tab][i][j] += CountTable[tab1][i][k] * CountTable[tab2][k][j];
      }
     }
    } 
}
}


//return position of N in Names[0..lastName] - insert at end if not in table
int namePos(string N){
  for(int i=0;i<lastName;i++)
    if(Names[i]==N) return i;
  Names[lastName++] = N;
  return lastName - 1;
}

void init(int m){
 for(int k=1;k<10;k++)
  for(int i=0;i<Max;i++)
   for(int j=0;j<Max;j++)
     CountTable[k][i][j]=Table[k][i][j][0]=Table[k][i][j][1]=0;
 lastName = 2;
}


// print n/d rounded to 5 sig digits
void PrintNumber(int n, int d){
  double x,y;
  int p, left, moder;

  x = (double (n))/d;

  if(x>=1){
    left=0;
    while(x>=1){ x /= 10; left++; }
    // move x left 5 digits
    x *= 100000;
    p=int (x);
    y = x-p;   
    if(y >= 0.5 - 0.000001) p++;

    //p has the 5 sig digits, so print
    if(left>=5){
      cout<<p;
      for(int i=5; i<left;i++) cout<<0;
    }
    else { // need a decimal
      moder=10000;
      for(int i=0;i<left;i++){
        cout<< p/moder;
        p %= moder;
        moder /=10;
      }
      cout<<'.';
      for(int i=left;i<5;i++){
        cout<<p/moder;
        p %= moder;
        moder /= 10;
      }
    } //else 
  }
  else { // x<1
    cout<<"0.";
    // find how many 0's after decimal
    left=-1;
    while(x<1) {x *= 10; left++; }
    x *= 10000;
    p = int (x); 
    y = x-p;
    if(y>=0.5) p++;  //p now holds 5 sig digits
    // print 0's
    for(int i=0;i<left;i++) cout<<0;
    cout<<p;
  }
}


int main(){
  int best;

  cin >> n;
  for(int i=0;i<n;i++){
   cin>>name1>>name2>>m;
   init(m);
   Names[0]=name1;
   Names[1]=name2;
   //enter the one-step table
   for(int j=0;j<m;j++){
    cin>>no1>>name1>>no2>>name2;
    pos1 = namePos(name1);
    pos2 = namePos(name2);
    AddToGrid(no1, pos1, no2, pos2);
   }
   CreateTables();
//PrintTable(1);
//PrintCountTable(1);
//PrintTable(2);
//PrintCountTable(2);
//PrintTable(6);
//PrintTable(8);
   //find best ratio  in Table[*][0][1]
   best = 1;
   bestCount = CountTable[1][0][1];
   for(int k=2;k<10;k++){

/*
cout<<"in for loop, k= "<<k<<endl;
cout<<"Table[best]"<<endl;
PrintTable(best);
cout<<"Table[k]"<<endl;
PrintTable(k);
*/

     if(Table[best][0][1][0] == 0 ||
        Table[best][0][1][0]*Table[k][0][1][1] <
        Table[best][0][1][1]*Table[k][0][1][0]){
       best = k;
       bestCount = CountTable[k][0][1];
     } 
     else if ( Table[best][0][1][0]*Table[k][0][1][1]== 
        Table[best][0][1][1]*Table[k][0][1][0])
       bestCount += CountTable[k][0][1];
   }

   //print results
   cout<<"Case "<<caseNo++<<": ";
   numerator = Table[best][0][1][0];
   denominator = Table[best][0][1][1];
//   PrintNumber(numerator, denominator);
   PrintNumber(denominator, numerator);
   cout<<' '<<bestCount<<endl;
  }
 return 0;
}
