#include <bits/stdc++.h>
using namespace std;
signed main() {
  long long n;
  cin >> n;
  long long dp[n + 1][12];
  for (long long i = 0; i < n + 1; i++) {
    for (long long j = 0; j < 12; j++) {
      dp[i][j] = -1;
    }
  }
  dp[0][0] = 0;
  for (long long i = 1; i <= n; i++) {
    long long k;
    cin >> k;
    long long a = -1, b = -1, c = -1, d = -1, e = -1;
    for (long long i = 0; i < k; i++) {
      long long x, y;
      cin >> x >> y;
      if (x == 3) {
        e = max(e, y);
      }
      if (x == 2) {
        d = max(d, y);
      }
      if (x == 1) {
        if (y >= a) {
          c = b;
          b = a;
          a = y;
        } else if (y >= b) {
          c = b;
          b = y;
        } else if (y >= c) {
          c = y;
        }
      }
    }
    for (long long j = 0; j < 10; j++) {
      for (long long g = 0; g < 10; g++) {
        if (dp[i - 1][g] != -1) {
          long long z;
          if (j >= g) {
            z = j - g;
          } else {
            z = 10 - g + j;
          }
          if (z == 0) {
            dp[i][j] = dp[i - 1][g];
          }
          if (z == 1 && a != -1) {
            if (j == 0) {
              dp[i][j] = dp[i - 1][g] + a + a;
            } else {
              dp[i][j] = dp[i - 1][g] + a;
            }
            if (e != -1) {
              if (j <= 2) {
                dp[i][j] = max(dp[i][j], dp[i - 1][g] + e + e);
              } else {
                dp[i][j] = max(dp[i][j], dp[i - 1][g] + e);
              }
            }
            if (d != -1) {
              dp[i][j] = max(dp[i][j], dp[i - 1][g] + d + d);
            }
          }
          if (z == 2) {
            if (j == 0 || j == 1) {
              if (b != -1) {
                dp[i][j] = max(dp[i][j], dp[i - 1][g] + a + a + b);
              }
            } else {
              if (d != -1) {
                dp[i][j] = max(dp[i][j], dp[i - 1][g] + d);
              }
              if (b != -1) {
                dp[i][j] = max(dp[i][j], dp[i - 1][g] + a + b);
              }
            }
            if (d != -1 && a != -1) {
              if (j <= 2) {
                dp[i][j] = max(dp[i][j], dp[i - 1][g] + max(a, d) + a + d);
              } else {
                dp[i][j] = max(dp[i][j], dp[i - 1][g] + a + d);
              }
            }
          }
          if (z == 3) {
            if (a != -1 && b != -1 && c != -1) {
              if (j <= 2) {
                dp[i][j] = max(dp[i][j], dp[i - 1][g] + a + a + b + c);
              } else {
                dp[i][j] = max(dp[i][j], dp[i - 1][g] + a + b + c);
              }
            }
          }
        }
      }
    }
  }
  long long ans = 0;
  for (long long i = 0; i < 10; i++) {
    ans = max(ans, dp[n][i]);
  }
  cout << ans;
}
