#include #include #include #include #include #include using namespace std; typedef long long int llint; #define pub push_back #define mp make_pair #define fir first #define sec second const int big=((llint)1<<30); const llint mod=91254389315231;//そすー const int lol=101; //aabbaabb // int main(void){ llint x,y,n,i,gen,bmax,bmin,q,j,m,d,k,qx,qy,ans=0; vector a; vector> rolh; cin>>n; a.resize(n); rolh.resize(n); for(i=0;i>a[i]; rolh[i].resize(a[i].size());gen=0; for(j=0;j>m>>x>>d; for(k=1;k<=m;k++){ qx=(x/(n-1)); qy=(x%(n-1)); if(qx>qy){swap(qx,qy);} else{qy++;} x=(x+d)%(n*(n-1));bmax=min(rolh[qx].size(),rolh[qy].size());bmin=-1; while(bmax-bmin>1){ gen=(bmax+bmin)/2; if(rolh[qx][gen]!=rolh[qy][gen]){ bmax=gen; } else{ bmin=gen; } } ans+=bmax; } cout<