#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    int arr[n];
    for (int i = 0; i < n; i++) {
      cin >> arr[i];
    }
    int ind = 0, dec = 0, last = 0;
    for (int i = 1; i < n; i++) {
      if (dec && arr[i] > arr[i - 1]) {
        ind = last;
        dec = 0;
      }
      if (arr[i] < arr[i - 1]) {
        dec = 1;
      }
      if (arr[last] != arr[i]) {
        last = i;
      }
    }
    cout << ind << endl;
  }
}
