Under construction

Still being built. Things may change

Lyndon Substring

Time limit3sMemory limit256 MB

Summary
For each query (i, j), find the length of the longest Lyndon substring of the concatenation s_i s_j, given n strings and m queries.
Level

Hard9 of 10

Topics
String, String matching, Greedy
Solved
No attempts yet

Problem

A string ww is 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 that is a Lyndon word.

Chiaki has nn strings s1,s2,…,sns_1, s_2, \dots, s_n. She has some queries. For a pair (i,j)(i, j), find the length of the longest Lyndon substring of the string sisjs_i s_j.

Input

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

The first line contains two integers nn and mm (1≤n,m≤1051 \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 sis_i (1≤si≤1051 \le s_i \le 10^5) consisting of lowercase English letters.

Each of the next mm lines contains two integers ii and jj (1≤i,j≤n1 \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.

Output

For each query, output an integer denoting the answer.

Examples1

  1. Example 1

    Input
    1
    2 1
    aa
    bb
    1 2
    
    Expected output
    4