#include <bits/stdc++.h>
using namespace std;
long long int dp[300000][11];
vector<long long int> v1[300000], v2[300000], v3[300000];
long long int n;
long long int rec(long long int idx, long long int br) {
  if (idx == n) return 0;
  if (dp[idx][br] != -1) return dp[idx][br];
  br %= 10;
  if (v1[idx].size() > 0) {
    if (br == 9) {
      dp[idx][br] = max(dp[idx][br], rec(idx + 1, br + 1) + v1[idx][0] * 2);
    } else
      dp[idx][br] = max(dp[idx][br], rec(idx + 1, br + 1) + v1[idx][0]);
  }
  if (v2[idx].size() > 1) {
    if (br == 9 || br == 8) {
      dp[idx][br] =
          max(dp[idx][br], rec(idx + 1, br + 2) + v2[idx][0] * 2 + v2[idx][1]);
    } else
      dp[idx][br] =
          max(dp[idx][br], rec(idx + 1, br + 2) + v2[idx][0] + v2[idx][1]);
  }
  if (v3[idx].size() > 2) {
    if (br == 9 || br == 8 || br == 7) {
      dp[idx][br] = max(dp[idx][br], rec(idx + 1, br + 3) + v3[idx][0] * 2 +
                                         v3[idx][1] + v3[idx][2]);
    } else
      dp[idx][br] = max(dp[idx][br], rec(idx + 1, br + 3) + v3[idx][0] +
                                         v3[idx][1] + v3[idx][2]);
  }
  return dp[idx][br];
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  long long int k, i;
  for (i = 0; i < n; i++) {
    cin >> k;
    vector<long long int> jedan, dva, tri;
    long long int j;
    for (j = 0; j < k; j++) {
      long long int a, b;
      cin >> a >> b;
      if (a == 1) {
        jedan.push_back(b);
        tri.push_back(b);
      } else if (a == 2) {
        jedan.push_back(b);
        dva.push_back(b);
      } else {
        jedan.push_back(b);
      }
    }
    sort(jedan.begin(), jedan.end());
    sort(dva.begin(), dva.end());
    sort(tri.begin(), tri.end());
    if (jedan.size() > 0) v1[i].push_back(jedan[jedan.size() - 1]);
    long long int ans = -1, ans1 = -1;
    if (tri.size() >= 2) ans1 = tri[tri.size() - 1] + tri[tri.size() - 2];
    if (tri.size() > 0 && dva.size() > 0)
      ans = dva[dva.size() - 1] + tri[tri.size() - 1];
    if (ans != -1 && ans1 == -1) {
      v2[i].push_back(tri[tri.size() - 1]);
      v2[i].push_back(dva[dva.size() - 1]);
    } else if (ans1 != -1 && ans == -1) {
      v2[i].push_back(tri[tri.size() - 1]);
      v2[i].push_back(tri[tri.size() - 2]);
    } else if (ans != -1 && ans1 != -1 && ans < ans1) {
      v2[i].push_back(tri[tri.size() - 1]);
      v2[i].push_back(tri[tri.size() - 2]);
    } else if (ans != -1 && ans1 != -1 && ans > ans1) {
      v2[i].push_back(tri[tri.size() - 1]);
      v2[i].push_back(dva[dva.size() - 1]);
    } else if (ans != -1 && ans1 != -1 && ans == ans1) {
      if (dva[dva.size() - 1] > tri[tri.size() - 1]) {
        v2[i].push_back(tri[tri.size() - 1]);
        v2[i].push_back(dva[dva.size() - 1]);
      } else {
        v2[i].push_back(tri[tri.size() - 1]);
        v2[i].push_back(tri[tri.size() - 2]);
      }
    }
    sort(v2[i].begin(), v2[i].end());
    reverse(v2[i].begin(), v2[i].end());
    if (tri.size() > 2) {
      v3[i].push_back(tri[tri.size() - 1]);
      v3[i].push_back(tri[tri.size() - 2]);
      v3[i].push_back(tri[tri.size() - 3]);
    }
  }
  for (i = 0; i <= n + 1; i++)
    for (long long int j = 0; j <= 10; j++) {
      dp[i][j] = -1;
    }
  cout << rec(0, 0);
  return 0;
}
