#include <bits/stdc++.h>
using namespace std;
using namespace std;
const long long int N = 4e5;
int tree[N];
int n;
int minn(int l, int r) {
  int minnn = INT_MAX;
  for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
    if (l & 1) minnn = min(minnn, tree[l++]);
    if (r & 1) minnn = min(minnn, tree[--r]);
  }
  return minnn;
}
bool good(int s, int e) {
  if (s == e) return 1;
  int m = minn(s, e + 1);
  return (tree[s + n] <= m && good(s + 1, e) ||
          tree[e + n] <= m && good(s, e - 1));
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    cin >> n;
    for (int i = 0; i < n; i++) cin >> tree[n + i];
    for (int i = n - 1; i > 0; i--)
      tree[i] = min(tree[i << 1], tree[i << 1 | 1]);
    int ans;
    int lb = 0, ub = n - 1;
    while (lb <= ub) {
      int mb = (lb + ub) / 2;
      if (good(mb, n)) {
        ans = mb;
        ub = mb - 1;
      } else
        lb = mb + 1;
    }
    cout << ans << "\n";
  }
  return 0;
}
