#include <bits/stdc++.h>
#define ll long long
#define all(a) (a).begin(),(a).end()
#define pb push_back
#define fi first
#define se second
#define en "\n"
#define pii pair<int,int>
#define vt vector

using namespace std;

ll p(int n, int m, vector<int>& t)
{
    ll s1 = 0;
    vector<ll> suf(m);
    suf[m-1]=n - t[m-1];
   // cout << suf[m-1] << " ";
    for(int i = m- 2; i >= 0; i--)
    {
        suf[i] = n - t[i] + suf[i+1];
       // cout << suf[i] << " ";
    }

    for(int i = 0; i < m - 1; i++)
    {
        s1 += (t[i]+1)*suf[i+1];
    }
    return s1;
}

void solve()
{
    int n; cin >> n;
    vt<int> a(n); for(int& i : a) cin >> i;
    map<int,vt<int>> m;
    for(int i = 0; i < n; i++)
    {
        m[a[i]].pb(i);
    }
    ll ans = 0;
    for(auto x : m) ans += p(n,x.se.size(),x.se);
    cout << ans << en;
}

int main()
{

    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);

    int t; cin >> t;
    while(t--) solve();


}
