#include <bits/stdc++.h>
using namespace std;
long long power(long long a, long long b, long long m = 1000000007) {
  long long res = 1;
  while (b > 0) {
    if (b & 1) res = (res * a) % m;
    a = (a * a) % m;
    b >>= 1;
  }
  return res;
}
long long inverse(long long a, long long m = 1000000007) {
  return power(a, m - 2, m);
}
int main() {
  int n;
  cin >> n;
  long long dp[2][10];
  memset(dp, 0, sizeof(dp));
  for (int Q = 0; Q < n; Q++) {
    int k;
    cin >> k;
    vector<vector<int> > mx(3);
    for (int i = 0; i < k; i++) {
      int c, d;
      cin >> c >> d;
      c--;
      mx[c].push_back(d);
    }
    for (int i = 0; i < 3; i++)
      sort(mx[i].begin(), mx[i].end(), greater<int>());
    vector<long long> maxi(5, 0);
    for (int i = 0; i < min(3, (int)mx[0].size()); i++) maxi[i] = mx[0][i];
    if (mx[1].size()) maxi[3] = mx[1][0];
    if (mx[2].size()) maxi[4] = mx[2][0];
    long long mv1 = 0;
    for (int i = 0; i < 5; i++) mv1 = max(mv1, maxi[i]);
    pair<long long, long long> mv21 = make_pair(0, 0), mv22 = make_pair(0, 0);
    for (int i = 0; i < 5; i++)
      for (int j = i + 1; j < 5; j++) {
        int c = 0;
        if (i < 3)
          c++;
        else if (i == 3)
          c += 2;
        else
          c += 3;
        if (j < 3)
          c++;
        else if (j == 3)
          c += 2;
        else
          c += 3;
        if (c > 3) break;
        if (mv21.first + mv21.second < maxi[i] + maxi[j])
          mv21 = make_pair(max(maxi[i], maxi[j]), min(maxi[i], maxi[j]));
        if (2 * mv22.first + mv22.second <
            2 * max(maxi[i], maxi[j]) + min(maxi[i], maxi[j]))
          mv22 = make_pair(2 * max(maxi[i], maxi[j]), min(maxi[i], maxi[j]));
      }
    vector<long long> mv31, mv32;
    for (int i = 0; i < 3; i++) mv31.push_back(maxi[i]);
    sort(mv31.begin(), mv31.end(), greater<int>());
    int summ = 0;
    for (int i = 0; i < 5; i++)
      for (int j = i + 1; j < 5; j++)
        for (int k = j + 1; k < 5; k++) {
          int c = 0;
          if (i < 3)
            c++;
          else if (i == 3)
            c += 2;
          else
            c += 3;
          if (j < 3)
            c++;
          else if (j == 3)
            c += 2;
          else
            c += 3;
          if (k < 3)
            c++;
          else if (k == 3)
            c += 2;
          else
            c += 3;
          if (c > 3) continue;
          vector<long long> temp;
          temp.push_back(maxi[i]);
          temp.push_back(maxi[j]);
          temp.push_back(maxi[k]);
          sort(temp.begin(), temp.end(), greater<int>());
          int sum = 2 * temp[0] + temp[1] + temp[2];
          if (summ < sum) {
            summ = sum;
            mv32 = temp;
          }
        }
    for (int i = 0; i < 10; i++) {
      dp[1][i] = max(dp[1][i], dp[0][i]);
      if (i + 1 >= 10) {
        dp[1][(i + 1) % 10] = max(dp[1][(i + 1) % 10], dp[0][i] + 2 * mv1);
      } else {
        dp[1][(i + 1) % 10] = max(dp[1][(i + 1) % 10], dp[0][i] + mv1);
      }
      if (mv21.first > 0 && mv21.second > 0) {
        if (i + 2 >= 10) {
          dp[1][(i + 2) % 10] =
              max(dp[1][(i + 2) % 10], dp[0][i] + mv22.first + mv22.second);
        } else {
          dp[1][(i + 2) % 10] =
              max(dp[1][(i + 2) % 10], dp[0][i] + mv21.first + mv21.second);
        }
      }
      if (mv31[0] > 0 && mv31[1] > 0 && mv31[2] > 0) {
        if (i + 3 >= 10) {
          dp[1][(i + 3) % 10] = max(dp[1][(i + 3) % 10],
                                    dp[0][i] + 2 * mv32[0] + mv32[1] + mv32[2]);
        } else {
          dp[1][(i + 3) % 10] =
              max(dp[1][(i + 3) % 10], dp[0][i] + mv31[0] + mv31[1] + mv31[2]);
        }
      }
    }
    for (int i = 0; i < 10; i++) {
      dp[0][i] = dp[1][i];
      dp[1][i] = 0;
    }
  }
  long long ans = 0;
  for (int i = 0; i < 10; i++) ans = max(ans, dp[0][i]);
  cout << ans << endl;
}
