#include <bits/stdc++.h>
using namespace std;
int f[200010][15];
int maxnum(int a, int b) {
  if (a > b)
    return a;
  else
    return b;
}
int minnum(int a, int b) {
  if (a > b)
    return b;
  else
    return a;
}
int modsub(int a, int b) {
  int c = a - b;
  if (c <= 0) c = c + 10;
  return c;
}
int main() {
  int n, k, c, d, ans = 0, max11, max12, max13, max2, max3;
  cin >> n;
  for (int i = 0; i <= 10; i++) f[0][i] = -1000000001;
  f[0][10] = 0;
  for (int i = 1; i <= n; i++) {
    cin >> k;
    max11 = max12 = max13 = max2 = max3 = 0;
    for (int j = 1; j <= k; j++) {
      cin >> c >> d;
      if (c == 3 && d > max3) max3 = d;
      if (c == 2 && d > max2) max2 = d;
      if (c == 1 && d > max11) {
        max13 = max12;
        max12 = max11;
        max11 = d;
      } else if (c == 1 && d > max12) {
        max13 = max12;
        max12 = d;
      } else if (c == 1 && d > max13)
        max13 = d;
    }
    for (int j = 1; j <= 10; j++) {
      f[i][j] = f[i - 1][j];
      if (j == 10) {
        if (max3 > 0 && f[i - 1][9] + max3 * 2 > f[i][j])
          f[i][j] = f[i - 1][9] + max3 * 2;
        if (max2 > 0 && f[i - 1][9] + max2 * 2 > f[i][j])
          f[i][j] = f[i - 1][9] + max2 * 2;
        if (max2 > 0 && max11 > 0 &&
            f[i - 1][8] + maxnum(max2, max11) * 2 + minnum(max2, max11) >
                f[i][j])
          f[i][j] = f[i - 1][8] + maxnum(max2, max11) * 2 + minnum(max2, max11);
        if (max11 > 0 && max12 > 0 && max13 > 0 &&
            f[i - 1][7] + max12 + max13 + max11 * 2 > f[i][j])
          f[i][j] = f[i - 1][7] + max12 + max13 + max11 * 2;
        if (max11 > 0 && max12 > 0 && f[i - 1][8] + max11 * 2 + max12 > f[i][j])
          f[i][j] = f[i - 1][8] + max11 * 2 + max12;
        if (max11 > 0 && f[i - 1][9] + max11 * 2 > f[i][j])
          f[i][j] = f[i - 1][9] + max11 * 2;
      } else {
        if (max3 > 0 && f[i - 1][modsub(j, 1)] + max3 > f[i][j])
          f[i][j] = f[i - 1][modsub(j, 1)] + max3;
        if (max2 > 0 && f[i - 1][modsub(j, 1)] + max2 > f[i][j])
          f[i][j] = f[i - 1][modsub(j, 1)] + max2;
        if (max2 > 0 && max11 > 0 &&
            f[i - 1][modsub(j, 2)] + max2 + max11 > f[i][j])
          f[i][j] = f[i - 1][modsub(j, 2)] + max2 + max11;
        if (max11 > 0 && max12 > 0 && max13 > 0 &&
            f[i - 1][modsub(j, 3)] + max12 + max13 + max11 > f[i][j])
          f[i][j] = f[i - 1][modsub(j, 3)] + max12 + max13 + max11;
        if (max11 > 0 && max12 > 0 &&
            f[i - 1][modsub(j, 2)] + max11 + max12 > f[i][j])
          f[i][j] = f[i - 1][modsub(j, 2)] + max11 + max12;
        if (max11 > 0 && f[i - 1][modsub(j, 1)] + max11 > f[i][j])
          f[i][j] = f[i - 1][modsub(j, 1)] + max11;
      }
    }
  }
  for (int i = 1; i <= 10; i++)
    if (f[n][i] > ans) ans = f[n][i];
  cout << ans << endl;
}
