#include <bits/stdc++.h>   using namespace std;   template <typename T>   inline string toString(T a) {     ostringstream os(  );     os << a;     return os.str();   }   template <typename T>   inline long long toLong(T a) {     long long res;     istringstream os(a);     os >> res;     return res;   }   template <typename T>   inline int toInt(T a) {     int res;     istringstream os(a);     os >> res;     return res;   }   template <typename T>   inline double toDouble(T a) {     double res;     istringstream os(a);     os >> res;     return res;   }   template <typename T>   inline T SQ(T a) {     return a * a;   }   template <typename T>   inline T GCD(T a, T b) {     if (b == 0)       return a;     else       return GCD(b, a % b);   }   template <typename T>   inline T LCM(T a, T b) {     long long res = a * b;     res /= GCD(a, b);     return res;   }   template <typename T>   inline unsigned long long BIGMOD(T a, T b, T m) {     if (b == 0)       return 1;     else if (b % 2 == 0)       return SQ(BIGMOD(a, b / 2, m)) % m;     else       return (a % m * BIGMOD(a, b - 1, m)) % m;   }   template <typename T>   inline vector<string> PARSE(T str) {     vector<string> res;     string s;     istringstream os(str);     while (os >> s) res.push_back(s);     return res;   }   template <typename T>   inline unsigned long long DIST(T A, T B) {     unsigned long long res =         (A.x - B.x) * (A.x - B.x) + (A.y - B.y) * (A.y - B.y);     return res;   }   template <typename T>   inline long long CROSS(T A, T B, T C) {     return (B.x - A.x) * (C.y - A.y) - (C.x - A.x) * (B.y - A.y);   }   template <typename T>   inline double cosAngle(T a, T b, T c) {     double res = a * a + b * b - c * c;     res = res / (2 * a * b);     res = acos(res);     return res;   }   template <typename T>   inline T POWER(T base, int po) {     T res = 1;     if (base == 0) return 0;     for (int i = (0); i < (po); i++) res *= base;     return res;   }   template <typename T>   inline bool IS_ON(T mask, T pos) {     return mask & (1 << pos);   }   template <typename T>   inline int OFF(T mask, T pos) {     return mask ^ (1 << pos);   }   template <typename T>   inline int ON(T mask, T pos) {     return mask | (1 << pos);   }   template <typename T>   inline bool INSIDE_GRID(int R, int C, int ro, int clm) {     if (R >= 0 && C >= 0 && R < ro && C < clm) return 1;     return 0;   }   template <typename T>   inline void PRINT_GRID(T GRID, int ro, int clm) {     cout <<  GRID           <<  :   << GRID << endl;     for (int i = (0); i < (ro); i++) {       for (int j = (0); j < (clm); j++) cout << GRID[i][j] <<    ;       puts(  );     }   }   map<string, int> mp;   pair<long long, long long> dp[800005];   vector<int> g[800005];   vector<int> gr[800005];   int stk[800008];   string str, str2;   vector<string> inp;   pair<long long, long long> Tolower(string &str) {     int len = str.size(), r = 0;     for (int i = (0); i < (len); i++) {       str[i] = tolower(str[i]);       if (str[i] ==  r ) r++;     }     return make_pair((long long)r, (long long)len);   }   int vis[800005], top = 0, rng;   void trop(int u) {     vis[u] = 1;     for (int i = (g[u].size() - 1); i >= (0); i--)       if (!vis[g[u][i]]) trop(g[u][i]);     stk[++top] = u;   }   void manipul(int u, int papa) {     vis[u] = 3;     dp[u] = min(dp[u], dp[papa]);     for (int i = (gr[u].size() - 1); i >= (0); i--)       if (vis[gr[u][i]] != 3) manipul(gr[u][i], papa);   }   pair<long long, long long> DP(int pos) {     if (vis[pos] != 2) {       vis[pos] = 2;       for (int i = (g[pos].size() - 1); i >= (0); i--)         dp[pos] = min(dp[pos], DP(g[pos][i]));     }     return dp[pos];   }   int main() {     int n, m;     int cnt = 1;     int s, d, len, r;     for (int i = (0); i < (800000); i++)       dp[i] = make_pair(100000000000000LL, 100000000000000LL);     scanf( %d , &n);     pair<long long, long long> tmp;     for (int i = (0); i < (n); i++) {       cin >> str;       tmp = Tolower(str);       inp.push_back(str);       if (mp.find(str) == mp.end()) mp[str] = cnt++;       s = mp[str];       dp[s] = min(dp[s], tmp);     }     rng = cnt;     scanf( %d , &m);     for (int i = (0); i < (m); i++) {       cin >> str >> str2;       tmp = Tolower(str);       tmp = Tolower(str2);       if (mp.find(str) == mp.end()) mp[str] = cnt++;       if (mp.find(str2) == mp.end()) mp[str2] = cnt++;       s = mp[str];       d = mp[str2];       dp[d] = min(dp[d], tmp);       dp[s] = min(dp[s], dp[d]);       g[s].push_back(d);       gr[d].push_back(s);     }     pair<long long, long long> res;     res = make_pair(0, 0);     for (int i = (0); i < (n); i++) {       s = mp[inp[i]];       if (!vis[s]) trop(s);     }     for (int i = (top + 1 - 1); i >= (0); i--) {       DP(stk[i]);     }     for (int i = (top + 1 - 1); i >= (0); i--) {       if (vis[stk[i]] != 3) manipul(stk[i], stk[i]);     }     for (int i = (0); i < (n); i++) {       s = mp[inp[i]];       res.first += dp[s].first;       res.second += dp[s].second;     }     cout << res.first <<     << res.second << endl;     return 0;   }