#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define fixed(n) cout << fixed << setprecision(n);
#define mod 1000000007
#define cin(v) for (auto&i:v) cin >> i;
#define cout(v) for (auto&i:v) cout << i << " ";
#define ceil(n,m) (n / m + (n%m != 0))
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
void lil_codi_vert(){
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
#endif
}
//int dx[] = {1, 1, 1, 0, 0, -1, -1, -1};
//int dy[] = {-1, 0, 1, -1, 1, -1, 0, 1};
int main(){
lil_codi_vert();
ll t; cin >> t;
while(t--){
ll n; cin >> n;
vector<ll> v(n); cin(v);
int l = 0, r = 0;
map<ll,ll> ma;
for(int i = 0; i < n; i++){
ma[v[i]] = i;
}
l = r =ma[0];
ll res = n;
for(int i = 1; i < n; i++){
if(ma[i] > l && ma[i] < r) continue;
if(ma[i] > r){
ll left = l - 0;
ll right = ma[i] - r;
//cout << left << " " << right << " " << i << "\n";
res += i * (left * (left+1)/2);
res += i * (right * (right+1)/2);
//res += left + right;
r = ma[i];
}else if(ma[i] < l){
ll left = l - ma[i];
ll right = n - r - 1;
//cout << left << " " << right << " " << i << "\n";
res += i * (left * (left+1)/2);
res += i * (right * (right+1)/2);
//res += left + right;
l = ma[i];
}
}
cout << res << "\n";
}
return 0;
}
Copy