


ARR=[‘hot’, ‘a’, ‘b’, ‘dog’] and query = (‘hot’, ‘dog’)
The answer, in this case, is 3 as the minimum distance between ‘hot’ and ‘dog’ in the given document is 3.
If any one of the words is not present in the document then your program must return ‘N’ which is the length of the document.
The first line of input contains an integer 'T’ denoting the number of test cases to run. Then the test case follows.
The first line of each test case contains two space-separated integers, ‘N’ and ‘Q’ respectively.
The second line of each test case contains all the space-separated words in the document ARR.
The next ‘Q’ lines of each test case contain ‘Q’ queries, as two, space-separated words.
For each test case print ‘Q’ lines, i’th of which is the answer to i’th query.
Answer to each query is printed on a new line.
You don’t need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 5
1 <= N <= 10^4
1 <= Q <=100
Where, ‘N’ and ‘Q’, are the length of the ARR, number of queries, respectively.
All the strings in ARR contain only lowercase English letters.
Time Limit: 1 sec
For each query, we will iterate over the entire document and keep two-pointer ‘PT1’ and ‘PT2’ for two words. If the current word of the document is equal to any of these two words we will update the answer and pointers.
The algorithm will be:
For each word, we will store its indexes in the ‘ARR’. To answer a query we will iterate over these indexes and always increment an index that is behind. And minimize our ‘ANS’ with the differences between the two indexes.
The algorithm will be: