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.util.ArrayList;
import java.io.InputStream;

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

    static class FDestroyIt {
        public void solve(int testNumber, inputClass sc, PrintWriter out) {
            int n = sc.nextInt();
            long ans = 0;
            int maxk = 0;
            ArrayList<ArrayList<FDestroyIt.Card>> cards = new ArrayList<>();
            ArrayList<FDestroyIt.Card> act;
            for (int i = 0; i < n; i++) {
                int k = sc.nextInt();
                maxk = Math.max(maxk, k);
                cards.add(new ArrayList<>());
                int x, y;
                act = cards.get(i);
                for (int j = 0; j < k; j++) {
                    x = sc.nextInt();
                    y = sc.nextInt();
                    act.add(new FDestroyIt.Card(x, y));
                }
            }
            long[][][][] dp = new long[2][maxk + 1][4][10];
            boolean[][][][] go = new boolean[2][maxk + 1][4][10];
            go[0][0][0][0] = true;
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < cards.get(i).size(); j++) {
                    for (int k = 0; k < 4; k++) {
                        for (int l = 0; l < 10; l++) {
                            if (go[0][j][k][l]) {
                                dp[1][0][0][l] = Math.max(dp[1][0][0][l], dp[0][j][k][l]);
                                go[1][0][0][l] = true;
                                dp[0][j + 1][k][l] = Math.max(dp[0][j + 1][k][l], dp[0][j][k][l]);
                                go[0][j + 1][k][l] = true;
                                if (k + cards.get(i).get(j).cost <= 3) {
                                    go[0][j + 1][k + cards.get(i).get(j).cost][(l + 1) % 10] = true;
                                    if (l != 9) {
                                        dp[0][j + 1][k + cards.get(i).get(j).cost][(l + 1) % 10] = Math.max(dp[0][j + 1][k + cards.get(i).get(j).cost][(l + 1) % 10], dp[0][j][k][l] + cards.get(i).get(j).dmg);
                                    } else {
                                        dp[0][j + 1][k + cards.get(i).get(j).cost][(l + 1) % 10] = Math.max(dp[0][j + 1][k + cards.get(i).get(j).cost][(l + 1) % 10], dp[0][j][k][l] + 2 * cards.get(i).get(j).dmg);
                                    }
                                    go[1][0][0][(l + 1) % 10] = true;
                                    if (l != 9) {
                                        dp[1][0][0][(l + 1) % 10] = Math.max(dp[1][0][0][(l + 1) % 10], dp[0][j][k][l] + cards.get(i).get(j).dmg);
                                    } else {
                                        dp[1][0][0][(l + 1) % 10] = Math.max(dp[1][0][0][(l + 1) % 10], dp[0][j][k][l] + 2 * cards.get(i).get(j).dmg);
                                    }
                                }
                            }
                        }
                    }
                }
                for (int j = 0; j <= maxk; j++) {
                    for (int k = 0; k < 4; k++) {
                        for (int l = 0; l < 10; l++) {
                            dp[0][j][k][l] = dp[1][j][k][l];
                            go[0][j][k][l] = go[1][j][k][l];
                            dp[1][j][k][l] = 0;
                            go[1][j][k][l] = false;
                        }
                    }
                }
            }
            for (int i = 0; i < maxk + 1; i++) {
                for (int j = 0; j < 4; j++) {
                    for (int k = 0; k < 10; k++) {
                        ans = Math.max(ans, dp[0][i][j][k]);
                    }
                }
            }
            out.println(ans);
        }

        static class Card {
            int cost;
            long dmg;

            public Card(int a, long b) {
                cost = a;
                dmg = b;
            }

        }

    }

    static class inputClass {
        BufferedReader br;
        StringTokenizer st;

        public inputClass(InputStream in) {

            br = new BufferedReader(new InputStreamReader(in));
        }

        public String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

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

    }
}

