Problem:
Problem B: Butterfly

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 100 and Bob's satisfaction is 200. Then, since she can make it with both guys, she can get 300 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:

N

Guy1

...

GuyN

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

M L

S1 E1

...

SM EM

The first line contains two integers Mi (1 ≤ Mi ≤ 16) and Li (1 ≤ Li ≤ 100,000,000), the number of dates set for the guy and the satisfaction she would get from him respectively. Then M lines follow. The i-th line contains two integers Si and Ei (6 ≤ Si < Ei ≤ 22), the starting and ending time of the i-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.

Sample Input

2
2 100
10 12
14 16
2 200
12 13
18 20
4
1 100
6 22
1 1000
6 22
1 10000
6 22
1 100000
6 22
16
1 100000000
6 7
1 100000000
7 8
1 100000000
8 9
1 100000000
9 10
1 100000000
10 11
1 100000000
11 12
1 100000000
12 13
1 100000000
13 14
1 100000000
14 15
1 100000000
15 16
1 100000000
16 17
1 100000000
17 18
1 100000000
18 19
1 100000000
19 20
1 100000000
20 21
1 100000000
21 22
0

Output for the Sample Input

300
100000
1600000000

Solution in JAVA (judge verdict: Accepted)

import java.io.*;
import java.util.*;
import java.lang.ArrayIndexOutOfBoundsException;
import java.math.BigInteger;

/**
 * @author yoshikyoto
 */
class Main extends MyUtil{
	public static void main(String[] args) throws Exception{
		while(true){
			int n = readInt();
			if(n == 0) break;
			
			Guy[] guys = new Guy[n];
			for(int i = 0; i < n; i++){
				int m = readIntMap(0);
				int l = readIntMap(1);
				
				Guy guy = new Guy();
				guy.l = l;
				guys[i] = guy;
				for(int j = 0; j < m; j++){
					int[] in = readIntMap();
					guy.start.add(in[0] - 6);
					guy.end.add(in[1] - 6);
				}
			}
			
			System.out.println(dfs(guys, 0, n, new boolean[16]));
		}
	}
	
	static int dfs(Guy[] guys, int i, int n, boolean[] schedule){
		if(i >= n) return 0;
		// 使わない場合
		int ret0 = dfs(guys, i + 1, n, schedule);
		
		// 使える場合は使ってもいい
		int ret1 = 0;
		if(canFill(schedule, guys[i])){
			boolean[] schedule_cp = cp(schedule);
			fill(schedule_cp, guys[i]);
			ret1 = guys[i].l + dfs(guys, i + 1, n, schedule_cp);
		}
		
		return Math.max(ret0, ret1);
	}
	
	/**
	 * 予定を入れられるならtrue,入れられないならfalse
	 */
	static boolean canFill(boolean[] schedule, Guy guy){
		for(int i = 0; i < guy.start.size(); i++){
			int s = guy.start.get(i);
			int e = guy.end.get(i);
			for(int j= s; j < e; j++){
				if(schedule[j]) return false;
			}
		}
		return true;
	}
	
	static void fill(boolean[] schedule, Guy guy){
		for(int i = 0; i < guy.start.size(); i++){
			int s = guy.start.get(i);
			int e = guy.end.get(i);
			for(int j = s; j < e; j++){
				schedule[j] = true;
			}
		}
	}
	
	public static boolean[] cp(boolean[] a) {
		boolean[] b = new boolean[a.length];
		for (int i = 0; i < a.length; i++)
			b[i] = a[i];
		return b;
	}
}

class Guy{
	int l;
	ArrayList<Integer> start = new ArrayList<Integer>();
	ArrayList<Integer> end = new ArrayList<Integer>();
}

// --- ここから下はライブラリ ----------
/**
 * MyUtil
 * @author yoshikyoto
 */
class MyUtil {
	public static int toInt(boolean[] a){
		int pow = 1, ret = 0, l = a.length;
		for(int i = 0; i < l; i++){
			if(a[i]) ret += pow;
			pow *= 2;
		}
		return ret;
	}
	static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	public static int ins[];
	public static int[] readIntMap() throws IOException{return parseInt(readLine().split(" "));}
	public static int readIntMap(int i) throws Exception{
		if(i == 0) ins = readIntMap();
		return ins[i];
	}
	public static int[][] readIntMap(int n, int m) throws IOException{
		int[][] ret = new int[n][];
		for(int i = 0; i < n; i++) ret[i] = readIntMap();
		return ret;
	}
	public static int[] readIntToMap(int n) throws IOException{
		int[] ret = new int[n];
		for(int i = 0; i < n; i++) ret[i] = readInt();
		return ret;
	}
	public static int[] readNoDistIntMap() throws IOException{
		String[] strs = readLine().split("");
		int l = strs.length;
		int[] ret = new int[l-1];
		for(int i = 1; i < l; i++)
			ret[i-1] = parseInt(strs[i]);
		return ret;
	}
	public static String readLine() throws IOException{return br.readLine();}
	public static int readInt() throws IOException{return Integer.parseInt(br.readLine());}
	public static int[] parseInt(String[] arr){
		int[] res = new int[arr.length];
		for(int i = 0; i < arr.length; i++)res[i] = Integer.parseInt(arr[i]);
		return res;
	}
	public static double[] parseDouble(String[] arr){
		double[] res = new double[arr.length];
		for(int i = 0; i < arr.length; i++)res[i] = Double.parseDouble(arr[i]);
		return res;
	}
	public static boolean[] parseBool(String[] arr){
		int[] t = parseInt(arr);
		boolean[] res = new boolean[t.length];
		for(int i = 0; i < t.length; i++){
			if(t[i] == 1){res[i] = true;
			}else{res[i] = false;}
		}
		return res;
	}
	public static int parseInt(Object o){
		return Integer.parseInt(o.toString());
	}
}