#include <bits/stdc++.h>
using namespace std;
int dX[] = {0, 0, 1, -1};
int dY[] = {1, -1, 0, 0};
string alpha = "abcdefghijklmnopqrstuvwxyz";
const int MOD = 1000000007;
const int N = 200100;
vector<pair<int, int> > choices[N];
int n, k, c, d;
long long dp[N][12];
long long solve(int idx, int cnt) {
  if (idx == n) return 0;
  if (~dp[idx][cnt]) return dp[idx][cnt];
  vector<int> per;
  for (int i = 0; i < (choices[idx].size()); ++i) per.push_back(i);
  do {
    int cst = 0, ncnt = cnt, mx = 0;
    long long tot = 0;
    for (int i = 0; i < min(3, (int)(per.size())); ++i) {
      pair<int, int> tmp = choices[idx][per[i]];
      cst += tmp.first;
      if (cst > 3) break;
      ncnt++;
      mx = max(tmp.second, mx);
      tot += tmp.second;
      if (ncnt >= 10) tot += mx, ncnt = 0;
      dp[idx][cnt] = max(dp[idx][cnt], solve(idx + 1, ncnt) + tot);
    }
  } while (next_permutation(per.begin(), per.end()));
  return dp[idx][cnt];
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &k);
    priority_queue<pair<int, int> > pq;
    for (int j = 0; j < k; ++j)
      scanf("%d", &c), scanf("%d", &d), pq.push({c, d});
    for (int j = 3; j; --j) {
      int cnt = 1;
      if (j == 1) cnt = 3;
      while (!pq.empty() && pq.top().first == j && cnt--)
        choices[i].push_back(pq.top()), pq.pop();
      while (!pq.empty() && pq.top().first == j) pq.pop();
    }
  }
  memset(dp, -1, sizeof(dp));
  long long ans = solve(0, 0);
  printf("%lld\n", ans);
  return 0;
}
