import java.util.*;
import java.io.*;
public class F565
{
    public static void main(String [] args)
    {
        MyScanner sc = new MyScanner();
        PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
        int n = sc.nextInt();
        long [][][] dp = new long [n+1][10][4];
        for (long [][] arr: dp) {
            for (long [] arr2: arr) {
                Arrays.fill(arr2, Long.MIN_VALUE / 2);
            }
        }
        dp[0][0][0] = 0;
        ArrayList<Long> [] bestOnes = new ArrayList[n+1];
        for (int i = 1; i <= n; i++) bestOnes[i] = new ArrayList<>();
        long [] bestTwo = new long[n+1]; long [] bestThree = new long[n+1];
        for (int i = 1; i <= n; i++) {
            int k = sc.nextInt();
            PriorityQueue<Long> pq = new PriorityQueue<>(Comparator.reverseOrder());
            long two = Long.MIN_VALUE / 2; long three = Long.MIN_VALUE / 2;
            for (int j = 0; j < k; j++) {
                int c = sc.nextInt(); long d = sc.nextLong();
                if (c == 1) {
                    pq.add(d);
                    if (pq.size() > 3) pq.poll();
                } else if (c == 2) {
                    two = Math.max(two, d);
                } else three = Math.max(three, d);
            }
            ArrayList<Long> one = new ArrayList<>();
            while (!pq.isEmpty()) {
                one.add(pq.poll());
            }
            bestOnes[i] = one;
            bestTwo[i] = two;
            bestThree[i] = three;
        }
        long ans = 0;
        for (int i = 1; i <= n; i++) {
            for (int j = 0; j < 10; j++) {
                for (int k = 0; k < 4; k++) {
                    if (i <= 3 && j > 3 * i) continue;
                    if (k < 3 && j != k) continue;
                    int one = (j - 1) % 10; if (one < 0) one += 10;
                    int two = (j - 2) % 10; if (two < 0) two += 10;
                    int three = (j - 3) % 10; if (three < 0) three += 10;

                    long oneCard = Long.MIN_VALUE / 2; long oneCardBonus = Long.MIN_VALUE / 2;
                    oneCard = Math.max(Math.max(oneCard, bestTwo[i]), bestThree[i]);
                    if (bestOnes[i].size() > 0) oneCard = Math.max(oneCard, bestOnes[i].get(0));
                    if (j == 0 && bestOnes[i].size() > 0) oneCardBonus = oneCard + bestOnes[i].get(0); else oneCardBonus = oneCard;

                    long twoCard = Long.MIN_VALUE / 2; long twoCardBonus = Long.MIN_VALUE / 2; boolean twoOnes = true;
                    if (bestOnes[i].size() >= 2) {
                        twoCard = Math.max(twoCard, bestOnes[i].get(0) + bestOnes[i].get(1));
                    }
                    if (bestOnes[i].size() > 0 && bestOnes[i].get(0) + bestTwo[i] > twoCard) {
                        twoCard = bestOnes[i].get(0) + bestTwo[i]; twoOnes = false;
                    }
                    if (j < 2 && bestOnes[i].size() > 0) {
                        if (twoOnes) twoCardBonus = twoCard + bestOnes[i].get(0); else twoCardBonus = twoCard + Math.max(bestOnes[i].get(0), bestTwo[i]);
                    } else twoCardBonus = twoCard;

                    long threeCard = Long.MIN_VALUE / 2; long threeCardBonus = Long.MIN_VALUE / 2;
                    if (bestOnes[i].size() >= 3) {
                        threeCard = Math.max(threeCard, bestOnes[i].get(0) + bestOnes[i].get(1) + bestOnes[i].get(2));
                        if (j < 3) threeCardBonus = threeCard + bestOnes[i].get(0); else threeCardBonus = threeCard;
                    }

                    if (k == 0) {
                        dp[i][j][k] = dp[i-1][j][k];
                    } else if (k == 1) {
                        dp[i][j][k] = Math.max(dp[i-1][j][k], dp[i-1][one][k-1] + oneCard);
                    } else if (k == 2) {
                        dp[i][j][k] = Math.max(Math.max(dp[i-1][j][k], dp[i-1][one][k-1] + oneCard), dp[i-1][two][k-2] + twoCard);
                    } else {
                        dp[i][j][k] = Math.max(Math.max(dp[i-1][j][k], Math.max(dp[i-1][one][k-1] + oneCard, dp[i-1][one][3] + oneCardBonus)),
                                Math.max(dp[i-1][two][k-2] + twoCard, dp[i-1][two][3] + twoCardBonus));
                        dp[i][j][k] = Math.max(dp[i][j][k], Math.max(dp[i-1][three][k-3] + threeCard, dp[i-1][three][3] + threeCardBonus));
                    }
                    //System.out.println(i + " " + j + " " + k + " " + dp[i][j][k]);
                    if (i == n) ans = Math.max(dp[i][j][k], ans);
                }
            }
        }
        out.println(ans);
        out.close();
    }




    //-----------MyScanner class for faster input----------
    public static class MyScanner {
        BufferedReader br;
        StringTokenizer st;

        public MyScanner() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

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

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

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

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

        String nextLine(){
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }



    }

}