#include <bits/stdc++.h>
using namespace std;
long long n, k, c, d;
long long dp[2][10] = {-10000000000};
vector<long long> cst[3];
void input() {
  scanf("%I64d", &k);
  for (auto &tmp : cst) tmp.clear();
  for (int j = 0; j < k; j++) {
    scanf("%I64d %I64d", &c, &d);
    cst[c - 1].push_back(d);
  }
  for (auto &tmp : cst) {
    sort(tmp.begin(), tmp.end());
    reverse(tmp.begin(), tmp.end());
  }
}
long long get(int num, bool ten) {
  long long res = -1;
  long long rt = ten ? 2 : 1;
  if (num == 1) {
    for (auto &tmp : cst) {
      if (tmp.size()) res = max(res, tmp[0] * rt);
    }
  } else if (num == 2) {
    if (cst[0].size() >= 2) res = max(res, cst[0][0] * rt + cst[0][1]);
    if (cst[0].size() && cst[1].size())
      res =
          max(res, min(cst[0][0], cst[1][0]) + max(cst[0][0], cst[1][0]) * rt);
  } else if (cst[0].size() >= 3) {
    res = max(res, cst[0][0] * rt + cst[0][1] + cst[0][2]);
  }
  return res;
}
long long solve() {
  long long *next = dp[0], *last = dp[1];
  next[0] = last[0] = 0;
  while (n--) {
    input();
    for (int i = 1; i <= 10; i++) {
      for (int j = 1; j <= 3; j++) {
        long long tmp = get(j, i == 10);
        next[i % 10] = max(next[i % 10], last[((i - j) + 10) % 10] + tmp);
      }
    }
    long long *tmp = next;
    next = last;
    last = tmp;
  }
  long long res = -1;
  for (int i = 0; i < 10; i++) res = max(res, last[i]);
  return res;
}
int main() {
  scanf("%I64d", &n);
  printf("%I64d\n", solve());
  return 0;
}
