#include <bits/stdc++.h>
using namespace std;
long long n, k, c, d, ans;
vector<pair<long long, long long> > tmp, cards[200002];
long long dp[200002][11];
bool cmp(pair<long long, long long> a, pair<long long, long long> b) {
  if (a.first < b.first)
    return true;
  else if (a.first > b.first)
    return false;
  else
    return a.second > b.second;
}
long long dpf(long long ind, long long mask, bool dobel) {
  long long ret = 0, sez = cards[ind].size(), cost = 0;
  vector<long long> use;
  for (int i = 0; i < sez; i++) {
    if (mask & (1 << i)) {
      use.push_back(cards[ind][i].second);
      cost += cards[ind][i].first;
    }
  }
  if (cost > 3) return -99999999999999;
  sort(use.begin(), use.end());
  reverse(use.begin(), use.end());
  long long sezuse = use.size();
  for (int i = 0; i < sezuse; i++) {
    if (i == 0 && dobel)
      ret += use[i] * 2;
    else
      ret += use[i];
  }
  return ret;
}
void deb() {
  for (int i = 0; i < n; i++) {
    long long sez = cards[i].size();
    cout << i << ": \n";
    for (int j = 0; j < sez; j++) {
      cout << cards[i][j].first << " " << cards[i][j].second << "\n";
    }
  }
}
int main() {
  memset(dp, 0, sizeof dp);
  ans = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    long long cnt[4];
    for (int j = 0; j < 4; j++) cnt[j] = 0;
    cin >> k;
    for (int j = 0; j < k; j++) {
      cin >> c >> d;
      tmp.push_back(make_pair(c, d));
    }
    sort(tmp.begin(), tmp.end(), cmp);
    for (int j = 0; j < k; j++) {
      if (tmp[j].first == 1) {
        if (cnt[1] < 3) {
          cards[i].push_back(tmp[j]);
          cnt[1]++;
        }
      } else {
        if (cnt[tmp[j].first] < 1) {
          cards[i].push_back(tmp[j]);
          cnt[tmp[j].first]++;
        }
      }
    }
    tmp.clear();
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 10; j++) {
      long long sez = cards[i].size();
      for (int k = 0; k < (1 << sez); k++) {
        dp[i + 1][((j + __builtin_popcount(k)) % 10)] = max(
            dp[i + 1][((j + __builtin_popcount(k)) % 10)],
            dp[i][j] + dpf(i, k, (j + __builtin_popcount(k) >= 10 ? 1 : 0)));
      }
    }
  }
  for (int i = 0; i < 10; i++) {
    ans = max(ans, dp[n][i]);
  }
  cout << ans << "\n";
}
