#include<bits/stdc++.h>
const  long long int M= 1000000007;
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()	
#define fr(i,n) for(int i=0;i<n;i++)
#define pb push_back
#define int long long 
#define endl "\n"
using namespace std;
int power(int x,int y){int res=1;while(y>0){if(y&1)res=x*res; y=y>>1;x=x*x;}return res;}
int power(int x,int y,int mod){int res=1;while(y>0){if(y&1)res=x*res%mod; y=y>>1;x=x*x%mod;}return res;}

void nik()
{
	ios_base::sync_with_stdio(false); cin.tie(NULL);

	#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
	#endif
}

 	#define F first 
	#define S second 

const int N=3e5+5;
int a[N];
int sum;
int n;

int check(int i,int val)
{
	if(i<=1 or i>=n)return 0;
	if(val>max(a[i+1],a[i-1]))return 1;
	if(val<min(a[i+1],a[i-1]))return 1;
	return 0;
}

int work(int i,int val)
{
	int store=a[i];
	int contri=0;
	for(int idx=i-1;idx<=i+1;idx++)contri+=check(idx,a[idx]);
		int currans=sum-contri;
	a[i]=val;
	for(int idx=i-1;idx<=i+1;idx++)currans+=check(idx,a[idx]);
	
	a[i]=store;
	return currans;
}

signed main()
{	nik();

		int test=1;
		cin>>test;
		while(test--)
		{	

			sum=0;
			//memset(a,0,sizeof a);
			cin>>n;
			
			fr(i,n)cin>>a[i+1];
			
			
			for(int i=2;i<n;i++)sum+=check(i,a[i]);
		
			int ans=sum;
		for(int i=1;i<=n;i++)
		{
			ans=min(ans,work(i,a[i-1]));
			ans=min(ans,work(i,a[i+1]));
			ans=min(ans,work(i,0));
			ans=min(ans,work(i,2e9));
		}
		cout<<ans<<endl;

		}
		

return 0;
}
