#include <iostream>
#include <cmath>
#include <fstream>
#include <algorithm>
#include <map>
#include <set>
#include <vector>
#include <cstring>
using namespace std;
int main()
{
    freopen("DATA5.txt", "r", stdin);
    freopen("OUT5.txt", "w", stdout);
    int n, d;
    map<string,int>::iterator it;
    for (int ii = 0; ii < 5; ii++)
    {
        map<string, int> m;
        int dd[21][21], u = 0;
        string s, t;
        cin >> n;
        for (int i = 0; i < n; i++)
        {
            int temp, temp2;
            cin >> s >> t >> d;
            it = m.find(s);
            if (it==(m.end()))
            {
                temp = u++;
                m[s] = temp;
            }
            else temp = m.find(s)->second;
            it = m.find(t);
            if (it==(m.end()))
            {
                temp2 = u++;
                m[t] = temp2;
            }
            else temp2 = m.find(t)->second;
            dd[temp][temp2] = d;
        }
        for (int i = 0; i < u; i++)
            for (int j = 0; j < u; j++)
                for (int k = 0; k < u; k++)
                {
                    dd[j][k] = min(dd[j][i] + dd[i][k], dd[j][k]);
                }
        cout << dd[m.find("YYZ")->second][m.find("SEA")->second] << endl;
    }
    return 0;
}

