// Arup Guha
// 4/29/2020
// Alternate Solution to 2020 FHSPS Problem: Grading

#include <stdio.h>

int main(void) {

    int nC, loop;
    scanf("%d", &nC);

    // Process each case.
    for (loop=0; loop<nC; loop++) {

        // Get # of grades.
        int i, n;
        scanf("%d", &n);

        // Make this big enough for old school C!
        int grades[10000];

        // Read in the grades.
        for (i=0; i<n; i++)
            scanf("%d", &grades[i]);

        // Find smallest index with a minimum value.
        int minI = 0;
        for (i=1; i<n; i++)
            if (grades[i] < grades[minI])
                minI = i;

        // Find largest index with a max value.
        int maxI = 0;
        for (i=1; i<n; i++)
            if (grades[i] >= grades[maxI])
                maxI = i;

        // Answer in the typical case.
        int regAns = minI + n - 1 - maxI;

        // If they cross over, we save one swap.
        if (minI > maxI) regAns--;

        // Ta da!
        printf("%d\n", regAns);
    }

    return 0;
}
