#include<stdio.h>
#include<time.h>
#include<string.h>
#include<stdlib.h>
int i,j,m,n,s,t,x;
int a[1000000];
int main()
{
    while (~scanf("%d",&n))
    {
        while (n--)
        {
            scanf("%d",&m);
            for (i=1;i<=m;i++)
                scanf("%d",&a[i]);
            t=0;
            a[0]=a[1];a[m+1]=a[m];
            for (i=2;i<m;i++)
            {
                if (a[i]>a[i-1]&&a[i]>a[i+1])t++;
                if (a[i]<a[i-1]&&a[i]<a[i+1])t++;
            }
            s=1;
            for (i=4;i<=m;i++)
            {
                if (a[i-3]>a[i-2]&&a[i-2]<a[i-1]&&a[i-1]>a[i]&&2>s){s=2;if (a[i-4]>a[i-3]&&a[i-3]<a[i-1]&&a[i-2]<a[i]&&a[i]>a[i+1])s--;else break;}
                if (a[i-3]<a[i-2]&&a[i-2]>a[i-1]&&a[i-1]<a[i]&&2>s){s=2;if (a[i-4]<a[i-3]&&a[i-3]>a[i-1]&&a[i-2]>a[i]&&a[i]<a[i+1])s--;else break;}
            }
            for (i=5;i<=m;i++)
            {
                if (a[i-4]>a[i-3]&&a[i-3]<a[i-2]&&a[i-2]>a[i-1]&&a[i-1]<a[i])s=3;
                if (a[i-4]<a[i-3]&&a[i-3]>a[i-2]&&a[i-2]<a[i-1]&&a[i-1]>a[i])s=3;
            }
            if (t<=s)printf("0\n");else printf("%d\n",t-s);
        }
    }
}
