#include <bits/stdc++.h>
using namespace std;
void Solve() {
int n;
cin >> n;
vector<pair<int, int>> a(n);
for (int i = 0; i < n; ++i) {
cin >> a[i].first;
a[i].second = i + 1;
}
sort(a.begin(), a.end());
int minIndex = INT_MAX;
int maxIndex = INT_MIN;
long long sumOfF = 0;
for (int i = 0; i < n; ++i) {
minIndex = min(minIndex, a[i].second);
maxIndex = max(maxIndex, a[i].second);
sumOfF += 1LL * minIndex * (n - maxIndex + 1);
}
cout << sumOfF << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--) {
Solve();
}
return 0;
}
Copy