//#include <bits/stdc++.h>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <unordered_map>
#include <map>
using namespace std;
const int N = 500010,M = 80010;
typedef long long ll;
int n,m;
int a[N],h[N];
vector<int> re[N];int t = 0;
int find(int x)
{
	return lower_bound(h+1,h+1+t,x)-h;
}

void slove()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&a[i]);
		re[i].clear();
		h[i] = a[i];
	}
	sort(h+1,h+1+n);
	t = unique(h+1,h+1+n)-h-1;
	for(int i=1;i<=n;i++) a[i] = find(a[i]);;
	//for(int i=1;i<=n;i++) cout<<a[i]<<" ";
	ll da = 0;
	for(int i=1;i<=n;i++) re[a[i]].push_back(i);
	
	//system("pause");
	for(int i=1;i<=t;i++)
	{
		ll res = 0;
		for(auto x:re[i])
		{
			da+=(n-x+1)*res;
			res+=x;
		}
	}
	cout<<da<<"\n";
}
int main()
{
    int T;cin>>T;
    while(T--)
    {
    	slove();
	}
    return 0;
}
