#include<bits/stdc++.h>
using namespace std;
unordered_map<string,vector<pair<string,double>>> adj;
double bfs(string src, string d) {
if (src == d) return 1.0;
queue<pair<string,double>> q;
q.push({src, 1.0});
unordered_map<string, bool> vis;
vis[src] = true;
while (!q.empty()) {
auto x = q.front();
q.pop();
string node = x.first;
double dis = x.second;
if (node == d)
return dis;
for (auto& c : adj[node]) {
if (!vis[c.first]) {
vis[c.first] = true;
q.push({c.first, dis * c.second});
}
}
}
return -1;
}
vector<double> evaluateEquations(vector<pair<string, string>> &a, vector<pair<string, string>> &q, vector<double> &b) {
adj.clear(); // Clear the adjacency map at the start of the function
int n = a.size();
for (int i = 0; i < n; i++) {
adj[a[i].first].push_back({a[i].second, b[i]});
adj[a[i].second].push_back({a[i].first, 1 / b[i]});
}
vector<double> ans;
for (auto& x : q) {
if (adj.find(x.first) == adj.end() || adj.find(x.second) == adj.end())
ans.push_back(-1);
else
ans.push_back(bfs(x.first, x.second));
}
return ans;
}