#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
void Code_By_Mohamed_Khaled() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
// #ifndef ONLINE_JUDGE
//     freopen("input.txt", "r", stdin);
//     freopen("output.txt", "w", stdout);
// #endif
}
const ll mod=1e9+7;
ll add(ll a, ll b) { return ((a % mod) + (b % mod)) % mod; }
ll mul(ll a, ll b) { return ((a % mod) * (b % mod)) % mod; }
ll sub(ll a, ll b) { return ((a % mod) - (b % mod) + mod) % mod; }
// double a;string s;ll N;map<string,vector<ll>>m_base,m_quote;
// vector<map<string,double>>dp;vector<string>base,quote;vector<double>p;
// double sol(ll idx,string &cur) {
//     if (idx==N) {
//         return (cur==s ? 1.0:0.0);
//     }
//     auto it=dp[idx].find(cur);
//     if (it!=dp[idx].end()) return it->second;
//     double best=(cur==s ? 1.0:0.0);
//     auto vec=lower_bound(m_base[cur].begin(),m_base[cur].end(), idx);
//     while (vec!=m_base[cur].end()) {
//         int j=*vec;
//         double val=p[j]*sol(j+1,quote[j]);
//         best=max(best,val);
//         ++vec;
//     }
//     auto index=lower_bound(m_quote[cur].begin(),m_quote[cur].end(), idx);
//     while (index!=m_quote[cur].end()) {
//         int j=*index;
//         double val=(1.0/p[j])*sol(j+1,base[j]);
//         best=max(best,val);
//         ++index;
//     }
//     return dp[idx][cur]=best;
// }
int main() {
    Code_By_Mohamed_Khaled();
    // freopen("crypto.in", "r", stdin);
    ll t;cin>>t;
    while (t--) {
        ll a,N;string s;cin>>a>>s>>N;
        map<string,double>dp;
        dp[s]=a;
        for (ll i=0;i<N;i++) {
            string b,q;double p;cin>>b>>q>>p;
            double bb=0,qq=0;
            if (dp.find(b)!=dp.end())bb=dp[b];
            if (dp.find(q)!=dp.end())qq=dp[q];
            if (bb>0) {
                dp[q]=max(dp[q],bb*p);
            }
            if (qq>0)dp[b]=max(dp[b],qq/p);
        }
        cout<<fixed<<setprecision(6)<<dp[s]<<"\n";
    }
    return 0;
}