Lyndon Substring

각 질의 (i, j)마다 s_i와 s_j를 이어 붙인 문자열에서 모든 순환 회전보다 사전순으로 작은 부분 문자열, 즉 Lyndon 단어의 최대 길이를 구한다.

어려움9문자열문자열 매칭분할 정복아직 제출이 없습니다시간 제한3초메모리 제한256 MB

문제

A string ww is said to be a Lyndon word if ww is lexicographically smaller than any of its cyclic rotations. 

The longest Lyndon substring of a string ss is the longest substring of ss which is a Lyndon word.

Chiaki has nn strings s_1,s_2,,s_ns\_1,s\_2,\dots,s\_n. She has some queries: for some pair (i,j)(i,j), find the length of the longest Lyndon substring of string s_is_js\_is\_j.

입력

There are multiple test cases. The first line of input contains an integer TT, indicating the number of test cases. For each test case:

The first line contains two integers nn and mm (1n,m105)(1 \le n, m \le 10^5) -- the number of strings and the number of queries.

Each of the next nn lines contains a nonempty string s_is\_i (1s_i105)(1 \le s\_i \le 10^5) consisting of lowercase English letters.

Each of the next mm lines contains two integers ii and jj (1i,jn1 \le i, j \le n) denoting a query.

It is guaranteed that in one test case the sum of all s|s| does not exceed 5×1055 \times 10^5 and that in all cases the sum of all s|s| does not exceed 5×1065 \times 10^6.

It is guaranteed that neither the sum of all nn nor the sum of all mm exceeds 10610^6.

출력

For each query, output an integer denoting the answer.