#include<bits/stdc++.h>
using namespace std;
#define int long long



int32_t main(){

	
	int t;cin>>t;
	while(t--){
		int n;cin>>n;
		int a[n+1];
		for(int i=1;i<=n;i++){
			cin>>a[i];
		}
		int c=0;
		int dp[n+1];
		dp[0]=0;
		if(n<=3){
			cout<<0<<endl;
			continue;
		}
		dp[1]=0;
		dp[2]=0;
		dp[3]=0;
		int dp1[n+1];
		dp1[0]=0;
		dp1[1]=0;
		
		for(int i=2;i<n;i++){
			if(a[i]>a[i-1] && a[i]>a[i+1]){
				dp1[i]=dp1[i-1]+1;
			}
			else if(a[i]<a[i-1] && a[i]<a[i+1]){
				dp1[i]=dp1[i-1]+1;
			}
			else{
				dp1[i]=dp1[i-1];
			}
		}
		dp1[n]=dp1[n-1];


		// for(int i=0;i<=n;i++){
		// 	cout<<dp1[i]<<" ";
		// }
		// cout<<endl;

		int ans=dp1[n-3];
		for(int i=2;i<(n-1);i++){
			int x=dp1[i-2]+dp1[n]-dp1[i+1];
			
			if(a[i+1]>a[i-1] && a[i+1]>a[i+2]){
				x++;
			}
			else if(a[i+1]<a[i-1] && a[i+1]<a[i+2]){
				x++;
			}
			if(x<0){
				x=0;
			}
			ans=min(ans,x);


			



		}


		int yy=dp1[n]-dp1[3];
		ans=min(ans,yy);

		for(int i=3;i<(n-1);i++){
			int x=dp1[i-2]+dp1[n]-dp1[i+1];
			if(a[i-1]>a[i-2] && a[i-1]>a[i+1]){
				x++;
			}
			else if(a[i-1]<a[i-2] && a[i-1]<a[i+1]){
				x++;
			}
			if(x<0){
				x=0;
			}
			ans=min(ans,x);

		}

		cout<<ans<<endl;






	}

	return 0;
}