PERMRANK - Rank of a Permutation

no tags 

Given a permutation of the first N (0 < N < 20) natural numbers, find the lexicographic number of the permutation. For example, if N = 3, then :
Rank    Permutation
1    1 2 3
2    1 3 2
3    2 1 3
4    2 3 1
5    3 1 2
6    3 2 1
 
Input:  
The first line consists of the number of test cases T (T <= 10000). Each test case consists of 2 lines- the first line has an integer N, the length of the permuatation, and the second line consists of a permutation of the integers from 1, 2, ... , N
 
Output:
For each test case, output 1 integer, the rank of the permutation. Print each answer on a new line.
It is guaranteed that the result will fit in a 64-bit signed integer.
 
example:
Input:
1
4
2 1 4 3
 
output:
8



Added by:Varun Jalan
Date:2010-09-06
Time limit:1s-1.177s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: NODEJS OBJC VB.NET
Resource:own problem