#include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 99999999999999999
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


#define NUM 1440

struct Info{
	Info(int arg_end_time,int arg_live_value,int arg_record_value){
		end_time = arg_end_time;
		live_value = arg_live_value;
		record_value = arg_record_value;
	}
	int end_time,live_value,record_value;
};

int N;
int dp[NUM][NUM];
vector<Info> G[NUM];

int get_time(char buf[6]){
	int hour = 10*(buf[0]-'0')+buf[1]-'0';
	int minute = 10*(buf[3]-'0')+buf[4]-'0';
	return 60*hour+minute;
}

int recursive(int live_time,int record_time){

	if(dp[live_time][record_time] != -1){
		return dp[live_time][record_time];
	}

	int ret = 0;

	if(live_time < record_time){

		ret = max(ret,recursive(live_time+1,record_time));

		for(int i = 0; i < G[live_time].size(); i++){

			ret = max(ret,recursive(G[live_time][i].end_time,record_time)+G[live_time][i].live_value);
		}

		return dp[live_time][record_time] = ret;

	}else if(live_time > record_time){

		ret = max(ret,recursive(live_time,record_time+1));

		for(int i = 0; i < G[record_time].size(); i++){
			ret = max(ret,recursive(live_time,G[record_time][i].end_time)+G[record_time][i].record_value);
		}
		return dp[live_time][record_time] = ret;

	}else{ //live_time == record_time

		if(live_time == NUM-1){

			return 0;
		}

		ret = max(ret,recursive(live_time+1,record_time+1));

		for(int i = 0; i < G[live_time].size(); i++){
			ret = max(ret,recursive(G[live_time][i].end_time,record_time+1)+G[live_time][i].live_value);
		}

		for(int i = 0; i < G[record_time].size(); i++){
			ret = max(ret,recursive(live_time+1,G[record_time][i].end_time)+G[record_time][i].record_value);
		}

		if(G[live_time].size() > 1){
			for(int i = 0; i < G[live_time].size(); i++){
				for(int k = 0; k < G[live_time].size(); k++){
					if(k == i)continue;

					ret = max(ret,recursive(G[live_time][i].end_time,G[live_time][k].end_time)+
							G[live_time][i].live_value+G[live_time][k].record_value);

				}
			}
		}
		return dp[live_time][record_time] = ret;
	}
}

void func(){

	for(int i = 0; i < NUM; i++){
		G[i].clear();
	}

	int live_value,record_value,start_time,end_time;
	char name[33],start_buf[6],end_buf[6];

	for(int loop = 0; loop < N; loop++){

		scanf("%s %s %s %d %d",name,start_buf,end_buf,&live_value,&record_value);
		start_time = get_time(start_buf);
		end_time = get_time(end_buf);

		G[start_time].push_back(Info(end_time,live_value,record_value));
	}

	for(int i = 0; i < NUM; i++){
		for(int k = 0; k < NUM; k++){
			dp[i][k] = -1;
		}
	}

	printf("%d\n",recursive(0,0));
}

int main(){

	while(true){
		scanf("%d",&N);
		if(N == 0)break;

		func();
	}

	return 0;
}

