#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int t;
  cin >> t;
  while (t--) {
    long long int n, m = 0;
    cin >> n;
    long long int ara[n];
    long long int ans = 0, s = 0;
    for (long long int i = 0; i < n; i++) cin >> ara[i];
    for (long long int i = 0; i < n; i++) {
      if (ara[i] <= ara[i + 1]) m++;
    }
    if (m == n) {
      cout << 0 << endl;
      continue;
    }
    m = 0;
    for (long long int i = n - 1; i > 0; i--) {
      if (ara[i] <= ara[i - 1]) m++;
    }
    if (m + 1 == n) {
      cout << 0 << endl;
      continue;
    }
    if (n == 1 || n == 2) {
      cout << 0 << endl;
      continue;
    }
    long long int i, j;
    for (i = n - 1; i >= 0; i--) {
      if (ara[i] > ara[i - 1]) break;
    }
    for (j = i - 1; j >= 0; j--) {
      if (ara[j] < ara[j - 1]) break;
    }
    cout << j << endl;
  }
}
