#include <bits/stdc++.h>
using namespace std;
const int nax = 1e7;
const int mod = 1e9 + 7;
void func() {
  long long int n, *a;
  cin >> n;
  a = new long long int[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  };
  int dikkatF = -1, dikkatR = -1;
  for (int i = 0; i < n - 1; i++) {
    if (a[i] > a[i + 1]) {
      dikkatF = i;
      break;
    }
  }
  for (int i = n - 1; i > 0; i--) {
    if (a[i] > a[i - 1]) {
      dikkatR = i - 1;
      break;
    }
  }
  if (dikkatR == -1 || dikkatF == -1) {
    cout << 0 << endl;
    return;
  }
  if (dikkatF > dikkatR) {
    cout << 0 << endl;
    return;
  }
  if (dikkatF == n - 2 || dikkatR == 0) {
    cout << 0 << endl;
    return;
  }
  int j = dikkatR;
  while (j > 0 && a[j - 1] <= a[j]) {
    j--;
  }
  cout << j << endl;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long int t;
  cin >> t;
  while (t--) {
    func();
  }
}
