#include <bits/stdc++.h>
using namespace std;
long long n, k;
const int inf = 0x7fffffff;
const int maxn = 200005;
long long card[maxn][2];
bool vis[maxn];
long long ans = -inf;
struct zt {
  long long cnt;
  long long sum;
  zt() {}
  zt(long long x, long long y) {
    cnt = x;
    sum = y;
  }
  bool operator<(const zt &r) const {
    if (cnt == r.cnt) {
      return sum > r.sum;
    } else {
      return cnt > r.cnt;
    }
  }
};
set<zt> s;
set<zt> zy;
map<long long, long long> mp;
void dfs(long long cost, long long cnt, long long sum) {
  if (mp.count(cnt) != 0) {
    if (sum > mp[cnt]) {
      mp[cnt] = sum;
      s.insert(zt(cnt, sum));
    }
  } else {
    mp[cnt] = sum;
    s.insert(zt(cnt, sum));
  }
  ans = max(ans, sum);
  for (int i = 0; i < k; i++) {
    if (!vis[i] && cost + card[i][0] <= 3) {
      vis[i] = true;
      if (cnt % 10 == 9) {
        dfs(cost + card[i][0], 0, sum + 2 * card[i][1]);
      } else {
        dfs(cost + card[i][0], cnt + 1, sum + card[i][1]);
      }
      vis[i] = false;
    }
  }
}
int main() {
  cin >> n;
  long long cnt, sum;
  zt now;
  for (int i = 0; i < n; i++) {
    cin >> k;
    for (int j = 0; j < k; j++) {
      cin >> card[j][0] >> card[j][1];
    }
    if (!i) {
      dfs(0, 0, 0);
    } else {
      for (set<zt>::iterator it = s.begin(); it != s.end(); it++) {
        zy.insert(*it);
      }
      s.clear();
      for (set<zt>::iterator it = zy.begin(); it != zy.end(); it++) {
        dfs(0, it->cnt, it->sum);
      }
      zy.clear();
    }
  }
  cout << ans << endl;
  return 0;
}
