#d21001. 数数

数数

Counting

Problem Description

For two points P(x0,y0)P(x_0, y_0) and Q(x1,y1)Q(x_1, y_1) on the plane, the Manhattan distance between them is d(P,Q)=∣x0−x1∣+∣y0−y1∣d(P, Q) = |x_0 - x_1| + |y_0 - y_1|.

A triple of points p,q,rp, q, r is called bad if and only if d(p,r)=d(p,q)+d(q,r)d(p, r) = d(p, q) + d(q, r).

A sequence aa is called good if and only if you cannot choose three distinct indices i,j,ki, j, k from aa such that (ai,i),(aj,j),(ak,k)(a_i, i), (a_j, j), (a_k, k) form a bad triple.

You are given a sequence aa of length nn. How many subsegments of aa are good?

Input Format

The first line contains an integer TT (1≤T≤50001 \le T \le 5000), the number of test cases.

For each test case:

The first line contains an integer nn (1≤n≤2×1051 \le n \le 2 \times 10^5), the length of aa.

The second line contains the sequence a1,a2,…,ana_1, a_2, \dots, a_n (0≤ai≤1090 \le a_i \le 10^9).

Output Format

Output one number: the number of good subsegments of aa.

Sample Input

3
4
2 4 1 3
5
6 9 1 9 6
2
13 37

Sample Output

10
12
3

Sample Explanation

In the first sample, no subsegment is bad, so ans=4+3+2+1=10ans = 4 + 3 + 2 + 1 = 10.

In the second sample, 1,2,41, 2, 4 is bad, because

d((a1,1),(a4,4))=∣6−9∣+∣1−4∣=6d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6

d((a1,1),(a2,2))=∣6−9∣+∣1−2∣=4d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4

d((a2,2),(a4,4))=∣9−9∣+∣2−4∣=2d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2

so $d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4))$.