#include <bits/stdc++.h>
using namespace std;
const int N = 3 + 2;
int n, k;
int dp[N][N];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    cin >> k;
    priority_queue<int> pq[N];
    for (int j = 1; j <= n; j++) {
      int c, d;
      cin >> c >> d;
      pq[c].push(d);
    }
    for (int j = 0; j <= 3; j++) {
      dp[i][0] = 0;
      dp[0][i] = 0;
    }
    int cur = 0;
    for (int j = 1; j <= 3; j++) {
      for (int k = 1; j * k <= 3 && !pq[j].empty(); k++) {
        dp[j][k] = dp[j][k - 1] + pq[j].top();
        pq[j].pop();
        cur = max(cur, dp[j][k]);
      }
    }
    cout << i << " -> " << cur << '\n';
    ans += cur;
  }
  cout << ans << '\n';
  return 0;
}
