import java.io.*;
import java.util.StringTokenizer;

public class F {

    static final int MAX = 2 * 100_000;

    public static void main(String[] args) throws IOException {

        Scanner sc = new Scanner(System.in);
        PrintWriter out = new PrintWriter(System.out);

        int n = sc.nextInt(), nxt[] = new int[MAX], c[] = new int[MAX], d[] = new int[MAX];
        int m = 0;
        for(int i = 0; i < n; ++i) {
            int k = sc.nextInt();
            for(int j = 0; j < k; ++j) {
                c[m + j] = sc.nextInt();
                d[m + j] = sc.nextInt();
                nxt[m + j] = m + k;
            }
            m += k;
        }
        long[][][] dp = new long[m + 1][4][10];
        for(int i = m - 1; i >= 0; --i)
            for(int cost = 0; cost <= 3; ++cost)
                for(int mod = 0; mod < 10; ++mod) {
                    long take = 0;
                    // take
                    if (cost >= c[i])
                        take = dp[i + 1][nxt[i] == nxt[i + 1] ? cost - c[i] : 3][(mod + 1) % 10] + d[i] * (mod == 9 ? 2 : 1);
                    // leave
                    long leave = dp[i + 1][nxt[i] == nxt[i + 1] ? cost : 3][mod];

                    dp[i][cost][mod] = Math.max(take, leave);
                }
        out.println(dp[0][3][0]);
        out.close();
    }

    static class Scanner
    {
        StringTokenizer st;
        BufferedReader br;

        public Scanner(InputStream s){	br = new BufferedReader(new InputStreamReader(s));}

        public String next() throws IOException
        {
            while (st == null || !st.hasMoreTokens())
                st = new StringTokenizer(br.readLine());
            return st.nextToken();
        }

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

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

        public String nextLine() throws IOException {return br.readLine();}

        public boolean ready() throws IOException {return br.ready();}


    }
}