#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n, ans = 0;
  cin >> n;
  int a[n];
  for (size_t i = 0; i < n; i++) {
    cin >> a[i];
  }
  int ctr = 0;
  if (n < 3) {
    cout << ans << endl;
    return;
  }
  bool increase = false;
  for (int i = n - 1; i >= 0; i--) {
    if ((a[i] > a[i - 1]) != increase) {
      ctr++;
      increase = !increase;
    }
    if (ctr > 1) {
      ans = i;
      break;
    }
  }
  cout << ans << endl;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t;
  cin >> t;
  while (t--) solve();
  return 0;
}
