#include <bits/stdc++.h>
using namespace std;
typedef int64_t i64;
typedef long long ll;
typedef long double ld;
typedef unsigned long long Ull;

#define endl "\n"
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define pb push_back
void read(int &sum) {sum = 0; char c = getchar( ); bool f = 0; while (c < '0' || c > '9') {if (c == '-') f = 1; c = getchar( );} while (c >= '0' && c <= '9') {sum = sum * 10 + c - '0'; c = getchar( );} if (f) sum = -sum;}
void read(ll &sum) {sum = 0; char c = getchar( ); bool f = 0; while (c < '0' || c > '9') {if (c == '-') f = 1; c = getchar( );} while (c >= '0' && c <= '9') {sum = sum * 10 + c - '0'; c = getchar( );} if (f) sum = -sum;}
void readarr(ll a[], ll n) {for (int i = 0; i < n; i++)read(a[i]);}
void readarr(int a[], int n) {for (int i = 0; i < n; i++)read(a[i]);}
void readvec(vector<ll> &a, ll n) {for (int i = 0; i < n; i++)read(a[i]);}
void readvec(vector<int> &a, int n) {for (int i = 0; i < n; i++)read(a[i]);}
void aout(ll a[], ll n) {for (int i = 0; i < n; i++)cout << a[i] << " ";}
void aout(int a[], int n) {for (int i = 0; i < n; i++)cout << a[i] << " ";}
const int MAXN = (int)1e6 + 7;
const int MOD = 998244353;
const int INF = (int)2e9 + 7;
const ll LINF = (i64)1e18;
const ld PI = 3.1415926535897932384626433832795;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

void solve() {
	int n;
	read(n);
	vector<ll> a(n), b(n), c(n);
	for (int i = 0; i < n; i++) {
		read(a[i]);
		b[i] = a[i];
		c[i] = 0;
	}
	if (n <= 2) {
		cout << 0 << endl;
		return;
	}
	int s = 0, ans = INT_MAX;
	for (int i = 1; i < n - 1; i++) {
		if (a[i] > a[i - 1] && a[i] > a[i + 1]) {
			c[i] = 1;
			s++;
		}
		if (a[i] < a[i - 1] && a[i] < a[i + 1]) {
			c[i] = 1;
			s++;
		}
	}
	ans = s;
	int cnt = s;
	for (int i = 1; i < n - 1; i++) {
		cnt = s;
		if (c[i - 1] == 1)cnt--;
		if (c[i] == 1)cnt--;
		if (c[i + 1] == 1)cnt--;
		b[i] = INT_MIN;
		if (b[i] > b[i - 1] && b[i] > b[i + 1])cnt++;
		if (b[i] < b[i - 1] && b[i] < b[i + 1])cnt++;
		if (i >= 2) {
			if (b[i - 1] > b[i - 2] && b[i - 1] > b[i])cnt++;
			if (b[i - 1] < b[i - 2] && b[i - 1] < b[i])cnt++;
		}
		if (i < n - 2) {
			if (b[i + 1] > b[i + 2] && b[i + 1] > b[i])cnt++;
			if (b[i + 1] < b[i + 2] && b[i + 1] < b[i])cnt++;
		}
		ans = min(ans, cnt);
		cnt = s;
		b[i] = a[i];
		if (c[i - 1] == 1)cnt--;
		if (c[i] == 1)cnt--;
		if (c[i + 1] == 1)cnt--;
		b[i] = a[i - 1];
		if (b[i] > b[i - 1] && b[i] > b[i + 1])cnt++;
		if (b[i] < b[i - 1] && b[i] < b[i + 1])cnt++;
		if (i >= 2) {
			if (b[i - 1] > b[i - 2] && b[i - 1] > b[i])cnt++;
			if (b[i - 1] < b[i - 2] && b[i - 1] < b[i])cnt++;
		}
		if (i < n - 2) {
			if (b[i + 1] > b[i + 2] && b[i + 1] > b[i])cnt++;
			if (b[i + 1] < b[i + 2] && b[i + 1] < b[i])cnt++;
		}
		ans = min(ans, cnt);
		cnt = s;
		b[i] = a[i];
		if (c[i - 1] == 1)cnt--;
		if (c[i] == 1)cnt--;
		if (c[i + 1] == 1)cnt--;
		b[i] = a[i + 1];
		if (b[i] > b[i - 1] && b[i] > b[i + 1])cnt++;
		if (b[i] < b[i - 1] && b[i] < b[i + 1])cnt++;
		if (i >= 2) {
			if (b[i - 1] > b[i - 2] && b[i - 1] > b[i])cnt++;
			if (b[i - 1] < b[i - 2] && b[i - 1] < b[i])cnt++;
		}
		if (i < n - 2) {
			if (b[i + 1] > b[i + 2] && b[i + 1] > b[i])cnt++;
			if (b[i + 1] < b[i + 2] && b[i + 1] < b[i])cnt++;
		}
		ans = min(ans, cnt);
		cnt = s;
		b[i] = a[i];
		if (c[i - 1] == 1)cnt--;
		if (c[i] == 1)cnt--;
		if (c[i + 1] == 1)cnt--;
		b[i] = INT_MAX;
		if (b[i] > b[i - 1] && b[i] > b[i + 1])cnt++;
		if (b[i] < b[i - 1] && b[i] < b[i + 1])cnt++;
		if (i >= 2) {
			if (b[i - 1] > b[i - 2] && b[i - 1] > b[i])cnt++;
			if (b[i - 1] < b[i - 2] && b[i - 1] < b[i])cnt++;
		}
		if (i < n - 2) {
			if (b[i + 1] > b[i + 2] && b[i + 1] > b[i])cnt++;
			if (b[i + 1] < b[i + 2] && b[i + 1] < b[i])cnt++;
		}
		ans = min(ans, cnt);
		cnt = s;
		b[i] = a[i];
	}
	if (c[n - 2] == 1) {
		ans = min(ans, s - 1);
	}
	if (c[1] == 1) {
		ans = min(ans, s - 1);
	}
	cout << ans << endl;
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	int t = 1;
	read(t);
	while (t--)
	{
		solve();
	}
}
