#include <cstdio>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <climits>
#include <cfloat>
using namespace std;

class Data
{
public:
    int t1, t2, r1, r2;
    bool operator< (const Data& d) const{
        return t1 < d.t1;
    }
};

int n;
vector<Data> d;
vector<int> nextTV;

vector<vector<int> > memo;

int solve(int a, int b)
{
    if(a == n+1 || b == n+1)
        return 0;
    if(a == n && b == n)
        return 0;
    if(memo[a][b] != -1)
        return memo[a][b];

    int ret = 0;
    ret = max(ret, solve(a, b+1));
    ret = max(ret, solve(a+1, b));
    if(a == b){
        ret = max(ret, d[a].r1 + solve(nextTV[a], b+1));
        ret = max(ret, d[b].r2 + solve(a+1, nextTV[b]));
    }else if(a > b){
        ret = max(ret, d[b].r2 + solve(a, nextTV[b]));
    }else{
        ret = max(ret, d[a].r1 + solve(nextTV[a], b));
    }

    return memo[a][b] = ret;
}

int main()
{
    for(;;){
        cin >> n;
        if(n == 0)
            return 0;

        d.resize(n+1);
        for(int i=0; i<n; ++i){
            string s;
            int h1, m1, h2, m2;
            char c;
            cin >> s >> h1 >> c >> m1 >> h2 >> c >> m2 >> d[i].r1 >> d[i].r2;
            d[i].t1 = h1 * 60 + m1;
            d[i].t2 = h2 * 60 + m2;
        }
        d[n].t1 = INT_MAX / 2;
        d[n].t2 = INT_MAX / 2 + 1;
        d[n].r1 = d[n].r2 = 0;
        sort(d.begin(), d.end());

        nextTV.resize(n+1);
        for(int i=0; i<=n; ++i){
            Data tmp;
            tmp.t1 = d[i].t2;
            nextTV[i] = lower_bound(d.begin(), d.end(), tmp) - d.begin();
        }

        memo.assign(n+1, vector<int>(n+1, -1));
        cout << solve(0, 0) << endl;
    }
}