Problem: The volume of access to a web service varies from time to time in a day. Also, the hours with the highest volume of access varies from service to service. For example, a service popular in the United States may receive more access in the daytime in the United States, while another service popular in Japan may receive more access in the daytime in Japan. When you develop a web service, you have to design the system so it can handle all requests made during the busiest hours.

You are a lead engineer in charge of a web service in the 30th century. It’s the era of Galaxy Wide Web (GWW), thanks to the invention of faster-than-light communication. The service can be accessed from all over the galaxy. Thus many intelligent creatures, not limited to human beings, can use the service. Since the volume of access to your service is increasing these days, you have decided to reinforce the server system. You want to design a new system that handles requests well even during the hours with the highest volume of access. However, this is not a trivial task. Residents in each planet have their specific length of a day, say, a cycle of life. The length of a day is not always 24 hours. Therefore, a cycle of the volume of access are different by planets of users.

You have obtained hourly data of the volume of access for all planets where you provide the service. Assuming the volume of access follows a daily cycle for each planet, you want to know the highest volume of access in one hour. It should be a quite easy task for you, a famous talented engineer in the galaxy.



Input

The input consists of multiple datasets. Each dataset has the following format:

N
d1 t1 q1,0 ... q1,d1-1
...
dN tN qN,0 ... qN,dN-1


N is the number of planets. di (1 ≤ i ≤ N) is the length of a day in the planet i.  ti (0 ≤ ti ≤ di - 1) is the current time of the planet i. qi, j is the volume of access on the planet i during from the j-th hour to the (j+1)-th hour.

You may assume that N ≤ 100, di ≤ 24, qi, j ≤ 1000000 (1 ≤ i ≤ N, 0 ≤ j ≤ di - 1).

The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.

Output

For each dataset, output the maximum volume of access in one hour in a line.

Example

Input

2
4 0 1 2 3 4
2 0 2 1
0


Output

5

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<ll>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using P = pair<ll, ll>;
const double eps = 1e-8;
const ll MOD = 1000000007;
const ll INF = INT_MAX / 2;
const ll LINF = LLONG_MAX / 2;
template <typename T1, typename T2>
bool chmax(T1 &a, const T2 &b) {
  if(a < b) {a = b; return true;}
  return false;
}
template <typename T1, typename T2>
bool chmin(T1 &a, const T2 &b) {
  if(a > b) {a = b; return true;}
  return false;
}
template<typename T1, typename T2>
ostream& operator<<(ostream &os, const pair<T1, T2> p) {
  os << p.first << ":" << p.second;
  return os;
}
template<class T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  for(ll i=0;i<((ll)(v.size()));++i) {
    if(i) os << " ";
    os << v[i];
  }
  return os;
}
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  while(1) {
    ll n;
    cin >> n;
    if (n == 0) break;
    map<ll, bool> mp;
    mp[13] = true;
    mp[17] = true;
    mp[19] = true;
    mp[23] = true;
    ll l = 55440;
    vvi v(n);
    for(ll i=0;i<(n);++i) {
      ll d;
      cin >> d;
      v[i].resize(d);
      ll t; cin >> t;
      for(ll j=0;j<(d);++j) {
        cin >> v[i][(j + d - t) % d];
      }
    }
    vi tmp(l);
    vi tmp13(13), tmp17(17), tmp19(19), tmp23(23);
    ll ans = 0;
    for(ll i=0;i<(n);++i) {
      if(mp[(ll)(v[i].size())]) {
        ll sz = (ll)(v[i].size());
        if(sz == 13) {
          for(ll j=0;j<(13);++j) {
            tmp13[j] += v[i][j];
          }
        } else if(sz == 17) {
          for(ll j=0;j<(17);++j) {
            tmp17[j] += v[i][j];
          }
        } else if(sz == 19) {
          for(ll j=0;j<(19);++j) {
            tmp19[j] += v[i][j];
          }
        } else {
          for(ll j=0;j<(23);++j) {
            tmp23[j] += v[i][j];
          }
        }
      } else {
        for(ll j=0;j<(l);++j) {
          tmp[j] += v[i][j%(ll)(v[i].size())];
        }
      }
    }
    ll ma = 0;
    for(ll i=0;i<(l);++i) {
      chmax(ma, tmp[i]);
    }
    ans += ma;
    vi hoge = {13, 17, 19, 23};
    for(ll i=0;i<(4);++i) {
      ma = 0;
      if(hoge[i] == 13) {
        for(ll j=0;j<(hoge[i]);++j) {
          chmax(ma, tmp13[j]);
        }
      } else if(hoge[i] == 17) {
        for(ll j=0;j<(hoge[i]);++j) {
          chmax(ma, tmp17[j]);
        }
      } else if(hoge[i] == 19) {
        for(ll j=0;j<(hoge[i]);++j) {
          chmax(ma, tmp19[j]);
        }
      } else {
        for(ll j=0;j<(hoge[i]);++j) {
          chmax(ma, tmp23[j]);
        }
      }
      ans += ma;
    }
    cout << ans << endl;
  }
}