import java.io.IOException;
import java.io.InputStream;
import java.util.InputMismatchException;
public class F565 {

	public static void main(String[] args) {
		FastScanner in = new FastScanner(System.in);
		
		int N = in.nextInt();
		int[][] maxCards = new int[N][5];
		long[][][] dp = new long[N+1][10][4];
		int[][][] minCards = new int[N+1][10][4];
		
		for(int i = 1; i < N+1; i++)
			for(int j = 0; j < 10; j++)
				for(int k = 0; k < 4; k++)
					minCards[i][j][k] = Integer.MAX_VALUE;
		
		
		for(int i = 0; i < N; i++) {
			int numCards = in.nextInt();
			for(int j = 0; j < numCards; j++) {
				int cost = in.nextInt();
				int damage = in.nextInt();
				update(maxCards, i, cost, damage);
			}
		}
		
	//	System.out.println("maxCards:");
	//	print(maxCards);
		
		for(int i = 1; i < dp.length; i++) {
			for(int j = 0; j < 10; j++) {
				for(int k = 0; k < 4; k++) {
			/*		long[] temp = new long[4];
					int[] tempMinCards = new int[4];
					for(int m = 0; m < 4; m++) {
						tempMinCards[m] = minCards[i-1][m][(k+10-m) % 10] + m;
					}
					
					int[] multipliers = new int[4];
					for(int m = 0; m < 4; m++) {
						multipliers[m] = (tempMinCards[m] % 10 == 0) ? 2 : 1;
					}
					
					temp[0] = dp[i-1][0][k];
					temp[1] = dp[i-1][1][(k+9) % 10] + multipliers[1] * max1(maxCards[i-1]);
					temp[2] = dp[i-1][2][(k+8) % 10] + multipliers[2] * max2Sum(maxCards[i-1]);
					temp[3] = dp[i-1][3][(k+7) % 10] + multipliers[3] * max3Sum(maxCards[i-1]);
					
					for(int m = 0; m < 4; m++) {
						if(temp[m] > dp[i][j][k]) {
							dp[i][j][k] = temp[m];
							minCards[i][j][k] = m + minCards[i-1][m][(k+10-m) % 10];
						}
					} */
					
					long[] temp = new long[4];
					long[] addOn = new long[4];
					int[] tempMinCards = new int[4];
					int[] multipliers = new int[4];
					for(int m = 0; m < 4; m++) {
						tempMinCards[m] = m + minCards[i-1][(j+10-k) % 10][k];
						multipliers[m] = (tempMinCards[m] % 10 == 0) ? 2 : 1;
					}
					
					temp[0] = dp[i-1][j][0];
					temp[1] = dp[i-1][(j+9) % 10][1];
					temp[2] = dp[i-1][(j+8) % 10][2];
					temp[3] = dp[i-1][(j+7) % 10][3];
					
					addOn[0] = 0;
					addOn[1] = multipliers[1] * max1(maxCards[i-1]);
					addOn[2] = multipliers[2] * max2Sum(maxCards[i-1]);
					addOn[3] = multipliers[3] * max3Sum(maxCards[i-1]);
					
					long max = -1;
					for(int m = 0; m < 4; m++) {
						if(temp[m] > max) {
							max = temp[m];
						}
					}
					
					dp[i][j][k] = max + addOn[k];
					minCards[i][j][k] = tempMinCards[k];
				}
			}
		}
		
	//	System.out.println("dp:");
	//	print(dp);
		
	//	System.out.println();
	//	System.out.println("minCards:");
	//	print(minCards);
		
		long ans = 0;
		for(int j = 0; j < dp[0].length; j++) {
			for(int k = 0; k < dp[0][j].length; k++) {
				ans = Math.max(dp[N][j][k], ans);
			}
		}
		
		System.out.println(ans);
	}
	
	public static void update(int[][] maxCards, int index, int cost, int damage) {
		if(cost == 1) {
			if(damage > maxCards[index][0]) {
				maxCards[index][0] = damage;
				if(damage > maxCards[index][1]) {
					maxCards[index][0] = maxCards[index][1];
					maxCards[index][1] = damage;
					if(damage > maxCards[index][2]) {
						maxCards[index][1] = maxCards[index][2];
						maxCards[index][2] = damage;
					}
				}
			}
		}
		else if(cost == 2) {
			if(damage > maxCards[index][3]) {
				maxCards[index][3] = damage;
			}
		}
		else {
			if(damage > maxCards[index][4]) {
				maxCards[index][4] = damage;
			}
		}
	}
	
	public static long max3Sum(int[] a) {
		return ((long) a[0]) + a[1] + a[2];
	}
	
	public static long max2Sum(int[] a) {
		return a[2] + Math.max(a[1], a[3]);
	}
	
	public static long max1(int[] a) {
		long max = 0;
		for(int i : a)
			max = Math.max(i, max);
		
		return max;
	}
	
	public static void print(int[][] a) {
		for(int i = 0; i < a.length; i++) {
			for(int j = 0; j < a[i].length; j++) {
				System.out.print(a[i][j] + " ");
			}
			System.out.println();
		}
		System.out.println();
	}
	
	public static void print(int[][][] a) {
		for(int i = 0; i < a.length; i++) {
			System.out.println("i = " + i);
			for(int j = 0; j < a[i].length; j++) {
				for(int k = 0; k < a[i][j].length; k++) {
					System.out.print(a[i][j][k] + " ");
				}
				System.out.println();
			}
			System.out.println();
		}
		System.out.println();
	}
	
	public static void print(long[][][] a) {
		for(int i = 0; i < a.length; i++) {
			System.out.println("i = " + i);
			for(int j = 0; j < a[i].length; j++) {
				for(int k = 0; k < a[i][j].length; k++) {
					System.out.print(a[i][j][k] + " ");
				}
				System.out.println();
			}
			System.out.println();
		}
		System.out.println();
	}
	
	/**
	 * Source: Matt Fontaine
	 */
	static class FastScanner {
		private InputStream stream;
		private byte[] buf = new byte[1024];
		private int curChar;
		private int chars;

		public FastScanner(InputStream stream) {
			this.stream = stream;
		}

		int read() {
			if (chars == -1)
				throw new InputMismatchException();
			if (curChar >= chars) {
				curChar = 0;
				try {
					chars = stream.read(buf);
				} catch (IOException e) {
					throw new InputMismatchException();
				}
				if (chars <= 0)
					return -1;
			}
			return buf[curChar++];
		}

		boolean isSpaceChar(int c) {
			return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
		}

		boolean isEndline(int c) {
			return c == '\n' || c == '\r' || c == -1;
		}

		public int nextInt() {
			return Integer.parseInt(next());
		}

		public long nextLong() {
			return Long.parseLong(next());
		}

		public double nextDouble() {
			return Double.parseDouble(next());
		}

		public String next() {
			int c = read();
			while (isSpaceChar(c))
				c = read();
			StringBuilder res = new StringBuilder();
			do {
				res.appendCodePoint(c);
				c = read();
			} while (!isSpaceChar(c));
			return res.toString();
		}

		public String nextLine() {
			int c = read();
			while (isEndline(c))
				c = read();
			StringBuilder res = new StringBuilder();
			do {
				res.appendCodePoint(c);
				c = read();
			} while (!isEndline(c));
			return res.toString();
		}
	}

}
/*
5
3
1 6
1 7
1 5
2
1 4
1 3
3
1 10
3 5
2 3
3
1 15
2 4
1 10
1
1 100
outputCopy
263
*/