#include <bits/stdc++.h>
using namespace std;
inline void _(long long &x, long long y) {
  if (y > x) x = y;
}
int a1, a2, a3;
int b;
int c;
void update(int cost, int damage) {
  switch (cost) {
    case 1:
      if (damage <= a3) break;
      a3 = damage;
      if (a3 <= a2) break;
      swap(a3, a2);
      if (a2 <= a1) break;
      swap(a1, a2);
      break;
    case 2:
      b = max(b, damage);
      break;
    case 3:
      c = max(c, damage);
      break;
  }
}
int n;
long long dp[2][10];
const long long inf = 1e18;
inline int read() {
  int x = 0;
  char c = getchar();
  bool f = 0;
  while (c < '0' || c > '9') f |= (c == '-'), c = getchar();
  while (c >= '0' && c <= '9') x = x * 10 + (c - '0'), c = getchar();
  return f ? -x : x;
}
int main() {
  for (int i = 1; i < 10; i++) dp[0][i] = -inf;
  n = read();
  for (int i = 1; i <= n; i++) {
    int k = read();
    a1 = a2 = a3 = b = c = -1;
    while (k--) {
      int cost = read(), damage = read();
      update(cost, damage);
    }
    k = i & 1;
    for (int j = 0, val, sum; j < 10; j++) {
      dp[k][j] = dp[k ^ 1][j];
      sum = val = max(a1, max(b, c));
      if (~val) {
        if (j)
          _(dp[k][j], dp[k ^ 1][j - 1] + sum);
        else
          _(dp[k][j], dp[k ^ 1][9] + sum + val);
      }
      val = max(a1, max(a2, b)), sum = a1 + max(a2, b);
      if (~min(a1, max(a2, b))) {
        if (j > 1)
          _(dp[k][j], dp[k ^ 1][j - 2] + sum);
        else
          _(dp[k][j], dp[k ^ 1][j + 8] + sum + val);
      }
      val = a1, sum = a1 + a2 + a3;
      if (~a3) {
        if (j > 2)
          _(dp[k][j], dp[k ^ 1][j - 3] + sum);
        else
          _(dp[k][j], dp[k ^ 1][j + 7] + sum + val);
      }
    }
  }
  long long res = -inf;
  for (int i = 0; i < 10; i++) _(res, dp[n & 1][i]);
  cout << res << endl;
}
