#include <bits/stdc++.h>
using namespace std;
int t, n, a[200010];
int main() {
  ios_base::sync_with_stdio(false), cin.tie(0);
  cin >> t;
  while (t--) {
    cin >> n;
    for (int i = 1; i <= n; ++i) cin >> a[i];
    int ans = 0;
    int mx = -1 << 30, mn = 1 << 30;
    bool is = 0;
    for (int i = n; i > 0; --i) {
      if (is) {
        if (a[i] > mn) {
          ans = i;
          break;
        }
        mn = min(mn, a[i]);
      } else {
        if (a[i] >= mx)
          mx = a[i];
        else {
          is = 1;
          mn = a[i];
        }
      }
    }
    cout << ans << '\n';
  }
}
