#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 2e5 + 7;
ll mx[10], tmp[10], d[N];
int c[N];
ll f(int cur, int p) { return (cur - p + 10) % 10 == 9 ? 2LL : 1LL; }
int main() {
  int n;
  memset(mx, -1, sizeof(mx));
  mx[9] = 0;
  scanf("%d", &n);
  for (int r = 0; r < n; ++r) {
    int k;
    scanf("%d", &k);
    for (int i = 0; i < k; ++i) {
      scanf("%d%I64d", &c[i], &d[i]);
    }
    for (int i = 0; i < 10; ++i) {
      tmp[i] = mx[i];
      if (mx[(i + 9) % 10] != -1) {
        ll res = 0;
        for (int j = 0; j < k; ++j) {
          res = max(res, d[j]);
        }
        res = res * f(i, 0);
        tmp[i] = max(tmp[i], mx[(i + 9) % 10] + res);
      }
      if (mx[(i + 8) % 10] != -1 && k >= 2) {
        ll pmx = -1;
        ll res = -1;
        int j;
        for (j = 0; j < k; ++j) {
          if (c[j] == 1) {
            pmx = d[j];
            break;
          }
        }
        for (j = j + 1; j < k; ++j) {
          if (c[j] <= 2) res = max(res, pmx * f(j, 1) + d[j] * f(j, 0));
          if (c[j] == 1) pmx = max(pmx, d[j]);
        }
        for (j = 0; j < k; ++j) {
          if (c[j] == 2) {
            pmx = d[j];
            break;
          }
        }
        for (j = j + 1; j < k; ++j) {
          if (c[j] == 1) res = max(res, pmx * f(j, 1) + d[j] * f(j, 0));
          if (c[j] == 2) pmx = max(pmx, d[j]);
        }
        if (res != -1) {
          tmp[i] = max(tmp[i], res + mx[(i + 8) % 10]);
        }
      }
      if (mx[(i + 7) % 10] != -1 && k >= 3) {
        ll dp[4];
        memset(dp, -1, sizeof(dp));
        dp[0] = 0;
        for (int j = 0; j < k; ++j) {
          if (c[j] != 1) continue;
          for (int p = 3; p >= 1; --p) {
            if (dp[p - 1] != -1) {
              dp[p] = max(dp[p], dp[p - 1] + d[j] * f(i, p - 1));
            }
          }
        }
        ll res = dp[3];
        if (res != -1) {
          tmp[i] = max(tmp[i], res + mx[(i + 7) % 10]);
        }
      }
    }
    for (int i = 0; i < 10; ++i) mx[i] = tmp[i];
  }
  ll ans = 0;
  for (int i = 0; i < 10; ++i) ans = max(ans, mx[i]);
  printf("%I64d\n", ans);
  return 0;
}
