#include <bits/stdc++.h>
using namespace std;
int const N = 2 * 1e5 + 9;
int arr[N];
long long seg[N * 4];
void build(int node, int l, int r) {
  if (l == r) {
    seg[node] = arr[l];
    return;
  }
  int mid = (l + r) / 2;
  build(node * 2, l, mid), build(node * 2 + 1, mid + 1, r);
  seg[node] = min(seg[node * 2], seg[node * 2 + 1]);
}
long long query(int node, int l, int r, int s, int e) {
  if (r < s || l > e) return 1e15;
  if (l >= s && r <= e) return seg[node];
  int mid = (l + r) / 2;
  return min(query(node * 2, l, mid, s, e),
             query(node * 2 + 1, mid + 1, r, s, e));
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    for (int i = 0; i < int(n); ++i) {
      cin >> arr[i];
    }
    build(1, 0, n - 1);
    int p1 = 0, p2 = n - 1, ans = 0, final = -1, last_index = 0;
    while (p1 <= p2) {
      int smallest = (arr[p1] < arr[p2]) ? arr[p1] : arr[p2];
      bool flag = false;
      if (query(1, 0, n - 1, p1, p2) < smallest) {
        final = query(1, 0, n - 1, p1, p2);
        p1++;
        flag = true;
        last_index = p1 - 1;
      }
      if (!flag) {
        if (arr[p1] < arr[p2])
          p1++;
        else
          p2--;
      }
    }
    int as = 0;
    if (final != -1) {
      for (int i = last_index; i < n; i++) {
        if (arr[i] == final) {
          as = i;
          break;
        }
      }
    }
    cout << as << '\n';
  }
}
