// Problem: B. Hills And Valleys
// Contest: Codeforces - Codeforces Round #695 (Div. 2)
// URL: https://codeforces.ml/contest/1467/problem/B
// Memory Limit: 256 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define forn(i,x,n) for(int i = x;i <= n;++i)	

const int N = 3e5+7;
int up[N],dw[N],a[N],n;
int calc(int i)
{
	if(i <= 1 || i >= n)	return 0;
	int res = 0;
	if(a[i] > a[i - 1] && a[i] > a[i + 1])	++res;
	if(a[i] < a[i - 1] && a[i] < a[i + 1])	++res;
	return res;
}
int main()
{
    int T;scanf("%d",&T);
    while(T--)
    {
		scanf("%d",&n);
		forn(i,1,n)	scanf("%d",&a[i]),up[i] = 0,dw[i] = 0;
		up[0] = dw[0] = up[n + 1] = dw[n + 1] = 0;
		int res = 1e9,sum = 0;
		forn(i,2,n - 1)
		{
			if(a[i] > a[i - 1] && a[i] > a[i + 1])	up[i] = 1;
			if(a[i] < a[i - 1] && a[i] < a[i + 1])	dw[i] = 1;
			sum += up[i] + dw[i];
		}
		
		res = sum;

		forn(i,1,n)
		{
			int tmp = sum;
			tmp -= up[i] + dw[i];
			tmp -= up[i - 1] + dw[i - 1];
			tmp -= up[i + 1] + dw[i + 1];	
			// cout << tmp << endl;
			
			int p = a[i];
			a[i] = a[i - 1];
			tmp += calc(i - 1);
			tmp += calc(i);
			tmp += calc(i + 1);
			a[i] = p;
			
			res = min(res,tmp);
			
			tmp = sum;
			tmp -= up[i] + dw[i];
			tmp -= up[i - 1] + dw[i - 1];
			tmp -= up[i + 1] + dw[i + 1];	
			// cout << tmp << endl;
			a[i] = a[i + 1];
			tmp += calc(i - 1);
			tmp += calc(i);
			tmp += calc(i + 1);
			a[i] = p;
			
			res = min(res,tmp);
		}
		printf("%d\n",res);
    }
    return 0;
}
