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


public class F2{
	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){
		long res = 0;
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		long[] dp = new long[3*n+1];
		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;
			}
			int temp = (ones.size()>=3)?3:ones.size();
			for(int j = cards; j >=(cards-12>=0 ? cards-12:0);--j){
				if(max3 != 0 && j>=1) dp[j] = Math.max(dp[j], dp[j-1] + max3*((j-1)%10==9 ? 2:1));
				if(max2 != 0 && j>=1){
					dp[j] = Math.max(dp[j], dp[j-1] + max2*((j-1)%10==9 ? 2:1));
					if(ones.size()>0 && j >=2)
						dp[j] = Math.max(dp[j], dp[j-2] +Math.max(max2,ones.get(0))*((j-2)%10>=8 ? 2:1) + Math.min(max2,ones.get(0)));
				}
				switch(temp){
					case 3:	if(j>=3)dp[j] = Math.max(dp[j],dp[j-3] + ones.get(0)*((j-3)%10>=7 ? 2:1) + ones.get(1)+ones.get(2));
					case 2: if(j>=2)dp[j] = Math.max(dp[j],dp[j-2] + ones.get(0)*((j-2)%10>=8 ? 2:1) + ones.get(1));
					case 1: if(j>=1)dp[j] = Math.max(dp[j],dp[j-1] + ones.get(0)*((j-1)%10==9 ? 2:1));
				}
				res = Math.max(res,dp[j]);
			}
			//System.out.println(Arrays.toString(Arrays.copyOf(dp,100)));
		}
		
		//for(int i = (cards-20>=0 ? cards-20 : 0);i<=cards;++i)
		//	res = Math.max(dp[i],res);
		System.out.println(res);


	}
}
