#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
const int mod = 1e9 + 7;
int a[maxn];
int main(void) {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> a[i];
    if (is_sorted(a + 1, a + n + 1)) {
      cout << 0 << endl;
    } else {
      int cnt = 1;
      int i;
      for (i = n; i >= 1; i--) {
        if (a[i] <= a[i - 1] && i > 1) {
          cnt++;
        } else
          break;
      }
      for (int j = i; j >= 1; j--) {
        if (a[j] >= a[j - 1] && j > 1) {
          cnt++;
        } else
          break;
      }
      cout << n - cnt << endl;
    }
  }
  return 0;
}
