import java.util.Scanner;
import java.util.Vector;
import java.util.Arrays;
import java.util.Comparator;


public class F{
	public static void printSolve(int[] colors, int color, int n){
		StringBuilder str = new StringBuilder();
		for(int i = 1;i<=n;++i)
			if(colors[i] == color)
				str.append(i + " ");
		System.out.println(str);
	}
	public static void main(String[] args){
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		long[] dp = new long[10];
		long[] dp2 = dp;
		Comparator<Long> comp = new Comparator<Long>(){
			public int compare(Long a, Long b){
				return Long.compare(b,a);
			}};
		int cards = 0;
		int last = 0;
		for(int i = 0;i<n;++i){
			int k = in.nextInt();
			long max3 = 0;
			long max2 = 0;
			Vector<Long> ones = new Vector<Long>();
			last = cards;
			for(int j = 0;j<k;++j){
				int c = in.nextInt();
				if(c == 1) ones.add(in.nextLong());
				if(c == 2) max2 = Math.max(max2,in.nextLong());
				if(c==3) max3 = Math.max(max3,in.nextLong());
			}
			ones.sort(comp);
			switch(ones.size()){
				case 2: cards+=2;break;
				case 1: cards++; if(max2 != 0) cards++;break;
				case 0: if(max2 != 0  || max3!=0) cards++; break;
				default:
					if(ones.size()>=3) cards+=3;
			}
			dp2 = dp;
			dp = Arrays.copyOf(dp2,10);
			//System.out.println(ones);
			//System.out.println(cards);
			int temp = (ones.size()>=3)?3:ones.size();
			if(last < 9){
				for(int j = 0;j<last+1;++j) dp[j] = dp2[j];
				for(int j = last+1;j<((cards>=10) ? 10: cards+1);++j){
					if(max3 != 0 && j>=1) dp[j] = Math.max(dp[j],dp2[j-1] + max3);
					if(max2 != 0 && j>=1){
						dp[j] = Math.max(dp[j],dp2[j-1] + max2);
						if(j >=2 && ones.size()>0)
							dp[j] = Math.max(dp[j],dp2[j-2] + max2 + ones.get(0));
					}
					
					switch(temp){
						case 3: if(j>=3) dp[j] = Math.max(dp[j],dp2[j-3] +ones.get(2) +  ones.get(1) + ones.get(0));
						case 2: if(j>=2) dp[j] = Math.max(dp[j],dp2[j-2] + ones.get(1) + ones.get(0));
						case 1: if(j>=1)dp[j] = Math.max(dp[j],dp2[j-1] + ones.get(0));
					}						
				}
			} 
			for(int j = 0;j<=((cards-10<=9)?cards-10 : 9);++j){
				int pr3 = ((j-3) >= 0) ? j-3 : 10 + j - 3;
				int pr2 = ((j-2) >= 0) ? j-2 : 10 + j - 2;
				int pr1 = ((j-1) >= 0) ? j-1 : 10 + j - 1;
				if(max3 != 0){
					if(j == 0)dp[j] = Math.max(dp[j], dp2[pr1] + max3*2);
					else dp[j] = Math.max(dp[j], dp2[pr1] + max3);
				}
				if(max2 != 0){
					if(j == 0)dp[j] = Math.max(dp[j], dp2[pr1] + max2*2);
					else dp[j] = Math.max(dp[j], dp2[pr1] + max2);
					if(ones.size()>0){
						if(j<=1)dp[j] = Math.max(dp[j], dp2[pr2] + Math.max(max2,ones.get(0))*2 + Math.min(max2,ones.get(0)));
						else dp[j] = Math.max(dp[j], dp2[pr2] + ones.get(0) + max2);
					}
				}
				switch(temp){
					case 3:	if(j<=2)dp[j] = Math.max(dp[j],dp2[pr3] + ones.get(0)*2 + ones.get(1)+ones.get(2));
						else dp[j] = Math.max(dp[j],dp2[pr3] + ones.get(0) + ones.get(1)+ones.get(2));
					case 2: if(j<=1)dp[j] = Math.max(dp[j],dp2[pr2] + ones.get(0)*2 + ones.get(1));
						else dp[j] = Math.max(dp[j],dp2[pr2] + ones.get(0) + ones.get(1));
					case 1: if(j==0)dp[j] = Math.max(dp[j],dp2[pr1] + ones.get(0)*2);
						else dp[j] = Math.max(dp[j],dp2[pr1] + ones.get(0));
				}
			}
			//System.out.println(cards + " " + Arrays.toString(dp));
		}
		long res = 0;
		for(int i = 0;i<10;++i)
			res = Math.max(dp[i],res);
		System.out.println(res);


	}
}
