#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
using ii = pair<int, ll>;
using li = pair<ll, int>;
using vi = vector<int>;
using vii = vector<ii>;
using graph = vector<vi>;
const ll MOD = 1000000007;
const int INF = 0x3f3f3f3f;
const ll INFL = 0x3f3f3f3f3f3f3f3f;
const int MAXN = 312345;
const double PI = 3.14159265359;
const double EPS = 0.000000001;
ll cost[8][MAXN];
ll dp[15][MAXN];
vii o, t, s;
void process(int j) {
  sort(o.begin(), o.end());
  sort(t.begin(), t.end());
  sort(s.begin(), s.end());
  if (o.size() >= 1) {
    cost[1][j] = -o[0].second;
    cost[4][j] = cost[1][j] << 1;
  }
  if (o.size() >= 2) {
    cost[2][j] = -(o[0].second + o[1].second);
    cost[5][j] = cost[2][j] - o[0].second;
  }
  if (o.size() >= 3) {
    for (int i = 0; i < 3; ++i) {
      cost[3][j] += -o[i].second;
    }
    cost[6][j] = cost[3][j] - o[0].second;
  }
  if (t.size() >= 1) {
    cost[1][j] = max(cost[1][j], -t[0].second);
    cost[4][j] = cost[1][j] << 1;
    if (o.size() >= 1) {
      cost[2][j] = max(cost[2][j], -(o[0].second + t[0].second));
      cost[5][j] = max(cost[5][j], -(o[0].second + t[0].second +
                                     min(o[0].second, t[0].second)));
    }
  }
  if (s.size() >= 1) {
    cost[1][j] = max(cost[1][j], -s[0].second);
    cost[4][j] = cost[1][j] << 1;
  }
}
int q;
ll sol(int i, int r) {
  if (i >= q + 1) return 0;
  if (dp[r][i]) return dp[r][i];
  ll ans = 0;
  for (int j = 1; j <= 3; ++j) {
    if (cost[j][i]) {
      ans = max(ans, cost[j][i] + sol(i + 1, (r + j) % 10));
    }
    if (r >= 10 - j && cost[3 + j][i]) {
      ans = max(ans, cost[3 + j][i] + sol(i + 1, (r + j) % 10));
    }
  }
  return dp[r][i] = ans;
}
int main() {
  int n;
  scanf("%d", &q);
  int a, b;
  for (int j = 1; j <= q; ++j) {
    scanf("%d", &n);
    for (int i = 1; i <= 6; ++i) {
      cost[i][j] = 0;
    }
    o.clear();
    t.clear();
    s.clear();
    for (int i = 0; i < n; ++i) {
      scanf("%d %d", &a, &b);
      if (a == 1) {
        o.emplace_back(a, -b);
      } else if (a == 2) {
        t.emplace_back(a, -b);
      } else {
        s.emplace_back(a, -b);
      }
    }
    process(j);
  }
  printf("%lld\n", sol(1, 0));
}
