#include <bits/stdc++.h>
using namespace std;
const int mm = 2e5 + 5;
int n;
long long f[mm][15];
vector<int> c[4];
int tinh(int x, int y) {
  if (y - x > 0) return y - x;
  return y + 10 - x;
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i <= n; i++)
    for (int j = 1; j <= 9; j++) f[i][j] = -1;
  long long re = 0;
  for (int i = 1; i <= n; i++) {
    int k;
    scanf("%d", &k);
    for (int j = 1; j < 4; j++) c[j].clear();
    for (int j = 1; j <= k; j++) {
      int x, y;
      scanf("%d %d", &x, &y);
      c[x].push_back(y);
    }
    for (int j = 1; j < 4; j++) sort(c[j].begin(), c[j].end());
    for (int j = 1; j <= 10; j++) {
      int tam1 = tinh(1, j), tam2 = tinh(2, j), tam3 = tinh(3, j);
      f[i][j] = f[i - 1][j];
      if (c[3].size() && f[i - 1][tam1] + 1) {
        if (j == 10)
          f[i][j] = max(f[i][j],
                        f[i - 1][tam1] + (long long)2 * c[3][c[3].size() - 1]);
        else
          f[i][j] =
              max(f[i][j], f[i - 1][tam1] + (long long)c[3][c[3].size() - 1]);
      }
      if (c[2].size()) {
        if (c[1].size()) {
          if (f[i - 1][tam2] + 1) {
            if (j == 10)
              f[i][j] = max(f[i][j], f[i - 1][tam2] +
                                         (long long)c[2][c[2].size() - 1] +
                                         (long long)c[1][c[1].size() - 1] +
                                         max((long long)c[2][c[2].size() - 1],
                                             (long long)c[1][c[1].size() - 1]));
            else
              f[i][j] = max(f[i][j], f[i - 1][tam2] +
                                         (long long)c[2][c[2].size() - 1] +
                                         (long long)c[1][c[1].size() - 1]);
          }
        } else {
          if (f[i - 1][tam1] + 1) {
            if (j == 10)
              f[i][j] = max(f[i][j], f[i - 1][tam1] +
                                         (long long)2 * c[2][c[2].size() - 1]);
            else
              f[i][j] = max(f[i][j],
                            f[i - 1][tam1] + (long long)c[2][c[2].size() - 1]);
          }
        }
      }
      if (c[1].size() >= 3) {
        if (f[i - 1][tam3] + 1) {
          if (j == 10)
            f[i][j] = max(f[i][j], f[i - 1][tam3] +
                                       (long long)2 * c[1][c[1].size() - 1] +
                                       (long long)c[1][c[1].size() - 2] +
                                       (long long)c[1][c[1].size() - 3]);
          else
            f[i][j] =
                max(f[i][j], f[i - 1][tam3] + (long long)c[1][c[1].size() - 1] +
                                 (long long)c[1][c[1].size() - 2] +
                                 (long long)c[1][c[1].size() - 3]);
        }
      }
      if (c[1].size() >= 2) {
        if (f[i - 1][tam2] + 1) {
          if (j == 10)
            f[i][j] = max(f[i][j], f[i - 1][tam2] +
                                       (long long)2 * c[1][c[1].size() - 1] +
                                       (long long)c[1][c[1].size() - 2]);
          else
            f[i][j] =
                max(f[i][j], f[i - 1][tam2] + (long long)c[1][c[1].size() - 1] +
                                 (long long)c[1][c[1].size() - 2]);
        }
      }
      if (c[1].size()) {
        if (f[i - 1][tam1] + 1) {
          if (j == 10)
            f[i][j] = max(
                f[i][j], f[i - 1][tam1] + (long long)2 * c[1][c[1].size() - 1]);
          else
            f[i][j] =
                max(f[i][j], f[i - 1][tam1] + (long long)c[1][c[1].size() - 1]);
        }
      }
      re = max(re, f[i][j]);
    }
  }
  printf("%lld", re);
}
