//#pragma GCC optimize ("O3", "unroll-loops")
//#pragma GCC target ("avx2")
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
#define LL long long
#define PII pair<int, int>
#define PLL pair<LL, LL>
#define all_of(v) (v).begin(), (v).end()
#define sort_unique(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define fi first
#define se second
//const int MAXN = 300005;
//const LL INF = (LL) 1e9 + 8763;
//const LL MOD = (LL) 998244353;
using namespace std;
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

void solve() {
	LL n, ans = 0;
	cin >> n;
	vector<int> A(n + 1);
	map<int, LL> occ, sum;
	for (int i = 1; i <= n; i++) {
		cin >> A[i];
	}
	for (int i = n; i >= 1; i--) {
		LL x = A[i];
		ans += (n * i + i) * occ[x] - i * sum[x];
		occ[x]++;
		sum[x] += i;
	}
	cout << ans << '\n';
}

int main() {
	ios_base::sync_with_stdio(0); cin.tie(0);

	int tc = 1;
	cin >> tc;
	for (int i = 1; i <= tc; i++) {
		solve();
	}

	return 0;
}