#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
const long long LINF = 1e18;
const int INF = 0x3f3f3f3f;
const int MAXN = 200050;
vector<int> a;
int dp[MAXN][10], now[5];
bool cmp(int a, int b) { return a > b; }
int ans = 0;
void solve(int T) {
  int n, tmp1, tmp2, b2 = 0, b3 = 0, b1[4], siz = 0;
  scanf("%d", &n);
  a.clear();
  for (int i = 1; i <= n; i++) {
    scanf("%d %d", &tmp1, &tmp2);
    if (tmp1 == 1) {
      a.push_back(tmp2);
    } else if (tmp1 == 2) {
      b2 = max(b2, tmp2);
    } else if (tmp1 == 3) {
      b3 = max(b3, tmp2);
    }
  }
  sort(a.begin(), a.end(), cmp);
  for (int i = 0; i < min(int(a.size()), 3); i++) {
    b1[++siz] = a[i];
  }
  for (int i = 0; i <= 9; i++) {
    if (i == 9) {
      if (dp[T - 1][i] == -1) continue;
      if (siz >= 1) dp[T][0] = max(dp[T][0], dp[T - 1][i] + 2 * b1[1]);
      if (b2) dp[T][0] = max(dp[T][0], dp[T - 1][i] + 2 * b2);
      if (b3) dp[T][0] = max(dp[T][0], dp[T - 1][i] + 2 * b3);
    } else {
      if (dp[T - 1][i] == -1) continue;
      if (siz >= 1) dp[T][i + 1] = max(dp[T][i + 1], dp[T - 1][i] + b1[1]);
      if (b2) dp[T][i + 1] = max(dp[T][i + 1], dp[T - 1][i] + b2);
      if (b3) dp[T][i + 1] = max(dp[T][i + 1], dp[T - 1][i] + b3);
    }
  }
  for (int i = 0; i <= 9; i++) {
    if (i >= 8) {
      if (dp[T - 1][i] == -1) continue;
      if (siz >= 2)
        dp[T][i - 8] = max(dp[T][i - 8], dp[T - 1][i] + 2 * b1[1] + b1[2]);
      else if (siz >= 1 && b2) {
        if (b1[1] >= b2) {
          dp[T][i - 8] = max(dp[T][i - 8], dp[T - 1][i] + 2 * b1[1] + b2);
        } else {
          dp[T][i - 8] = max(dp[T][i - 8], dp[T - 1][i] + 2 * b2 + b1[1]);
        }
      }
    } else {
      if (dp[T - 1][i] == -1) continue;
      if (siz >= 2)
        dp[T][i + 2] = max(dp[T][i + 2], dp[T - 1][i] + b1[1] + b1[2]);
      else if (siz >= 1 && b2) {
        if (b1[1] >= b2) {
          dp[T][i + 2] = max(dp[T][i + 2], dp[T - 1][i] + b1[1] + b2);
        } else {
          dp[T][i + 2] = max(dp[T][i + 2], dp[T - 1][i] + b2 + b1[1]);
        }
      }
    }
  }
  for (int i = 0; i <= 9; i++) {
    if (i >= 7) {
      if (dp[T - 1][i] == -1) continue;
      if (siz >= 3)
        dp[T][i - 7] =
            max(dp[T][i - 7], dp[T - 1][i] + 2 * b1[1] + b1[2] + b1[3]);
    } else {
      if (dp[T - 1][i] == -1) continue;
      if (siz >= 3)
        dp[T][i + 3] = max(dp[T][i + 3], dp[T - 1][i] + b1[1] + b1[2] + b1[3]);
    }
  }
  for (int i = 0; i <= 9; i++) ans = max(ans, dp[T][i]);
}
signed main() {
  int t = 1;
  scanf("%d", &t);
  memset(dp, -1, sizeof(dp));
  for (int i = 0; i <= t; i++) dp[i][0] = 0;
  for (int i = 1; i <= t; i++) {
    solve(i);
  }
  printf("%d", ans);
}
