#include<iostream>
#include<vector>
#include<cmath>
#include<string>
#include<sstream>
#include<algorithm>
#define rep(i,a) for(int i = 0 ; i < a ; i ++)
#define loop(i,a,b) for(int i = a ; i < b ; i ++)

using namespace std;

struct Data{
  int start,end,onAir,recorded;
  bool operator<(const Data &data) const{
    if(end != data.end) return end < data.end;
    if(start != data.start) return start < data.start;
    return (onAir != data.onAir ) ? onAir < data.onAir : recorded < data.recorded;
  }
};

const int MAX_N = 1100;
int N,dp[2000][2000],h,m;
vector<int> buf;


int toTime(string s){
  rep(i,(int)s.size()) if(s[i] == ':') s[i] = ' ';
  stringstream ss(s);
  ss >> h >> m;
  return h * 60 + m;
}

int main(void){
  while(cin>>N,N){
    vector<Data> vec(N);
    rep(i,N){
      string a,b,c;
      cin>>a>>b>>c>>vec[i].onAir>>vec[i].recorded;
      vec[i].start = toTime(b); vec[i].end = toTime(c); 
    }
    vec.push_back((Data){0,0,0,0});
    rep(i,N+2)rep(j,N+2)dp[i][j] = 0;
    sort(vec.begin(),vec.end());
    buf.clear();
    rep(i,vec.size())buf.push_back(vec[i].end);
    rep(i,vec.size()){
      rep(j,vec.size()){
        if(i==j)dp[i][j] = max((i?dp[i-1][j]:0),(j?dp[i][j-1]:0));
        else if(i<j){
          int prev = upper_bound(buf.begin(),buf.begin()+j,vec[j].start) - buf.begin() - 1;
          dp[i][j] = max(dp[i][j-1],((prev!=-1)?dp[i][prev]:0)+vec[j].recorded);
        }else{
          int prev = upper_bound(buf.begin(),buf.begin()+i,vec[i].start) - buf.begin() - 1;
          dp[i][j] = max(dp[i-1][j],((prev!=-1)?dp[prev][j]:0)+vec[i].onAir);
        }
      }
    }
    cout<<dp[(int)vec.size()-1][(int)vec.size()-1]<<endl;
  }
}
### Prompt

Generate a Cpp solution to the following problem:

You are addicted to watching TV, and you watch so many TV programs every day. You have been in trouble recently: the airtimes of your favorite TV programs overlap.

Fortunately, you have both a TV and a video recorder at your home. You can therefore watch a program on air while another program (on a different channel) is recorded to a video at the same time. However, it is not easy to decide which programs should be watched on air or recorded to a video. As you are a talented computer programmer, you have decided to write a program to find the way of watching TV programs which gives you the greatest possible satisfaction.

Your program (for a computer) will be given TV listing of a day along with your score for each TV program. Each score represents how much you will be satisfied if you watch the corresponding TV program on air or with a video. Your program should compute the maximum possible sum of the scores of the TV programs that you can watch.



Input

The input consists of several scenarios.

The first line of each scenario contains an integer N (1 ≤ N ≤ 1000) that represents the number of programs. Each of the following N lines contains program information in the format below:

T Tb Te R1 R2

T is the title of the program and is composed by up to 32 alphabetical letters. Tb and Te specify the start time and the end time of broadcasting, respectively. R1 and R2 indicate the score when you watch the program on air and when you have the program recorded, respectively.

All times given in the input have the form of “hh:mm” and range from 00:00 to 23:59. You may assume that no program is broadcast across the twelve midnight.

The end of the input is indicated by a line containing only a single zero. This is not part of scenarios.

Output

For each scenario, output the maximum possible score in a line.

Example

Input

4
OmoikkiriTV 12:00 13:00 5 1
WaratteIitomo 12:00 13:00 10 2
WatarusekennhaOnibakari 20:00 21:00 10 3
SuzumiyaharuhiNoYuuutsu 23:00 23:30 100 40
5
a 0:00 1:00 100 100
b 0:00 1:00 101 101
c 0:00 1:00 102 102
d 0:00 1:00 103 103
e 0:00 1:00 104 104
0


Output

121
207

### Response

```cpp
#include<iostream>
#include<vector>
#include<cmath>
#include<string>
#include<sstream>
#include<algorithm>
#define rep(i,a) for(int i = 0 ; i < a ; i ++)
#define loop(i,a,b) for(int i = a ; i < b ; i ++)

using namespace std;

struct Data{
  int start,end,onAir,recorded;
  bool operator<(const Data &data) const{
    if(end != data.end) return end < data.end;
    if(start != data.start) return start < data.start;
    return (onAir != data.onAir ) ? onAir < data.onAir : recorded < data.recorded;
  }
};

const int MAX_N = 1100;
int N,dp[2000][2000],h,m;
vector<int> buf;


int toTime(string s){
  rep(i,(int)s.size()) if(s[i] == ':') s[i] = ' ';
  stringstream ss(s);
  ss >> h >> m;
  return h * 60 + m;
}

int main(void){
  while(cin>>N,N){
    vector<Data> vec(N);
    rep(i,N){
      string a,b,c;
      cin>>a>>b>>c>>vec[i].onAir>>vec[i].recorded;
      vec[i].start = toTime(b); vec[i].end = toTime(c); 
    }
    vec.push_back((Data){0,0,0,0});
    rep(i,N+2)rep(j,N+2)dp[i][j] = 0;
    sort(vec.begin(),vec.end());
    buf.clear();
    rep(i,vec.size())buf.push_back(vec[i].end);
    rep(i,vec.size()){
      rep(j,vec.size()){
        if(i==j)dp[i][j] = max((i?dp[i-1][j]:0),(j?dp[i][j-1]:0));
        else if(i<j){
          int prev = upper_bound(buf.begin(),buf.begin()+j,vec[j].start) - buf.begin() - 1;
          dp[i][j] = max(dp[i][j-1],((prev!=-1)?dp[i][prev]:0)+vec[j].recorded);
        }else{
          int prev = upper_bound(buf.begin(),buf.begin()+i,vec[i].start) - buf.begin() - 1;
          dp[i][j] = max(dp[i-1][j],((prev!=-1)?dp[prev][j]:0)+vec[i].onAir);
        }
      }
    }
    cout<<dp[(int)vec.size()-1][(int)vec.size()-1]<<endl;
  }
}
```