#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[5];
int dp[MAXN][10], now[5];
bool cmp(int a, int b) { return a > b; }
int pos = 0;
int ans = 0;
void solve(int T) {
  int n, tmp1, tmp2, f = 0, now1[4], siz = 0;
  now[2] = -INF, now[3] = -INF;
  scanf("%d", &n);
  for (int i = 1; i <= 3; i++) a[i].clear();
  for (int i = 1; i <= n; i++) {
    scanf("%d %d", &tmp1, &tmp2);
    if (tmp1 == 2 || tmp1 == 3)
      now[tmp1] = max(now[tmp1], tmp2);
    else
      a[1].push_back(tmp2);
  }
  for (int i = 1; i <= 3; i++) sort(a[i].begin(), a[i].end(), cmp);
  siz = a[1].size();
  for (int i = 0; i < min(siz, 3); i++) {
    now1[i + 1] = a[1][i];
  }
  if (siz < 3)
    for (int i = siz + 1; i <= 3; i++) now1[i] = -INF;
  for (int i = 0; i <= 9; i++) {
    if (dp[T - 1][i] == -1) continue;
    if (i == 9) {
      dp[T][0] =
          max(dp[T][0], dp[T - 1][9] + 2 * max(now1[1], max(now[2], now[3])));
    } else {
      dp[T][i + 1] =
          max(dp[T][i + 1], dp[T - 1][i] + max(now1[1], max(now[2], now[3])));
    }
    if (i == 8) {
      dp[T][0] = max(dp[T][0], dp[T - 1][8] + max(now1[1] * 2 + now1[2],
                                                  max(now1[1] * 2 + now[2],
                                                      now1[1] + now[2] * 2)));
    } else {
      dp[T][i + 2] =
          max(dp[T][i + 2], dp[T - 1][i] + now1[1] + max(now1[2], now[2]));
    }
    if (i == 7) {
      dp[T][0] = max(dp[T][0], dp[T - 1][7] + now1[1] * 2 + now1[2] + now1[3]);
    } else {
      dp[T][i + 3] =
          max(dp[T][i + 3], dp[T - 1][i] + now1[1] + now1[2] + now1[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));
  dp[0][0] = 0;
  for (int i = 1; i <= t; i++) {
    solve(i);
  }
  printf("%d", ans);
}
