#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
const long long M = (long long)1e9 + 7;
const double pi = 2 * acos(0.0);
const double esp = 1e-9;
int Set(int N, int pos) { return N = N | (1 << pos); }
int reset(int N, int pos) { return N = N & ~(1 << pos); }
bool check(int N, int pos) { return (bool)(N & (1 << pos)); }
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
int ar[2 * N];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int test = 1;
  cin >> test;
  for (int cs = 1; cs <= test; ++cs) {
    int n;
    cin >> n;
    for (int i = 1; i <= n; ++i) {
      cin >> ar[i];
    }
    if (n <= 2) {
      cout << 0 << endl;
    } else {
      int in = n, si = 0;
      for (int i = n - 1; i >= 1; --i) {
        if (ar[i] < ar[i + 1]) break;
        in = i;
      }
      for (int i = in - 1; i >= 1 && in; --i) {
        if (ar[i] > ar[i + 1]) break;
        si = i;
      }
      if (si != 0)
        cout << si - 1 << endl;
      else
        cout << si << endl;
    }
  }
  return 0;
}
