#include <bits/stdc++.h>
using namespace std;
int solve(vector<int>& a, int n) {
  if (is_sorted(a.begin(), a.end()) || is_sorted(a.rbegin(), a.rend()))
    return 0;
  int i = n - 1;
  while (a[i] <= a[i - 1]) i--;
  while (a[i] >= a[i - 1]) i--;
  return max(i, 0);
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<int> a(n);
    for (int i = 0; i < n; i++) cin >> a[i];
    cout << solve(a, n) << "\n";
  }
  return 0;
}
