#include <bits/stdc++.h>
using namespace std;
long long int max(long long int a, long long int b) {
  if (a > b)
    return a;
  else
    return b;
}
long long int min(long long int a, long long int b) {
  if (a < b)
    return a;
  else
    return b;
}
const int dx[4] = {-1, 1, 0, 0};
const int dy[4] = {0, 0, -1, 1};
int XX[] = {-1, -1, -1, 0, 0, 1, 1, 1};
int YY[] = {-1, 0, 1, -1, 1, -1, 0, 1};
vector<pair<long long int, long long int> > vec, vec2;
long long int c, d;
long long int dp[500005][5][12];
long long int func(long long int ind, long long int c1, long long int c2) {
  if (ind >= vec.size()) return 0;
  if (dp[ind][c1][c2] != -1) return dp[ind][c1][c2];
  long long int count = -9000000000000000000;
  if (vec[ind].first == 0) {
    count = max(count, func(ind + 1, 0, c2));
  } else {
    count = max(count, func(ind + 1, c1, c2));
    if (c1 + vec[ind].first <= 3) {
      if (c2 == 9) {
        count = max(
            count, vec[ind].second * 2 + func(ind + 1, c1 + vec[ind].first, 0));
      } else {
        count = max(count, vec[ind].second +
                               func(ind + 1, c1 + vec[ind].first, c2 + 1));
      }
    }
  }
  return dp[ind][c1][c2] = count;
}
int main() {
  memset(dp, -1, sizeof(dp));
  ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  long long int t, n, i, j, k;
  cin >> n;
  for (i = 0; i < n; i++) {
    vec2.clear();
    cin >> k;
    for (j = 0; j < k; j++) {
      cin >> c >> d;
      vec2.push_back(make_pair(d, c));
    }
    sort(vec2.begin(), vec2.end());
    for (j = 0; j < k; j++) {
      vec.push_back(make_pair(vec2[j].second, vec2[j].first));
    }
    vec.push_back(make_pair(0, 0));
  }
  long long int ans = func(0, 0, 0);
  cout << ans;
}
