CPython lite logoProblemsContests

I. Prefixes on segments Upsolve

You are given n strings, the length of each of them is m. You are given q queries, for each query

  • You are given l,r,k numbers. For strings in the interval [l,r], how many unique strings are there if for each string we take the prefix of length k?

Input

The first line contains two natural numbers n,m(1nm106).

The following lines contain strings of length m. The lines consist of lowercase Latin letters.

The number of queries is given in the next line, q(1q2105).

The following q lines give the query parameters, x,y,k(1x,y106,1km).

Please note that you are provided with the values ​​of the x and y, for the query l and r is calculated as follows:

 l=(x+prev_ans1)modn+1

 r=(y+prev_ans1)modn+1

Here prev_ans means the answer to the previous query. For the first query prev_ans=0.

If the query resulted in l>r, their values ​​should be swapped.                                            

Output

Print the answer for each query.


Sample input 1

5 3
aba
abc
aaa
bab
bba
5
1 3 2
2 3 1
1 4 2
2 4 3
1 1 1

Sample output 1

2
1
4
3
1