Count the wheel positions where the M digits read clockwise form a number between X and Y.
Easy2Brute forceStringInterviewNo attempts yetTime limit1sMemory limit256 MBA television segment hands out prizes with a spinning wheel. The wheel is a large disc cut into N equal slots, and each slot has one digit from 0 to 9 written on it.
When the game starts, two M-digit numbers X and Y are announced. They satisfy X≤Y, and either of them may start with the digit 0.
The contestant spins the wheel hard. Once it stops, the pointer at the twelve o'clock position rests on one slot. Reading M slots in a row clockwise from that slot gives an M-digit number Z. A reading that passes the last slot continues from the first slot. Since M≤N, no slot is read twice.
If Z satisfies X≤Z≤Y, the contestant wins the game and takes Z times 10,000 won.

For example, let N=8 with the wheel reading [3, 7, 8, 3, 1, 9, 2, 7] clockwise, and let X=200 and Y=311. Starting from the slot holding 2 gives Z=273, and 200≤273≤311 holds, so that spin wins.
Given the wheel together with X and Y, write a program that counts the starting slots that win the game.
The first line has the number of test cases T.
The first line of each test case has N (1≤N≤100), the number of slots on the wheel, and M (1≤M≤9, M≤N), the number of digits in X and Y. The next three lines hold the digits of X, the digits of Y, and the state of the wheel, in that order.
Each digit of X and of Y is a single digit from 0 to 9, separated by spaces.
The state of the wheel is given as N digits from 0 to 9, separated by spaces, in the order they appear when the wheel is read clockwise from some slot.
Print the answer for each test case on its own line. That is, print how many starting slots give an M-digit number Z with X≤Z≤Y.
Two starting slots count separately even when they give the same value of Z. For example, if 123 is the only number between X and Y and the wheel reads 123 from two different slots, print 2 rather than 1.