#include <bits/stdc++.h>
using namespace std;
int n;
int k;
vector<long long> v[5];
long long max_hurt[5][5];
long long dp[2][15];
int vis[15];
int now = 1;
void gethurt() {
  for (int i = 0; i < 5; i++)
    for (int j = 0; j < 3; j++) max_hurt[i][j] = -1;
  max_hurt[0][0] = max_hurt[0][1] = 0;
  for (int i = 1; i <= 3; i++) sort(v[i].begin(), v[i].end(), greater<int>());
  int len1 = v[1].size(), len2 = v[2].size(), len3 = v[3].size();
  if (len1 >= 3) {
    max_hurt[3][0] = v[1][1] + v[1][2] + v[1][0];
    max_hurt[3][1] = v[1][0] * 2 + v[1][1] + v[1][2];
  }
  if (len2 != 0) {
    max_hurt[1][0] = max(max_hurt[1][0], v[2][0]);
    max_hurt[1][1] = max(max_hurt[1][1], v[2][0] * 2);
    if (len1 != 0) {
      max_hurt[2][0] = max(max_hurt[2][0], v[1][0] + v[2][0]);
      max_hurt[2][1] = max(max_hurt[2][1],
                           max(v[1][0], v[2][0]) * 2 + min(v[1][0], v[2][0]));
    }
  }
  if (len3 != 0) {
    max_hurt[1][0] = max(max_hurt[1][0], v[3][0]);
    max_hurt[1][1] = max(max_hurt[1][1], v[3][0] * 2);
  }
  if (len1 >= 2) {
    max_hurt[2][0] = max(max_hurt[2][0], v[1][1] + v[1][0]);
    max_hurt[2][1] = max(max_hurt[2][1], v[1][0] * 2 + v[1][1]);
  }
  if (len1 >= 1) {
    max_hurt[1][0] = max(max_hurt[1][0], v[1][0]);
    max_hurt[1][1] = max(max_hurt[1][1], v[1][0] * 2);
  }
}
int main() {
  vis[0] = 1;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    int k;
    scanf("%d", &k);
    for (int p = 0; p < k; p++) {
      long long f, h;
      scanf("%lld%lld", &f, &h);
      v[f].push_back(h);
    }
    gethurt();
    for (int l = 0; l <= 9; l++) {
      if (vis[l] != 1) continue;
      for (int m = 0; m <= 3; m++) {
        int nex = (l + m) % 10;
        int d = ((l + m) >= 10 ? 1 : 0);
        dp[now][nex] = max(dp[now][nex], dp[(now ^ 1)][l] + max_hurt[m][d]);
        vis[nex] = 2;
      }
    }
    now ^= 1;
    for (int i = 0; i <= 9; i++)
      if (vis[i] == 2) vis[i]--;
    memset(dp[now], 0, sizeof(dp[now]));
    for (int i = 1; i <= 3; i++) v[i].clear();
  }
  long long ans = 0;
  for (int i = 0; i <= 9; i++) {
    ans = max(ans, dp[now ^ 1][i]);
  }
  printf("%lld\n", ans);
  return 0;
}
