#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <math.h>
#include <assert.h>
#include <vector>

using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
static const double EPS = 1e-9;
static const double PI = acos(-1.0);

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define FORIT(it, c) for (__typeof((c).begin())it = (c).begin(); it != (c).end(); it++)
#define MEMSET(v, h) memset((v), h, sizeof(v))

const ll MAXCYCLE = 16 * 9 * 5 * 7 * 11;
ll cycle[25][25];
ll total[MAXCYCLE];

int main() {
  int n;
  while (scanf("%d", &n), n) {
    MEMSET(cycle, 0);
    MEMSET(total, 0);
    REP(i, n) {
      int d, offset;
      scanf("%d %d", &d, &offset);
      assert(0 <= offset);
      REP(j, d) {
        int q;
        scanf("%d", &q);
        cycle[d][(d - offset + j) % d] += q;
      }
    }
    ll ans = 0;
    FOR(i, 1, 25) {
      if (i == 13 || i == 17 || i == 19 || i == 23) {
        ll maxvalue = 0;
        REP(j, i) {
          maxvalue = max(maxvalue, cycle[i][j]);
        }
        ans += maxvalue;
        continue;
      }
      int hour = 0;
      REP(j, MAXCYCLE) {
        total[j] += cycle[i][hour];
        hour = (hour + 1) % i;
      }
    }
    ll maxvalue = 0;
    REP(i, MAXCYCLE) {
      maxvalue = max(maxvalue, total[i]);
    }
    ans += maxvalue;
    printf("%lld\n", ans);
  }
}