This page is still under construction.

Parts of this page are still being built. What you see may change.

Alpha of Degree k

Time limit1sMemory limit128 MB

Summary
Given a dictionary, answer queries asking for the shortest chain from word s to word t where each step shares a suffix-prefix overlap of length at least k, with a cap on chain length.
Level

Hard8 of 10

Topics
Graph, BFS, String matching, String
Solved
No attempts yet

Problem

Given two strings ss and tt, we say that the relation "alpha of degree kk", written s→αkts \xrightarrow{\alpha^k} t, holds between ss and tt if and only if there exists a suffix of ss of length at least kk that is also a prefix of tt. The alpha relation is not defined for k=0k = 0.

For example, "telnet" is α3\alpha^3-related to "network" (the suffix "net" of "telnet" is a prefix of "network"), while "block" is α4\alpha^4-related (and therefore also α3\alpha^3-, α2\alpha^2-, and α1\alpha^1-related) to "locker" (the suffix "lock" of "block" is a prefix of "locker").

An αk\alpha^k chain of length LL (with L>0L > 0) from a word ss to a word tt is a list of L+1L+1 words in which ss is the first word, tt is the last word, and the αk\alpha^k relation holds between every pair of consecutive words. For example, the following is an α2\alpha^2 chain of length 44 from "cartoon" to "manual":

cartoon→α2one→α2new→α2newsman→α2manualcartoon \xrightarrow{\alpha^2} one \xrightarrow{\alpha^2} new \xrightarrow{\alpha^2} newsman \xrightarrow{\alpha^2} manual

You are given a dictionary of words CC together with several queries. Each query gives two words ss and tt (both taken from the dictionary) and two integers kk and LL. For each query, decide whether there is an αk\alpha^k chain from ss to tt that uses only words from CC and whose length does not exceed LL; if so, report the length of the shortest such chain.

Input

The first line contains a single integer DD, the number of test cases.

Each test case begins with a line containing two integers WW and QQ, where WW is the number of words in this test case's dictionary and QQ is the number of queries. It is guaranteed that 0<W<500000 < W < 50000 and 0<Q<1000 < Q < 100.

The next WW lines each contain one dictionary word. Every word consists only of lowercase letters, contains no spaces, is at most 6464 characters long, and no word appears more than once.

Each of the following QQ lines describes one query: two words ss and tt (both from the dictionary) and two integers kk and LL, separated by single spaces.

Output

For each query, print exactly one line.

Let aa be the test case number (starting from 11) and let bb be the query number within that test case (also starting from 11).

If there is no αk\alpha^k chain from ss to tt that uses only dictionary words and has length at most LL, print:

a.b none

Otherwise, let cc be the length (the number of consecutive-word steps) of the shortest such chain, and print:

a.b c

The value cc is uniquely determined, so exactly one output is correct for each query.

Examples2

  1. Example 1

    Input
    2
    8 3
    news
    perusal
    symbolic
    newspaper
    salon
    longstreet
    cartoon
    streetcar
    news cartoon 3 8
    news cartoon 3 4
    news cartoon 1 4
    2 2
    link
    blink
    blink link 3 10
    link blink 2 100
    
    Expected output
    1.1 6
    1.2 none
    1.3 2
    2.1 1
    2.2 none
    
  2. Example 2

    Input
    1
    2 1
    abcd
    cdef
    abcd cdef 2 5
    
    Expected output
    1.1 1