#include <bits/stdc++.h>
using namespace std;
int t, n;
int a[300005];
int deal() {
  int i;
  for (i = n; i >= 2; i--) {
    if (a[i - 1] < a[i]) {
      break;
    }
  }
  for (i; i >= 2; i--) {
    if (a[i - 1] > a[i]) {
      break;
    }
  }
  return i - 1;
}
int main(int argc, char** argv) {
  cin >> t;
  while (t--) {
    cin >> n;
    for (int i = 1; i <= n; i++) {
      cin >> a[i];
    }
    if (n < 3) {
      cout << 0 << endl;
      continue;
    }
    cout << deal() << endl;
  }
  return 0;
}
