#include <cstdio>
#include <algorithm>
using namespace std;

int main(){
	const int M = 11 * 7 * 5 * 3 * 3 * 2 * 2 * 2 * 2;
	int n, d, t, q;
	while(scanf("%d", &n), n){
		int v[25][24] = {};
		for(int i = 0; i < n; ++i){
			scanf("%d%d", &d, &t);
			for(int j = 0; j < d; ++j){
				scanf("%d", &q);
				v[d][(j + d - t) % d] += q;
			}
		}
		int ans = 0;
		int s[M] = {};
		for(int i = 1; i <= 24; ++i){
			if(i == 13 || i == 17 || i == 19 || i == 23){
				ans += *max_element(v[i], v[i] + i);
			}
			else{
				for(int j = 0; j < M; ++j){
					s[j] += v[i][j % i];
				}
			}
		}
		ans += *max_element(s, s + M);
		printf("%d\n", ans);
	}
}