#include using namespace std; #define FOR(i,l,r) for(int i = (int) (l);i < (int) (r);i++) #define ALL(x) x.begin(),x.end() template bool chmax(T& a,const T& b){ return a < b ? (a = b,true) : false; } template bool chmin(T& a,const T& b){ return b < a ? (a = b,true) : false; } typedef long long ll; const ll pow1 = 1003,pow2 = 1007,mod1 = 1e9 + 7,mod2 = 1e9 + 9; struct RollingHash{ int n; vector dat,powTable; ll pow,mod; void init(string& str,ll pow,ll mod){ n = str.size(); this->pow = pow; this->mod = mod; dat.assign(n + 1,0); powTable.assign(n + 1,1); for(int i = 1;i <= n;i++){ dat [i] = (dat [i - 1] * pow + str [i - 1]) % mod; powTable [i] = (powTable [i - 1] * pow) % mod; } } ll get(int l,int r){ ll res = dat [r] - dat [l] * powTable [r - l]; res = (res % mod + mod) % mod; return res; } }; int N; vector S; RollingHash rh [100000]; char buffer [1000000]; ll M,X,D; void get_query(int& a,int& b) { a = (X / (N - 1)) + 1; b = (X % (N - 1)) + 1; if(a > b){ swap(a,b); } else{ b++; } X = (X + D) % (N * (N - 1)); } int main() { scanf("%d",&N); S.assign(N,""); FOR(i,0,N){ scanf("%s",buffer); S [i] = buffer; rh [i].init(S [i],pow1,mod1); } scanf("%lld%lld%lld",&M,&X,&D); ll ans = 0; FOR(i,0,M){ int a,b; get_query(a,b); a--; b--; int ok = 0,ng = min(S [a].size(),S [b].size()) + 1,mid; while(ng - ok > 1){ mid = (ok + ng) / 2; if(rh [a].get(0,mid) == rh [b].get(0,mid)){ ok = mid; } else{ ng = mid; } } ans += ok; } printf("%lld\n",ans); return 0; }