function Main(input) {
    input = input.trim().split("\n").map(function(x) { return x.split(" ")});
  	var arrSum = arr => arr.reduce((a,b) => a + b, 0);
    var N = parseInt(input[0], 10);
    var vec_A = [ ];
    var ary_xy = [ ];
    var ans = 0;
    var nHonest = 0;
    var tmpLine = 1;
    var tmpXy = [];
    var vec_honest = Array.from({ length: N }, () => 0);
    var vec_liar = Array.from({ length: N }, () => 0);
    for (var Ai = 1; Ai <=N; Ai++){
    	vec_A.push(parseInt(input[tmpLine], 10));
    	tmpLine ++;
    	for (var Li = 1; Li <= vec_A[Ai-1]; Li++){
          	tmpXy.push(input[tmpLine].map(e => parseInt(e, 10)));
            tmpLine++;
    	}
		ary_xy.push(tmpXy);
      	tmpXy = [];
    }
    for (var i = Math.pow(2, N); i <  Math.pow(2, N) * 2; i++){
    	tmpAry = i.toString(2).substr(1).split('').map(e => parseInt(e, 10));
    	vec_honest = VO(vec_honest, [0], '*');
	    vec_liar = VO(vec_liar, [0], '*');
    	tmpAry_loop:
    	for (var Ai = 1; Ai <=N; Ai++){
    		if (tmpAry[Ai - 1] == '1'){
    			for (var j =  1; j <= vec_A[Ai-1]; j++){
    				vec_honest[ary_xy[Ai-1][j-1][0]-1] = Math.max(vec_honest[ary_xy[Ai-1][j-1][0]-1],ary_xy[Ai-1][j-1][1]);
    				vec_liar[ary_xy[Ai-1][j-1][0]-1] = Math.max(vec_liar[ary_xy[Ai-1][j-1][0]-1],(1 - ary_xy[Ai-1][j-1][1]));
    			}
    		}
    	}
    	if (VO(vec_honest, vec_liar, '+').indexOf(2) == -1){
    			if (VO(vec_honest, tmpAry, '-').indexOf(1) == -1){	

    				if (VO(vec_liar, tmpAry, '+').indexOf(2) == -1){	
    					nHonest = arrSum(tmpAry);
    					ans = Math.max(ans, nHonest);
    				}
    			}
    	}		
    }
    console.log(ans);
}

function VO(v1, v2, op){
	var ans = []
	if (v1.length !== v2.length) {
      if (v2.length == 1){
        v2 = Array.from({ length: (v1.length) }, () => v2);
      } else {
      	return null;
      }
    }
	for (var i = 0; i < v1.length; i++){
		if (op == '+') {
			ans.push(v1[i] + v2[i]);
		} 
		if (op == '-') {
			ans.push(v1[i] - v2[i]);
		} 
		if (op == '*') {
			ans.push(v1[i] * v2[i]);
		} 
		if (op == '/') {
			ans.push(v1[i] / v2[i]);
		} 
		if (op == '%') {
			ans.push(v1[i] % v2[i]);
		} 
		if (op == '||') {
			ans.push(Math.max(v1[i],v2[i]));
		} 
	}
	return ans;	
}


Main(require("fs").readFileSync("/dev/stdin", "utf8"));