#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<vector<pair<long long, int>>> info(n);
  for (int i = 0; i < n; i++) {
    int l;
    cin >> l;
    info[i].resize(l);
    for (int j = 0; j < l; j++) cin >> info[i][j].second >> info[i][j].first;
    sort(info[i].begin(), info[i].end());
  }
  vector<vector<vector<long long>>> a, b;
  for (int i = n - 1; i >= 0; i--) {
    a.assign(info[i].size(),
             vector<vector<long long>>(10, vector<long long>(4)));
    for (int j = info[i].size() - 1; j >= 0; j--)
      for (int mod = 0; mod < 10; mod++)
        for (int c = 0; c < 4; c++) {
          long long gain = 0;
          bool valid = c >= info[i][j].second;
          if (valid) gain = info[i][j].first;
          if (mod == 9) gain *= 2;
          a[j][mod][c] = gain;
          if (j < info[i].size() - 1) {
            a[j][mod][c] =
                max({a[j + 1][mod][c],
                     !valid ? 0
                            : (a[j + 1][(mod + 1) % 10][c - info[i][j].second] +
                               gain)});
          } else if (i < n - 1) {
            a[j][mod][c] = max(
                {b[0][mod][3], !valid ? 0 : (b[0][(mod + 1) % 10][3] + gain)});
          }
        }
    swap(b, a);
  }
  cout << b[0][0][3] << endl;
  return 0;
}
