#include <bits/stdc++.h>
typedef long long ll;
using namespace std;

const ll INF = 1e18;

int main() {
	int n;
	cin >> n;
	ll dp[n + 1][10]{};
	for (int i = 0; i <= n; i++) {
		for (int j = 0; j < 10; j++) {
			dp[i][j] = -INF;
		}
	}
	dp[0][0] = 0;
	for (int i = 1; i <= n; i++) {
		int k;
		cin >> k;
		vector <int> c(k);
		vector <int> d(k);
		for (int j = 0; j < k; j++) {
			cin >> c[j] >> d[j];
		}
		ll m[4][4]{}; // number, cost
		for (int t1 = 0; t1 < 4; t1++) for (int t2 = 0; t2 < 4; t2++) m[t1][t2] = -INF;
		ll m1[4][4]{};
		for (int t1 = 0; t1 < 4; t1++) for (int t2 = 0; t2 < 4; t2++) m1[t1][t2] = -INF;
		m1[0][0] = 0;
		m[0][0] = 0;
		for (int j = 0; j < k; j++) {
			for (int q = 3; q >= 0; q--) {
				for (int w = 3; w >= 0; w--) {
					if (q + 1 <= 3 && w + c[j] <= 3) {
						m[q + 1][w + c[j]] = max(m[q + 1][w + c[j]], m[q][w] + d[j]);
						m1[q + 1][w + c[j]] = max(m1[q + 1][w + c[j]], m[q][w] + 2 * d[j]);
					}
				}
			}
		}
		ll p1[4]{};
		ll p[4]{};
		for (int q = 1; q <= 3; q++) {
			for (int w = 1; w <= 3; w++) {
				p[q] = max(p[q], m[q][w]);
				p1[q] = max(p1[q], m1[q][w]);
			}
		}
		for (int t1 = 0; t1 < 10; t1++) {
			for (int a = 1; a <= 3; a++) {
				int r = (t1 + a) % 10;
				if (r < t1) {
					dp[i][r] = max(dp[i][r], dp[i - 1][t1] + p1[a]);
				} else {
					dp[i][r] = max(dp[i][r], dp[i - 1][t1] + p[a]);
				}
			}
		}
	}
	ll ans = 0;
	for (int t1 = 0; t1 < 10; t1++) {
		ans = max(ans, dp[n][t1]);
	}
	cout << ans;
	return 0;
}
