#include <bits/stdc++.h>
using namespace std;
int hhh[200020];
int hh[200020];
int main() {
  int n, k, c, d;
  int x, y, z[3];
  int plus[4];
  int ten[4];
  int flag, i, ans = 0;
  int cnt = 0;
  scanf("%d", &n);
  while (n--) {
    scanf("%d", &k);
    x = 0;
    y = 0;
    z[0] = 0;
    z[1] = 0;
    z[2] = 0;
    while (k--) {
      scanf("%d%d", &c, &d);
      switch (c) {
        case 3:
          if (x < d) x = d;
          break;
        case 2:
          if (y < d) y = d;
          break;
        case 1:
          if (z[2] < d) {
            if (z[1] < d) {
              if (z[0] < d) {
                z[2] = z[1];
                z[1] = z[0];
                z[0] = d;
              } else {
                z[2] = z[1];
                z[1] = d;
              }
            } else
              z[2] = d;
          }
          break;
        default:
          break;
      }
      for (i = 1; i <= 3; i++) {
        plus[i] = 0;
        ten[i] = 0;
      }
      plus[1] = max(x, max(y, z[0]));
      ten[1] = plus[1] * 2;
      if (z[0] > 0) {
        if (y > z[1]) {
          if (y > 0) {
            plus[2] = y + z[0];
            if (z[0] > y)
              ten[2] = plus[2] + z[0];
            else
              ten[2] = plus[2] + y;
          }
        } else {
          if (z[1] > 0) {
            plus[2] = z[0] + z[1];
            ten[2] = plus[2] + z[0];
          }
        }
      }
      if (z[2] != 0) {
        plus[3] = z[0] + z[1] + z[2];
        ten[3] = plus[3] + z[0];
      }
    }
    if (cnt % 10 >= 7)
      flag = (cnt / 10 + 1) * 10;
    else
      flag = cnt / 10 * 10;
    i = flag - 3;
    if (i < 0) i = 0;
    for (int j = i; j <= cnt; j++) hh[j] = hhh[j];
    for (; i <= cnt; i++) {
      if (hh[i] != 0 || i == 0) {
        for (int j = 1; j <= 3; j++) {
          if (plus[j] != 0) {
            if ((j < flag && j + i >= flag)) {
              if (ten[j] + hh[i] > hhh[i + j]) hhh[j + i] = ten[j] + hh[i];
            } else {
              if (plus[j] + hh[i] > hhh[i + j]) hhh[j + i] = plus[j] + hh[i];
            }
          }
        }
      }
    }
    if (plus[3] != 0)
      cnt += 3;
    else if (plus[2] != 0)
      cnt += 2;
    else if (plus[1] != 0)
      cnt += 1;
  }
  flag = cnt / 10 * 10;
  i = flag - 3;
  if (i < 0) i = 0;
  for (; i <= cnt; i++) {
    if (hhh[i] > ans) ans = hhh[i];
  }
  cout << ans;
  return 0;
}
