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

	public static void main(String[] args) {
		FastScanner in = new FastScanner(System.in);
		
		int N = in.nextInt();
		int[][] maxCards = new int[N][5];
		
		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);
			}
		}
		
		long[] damage1 = new long[3*N+1];
		long[] damage2 = new long[3*N+1];
		
		for(int i = 0; i < N; i++) {
			
			long[][] arr = new long[4][2];
			arr[0] = new long[] {0, max1(maxCards[i]), max2Sum(maxCards[i]), max3Sum(maxCards[i])};
			arr[1] = new long[] {0, max1B(maxCards[i]), max2SumB(maxCards[i]), max3SumB(maxCards[i])};
			for(int j = 0; j <= 3*i; j++) {
				
				damage2[j] = Math.max(damage1[j], damage2[j]);
				
				
				if((j+1) % 10 != 0) {
					if(arr[0][1] != -1) {
						damage2[j+1] = Math.max(damage1[j] + arr[0][1], damage2[j+1]);
					}
					else if(damage2[j+1] == 0){
						damage2[j+1] = -1;
					}
				}
				else {
					if(arr[1][1] != -1)
						damage2[j+1] = Math.max(damage1[j] + arr[1][1], damage2[j+1]);
					else if(damage2[j+1] == 0){
						damage2[j+1] = -1;
					}
				}
				
				if((j+2) % 10 != 0) {
					if(arr[0][2] != -1)
						damage2[j+2] = Math.max(damage1[j] + arr[0][2], damage2[j+2]);
					else if(damage2[j+2] == 0){
						damage2[j+2] = -1;
					}
				}
				else {
					if(arr[1][2] != -1)
						damage2[j+2] = Math.max(damage1[j] + arr[1][2], damage2[j+2]);
					else if(damage2[j+2] == 0)
						damage2[j+2] = -1;
				}
				
				if((j+3) % 10 != 0) {
					if(arr[0][3] != -1)
						damage2[j+3] = Math.max(damage1[j] + arr[0][3], damage2[j+3]);
					else if(damage2[j+3] == 0)
						damage2[j+3] = -1;
				}
				else {
					if(arr[1][3] != -1)
						damage2[j+3] = Math.max(damage1[j] + arr[1][3], damage2[j+3]);
					else if(damage2[j+3] == 0)
						damage2[j+3] = -1;
				}
			}
			
			long[] temp = damage1;
			damage1 = damage2;
			damage2 = temp;
		}
		
		long max = max(damage1);
		System.out.println(max);
	}
	
	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) {
		if(a[0] == 0 || a[1] == 0 || a[2] == 0) {
			return -1;
		}
		
		return ((long) a[0]) + a[1] + a[2];
	}
	
	public static long max3SumB(int[] a) {
		long sum = a[2];
		sum *= 2;
		sum += a[0] + a[1];
		
		if(a[0] == 0 || a[1] == 0 || a[2] == 0) {
			return -1;
		}
		
		return sum;
	}
	
	public static long max2Sum(int[] a) {
		long ans = a[2];
		ans += Math.max(a[1], a[3]);
		if(a[2] == 0 || Math.max(a[1], a[3]) == 0) {
			return -1;
		}
		
		return ans;
	}
	
	public static long max2SumB(int[] a) {
		long[] b = {a[1], a[2], a[3]};
		Arrays.sort(b);
		
		if(b[2] == 0 || b[1] == 0)
			return -1;
		
		return 2*b[2] + b[1];
	}
	
	public static long max(long[] a) {
		long max = 0;
		for(long i : a)
			max = Math.max(i, max);
		
		return max;
	}
	
	public static long max1(int[] a) {
		long max = 0;
		for(int i : a)
			max = Math.max(i, max);
		
		return max;
	}
	
	public static long max1B(int[] a) {
		long max = 0;
		for(int i : a)
			max = Math.max(i, max);
		
		return 2 * 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



15
5
1 5
3 10
2 42
2 73
1 5
5
1 38
2 39
3 33
3 33
1 80
5
3 39
1 95
1 18
1 15
2 91
5
3 43
1 85
3 14
3 23
2 80
5
3 3
1 92
3 20
3 29
2 81
5
2 73
1 88
3 27
1 100
1 75
5
2 91
1 77
1 16
3 81
3 87
5
3 14
2 28
3 74
3 96
1 16
5
1 28
1 42
1 3
1 95
2 29
5
3 18
3 78
1 97
1 43
1 84
5
2 6
2 31
3 37
3 7
3 75
5
1 34
1 2
1 6
3 54
3 58
5
3 21
2 89
3 41
2 97
2 1
5
2 29
1 76
1 44
3 29
2 56
5
3 61
3 47
3 4
1 66
1 16

2588
2282

4
4
1 1
1 1
1 1
2 100
4
1 1
1 1
1 1
2 100
4
1 1
1 1
1 1
2 100
1
2 1000

2009
*/