#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <climits>
#include <cfloat>
using namespace std;

// 最大公約数
long long gcd(long long a, long long b){
    while(b != 0){
        long long tmp = a % b;
        a = b;
        b = tmp;
    }
    return a;
}

// 最小公倍数
long long lcm(long long a, long long b){
    return a / gcd(a, b) * b;
}

int main()
{
    const int prime[] = {13, 17, 19, 23};

    long long a = 1;
    for(int i=1; i<=24; ++i){
        if(find(prime, prime+4, i) == prime+4)
            a = lcm(a, i);
    }

    for(;;){
        int n;
        cin >> n;
        if(n == 0)
            return 0;

        vector<vector<int> > x(25, vector<int>(24, 0));
        for(int i=0; i<n; ++i){
            int d, t;
            cin >> d >> t;
            for(int j=0; j<d; ++j){
                int q;
                cin >> q;
                x[d][(t-j+d)%d] += q;
            }
        }

        for(int i=0; i<4; ++i){
            int d = prime[i];
            x[d].assign(d, *max_element(x[d].begin(), x[d].end()));
        }

        int ret = 0;
        for(int i=0; i<a; ++i){
            int sum = 0;
            for(int j=1; j<=24; ++j)
                sum += x[j][i%j];
            ret = max(ret, sum);
        }
        cout << ret << endl;
    }
}