#include <bits/stdc++.h>
using namespace std;
int c[200000], d[200000], nxt[200000];
int k = 0;
long long dp[200000][4][10];
long long rek(int cur, int mana, int hits) {
  if (cur == k) return 0;
  if (dp[cur][mana][hits] == -1) {
    dp[cur][mana][hits] = rek(cur + 1, (cur + 1 == nxt[cur]) ? 3 : mana, hits);
    if (c[cur] <= mana)
      dp[cur][mana][hits] =
          max(dp[cur][mana][hits],
              ((hits == 9) ? 2 : 1) * d[cur] +
                  rek(cur + 1, (cur + 1 == nxt[cur]) ? 3 : (mana - c[cur]),
                      (hits + 1) % 10));
  }
  return dp[cur][mana][hits];
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  memset(dp, -1, sizeof(dp));
  int n, t;
  cin >> n;
  for (int ctr1 = 0; ctr1 < n; ++ctr1) {
    cin >> t;
    for (int ctr2 = 0; ctr2 < t; ++ctr2)
      cin >> c[k + ctr2] >> d[k + ctr2], nxt[k + ctr2] = k + t;
    k += t;
  }
  cout << rek(0, 3, 0);
  return 0;
}
