#include <bits/stdc++.h>
const long long md = 1e9 + 7;
const long long Inf = 1e9;
const long long Inf64 = 1e18;
const long long MaxN = 1e5 + 100;
const long long LogN = 20;
const long long MaxM = 5e5 + 10;
const double eps = 1e-3;
const int dx[8] = {1, -1, 2, 2, -1, 1, -2, -2};
const int dy[8] = {2, 2, 1, -1, -2, -2, 1, -1};
const int ddx[4] = {1, 1, -1, -1};
const int ddy[4] = {1, -1, 1, -1};
const double Pi = acos(-1);
using namespace std;
template <typename T>
istream &operator>>(istream &in, pair<T, T> &a) {
  in >> a.first >> a.second;
  return in;
}
template <typename T>
ostream &operator<<(ostream &out, pair<T, T> a) {
  out << a.first << ' ' << a.second;
  return out;
}
template <typename T>
istream &operator>>(istream &in, vector<T> &Mt) {
  for (T &a : Mt) in >> a;
  return in;
}
template <typename T>
ostream &operator<<(ostream &out, vector<T> Mt) {
  for (int i = 0; i < (int)Mt.size(); i++) {
    out << Mt[i];
    if (i != (int)Mt.size()) out << ' ';
  }
  out << '\n';
  return out;
}
inline long long Mod(long long &a, long long m = md) {
  while (a >= m) a -= m;
  return a;
}
inline long long gcd(long long a, long long b) {
  while (a) {
    b %= a;
    swap(a, b);
  }
  return b;
}
inline long long gcdex(long long a, long long mod = md) {
  long long g = mod, r = a, x = 0, y = 1;
  while (r != 0) {
    long long q = g / r;
    g %= r;
    swap(g, r);
    x -= q * y;
    swap(x, y);
  }
  return x < 0 ? x + mod : x;
}
inline long long binpow(long long a, long long n, long long m = md) {
  long long res = 1;
  while (n) {
    if (n & 1) {
      res *= a;
      res %= m;
    }
    a *= a;
    a %= m;
    n >>= 1;
  }
  return res % md;
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  {
    long long N;
    cin >> N;
    vector<vector<long long> > dp(N, vector<long long>(10, -Inf64));
    for (int i = 0; i < N; i++) {
      long long d;
      cin >> d;
      vector<vector<long long> > Mt(3, vector<long long>(3, -Inf64));
      for (int j = 0; j < d; j++) {
        long long c, t;
        cin >> c >> t;
        if (c == 1) {
          long long r = min({Mt[0][0], Mt[0][1], Mt[0][2], t});
          if (r != t) {
            for (int w = 0; w < 3; w++) {
              if (Mt[0][w] == r) {
                Mt[0][w] = t;
                break;
              }
            }
          }
        } else if (c == 2) {
          Mt[1][0] = max(Mt[1][0], t);
        } else {
          Mt[2][0] = max(Mt[2][0], t);
        }
      }
      if (i == 0) {
        dp[0][1] = max({Mt[0][0], Mt[0][1], Mt[0][2], Mt[1][0], Mt[2][0]});
        dp[0][2] =
            max({Mt[0][0] + Mt[0][1], Mt[0][1] + Mt[0][2], Mt[0][2] + Mt[0][0],
                 Mt[1][0] + max({Mt[0][0], Mt[0][1], Mt[0][2]})});
        dp[0][3] = Mt[0][0] + Mt[0][1] + Mt[0][2];
      } else {
        dp[i] = dp[i - 1];
        long long k =
            max({max({Mt[0][0], Mt[0][1], Mt[0][2]}), Mt[1][0], Mt[2][0]});
        long long Mx =
            max({max({Mt[0][0], Mt[0][1], Mt[0][2]}), Mt[1][0], Mt[2][0]});
        for (int j = 0; j <= 9; j++) {
          if (j + 1 <= 9) {
            dp[i][j + 1] = max(dp[i][j + 1], dp[i - 1][j] + k);
          } else {
            dp[i][0] = max(dp[i][0], k + Mx + dp[i - 1][j]);
          }
        }
        Mx = -Inf64;
        k = max({Mt[0][0] + Mt[0][1], Mt[0][1] + Mt[0][2], Mt[0][2] + Mt[0][0],
                 Mt[1][0] + max({Mt[0][0], Mt[0][1], Mt[0][2]})});
        if (Mt[0][0] + Mt[0][1] == k) Mx = max({Mx, Mt[0][0], Mt[0][1]});
        if (Mt[0][1] + Mt[0][2] == k) Mx = max({Mx, Mt[0][2], Mt[0][1]});
        if (Mt[0][2] + Mt[0][0] == k) Mx = max({Mx, Mt[0][0], Mt[0][2]});
        if (Mt[1][0] + max({Mt[0][0], Mt[0][1], Mt[0][2]}) == k)
          Mx = max({Mx, Mt[1][0], max({Mt[0][0], Mt[0][1], Mt[0][2]})});
        if (k >= 0) {
          for (int j = 0; j <= 9; j++) {
            if (j + 2 <= 9) {
              dp[i][j + 2] = max(dp[i][j + 2], dp[i - 1][j] + k);
            } else {
              dp[i][(j + 2) % 10] =
                  max(dp[i][(j + 2) % 10], k + Mx + dp[i - 1][j]);
            }
          }
        }
        Mx = max({Mt[0][0], Mt[0][1], Mt[0][2]});
        k = Mt[0][0] + Mt[0][1] + Mt[0][2];
        if (k >= 0) {
          for (int j = 0; j <= 9; j++) {
            if (j + 3 <= 9) {
              dp[i][j + 3] = max(dp[i][j + 3], dp[i - 1][j] + k);
            } else {
              dp[i][(j + 3) % 10] =
                  max(dp[i][(j + 3) % 10], k + Mx + dp[i - 1][j]);
            }
          }
        }
      }
    }
    long long Ans = 0;
    for (int i = 0; i < 10; i++) Ans = max(Ans, dp[N - 1][i]);
    cout << Ans;
  }
END:
  return 0;
}
