import java.util.*;

public class destroyyyyy {
	public static void main(String[] args) {
		Scanner scan=new Scanner(System.in);
		int t=scan.nextInt();
		c=new ArrayList<>();
		d=new ArrayList<>();
		r=new ArrayList<>();
		ct=0;
		for(int tt=0;tt<t;tt++) {
			int n=scan.nextInt();
			ct+=n;
			r.add(ct);
			for(int i=0;i<n;i++) {
				c.add(scan.nextInt());
				d.add(scan.nextInt());
			}
		}
//		System.out.println(r);
		dp=new long[4][10][ct];
		for(long[][] d:dp) for(long[] dd:d) Arrays.fill(dd,-1);
		System.out.println(go(0,0,0,0));
	}
	static int ct;
	public static long go(int at, int cost, int cycle, int ptr) {
//		System.out.println(at+" "+cost+" "+cycle);
		if(at==ct) return 0L;
		if(dp[cost][cycle][at]!=-1) return dp[cost][cycle][at];
		
		long res=0L;
		
		long add=d.get(at);
		if(cycle==9) add*=2;
		boolean reset=ptr<r.size()&&r.get(ptr)==at+1;
		if(reset) ptr++;
		
		if(cost+c.get(at)<=3) {
			res=Math.max(res,add+go(at+1,reset?0:cost+c.get(at),(cycle+1)%10,ptr));
		}
		res=Math.max(res,go(at+1,reset?0:cost,cycle,ptr));
		return dp[cost][cycle][at]=res;
	}
	static long[][][] dp;
	static ArrayList<Integer> c,d,r;
}