#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define ff first
#define ss second
#define endl '\n'
#define fast ios::sync_with_stdio(false); cin.tie(NULL);
#ifdef LOCAL
#include "debug.h"
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x);
#else
#define debug(x...)
#endif

#define int long long

int check(int i ,vector<int> &a){
	
	int ans = 0;
	if(a[i] > a[i + 1] && a[i] > a[i - 1])
		++ans;
	if(a[i] < a[i - 1] && a[i] < a[i + 1])
		++ans;
	return ans;
}

void solve(){
	
	int n;
	cin>>n;
	vector<int> a(n);
	for(int i = 0 ; i < n ; ++i){
		
		cin>>a[i];
	}
	a.pb(a.back());
	int val = a[0];
	a.insert(a.begin(),val);
	int ans = 0;
	for(int i = 1 ; i <= n ; ++i){
		
		if(a[i] > a[i -1 ] && a[i] > a[i + 1])
			ans++;
		if(a[i] < a[i - 1] && a[i] < a[i + 1])
			ans++;
	}
	int x = ans;
	debug(ans);
	for(int i = 2 ; i <= n - 1 ; ++i){
		
		int org = a[i];
		int cur = check(i - 1 , a ) + check(i , a) + check(i + 1 , a);
		a[i] = a[i - 1];
		int val = check(i - 1 , a ) + check(i , a) + check(i + 1 , a);
		ans = min(ans , x - cur + val);
		a[i] = a[i + 1];
		val = check(i - 1 , a ) + check(i , a) + check(i + 1 , a);
		ans = min(ans , x - cur + val);
		a[i] = org;
	}
	cout<<ans<<endl;
}	
 
signed main(){
	
	fast;
	int t = 1;
	cin>>t;
	while(t--)
		solve();
}