#include <bits/stdc++.h>
using namespace std;
long long dp[200001][10];
long long dp1[4][2];
vector<int> cards[200001][4];
int n;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  cin >> n;
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j < 10; j++) dp[i][j] = -1e18;
  }
  dp[0][0] = 0;
  for (int i = 0; i < n; i++) {
    int k;
    cin >> k;
    for (int j = 0; j < k; j++) {
      int c, d;
      cin >> c >> d;
      cards[i][c].push_back(d);
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 1; j <= 3; j++) {
      int s = (j == 1 ? 3 : 1);
      sort(cards[i][j].begin(), cards[i][j].end());
      reverse(cards[i][j].begin(), cards[i][j].end());
      while (cards[i][j].size() > s) cards[i][j].pop_back();
    }
  }
  for (int i = 0; i < n; i++) {
    vector<pair<int, int>> v;
    dp1[0][0] = 0;
    for (int j = 0; j < 4; j++) dp1[j][0] = dp[j][1] = -1e18;
    for (int j = 1; j <= 3; j++) {
      for (auto k : cards[i][j]) {
        v.push_back({j, k});
      }
    }
    sort(v.begin(), v.end());
    do {
      long long cnt = 0;
      long long mx = 0;
      long long x = 3;
      long long score = 0;
      for (auto j : v) {
        if (x < j.first) break;
        cnt++;
        x -= j.first;
        score += j.second;
        mx = max(mx, (long long)j.second);
        dp1[cnt][0] = max(dp1[cnt][0], score);
        dp1[cnt][1] = max(dp1[cnt][1], score + mx);
      }
    } while (next_permutation(v.begin(), v.end()));
    for (int j = 0; j < 10; j++) {
      for (int k = 0; k < 4; k++) {
        int nxt = (j + k) % 10;
        int l = (j + k >= 10 ? 1 : 0);
        dp[i + 1][nxt] = max(dp[i + 1][nxt], dp[i][j] + dp1[k][l]);
      }
    }
  }
  long long ans = 0;
  for (int i = 0; i < 10; i++) {
    ans = max(ans, dp[n][i]);
  }
  cout << ans << endl;
  return 0;
}
