#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<long long> dp(10, INT64_MIN / 2), tp(10, 0);
  dp[0] = 0;
  for (int i = 0; i < n; i++) {
    int k;
    cin >> k;
    long long max11 = INT32_MIN, max12 = INT32_MIN, max13 = INT32_MIN,
              max2 = INT32_MIN, max3 = INT32_MIN;
    for (int j = 0; j < k; j++) {
      long long cost, damage;
      cin >> cost >> damage;
      if (cost == 1 && damage > max11) {
        max13 = max12;
        max12 = max11;
        max11 = damage;
      } else if (cost == 1 && damage > max12) {
        max13 = max12;
        max12 = damage;
      } else if (cost == 1 && damage > max13) {
        max13 = damage;
      } else if (cost == 2 && damage > max2) {
        max2 = damage;
      } else if (cost == 3 && damage > max3) {
        max3 = damage;
      }
    }
    long long d1 = max(max11, max(max2, max3));
    long long d2 = max(max11 + max12, max11 + max2);
    long long d3 = max11 + max12 + max13;
    for (int j = 1; j < 10; j++) {
      tp[j] = dp[j];
      tp[j] = max(tp[j], d1 + dp[(j - 1 + 10) % 10]);
      tp[j] = max(tp[j], d2 + dp[(j - 2 + 10) % 10]);
      tp[j] = max(tp[j], d3 + dp[(j - 3 + 10) % 10]);
    }
    tp[0] = dp[0];
    tp[0] = max(tp[0], d3 + dp[7] + max11);
    tp[0] = max(tp[0], d2 + dp[8] + max11);
    tp[0] = max(tp[0], d1 + dp[9] + d1);
    dp = tp;
  }
  long long result = 0;
  for (int i = 0; i < 10; i++) {
    result = max(result, dp[i]);
  }
  cout << result << endl;
}
