#include <bits/stdc++.h>
using namespace std;
const long long int logx = 30;
const long long int N = 2e5 + 5;
const long long int INF = 1e18 + 5;
const long long int mod = 1e9 + 7;
const double PI = 3.14159265358979323846;
inline long long int mul(long long int a, long long int b) {
  return (a * 1ll * b) % mod;
}
inline long long int sub(long long int a, long long int b) {
  long long int c = a - b;
  if (c < 0) c += mod;
  return c;
}
inline long long int add(long long int a, long long int b) {
  long long int c = a + b;
  if (c > mod) c -= mod;
  return c;
}
template <typename T>
T power(T x, T y, long long int m) {
  T ans = 1;
  while (y > 0) {
    if (y & 1LL) ans = (ans * x) % m;
    y >>= 1ll;
    x = (x * x) % m;
  }
  return ans % m;
}
long long int n, dp[N][11];
vector<pair<long long int, pair<long long int, long long int> > > v[N];
long long int F(long long int idx, long long int day) {
  long long int maxi = 0;
  if (idx == n) {
    return 0;
  }
  if (dp[idx][day] != -1) {
    return dp[idx][day];
  }
  long long int m = v[idx].size();
  for (long long int i = 0; i < (1 << m); i++) {
    long long int ff = 0, tot = day, sum = 0, damage = 0;
    for (long long int j = 0; j < m; j++) {
      if (i & (1 << j)) {
        ff = 2;
        tot %= 10;
        if (tot == 9) {
          damage += (2 * v[idx][j].second.second);
        } else {
          damage += v[idx][j].second.second;
        }
        sum += v[idx][j].second.first;
        tot++;
        tot %= 10;
      }
    }
    if (ff == 0 || sum > 3) {
      continue;
    }
    maxi = max(maxi, damage + F(idx + 1, tot));
    maxi = max(maxi, F(idx + 1, day));
  }
  return dp[idx][day] = maxi;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  cin >> n;
  memset(dp, -1, sizeof(dp));
  for (long long int i = 0; i < n; i++) {
    long long int k;
    cin >> k;
    vector<pair<long long int, long long int> > cur[4];
    for (long long int j = 0; j < k; j++) {
      long long int x, y;
      cin >> x >> y;
      cur[x].push_back(make_pair(j, y));
    }
    for (long long int h = 1; h <= 3; h++) {
      long long int limit;
      sort(cur[h].begin(), cur[h].end());
      reverse(cur[h].begin(), cur[h].end());
      if (h == 1) {
        limit = 3;
      } else {
        limit = 1;
      }
      for (auto j : cur[h]) {
        if (limit == 0) {
          break;
        }
        v[i].push_back(make_pair(j.first, make_pair(h, j.second)));
        limit--;
      }
    }
    sort(v[i].begin(), v[i].end());
  }
  cout << F(0, 0) << endl;
}
