Butterfly#3594

Butterfly

30 seconds
256 megabytes
Hard

Claire is a man-eater. She's a real man-eater. She's going around with dozens of guys. She's dating all the time. And one day she found some conflicts in her date schedule. D'oh!

So she needs to pick some dates and give the others up. The dates are set by hours like 13:00 to 15:00. She may have more than one date with a guy. For example, she can have dates with Adam from 10:00 to 12:00 and from 14:00 to 16:00 and with Bob from 12:00 to 13:00 and from 18:00 to 20:00. She can have these dates as long as there is no overlap of time. Time of traveling, time of make-up, trouble from love triangles, and the likes are not of her concern. Thus she can keep all the dates with Adam and Bob in the previous example. All dates are set between 6:00 and 22:00 on the same day.

She wants to get the maximum amount of satisfaction in total. Each guy gives her some satisfaction if he has all scheduled dates. Let's say, for example, Adam's satisfaction is and Bob's satisfaction is . Then, since she can make it with both guys, she can get in total.

Your task is to write a program to satisfy her demand. Then she could spend a few hours with you... if you really want.

Input

The input consists of a sequence of datasets. Each dataset has the following format:

\text{Guy}_1 \text{Guy}_N

The first line of the input contains an integer (1 \leq N \leq 100), the number of guys. Then there come the descriptions of guys. Each description is given in this format:

S_1 E_1 S_M E_M

The first line contains two integers M_i (1 \leq M_i \leq 16) and L_i (1 \leq L_i \leq 100000000), the number of dates set for the guy and the satisfaction she would get from him respectively. Then lines follow. The -th line contains two integers S_i and E_i (6 \leq S_i < E_i \leq 22), the starting and ending time of the -th date.

The end of input is indicated by N = 0.

Output

For each dataset, output in a line the maximum amount of satisfaction she can get.

Examples

Example 1

Loading
Loading