#include <bits/stdc++.h>
using namespace std;
#define FOR(i,k,n) for(int i = (int)(k); i < (int)(n); i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(a) a.begin(), a.end()
#define MS(m,v) memset(m,v,sizeof(m))
#define D10 fixed<<setprecision(10)
typedef long long ll;
typedef long double ld;
typedef vector<ll> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-12;
template<class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template<class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

/*--------------------template--------------------*/

ll gcd(ll a, ll b)
{
	if (a < b) swap(a, b);
	return (a%b ? gcd(a%b, b) : b);
}

ll lcm(ll a, ll b)
{
	return a / gcd(a, b)*b;
}

void extgcd(ll a, ll b, ll& x, ll& y)
{
	if (b != 0) { extgcd(b, a % b, y, x); y -= a / b * x; }
	else { x = 1; y = 0; }
}


int main()
{
	cin.sync_with_stdio(false);
	int lcma = 1, lcmb = 1;
	FOR(i, 1, 24)
	{
		if (i == 13 || i == 17 || i == 19 || i == 23)
		{
			lcmb = lcm(lcmb, i);
		}
		else
		{
			lcma = lcm(lcma, i);
		}
	}
	int n;
	while (cin >> n, n)
	{
		vi a(lcma), b(lcmb);
		REP(i, n)
		{
			int d, t;
			cin >> d >> t;
			vi v(d);
			REP(i, d) cin >> v[i];
			if (d == 13 || d == 17 || d == 19 || d == 23)
			{
				REP(i, lcmb)
				{
					if (t == d) t = 0;
					b[i] += v[t];
					t++;
				}
			}
			else
			{
				REP(i, lcma)
				{
					if (t == d) t = 0;
					a[i] += v[t];
					t++;
				}
			}
		}
		cout << *max_element(ALL(a)) + *max_element(ALL(b)) << endl;
	}
	return 0;
}