#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
using namespace std;
const int N = (int)2e5 + 5;
const int INF = (int)1e9 + 7;
long long dp[N][11];
void solve() {
  int n;
  scanf("%d", &n);
  memset(dp, -1, sizeof dp);
  dp[0][0] = 0;
  for (int xod = 1; xod <= n; xod++) {
    int k;
    scanf("%d", &k);
    vector<long long> best[4];
    for (int i = 1; i <= k; i++) {
      int cost, d;
      scanf("%d %d", &cost, &d);
      best[cost].push_back(d);
    }
    for (int cost = 1; cost <= 3; cost++) {
      sort(best[cost].begin(), best[cost].end(), greater<int>());
    }
    vector<int> cards = {};
    vector<int> costs = {};
    for (int i = 0; i < min(3, (int)best[1].size()); i++) {
      cards.push_back(best[1][i]);
      costs.push_back(1);
    }
    if (!best[2].empty()) {
      cards.push_back(best[2][0]);
      costs.push_back(2);
    }
    if (!best[3].empty()) {
      cards.push_back(best[3][0]);
      costs.push_back(3);
    }
    assert((int)cards.size() <= 5);
    int m = costs.size();
    for (int mod = 0; mod < 10; mod++) {
      for (int mask = 0; mask < (1 << m); mask++) {
        long long add = 0, cost = 0;
        int sz = 0, mx = 0;
        for (int i = 0; i < m; i++) {
          if (mask & (1 << i)) {
            mx = max(mx, cards[i]);
            add += cards[i];
            cost += costs[i];
            sz++;
          }
        }
        if (cost > 3) {
          continue;
        }
        if (dp[xod - 1][mod] == -1) {
          continue;
        }
        bool st = (xod - 1 == 0 && mod == 0);
        if (!st && dp[xod - 1][mod] == 0) {
          continue;
        }
        if (mod + sz >= 10) {
          add += mx;
        }
        dp[xod][(mod + sz) % 10] =
            max(dp[xod][(mod + sz) % 10], dp[xod - 1][mod] + add);
      }
    }
  }
  long long ans = 0;
  for (int mod = 0; mod < 10; mod++) {
    ans = max(ans, dp[n][mod]);
  }
  cout << ans;
}
int main() { solve(); }
