//* Problem  : 
//* Date     : 2009.07.25
//* Author   : alt
//* Tags     : 

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <numeric>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <memory>
#include <string>
#include <vector>
#include <cctype>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#include <algorithm>

using namespace std;

//stack
//#pragma comment(linker, "/STACK:16777216")

// to disable Visual C++ secure warnings
#pragma warning(disable : 4996)

///BEGIN CUT HERE
// types
typedef long long int64;
typedef unsigned long long uint64;
typedef pair <int,int> PII;
typedef vector <int> VI;
typedef vector <int64> VI64;
typedef vector <string> VS;
typedef vector <vector<string> > VVS;
typedef vector <vector<int> > VVI;
typedef vector <pair<int,int> > VPII;
typedef vector <vector<pair<int,int> > > VVPII;
typedef map<int, int > MII;
typedef map<string, int > MSI;
typedef queue<int > QI;
typedef queue<PII > QPII;
//loops
#define FOR(i, n) for (int i = 0; i < (int)(n); i++)
#define FORR(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define FORE(i, a, b) for (int i = (int)(a); i <= (int)(b); i++)
#define FORER(i, a, b) for (int i = (int)(a); i >= (int)(b); i--)
#define FORSZ(i, a) FOR(i, SZ(a))
#define FORSZR(i, a) FORR(i, SZ(a))
#define FORO(i, x) for (typeof((x).begin()) i = (x).begin(); i != (x).end(); i++)
#define FOROR(i, x) for (typeof((x).end()) i = (x).end(); i != (x).begin(); i--)
#define REP(n) for (int _foo = (int)(n) - 1; _foo >= 0; _foo--)
#define REP2(n) for (int _foo2 = (int)(n) - 1; _foo2 >= 0; _foo2--)

#define VAR(a, b) typeof(b) a=(b)
#define FOREACH(it, c) for(VAR(it,(c).begin()); it!=(c).end(); it++)
//sorting & c
#define ALL(a) a.begin(), a.end()
#define RALL(a) a.rbegin(), a.rend()
#define SORT(a) sort(ALL(a))
#define RSORT(a) sort(RALL(a))
#define UNIQUE(c) SORT(c),(c).resize(unique(ALL(c))-(c).begin())
#define REVERSE(a) reverse(ALL(a))
//filling
#define FLA(a,val) memset(a, val, sizeof(a))
#define FLO(o,val) memset(&o, val, sizeof(o))
#define CLA(a) FLA(a, 0)
#define CLO(a) FLO(a, 0)
//misc
#define _x first
#define _y second
#define MP make_pair
#define PB push_back
#define SZ(a) (int)a.size()
#define POP(a) a.top(), a.pop()
#define FRONT(a) a.front(), a.pop()
//const
const int INF = 1000000000;
const int64 INFL = 1000000000000000000LL;
const double PI = acos(-1.0);
const double EPS = 1E-9;

const int dx8[] = {-1, 1, 0, 0, -1, -1,  1, 1};
const int dy8[] = {0, 0, -1, 1, -1,   1, -1, 1};

//some math
template <typename T> inline T gcd(T a, T b)				{ return b ? gcd(b, a % b) : a; }
template <typename T> inline T gcd2(T a, T b)				{ while (a && b) (a > b) ? a %= b : b %= a; return a + b;}
template <typename T> inline T lcm(T a, T b)				{ return a / gcd(a, b) * b; }
template <typename T> inline T abs(T a)					{ return a < 0 ? -a : a; }
template <typename T> inline T sqr(T a)					{ return a * a; }
template <typename T> inline double hypot(T a, T b)		{ return sqrt(1.0 * a * a + b * b);}
//template <typename T> inline double hypot(T a, T b, T ñ)	{ return sqrt(1.0 * a * a + b * b + ñ * ñ);}
template <typename T> inline T hypot2(T a, T b)			{ return a * a + b * b;}
//template <typename T> inline T hypot2(T a, T b, T c)		{ return a * a + b * b + c * c;}
template <typename T> inline void chMin(T& a, T b) { if (b < a) a = b; }
template <typename T> inline void chMax(T& a, T b) { if (b > a) a = b; }

//assertions
#ifdef _DEBUG
#define ASSERT(f) if (!(f)) printf("%s\n", "ASSERTION FAILED!");
#define ASSERTS(f, s) if (!(f)) printf("%s [%s]\n", "ASSERTION FAILED!", s);
#else
#define ASSERT(f) f
#define ASSERTS(f, s) f
#endif

int cmp_int(const void *a, const void *b){return *(int*)a - *(int*)b;}
int cmp_double(const void *a, const void *b){double t = *(double*)a - *(double*)b;return (fabs(t) < EPS) ? 0 : t > 0 ? 1 : -1;}
int cmp_int64(const void *a, const void *b){int64 t = *(int64*)a - *(int64*)b; return !t ? 0 : t > 0 ? 1 : -1;}

//input
inline void ri(int &tt){ASSERTS(scanf("%d", &tt) == 1, "ReadInt failed");}
inline int ri(){int tt; ASSERTS(scanf("%d", &tt) == 1, "ReadInt failed"); return tt;}
inline void ri64(int64 &tt){ASSERTS(scanf("%lld", &tt) == 1, "ReadInt64 failed");}
inline int64 ri64(){int64 tt; ASSERTS(scanf("%lld", &tt) == 1, "ReadInt64 failed"); return tt;}
inline void rd(double &tt){ASSERTS(scanf("%lf", &tt) == 1, "ReadDouble failed"); }
inline double rd(){double tt; ASSERTS(scanf("%lf", &tt) == 1, "ReadDouble failed"); return tt;}
inline void rs(char *s){ASSERTS(scanf("%s", s) == 1, "ReadChar* failed");}
//output
inline void pvi(int *a, int n){FOR(i, n) printf("%d%c", a[i], i == n - 1 ? '\n' : ' ');}
inline void pvi(VI a){FORSZ(i,a) printf("%d%c", a[i], i == a.size() - 1 ? '\n' : ' ');}
inline void pi(int n){printf("%d\n", n);}
inline void pi64(int64 n){printf("%lld\n", n);}

inline void _pi64(int64 n)
{
	if (n < 0) {putchar('-'); n = -n;}
	if (!n) {putchar('0'); return;}
	char stack[30]; int istack = 0;
	while (n)
	{
		stack[istack++] = (n % 10) + '0'; n /= 10;
	}
	while (istack > 0)
		putchar(stack[--istack]);
}
const int dx4[] = {-1, 1, 0, 0};
const int dy4[] = {0, 0, -1, 1};
///END CUT HERE

template< class T >
class Rational
{
    private:
        /** Greatest Common Divisor. **/
        static T gcd( const T &a, const T &b ) { return( a == 0 ? b : gcd( b % a, a ) ); }

        /** Reduces to lowest terms. **/
        inline void norm() { T g = gcd( n, d ); n /= g; d /= g; if( d < T( 0 ) ) { n = -n; d = -d; } }

        /** Creates a Rational without calling norm(). **/
        static inline Rational makeRat( const T &num, const T &denom )
        {
            Rational r( num );
            r.d = denom;
            return r;
        }

    public:
        /** Numerator and denominator. **/
        T n, d;

        /** Creates a new Rational number with value zero. **/
        Rational(): n( 0 ), d( 1 ) {}

        /** Creates a new Rational number with denominator 1. **/
        Rational( const T &num ): n( num ), d( 1 ) {}

        /** Creates a new Rational number with a given value. **/
        Rational( const T &num, const T &denom ): n( num ), d( denom ) { norm(); }

        /** Arithmetic. **/
        Rational< T > &operator++();
        Rational< T > &operator++( int );
        Rational< T > &operator--();
        Rational< T > &operator--( int );
        Rational< T >  operator- ();
        Rational< T >  operator+ ( const T &t    ) const;
        Rational< T >  operator+ ( const Rational< T > &r ) const;
        Rational< T > &operator+=( const T &t    );
        Rational< T > &operator+=( const Rational< T > &r );
        Rational< T >  operator- ( const T &t    ) const;
        Rational< T >  operator- ( const Rational< T > &r ) const;
        Rational< T > &operator-=( const T &t    );
        Rational< T > &operator-=( const Rational< T > &r );
        Rational< T >  operator* ( const T &t    ) const;
        Rational< T >  operator* ( const Rational< T > &r ) const;
        Rational< T > &operator*=( const T &t    );
        Rational< T > &operator*=( const Rational< T > &r );
        Rational< T >  operator/ ( const T &t    ) const;
        Rational< T >  operator/ ( const Rational< T > &r ) const;
        Rational< T > &operator/=( const T &t    );
        Rational< T > &operator/=( const Rational< T > &r );

        /** Comparision. **/
        inline bool operator< ( const Rational< T > &r ) const;
        bool operator> ( const Rational< T > &r ) const;
        bool operator<=( const Rational< T > &r ) const;
        bool operator>=( const Rational< T > &r ) const;
        bool operator==( const Rational< T > &r ) const;
		bool operator!=( const Rational< T > &r ) const;

        /** Casting and type conversion **/
        bool operator!() const;
        operator string() const;
        string toString() const;

        /** Outputs value to stdout **/
        void print() const;

};

template< class T > istream &operator>>( istream &in, const Rational< T > &n );
template< class T > ostream &operator<<( ostream &out, const Rational< T > &n );

template< class T >
Rational< T > &Rational< T >::operator++()
{
    Rational old = *this;
    n += d;
    return old;
}

template< class T >
Rational< T > &Rational< T >::operator++( int )
{
    n += d;
    return *this;
}

template< class T >
Rational< T > &Rational< T >::operator--()
{
    Rational old = *this;
    n -= d;
    return old;
}

template< class T >
Rational< T > &Rational< T >::operator--( int )
{
    n -= d;
    return *this;
}

template< class T >
Rational< T > Rational< T >::operator-()
{
    return makeRat( -n, d );
}

template< class T >
Rational< T > Rational< T >::operator+( const T &t ) const
{
    return makeRat( n + t * d, d );
}

template< class T >
Rational< T > Rational< T >::operator+( const Rational< T > &r ) const
{
    //FIXME: Can make this potentially slower but more robust against overflow
    return Rational< T >( n * r.d + d * r.n, d * r.d );
}

template< class T >
Rational< T > &Rational< T >::operator+=( const T &t )
{
    n += t * d;
    return *this;
}

template< class T >
Rational< T > &Rational< T >::operator+=( const Rational< T > &r )
{
    n *= r.d;
    n += d * r.n;
    d *= r.d;
    norm();
    return *this;
}

template< class T >
Rational< T > Rational< T >::operator-( const T &t ) const
{
    return makeRat( n - t * d, d );
}

template< class T >
Rational< T > Rational< T >::operator-( const Rational< T > &r ) const
{
    return Rational< T >( n * r.d - d * r.n, d * r.d );
}

template< class T >
Rational< T > &Rational< T >::operator-=( const T &t )
{
    n -= t * d;
    return *this;
}

template< class T >
Rational< T > &Rational< T >::operator-=( const Rational< T > &r )
{
    n *= r.d;
    n -= d * r.n;
    d *= r.d;
    norm();
    return *this;
}

template< class T >
Rational< T > Rational< T >::operator*( const T &t ) const
{
    T bc = gcd( t, d );
    return makeRat( n * ( t / bc ), d / bc );
}

template< class T >
Rational< T > Rational< T >::operator*( const Rational< T > &r ) const
{
    T ad = gcd( n, r.d ), bc = gcd( r.n, d );
    return makeRat( ( n / ad ) * ( r.n / bc ), ( d / bc ) * ( r.d / ad ) );
}

template< class T >
Rational< T > &Rational< T >::operator*=( const T &t )
{
    T bc = gcd( t, d );
    n *= t / bc;
    d /= bc;
    return *this;
}

template< class T >
Rational< T > &Rational< T >::operator*=( const Rational< T > &r )
{
    T ad = gcd( n, r.d ), bc = gcd( r.n, d );
    n /= ad;
    n *= r.n / bc;
    d /= bc;
    d *= r.d / ad;
    return *this;
}

template< class T >
Rational< T > Rational< T >::operator/( const T &t ) const
{
    T ac = gcd( n, t );
    return makeRat( n / ac, d * ( t / ac ) );
}

template< class T >
Rational< T > Rational< T >::operator/( const Rational< T > &r ) const
{
    T ac = gcd( n, r.n ), bd = gcd( d, r.d );
    return makeRat( ( n / ac ) * ( r.d / bd ), ( d / bd ) * ( r.n / ac ) );
}

template< class T >
Rational< T > &Rational< T >::operator/=( const T &t )
{
    T ac = gcd( n, t );
    n /= ac;
    d *= t / ac;
    return *this;
}

template< class T >
Rational< T > &Rational< T >::operator/=( const Rational< T > &r )
{
    T ac = gcd( n, r.n ), bd = gcd( d, r.d );
    n /= ac;
    n *= r.d / bd;
    d /= bd;
    d *= r.n / ac;
    return *this;
}

/** Comparison. **/
template< class T >
inline bool Rational< T >::operator< ( const Rational< T > &r ) const
{
    return n * r.d < d * r.n;
}

template< class T >
inline bool Rational< T >::operator> ( const Rational< T > &r ) const
{
    return n * r.d > d * r.n;
}

template< class T >
inline bool Rational< T >::operator<=( const Rational< T > &r ) const
{
    return n * r.d <= d * r.n;
}

template< class T >
inline bool Rational< T >::operator>=( const Rational< T > &r ) const
{
    return n * r.d >= d * r.n;
}

template< class T >
inline bool Rational< T >::operator==( const Rational< T > &r ) const
{
    return n == r.n && d == r.d;
}

template< class T >
inline bool Rational< T >::operator!=( const Rational< T > &r ) const
{
    return n != r.n || d != r.d;
}

#define rational Rational<int64>


string r_str()
{
	char s[100];
	rs(s);
	return s;
}

int n,  m;

int64 res;

double res_fr;

vector <vector<pair<int, rational> > > a;

MSI mp;

int do_map(string s)
{
	int res = mp[s];
	if (!res)
		mp[s] = res = ++n;
	return res;
}

rational best[64][64];

int64 dp[64][64];

void dfs(int v, int d)
{
	FORSZ(i, a[v])
	{
		int to = a[v][i].first;
		rational r = a[v][i].second;
		if (best[to][d+1] > best[v][d] * r)
			best[to][d+1] = best[v][d] * r;
	}
}

void do_it(int v, int d)
{
	FORSZ(i, a[v])
	{
		int to = a[v][i].first;
		rational r = a[v][i].second;
		if (r * best[v][d] == best[to][d+1])
			dp[to][d+1] += dp[v][d];
	}
}

void solve()
{
	res_fr = INF;
	CLA(dp);
	FOR(i, 64) FOR(j, 12) best[i][j] = INF;
	best[2][0] = 1;
	FORE(d, 0, 9) FORE(v, 1, n)
	{
		dfs(v, d);
	}
	dp[2][0] = 1;
	FORE(d, 0, 9) FORE(v, 1, n)
	{
		do_it(v, d);
	}
	rational rs = INF;
	FORE(d, 1, 9)
	{
		res_fr = min(res_fr, 1.0 * best[1][d].n / best[1][d].d);
		if (best[1][d] < rs)
			rs = best[1][d];
	}
	res = 0;
	FORE(d, 1, 9)
	{
		if (best[1][d] == rs)
			res += dp[1][d];
	}
}

char *mask = "%%.%dlf";

void print(double n)
{
	char smask[64];
	char s[64];
	if (n >= 10000)
	{
		int c = 0;
		while (n / 10 >= 10000) {n /= 10; c++;}
		printf("%.0lf", n);
		REP(c)
			putchar('0');
		return;
	}
	FOR(k, 16)
	{
		sprintf(smask, mask, k);
		sprintf(s, smask, n);
		char *p = s;
		int cnt = 0;
		while (*p == '0' || *p == '.') p++;
		while (*p != 0)
		{
			if (*p != '.') cnt++;
			p++;
		}
		if (cnt >= 5)
		{
			printf("%s", s);
			return;
		}
	}
}

int main()
{
#ifdef _DEBUG
	freopen("1064", "r", stdin);
//	freopen("1064o", "w", stdout);
#endif
	int nt = ri();
	FORE(it, 1, nt)
	{
		printf("Case %d: ", it);
		mp.clear();
		n = 0;
		do_map(r_str());
		do_map(r_str());
		m = ri();
		a.assign(51, vector<pair<int, rational> >());
		REP(m)
		{
			int d = ri();
			int k1 = do_map(r_str());
			int n = ri();
			int k2 = do_map(r_str());
			swap(n, d);
			a[k2].PB(MP(k1, rational(d, n)));
		}
		solve();
		print(res_fr);
		putchar(' ');
		_pi64(res);
		putchar('\n');
	}
}
 