#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<utility>
#include<map>
#include<complex>
using namespace std;
typedef long long ll;
const ll mod = 1000000007;
const ll INF = mod * mod;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
typedef vector<int> vec;
#define rep(i,n) for(int i=0;i<n;i++)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define stop char nyaa;cin>>nyaa;
#define per(i,n) for(int i=n-1;i>=0;i--)
typedef long double ld;

vector<int> v = { 13,17,19,23 };
int trans(int x) {
	rep(j, 4)if (x == v[j])return j;
	return -1;
}
const int mn = 55440;
int n;
void solve() {
	vector<int> memo(mn, 0);
	vector<vector<int>> memo2(4);
	rep(i, 4)memo2[i].resize(v[i], 0);
	rep(i, n) {
		int d, t; cin >> d >> t;
		vector<int> a(d); rep(j, d)cin >> a[j];
		int z = trans(d);
		if (z < 0) {
			rep(j, mn) {
				memo[j] += a[(j + t) % d];
			}
		}
		else {
			rep(j, d)memo2[z][j] += a[(j+t)%d];
		}
	}
	int ans = 0;
	rep(i, mn)ans = max(ans, memo[i]);
	rep(i, 4) {
		int ma = 0;
		rep(j, v[i])ma = max(ma, memo2[i][j]);
		ans += ma;
	}
	cout << ans << endl;
}


signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	//cout << fixed << setprecision(10);
	while(cin>>n,n)solve();
	//stop
		return 0;
}

