#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

double a;
string s;
ll N;
unordered_map<string, vector<ll>> m_base, m_quote;
vector<unordered_map<string, double>> dp;
vector<string> base, quote;
vector<double> p;

struct State {
    ll idx;
    string cur;
    bool processing;
    double val;
    vector<ll>::iterator it, end;
    double best;
};

double iterative_sol(ll start_idx, string &start_cur) {
    stack<State> stk;
    unordered_map<string, double> memo;

    stk.push({start_idx, start_cur, false, 0.0, {}, {}, 0.0});

    while (!stk.empty()) {
        auto &state = stk.top();
        if (!state.processing) {
            if (state.idx == N) {
                double res = (state.cur == s) ? 1.0 : 0.0;
                memo[state.cur] = res;
                stk.pop();
                continue;
            }
            auto it = dp[state.idx].find(state.cur);
            if (it != dp[state.idx].end()) {
                stk.pop();
                continue;
            }
            state.best = (state.cur == s) ? 1.0 : 0.0;
            state.processing = true;

            if (m_base.count(state.cur)) {
                auto &vec = m_base[state.cur];
                state.it = lower_bound(vec.begin(), vec.end(), state.idx);
                state.end = vec.end();
                for (; state.it != state.end; ++state.it) {
                    ll j = *state.it;
                    if (j >= N) break;
                    stk.push({j + 1, quote[j], false, 0.0, {}, {}, 0.0});
                }
                state.it = lower_bound(vec.begin(), vec.end(), state.idx);
            }
        } else {
            if (m_base.count(state.cur)) {
                auto &vec = m_base[state.cur];
                for (; state.it != state.end; ++state.it) {
                    ll j = *state.it;
                    if (j >= N) break;
                    auto it_memo = memo.find(quote[j]);
                    if (it_memo != memo.end()) {
                        state.best = max(state.best, p[j] * it_memo->second);
                    }
                }
            }

            if (m_quote.count(state.cur)) {
                auto &vec = m_quote[state.cur];
                auto it_j = lower_bound(vec.begin(), vec.end(), state.idx);
                for (; it_j != vec.end(); ++it_j) {
                    ll j = *it_j;
                    if (j >= N) break;
                    auto it_memo = memo.find(base[j]);
                    if (it_memo != memo.end()) {
                        state.best = max(state.best, (1.0 / p[j]) * it_memo->second);
                    }
                }
            }

            dp[state.idx][state.cur] = state.best;
            memo[state.cur] = state.best;
            stk.pop();
        }
    }
    return memo[start_cur];
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    // freopen("crypto.in", "r", stdin);
    ll t;
    cin >> t;
    while (t--) {
        cin >> a >> s >> N;
        m_base.clear();
        m_quote.clear();
        dp.assign(N + 1, unordered_map<string, double>());
        base.resize(N);
        quote.resize(N);
        p.resize(N);
        for (ll i = 0; i < N; ++i) {
            cin >> base[i] >> quote[i] >> p[i];
            m_base[base[i]].push_back(i);
            m_quote[quote[i]].push_back(i);
        }
        for (auto &pair : m_base) sort(pair.second.begin(), pair.second.end());
        for (auto &pair : m_quote) sort(pair.second.begin(), pair.second.end());
        double ss = iterative_sol(0, s);
        ss *= a;
        cout << fixed << setprecision(6) << ss << "\n";
    }
    return 0;
}