#include <bits/stdc++.h>
#define int long long
#define fr first
#define sc second
#define all(s) s.begin(), s.end()
#define szof(s) (int)s.size()
#define pii pair <int, int>
using namespace std;


const int N = (int)2e5 + 5;
const int MOD = (int)1e9 + 7;
int tests = 1;

int dp[N][11];

main() {
  memset(dp, -1, sizeof dp);

  int t, n, cnt = 0;
  cin >> t;

  dp[0][0] = 0;
  while (++cnt <= t) {
    int mxcost[4] = {0, 0, 0, 0};

    int c, d;
    cin >> n;
    vector <int> v1;
    vector <pii> cards;
    int a2 = 0, a3 = 0;

    for (int i = 0; i < n; i++) {
      scanf("%I64d%I64d", &c, &d);
      if (c == 1) {
        v1.push_back(d);
      }
      if (c == 2) {
        a2 = max(a2, d);
      }
      if (c == 3) {
        a3 = max(a3, d);
      }
    }

    sort(v1.begin(), v1.end(), [&](int A, int B) {
      return A > B;
    });

    // 1
    for (int i = 0; i < min(3LL, szof(v1)); i++) {
      cards.push_back({1, v1[i]});
    }
    if (a2) {
      cards.push_back({2, a2});
    }
    if (a3) {
      cards.push_back({3, a3});
    }
    sort(cards.begin(), cards.end());


    for (int mod = 0; mod <= 9; mod++) {
      if (dp[cnt - 1][mod] == -1) {
        continue;
      }

      dp[cnt][mod] = max(dp[cnt][mod], dp[cnt - 1][mod]);
      do {
        int cost = 0, prefdmg = 0;
        for (int i = 0; i < min(3LL, szof(cards)); i++) {
          cost += cards[i].fr;
          if (cost <= 3) {
            int nmod = (mod + (i + 1)) % 10;
            int &res = dp[cnt][nmod];
            if (mod + (i + 1) == 10) {
              res = max(res, dp[cnt - 1][mod] + prefdmg + (2LL * cards[i].sc));
            }
            else {
              res = max(res, dp[cnt - 1][mod] + prefdmg + cards[i].sc);
            }
            prefdmg += cards[i].sc;
          } else {
            break;
          }
        }
      }
      while (next_permutation(cards.begin(), cards.end()));
    }

  }

  int ans = 0;
  for (int mod = 0; mod <= 9; mod++) {
    ans = max(ans, dp[t][mod]);
  }
  cout << ans << endl;
}
