//never give up.... just cry when you have to do 🌵🙂
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(), v.rend()
#define sz(v) v.size()
#define mem(dp) memset(dp, 0 , sizeof dp)
long double pi = acos(-1);
void FILe() { freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout); }
void FAST() { ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); }
ll n, a[200005], l[200005], r[200005];
int main()
{
//freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout);
FAST();
int t;cin >> t;
while (t--)
{
mem(r); mem(l);
cin >> n;
for (int i = 1;i <= n;i++)
{
cin >> a[i];
}
for (int i = 1;i <= n;i++)
{
for (int j = a[i];j <= n && !l[j];j++)
{
l[j] = i;
}
}
for (int i = n;i >=1;i--)
{
for (int j = a[i];j <= n && !r[j];j++)
{
r[j] = i;
}
}
/* for (int i = 0;i <= n;i++)
cout << l[i] << " ";
cout << "\n";
for (int i = 0;i <= n;i++)
cout << r[i] << " ";
cout << "\n";*/
ll ans = 0, res = 0;
for (int i = 1;i <= n;i++)
{
res = (l[i - 1] * (n - r[i - 1] + 1)) - (l[i] * (n - r[i] + 1));
//cout << res << " ";
ans += (res*i);
}
//cout << "\n";
cout <<ans+n << "\n";
}
return 0;
}
Copy