#include <bits/stdc++.h>
using namespace std;
const long long N = 2e5 + 5;
long long n;
long long a[N];
bool check(long long x) {
  map<long long, long long> vis;
  long long prv = -1e9;
  for (long long i = x + 1, j = n; i <= j; true) {
    long long mn = min(a[i], a[j]);
    if (mn < prv) return 0;
    if (vis[mn]) return 0;
    while (i <= j && a[i] == mn) i++;
    while (j >= i && a[j] == mn) j--;
    vis[mn] = 1;
    prv = mn;
  }
  return 1;
}
long long binsearch(long long lo, long long hi) {
  while (lo < hi) {
    long long mid = (lo + hi) / 2;
    if (check(mid))
      hi = mid;
    else
      lo = mid + 1;
  }
  return lo;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  long long t;
  cin >> t;
  while (t--) {
    cin >> n;
    for (long long i = 1; i <= n; i++) cin >> a[i];
    long long ans = binsearch(0, n - 1);
    cout << ans << "\n";
  }
  return 0;
}
