import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author anand.oza
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        FDestroyIt solver = new FDestroyIt();
        solver.solve(1, in, out);
        out.close();
    }

    static class FDestroyIt {
        static final int ten = 10;
        static final int maxCost = 3;
        static final int two = 2;

        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int n = in.nextInt();

            long[][] dp = new long[n + 1][ten]; // dp[i][j] is the max damage after i turns with j cards (mod 10)
            for (int i = 0; i <= n; i++) {
                for (int j = 0; j < ten; j++) {
                    dp[i][j] = Integer.MIN_VALUE;
                }
            }
            dp[0][0] = 0;
            for (int i = 1; i <= n; i++) {
                int k = in.nextInt();
                int[] c = new int[k];
                long[] d = new long[k];
                long[][][] turn = new long[k + 1][4][4]; // turn[cards considered][cost][cards used]
                for (int j = 0; j < k; j++) {
                    c[j] = in.nextInt();
                    d[j] = in.nextInt();
                }

                for (int cost = 0; cost <= maxCost; cost++) {
                    for (int used = 0; used <= maxCost; used++) {
                        turn[0][cost][used] = Long.MIN_VALUE;
                    }
                }
                turn[0][0][0] = 0;
                for (int previousTurnCards = 0; previousTurnCards < ten; previousTurnCards++) {
                    for (int avail = 1; avail <= k; avail++) {
                        int cost = c[avail - 1];
                        long damage = d[avail - 1];

                        for (int used = 0; used <= maxCost; used++) {
                            for (int prevCost = 0; prevCost <= maxCost; prevCost++) {
                                if (used > 0 && prevCost + cost <= maxCost) {
                                    long d2 = damage;
                                    if (used + previousTurnCards == 10) {
                                        d2 *= two;
                                    }
                                    turn[avail][prevCost + cost][used] = Math.max(turn[avail][prevCost + cost][used], d2 + turn[avail - 1][prevCost][used - 1]);
                                }
                                turn[avail][prevCost][used] = Math.max(turn[avail][prevCost][used], turn[avail - 1][prevCost][used]);
                            }
                        }
                    }
                    for (int thisTurnCards = 0; thisTurnCards <= maxCost; thisTurnCards++) {
                        long damage = 0;
                        for (int cost = 0; cost <= maxCost; cost++) {
                            damage = Math.max(damage, turn[k][cost][thisTurnCards]);
                        }
                        int totalCards = previousTurnCards + thisTurnCards;
                        totalCards %= ten;
                        dp[i][totalCards] = Math.max(dp[i][totalCards], dp[i - 1][previousTurnCards] + damage);
                    }
                }
            }

            long answer = 0;
            for (int j = 0; j < ten; j++) {
                answer = Math.max(answer, dp[n][j]);
            }

//        for (int i = 0; i <= n; i++) {
//            for (int j = 0; j < ten; j++) {
//                if (dp[i][j] < 0) {
//                    System.out.print("X\t");
//                } else System.out.print(dp[i][j] + "\t");
//            }
//            System.out.println();
//        }

            out.println(answer);
        }

    }

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

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

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

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

    }
}

