import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.*;
import java.util.stream.*;

public class Main {
    static InputReader in = new InputReader(System.in);

    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder(), sbFront = new StringBuilder();

        n = in.nextInt();
        cost = new ArrayList<>();
        damage = new ArrayList<>();
        change = new boolean[3000000];
        for (int i = 0; i < n; i++) {
            int k = in.nextInt();
            for (int j = 0; j < k; j++) {
                cost.add(in.nextInt());
                damage.add(in.nextInt());
            }
            change[cost.size()] = true;
        }
        dp = new long[cost.size() + 1][10][4];
        for (int i = 0; i < dp.length; i++)
            for (int j = 0; j < dp[i].length; j++)
                Arrays.fill(dp[i][j], -1);

        sb.append(solve(0,1, 0));
        System.out.println(sb.toString().trim());
    }

    static int n;
    static long[][][] dp;
    static boolean change[];
    static ArrayList<Integer> cost, damage;

    static long solve(int i, int m, int c) {
        if (i >= cost.size())
            return 0;
        if (change[i])
            c = 0;
        if (dp[i][m % 10][c] > -1)
            return dp[i][m % 10][c];
        dp[i][m % 10][c] = solve(i+1, m, c);
        if (c + cost.get(i) <= 3)
            dp[i][m % 10][c] = Math.max(dp[i][m % 10][c],
                            (m % 10 == 0 ? 2 : 1) * damage.get(i) +
                            solve(i+1, m + 1, c + cost.get(i)));
        return dp[i][m % 10][c];
    }


    static class InputReader {
        private BufferedReader reader;
        private StringTokenizer tokenizer;

        InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream));
        }

        public String nextLine() {
            try {
                return reader.readLine();
            } catch (Exception e) {
                return tokenizer.nextToken();
            }
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (Exception e) {
                }
            }
            return tokenizer.nextToken();
        }

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

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

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

        public int[] nextIntArray(int n) {
            return IntStream.generate(() -> in.nextInt()).limit(n).toArray();
        }

        public double[] nextDoubleArray(int n) {
            return DoubleStream.generate(() -> in.nextDouble()).limit(n).toArray();
        }

        public long[] nextLongArray(int n) {
            return LongStream.generate(() -> in.nextLong()).limit(n).toArray();
        }

    }

}
