#include <cstdio>
#include <algorithm>
using namespace std;
const int N=3e5+5;
int t,n,a[N],res,b[N],maxn;
inline int judge(int i){
    if(i==1||i==n) return 0;
    // 这里用 (a[i]-a[i-1])*(a[i]-a[i+1])>0 会爆 int
    return (a[i]>a[i-1]&&a[i]>a[i+1])||(a[i]<a[i-1]&&a[i]<a[i+1]);
}
int main(){
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n); res=maxn=0;
        for(int i=1;i<=n;i++) scanf("%d",a+i);
        for(int i=1;i<=n;i++) b[i]=judge(i),res+=b[i];
        for(int i=2;i<=n-1;i++){
            int org=b[i-1]+b[i]+b[i+1],tmp=a[i];
            a[i]=a[i-1]; maxn=max(maxn,org-(judge(i-1)+judge(i)+judge(i+1)));
            a[i]=a[i+1]; maxn=max(maxn,org-(judge(i-1)+judge(i)+judge(i+1)));
            a[i]=tmp;
        }
        printf("%d\n",res-maxn);
    }
    return 0;
}