Mobile Pattern Lock is a graphical password method where users create patterns on a line connecting 3*3 grid. You are given two integers ‘M’ and ‘N’. Count a total number of valid mobile pattern locks which consist of at least ‘M’ keys and at most ‘N’ keys. In a valid pattern, all keys must be distinct. The order of keys used matters. If the line joining two consecutive keys passes through any other keys, the other keys must be previously selected in pattern.
Example :
We can treat the 3*3 grid as follows:-

4-7-9-6 is an invalid pattern lock because while joining 7-9 it passes through key 8 which has not been previously selected in pattern.
9-1-2-3 is an invalid pattern lock because while joining 9-1 it passes through key 5 which has not been previously selected in pattern.
4-2-6-8 is a valid pattern lock.

The first line contains a single integer ‘T’ representing the number of test cases.
The only line of each test case contains two space-separated integers ‘M’ and ‘N’ representing two integers for which you need to return the total number of valid patterns.
Output Format :
For each test case print an integer denoting the total number of valid mobile pattern locks which consist of at least ‘M’ keys and at most ‘N’ keys.
Note :
You do not need to print anything; it has already been taken care of. Just implement the function.
1 <= T <= 50
1 <= M <= N <= 9
Time Limit: 1sec
2
1 1
1 2
9
65
Test case 1:
1,2,3,4,5,6,7,8 and 9 are 9 possible patterns.
Test case 2:
Key with 1- 1,2,3,4,5,6,7,8 and 9 are 9 possible patterns.
Key with 2- all adjacent pair, so total count is 56
Return 9+56.
2
2 2
4 6
56
34792
Try to iterate over all positions using DFS.
We can do DFS from all the starting points. We can easily observe 1,3,7, and 9 can be treated as the same i.e. the number of patterns for each one of them would be the same due to symmetry. Similarly, 2,4,6, and 8 will have the same number of patterns as a starting point. We can apply DFS as follows:
O(N!), where ‘N’ is the maximum number of keys we can take in our path.
As we iterate over most of the pattern.
O(K), where ‘K’ is the total number of keys.
As we are using an array ‘VIS’ of size ‘K’ to check which keys are visited already in the pattern.