Spin the number wheel

Count the wheel positions where the M digits read clockwise form a number between X and Y.

Easy2Brute forceStringInterviewNo attempts yetTime limit1sMemory limit256 MB

Problem

A television segment hands out prizes with a spinning wheel. The wheel is a large disc cut into NN equal slots, and each slot has one digit from 0 to 9 written on it.

When the game starts, two MM-digit numbers XX and YY are announced. They satisfy XYX \le 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 MM slots in a row clockwise from that slot gives an MM-digit number ZZ. A reading that passes the last slot continues from the first slot. Since MNM \le N, no slot is read twice.

If ZZ satisfies XZYX \le Z \le Y, the contestant wins the game and takes ZZ times 10,000 won.

The wheel of numbers

For example, let N=8N = 8 with the wheel reading [3, 7, 8, 3, 1, 9, 2, 7] clockwise, and let X=200X = 200 and Y=311Y = 311. Starting from the slot holding 2 gives Z=273Z = 273, and 200273311200 \le 273 \le 311 holds, so that spin wins.

Given the wheel together with XX and YY, write a program that counts the starting slots that win the game.

Input

The first line has the number of test cases TT.

The first line of each test case has NN (1N1001 \le N \le 100), the number of slots on the wheel, and MM (1M91 \le M \le 9, MNM \le N), the number of digits in XX and YY. The next three lines hold the digits of XX, the digits of YY, and the state of the wheel, in that order.

Each digit of XX and of YY is a single digit from 0 to 9, separated by spaces.

The state of the wheel is given as NN digits from 0 to 9, separated by spaces, in the order they appear when the wheel is read clockwise from some slot.

Output

Print the answer for each test case on its own line. That is, print how many starting slots give an MM-digit number ZZ with XZYX \le Z \le Y.

Two starting slots count separately even when they give the same value of ZZ. For example, if 123 is the only number between XX and YY and the wheel reads 123 from two different slots, print 2 rather than 1.