#include <bits/stdc++.h>
#define int long long int
#define ll long long int
#define ld long double
#define getFaster ios_base::sync_with_stdio(false), cin.tie(nullptr)
#define rep(i, init, n) for (int i = init; i < (int)n; i++)
#define rev(i, n, init) for (int i = (int)n; i >= init; i--)
#define MOD1 1e9 + 7
#define MOD2 998244353
#define f first
#define s second
#define endl '\n'
#define pii pair<int, int>
#define tii tuple<int, int>
#define all(v) v.begin(), v.end()
#define mt make_tuple
#define precise(i) cout << fixed << setprecision(i)
#define codejam cout << "Case #" << ii + 1 << ": ";
#define impossible cout << "IMPOSSIBLE" << endl;
#define error(s) throw runtime_error(s)
#define hash hash1
std::mt19937_64 rng(std::chrono::steady_clock::now().time_since_epoch().count());
int myRand(int R)
{
     int val = rng() % R;
     assert(val >= 0);
     return val;
}

const long double PI = atan(1.0) * 4;
const int32_t INF32 = 2e9 + 7;
const int64_t INF64 = 3e18;
const int32_t LOG = 21;
int32_t MOD = MOD1;

using namespace std;

//-------------------DEBUGGING-------------------------
void my_debugger(string s, int LINE_NUM) { cerr << endl; }
template <typename start, typename... end>
void my_debugger(string s, int LINE_NUM, start x, end... y)
{
     if (s.back() != ',')
     {
          s += ',';
          cerr << "LINE(" << LINE_NUM << "): ";
     }
     int i = s.find(',');
     cerr << s.substr(0, i) << " = " << x;
     s = s.substr(i + 1);
     if (!s.empty())
          cerr << ", ";
     my_debugger(s, LINE_NUM, y...);
}

#ifdef AJIT
#define debug(...) my_debugger(#__VA_ARGS__, __LINE__, __VA_ARGS__);
#else
#define debug(...) ;
#endif

//-----------------------------------------------------

int find1(int i, int val, vector<int> &lol, vector<int> &a)
{
     int hill = 1, valley = -1;
     int n = lol.size();
     n--;
     int sub = 0;
     if (lol[i] != 0)
          sub++;
     if (i - 1 >= 1)
     {
          if (lol[i - 1] == hill && a[i - 1] <= val)
               sub++;

          if (lol[i - 1] == valley && a[i - 1] >= val)
               sub++;

          if (i - 1 >= 2 && !lol[i - 1] && a[i - 1] < val && a[i - 1] < a[i - 2])
               sub--;

          if (i - 1 >= 2 && !lol[i - 1] && a[i - 1] > val && a[i - 1] > a[i - 2])
               sub--;
     }
     if (i + 1 <= n)
     {
          if (lol[i + 1] == hill && a[i + 1] <= val)
               sub++;

          if (lol[i + 1] == valley && a[i + 1] >= val)
               sub++;

          if (i + 2 <= n && !lol[i + 1] && a[i + 1] < val && a[i + 1] < a[i + 2])
               sub--;

          if (i + 2 <= n && !lol[i + 1] && a[i + 1] > val && a[i + 1] > a[i + 2])
               sub--;
     }
     return sub;
}

int32_t main()
{
     getFaster;
     //files_init();
     int tests = 1;
     cin >> tests;
     rep(ii, 0, tests)
     {
          int hill = 1, valley = -1;
          int n;
          cin >> n;
          vector<int> a(n + 1);
          vector<int> lol(n + 1, 0);
          rep(i, 1, n + 1) cin >> a[i];
          int cnt = 0;
          int min1 = 0;
          rep(i, 2, n)
          {
               if (a[i] > a[i - 1] && a[i] > a[i + 1])
               {
                    min1++;
                    lol[i] = hill;
               }
               if (a[i] < a[i - 1] && a[i] < a[i + 1])
               {
                    min1++;
                    lol[i] = valley;
               }
          }

          int ans = min1;
          
          rep(i, 1, n + 1)
          {
               if (i - 1 >= 1)
                    ans = min(ans, min1 - find1(i, a[i - 1], lol, a));
               if (i + 1 <= n)
                    ans = min(ans, min1 - find1(i, a[i + 1], lol, a));
          }

          ans = max(ans, 0LL);

          cout << ans << endl;
     }
     return 0;
}