#include <bits/stdc++.h>
using namespace std;
long long dp[2][4][10];
void init();
void work();
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
  work();
}
void work() {
  int n;
  cin >> n;
  memset(dp, 0xcf, sizeof(dp));
  dp[0][0][0] = 0;
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    int now = i & 1, pre = now ^ 1;
    memset(dp[now], 0xcf, sizeof(dp[now]));
    for (int j = 0; j <= 9; j++) {
      long long Max = 0;
      for (int x = 0; x <= 3; x++) Max = max(Max, dp[pre][x][j]);
      dp[now][0][j] = Max;
    }
    int k;
    cin >> k;
    int c, d;
    for (int j = 1; j <= k; j++) {
      cin >> c >> d;
      for (int cc = 3; cc >= c; cc--) {
        for (int ct = 0; ct <= 9; ct++) {
          int dd = ct == 9 ? 2 * d : d;
          dp[now][cc][(ct + 1) % 10] =
              max(dp[now][cc][(ct + 1) % 10], dp[now][cc - c][ct] + dd);
        }
      }
    }
    for (int j = 0; j <= 3; j++) {
      for (int ct = 0; ct <= 9; ct++) ans = max(ans, dp[now][j][ct]);
    }
  }
  cout << ans;
}
