#include <bits/stdc++.h>
using namespace std;
template <class A, class B>
ostream& operator<<(ostream& out, const pair<A, B>& a) {
  return out << "(" << a.first << ", " << a.second << ")";
}
template <class A>
ostream& operator<<(ostream& out, const vector<A>& v) {
  out << "[";
  for (int i = 0; i < int(int(v.size())); i++) {
    if (i) out << ", ";
    out << v[i];
  }
  return out << "]";
}
mt19937 rnd(time(NULL));
const int INF = int(1e9);
const long long INF64 = long long(1e18);
const int MOD = INF + 7;
const long double EPS = 1e-9;
const long double PI = acos(-1.0);
const int N = 200 * 1000 + 13;
int n;
vector<int> a[N][3];
bool read() {
  if (scanf("%d", &n) != 1) return false;
  for (int i = 0; i < int(n); i++) {
    int k;
    scanf("%d", &k);
    for (int j = 0; j < int(3); j++) a[i][j].clear();
    for (int j = 0; j < int(k); j++) {
      int first, second;
      scanf("%d%d", &first, &second);
      a[i][first - 1].push_back(second);
    }
  }
  return true;
}
long long dp[2][10];
void solve() {
  for (int i = 0; i < int(n); i++)
    for (int j = 0; j < int(3); j++) {
      sort(a[i][j].begin(), a[i][j].end(), greater<int>());
      a[i][j].resize(min(int(a[i][j].size()), j == 0 ? 3 : 1));
    }
  for (int i = 0; i < int(10); i++) dp[0][i] = (i == 0 ? 0 : -INF);
  for (int ii = 0; ii < int(n); ii++) {
    int i = ii & 1;
    int ni = i ^ 1;
    for (int j = 0; j < int(10); j++) dp[ni][j] = -INF;
    vector<pair<int, int> > cards;
    for (int j = 0; j < int(3); j++)
      for (auto it : a[ii][j]) cards.push_back(make_pair(it, j + 1));
    for (int j = 0; j < int(10); j++)
      if (dp[i][j] >= 0) {
        for (int mask = 0; mask < int(1 << int(cards.size())); mask++) {
          int sum = 0;
          int mx = 0;
          int add = 0;
          int cnt = 0;
          for (int k = 0; k < int(int(cards.size())); k++)
            if ((mask >> k) & 1) {
              sum += cards[k].second;
              mx = max(mx, cards[k].first);
              add += cards[k].first;
              ++cnt;
            }
          if (sum > 3) continue;
          if (j + cnt >= 10) add += mx;
          dp[ni][(j + cnt) % 10] = max(dp[ni][(j + cnt) % 10], dp[i][j] + add);
        }
      }
  }
  printf("%lld\n", *max_element(dp[n & 1], dp[n & 1] + 10));
}
int main() {
  cerr.precision(15);
  cout.precision(15);
  cerr << fixed;
  cout << fixed;
  while (read()) {
    solve();
  }
}
