子数组的代价是相邻数字的正序对的个数,也就是当前数字只和上一个数字有关。可以把原数组依次放入两个数组,考虑两个数组的最后一个数字,设两个数组的最后一个数字分别是 $x_1, x_2$,不失一般性设 $x_1 \le x_2$:

  1. 如果 $a_i > x_2$,那么 $a_i$ 必定会产生一个单位的代价,放在 $x_1$ 后面更优;
  2. 如果 $a_i \le x_1$,那么 $a_i$ 不会产生代价,放在 $x_1$ 后面更优;
  3. 如果 $x_1 < a_i \le x_2$,那么 $a_i$ 放在 $x_2$ 后面,不会产生代价,此时的局面 $A$ 是 $x_1, a_i$。假设放在 $x_1$ 后面,产生一个单位的代价,此时的局面 $B$ 是:$a_i, x_2$。看起来 $A$ 局面比 $B$ 局面更差,因为 $x_1 < x_2$,其实 $A$ 之后的代价最多比 $B$ 之后的代价多 $1$,也就是 $A$ 局面的总体代价不会更差。(假设 $x_2 > a_j > a_i > x_1$,此时局面 A 使用 $1$ 的代价变成了 $a_i, a_j$,局面 $B$ 使用 $0$ 的代价变成了 $a_i, a_j$,两个局面相同)
// Date: Sun Jan  7 00:21:51 2024

#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <algorithm>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> VI;
typedef pair<int, int> PII;
template <class T> using pq = priority_queue<T>;
template <class T> using pqg = priority_queue<T, vector<T>, greater<T>>;

const int INF = 0x3f3f3f3f, MOD = 1e9 + 7, MOD1 = 998'244'353;
const ll INFL = 0x3f3f3f3f'3f3f3f3f;
const double eps = 1e-8;
const int dir[8][2] = {
    {0, 1}, {0, -1}, {1, 0}, {-1, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1},
};

const ull Pr = 131;

#define For(i, a, b) for (int i = int(a); i < int(b); ++i)
#define Rof(i, a, b) for (int i = int(b) - 1; i >= int(a); --i)
#define For1(i, a, b) for (int i = int(a); i <= int(b); ++i)
#define Rof1(i, a, b) for (int i = int(b); i >= int(a); --i)
#define ForE(i, j) for (int i = h[j]; i != -1; i = ne[i])

#define f1 first
#define f2 second
#define pb push_back
#define has(a, x) (a.find(x) != a.end())
#define nemp(a) (!a.empty())
#define all(a) (a).begin(), (a).end()
#define SZ(a) int((a).size())
#define NL cout << '\n';

template <class T> bool ckmin(T &a, const T &b) { return b < a ? a = b, 1 : 0; }
template <class T> bool ckmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }

template <typename t> istream &operator>>(istream &in, vector<t> &vec) {
  for (t &x : vec)
    in >> x;
  return in;
}

template <typename t> ostream &operator<<(ostream &out, vector<t> &vec) {
  int n = SZ(vec);
  For(i, 0, n) {
    out << vec[i];
    if (i < n - 1)
      out << ' ';
  }
  return out;
}

void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V> void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ", ";
  __print(x.second);
  cerr << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? ", " : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}

#ifdef _DEBUG
#define debug1(x) cout << #x " = " << x << endl;
#define debug2(x, y) cout << #x " = " << x << " " #y " = " << y << endl;
#define debug3(x, y, z)                                                        \
  cout << #x " = " << x << " " #y " = " << y << " " #z " = " << z << endl;
#define dbg(x...)                                                              \
  cerr << "\e[91m" << __func__ << ":" << __LINE__ << " [" << #x << "] = [";    \
  _print(x);                                                                   \
  cerr << "\e[39m" << endl;
#else
#define debug1
#define debug2
#define debug3
#define dbg(x...)
#endif

const int N = 200100;
int n, a[N];

void solve() {
  cin >> n;
  For1(i, 1, n) cin >> a[i];

  if (n <= 2) {
    cout << "0\n";
    return;
  }

  int res = 0, x1 = a[1], x2 = INF;

  For1(i, 2, n) {
    if (a[i] <= x1 && a[i] <= x2) {
      if (x1 <= x2) {
        x1 = a[i];
      } else {
        x2 = a[i];
      }
    } else if (a[i] <= x1) {
      x1 = a[i];
    } else if (a[i] <= x2) {
      x2 = a[i];
    } else {
      res++;
      if (x1 <= x2) {
        x1 = a[i];
      } else {
        x2 = a[i];
      }
    }
  }

  cout << res << '\n';
}

int main(void) {
#ifdef _DEBUG
  freopen("c.in", "r", stdin);
#endif
  std::ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  int T = 1;
  cin >> T;

  while (T--) {
    solve();
  }

  return 0;
}