#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
#ifdef eclipse
#define debug(...) do{\
	fprintf(stderr, "LINE %d: (%s) = ",__LINE__, #__VA_ARGS__ ); \
	_do(__VA_ARGS__); \
}while(0)
template<typename T> void _do(T &&_x) {cerr << _x << endl;}
template<typename T, typename ...S> void _do(T &&_x, S &&..._t) {cerr << _x << ", "; _do(_t...);}
#else
#define debug(...)
#endif

const int MAXN = 300005;
int n, in[MAXN], in2[MAXN];

int calc() {
	int ans = 0;
	for (int i = 1; i < n - 1; ++i) {
		if (in[i] < min(in[i-1], in[i+1])) ans++;
		else if (in[i] > max(in[i-1], in[i+1])) ans++;
	} 
	return ans;
}


int calc2(int idx) {
	int x, y; x = y = 0;
	for (int i = idx - 1; i <= idx + 1; ++i) {
		if (i == 0 || i == n - 1) continue;
		if (in[i] < min(in[i-1], in[i+1])) y++;
		else if (in[i] > max(in[i-1], in[i+1])) y++;
	}

	for (int i = idx - 1; i <= idx + 1; ++i) {
		if (i == 0 || i == n - 1) continue;
		if (in2[i] < min(in2[i-1], in2[i+1])) x++;
		else if (in2[i] > max(in2[i-1], in2[i+1])) x++;
	}
	return x - y;
}

signed main (){
	ios::sync_with_stdio(0),cin.tie(0);
	int t;
	cin >> t;
	while (t--) {
		cin >> n;
		for (int i = 0; i < n; ++i) cin >> in[i];
		for (int i = 0; i < n; ++i) in2[i] = in[i];
		int ans, now = calc();
		ans = now;
		for (int i = 1; i < n - 1; ++i) {
			int tmp = in[i];
			in[i] = in[i-1];
			ans = min(ans, now - calc2(i));
			in[i] = in[i+1];
			ans = min(ans, now - calc2(i));
			in[i] = tmp;
		} 
		cout << ans << '\n';
	}
    return 0;
}
