#include <bits/stdc++.h>
using namespace std;
long long n, f[200021][11], g[200021][6][2];
vector<long long> card, c1[4], cost;
bool cmp(const long long& a, const long long& b) { return a > b; }
void init() {
  cin >> n;
  for (long long x, a, b, i = 1; i <= n; i++) {
    cin >> x;
    card.clear();
    cost.clear();
    for (long long j = 1; j <= 3; j++) c1[j].clear();
    while (x--) {
      cin >> a >> b;
      c1[a].push_back(b);
    }
    for (long long j = 1; j <= 3; j++)
      if (c1[j].size()) sort(c1[j].begin(), c1[j].end(), cmp);
    for (long long j = 0; j < c1[1].size(); j++) {
      if (j == 3) break;
      card.push_back(c1[1][j]);
      cost.push_back(1);
    }
    if (c1[2].size()) card.push_back(c1[2].front()), cost.push_back(2);
    if (c1[3].size()) card.push_back(c1[3].front()), cost.push_back(3);
    for (long long j = 1; j <= 5; j++) g[i][j][0] = g[i][j][1] = -1e18;
    long long cnt = card.size();
    for (long long pos = 0; pos < 1 << cnt; pos++) {
      long long tot = 0, Max = 0, sum = 0, ct = 0;
      for (long long j = 1; j <= cnt; j++) {
        if (pos & (1 << j - 1)) {
          tot++;
          Max = max(Max, card[j - 1]);
          sum += card[j - 1];
          ct += cost[j - 1];
        }
      }
      if (ct > 3) continue;
      g[i][tot][0] = max(g[i][tot][0], sum);
      g[i][tot][1] = max(g[i][tot][1], sum + Max);
    }
  }
}
int main() {
  ios::sync_with_stdio(false);
  init();
  for (long long i = 1; i <= n; i++) {
    for (long long a = 0; a <= 9; a++) {
      for (long long b = 0; b <= 5; b++) {
        if (a + b < 10)
          f[i][a + b] = max(f[i][a + b], f[i - 1][a] + g[i][b][0]);
        else
          f[i][(a + b) % 10] =
              max(f[i][(a + b) % 10], f[i - 1][a] + g[i][b][1]);
      }
    }
  }
  long long ans = 0;
  for (long long i = 0; i <= 9; i++) {
    ans = max(ans, f[n][i]);
  }
  cout << ans;
  return 0;
}
