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

#define fi first
#define se second

typedef pair<int, int> ii;

int main() {
#ifdef PDTH
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif //PDTH
  ios_base::sync_with_stdio(false);
  cin.tie(NULL); cout.tie(NULL);
  int T; cin >> T;
  while (T--) {
    int n; cin >> n;
    vector<int> a(n);
    for(auto &x : a) cin >> x;
    
    int ans = n+n-1;
    for(int i = 2; i < n; ++i) {
      if (a[i] > a[i-1] && a[i-1] < a[i-2]) {
        ++ans;
        if (i > 2) {
          if (a[i-2] > a[i-3] && !(a[i-3] <= a[i-1] || a[i-2] <= a[i])) ++ans;
        }
      }
      if (a[i] < a[i-1] && a[i-1] > a[i-2]) {
        ++ans;
        if (i > 2) {
          if (a[i-2] < a[i-3] && !(a[i-3] >= a[i-1] || a[i-2] >= a[i])) ++ans;
        }
      }
    }
    cout << ans << endl;
  }
}