#include <bits/stdc++.h>
using namespace std;
typedef long long llint;
#ifdef LOCAL
string to_string(string v){return "\""+v+"\"";}string to_string(char v){return "\'"+string(1,v)+"\'";}string to_string(bool v){return(v?"true":"false");}template<typename A,typename B>string to_string(pair<A,B>v){return"("+to_string(v.first)+", "+to_string(v.second)+")";}string to_string(vector<bool>v){bool f=true;string res="{";for(const auto &i:v){if(!f)res+=", ";f=false;res+=to_string(i);}res+="}";return res;}template<typename A>string to_string(A v){bool f=true;string res="{";for(const auto &i:v){if(!f)res+=", ";f=false;res+=to_string(i);}res+="}";return res;}template<typename Iter>string to_string(Iter beg,Iter end){bool f=true;string res="{";for(;beg!=end;beg++){if(!f)res+=", ";f=false;res+=to_string(*beg);}res+="}";return res;}
#define watch(v) cerr<<"["<< __func__<<":"<<__LINE__<<"]: "#v" = "<<to_string(v)<<"\n"
#define watchsl(f,l) cerr<<"["<< __func__<<":"<<__LINE__<<"]: ["#f" .. "#l"] = "<<to_string(f,l)<<"\n"
#define SEP cerr<<"---------------------------------\n\n"
#else
#define watch(v) ;
#define watchsl(f,l) ;
#define SEP ;
#endif
const llint LEN = (llint)2e5;

int n;
array<llint, 5> a[LEN];

llint dp[LEN][10];

int main()
{
#ifdef LOCAL
	freopen("input.txt", "rt", stdin); freopen("output.txt", "wt", stdout);
#endif
	ios_base::sync_with_stdio(false); cin.tie(nullptr);
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		int k;
		cin >> k;
		while (k--)
		{
			llint c, d;
			cin >> c >> d;
			if (c == 3)
				a[i][4] = max(a[i][4], d);
			else if (c == 2)
				a[i][3] = max(a[i][3], d);
			else
			{
				a[i][0] = max(a[i][0], d);
				sort(a[i].begin(), a[i].begin()+3);
			}
		}
	}
	watchsl(a, a+n);

	dp[0][1] = max(max(a[0][2], a[0][3]), a[0][4]);

	dp[0][2] = a[0][2] + max(a[0][1], a[0][3]);
	if (dp[0][2] == a[0][2] || !a[0][2])
		dp[0][2] = 0;

	dp[0][3] = (a[0][0] > 0) * (a[0][0] + a[0][1] + a[0][2]);

	for (int i = 1; i < n; i++)
	{
		int z[4][2];
		for_each(z, z + 4, [](auto i){ fill(i, i+2, '\0'); });

		z[1][0] = max(max(a[i][2], a[i][3]), a[i][4]);

		z[2][0] = a[i][2] + max(a[i][1], a[i][3]);
		if (z[2][0] == a[i][2] || !a[i][2])
			z[2][0] = 0;

		z[3][0] = (a[i][0] > 0) * (a[i][0] + a[i][1] + a[i][2]);

		z[1][1] = 2*z[1][0];
		z[2][1] = (z[2][0] > 0) * (z[2][0] + max(a[i][2], max(a[i][1], a[i][3])));
		z[3][1] = (z[3][0] > 0) * (z[3][0] + a[i][2]);

		copy(dp[i-1], dp[i-1]+10, dp[i]);

		for (int j = 0; j <= 9; j++)
			for (int q = 1; q <= 3; q++)
			{
				int k = j-q, w = 0;
				if (k < 0) k += 10, w = 1;
				if (z[q][0] && dp[i-1][k])
					dp[i][j] = max(dp[i][j], dp[i-1][k] + z[q][w]);
			}

		watch(i);
		watchsl(dp[i], dp[i]+10);
		SEP;
	}
	cout << *max_element(dp[n-1], dp[n-1]+10);
	return 0;
}
