#include <bits/stdc++.h>
const long long pinf = ((long long)2e18);
const long long ninf = ((long long)-2e18);
const long long mod = 1000000007;
using namespace std;
long long power(long long x, long long n, long long M) {
  if (n == 0)
    return 1;
  else if (n % 2 == 0)
    return power((x * x) % M, n / 2, M);
  else
    return (x * power((x * x) % M, (n - 1) / 2, M)) % M;
}
bool vowel(char c) {
  return (c == 'a' or c == 'e' or c == 'i' or c == 'o' or c == 'u' or
          c == 'A' or c == 'E' or c == 'I' or c == 'U' or c == 'O');
}
int32_t main() {
  ;
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  long long t;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    vector<long long> a(n), b(n);
    map<long long, long long> make_pair;
    for (long long i = 0; i < n; i++) {
      cin >> a[i];
      make_pair[a[i]]++;
      b[i] = a[i];
    }
    sort(b.begin(), b.end());
    long long p = 0;
    long long ans = 0;
    long long l = 0, r = n - 1;
    while (l < r) {
      if (make_pair[b[p]]) {
        if (a[l] == b[p]) {
          make_pair[a[l]]--;
          p++;
          l++;
        } else if (a[r] == b[p]) {
          make_pair[a[r]]--;
          r--;
          p++;
        } else {
          make_pair[a[l]]--;
          l++;
          ans = l;
        }
      } else {
        p++;
      }
    }
    cout << ans << "\n";
  }
  return 0;
}
