#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
/********************************************************************/
#define M1 1000000007
#define M2 998244353
const double pi = 3.14159265;

#define ll long long
#define pll pair<ll,ll>

#define forr(i, n) for(ll i=0;i<n;i++)
#define FOR(x) for(ll kk = 0;kk <(x);kk++)

#define pb push_back
#define ppb pop_back
#define mp make_pair
#define MT make_tuple
#define V(a) vector<a>
#define vi vector<ll>

#define CASE(t) ll t; cin>>t; while(t--)

#define endl '\n'
#define space ' '
#define enter cout<<endl
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl

#define all(x) x.begin(), x.end()
#define sortall(x) sort(all(x))
#define collect(v, n) for(ll i = 0; i < n;i++){cin>>v[i];}

#define deci(n) fixed << setprecision(n)		//sets no of digits to show after decimal
#define setcount(n) __builtin_popcount(n)       //the number of ones in the int
#define setcountll(n) __builtin_popcountll(n)   //the number of ones in the ll
#define parity(n) __builtin_parity(n)			//the parity (even or odd) of the number of ones
#define clz(n) __builtin_clz(n)					//the number of zeros at the beginning of the number
#define clzll(n) __builtin_clzll(n)				//the number of zeros at the beginning of the ll
#define ctz(n) __builtin_ctz(n)					//the number of zeros at the end of the number

void deb(ll x) { cout << x << endl;}
void deb(int x) { cout << x << endl;}
void deb(unsigned int x) { cout << x << endl;}
void deb(unsigned ll x) { cout << x << endl;}
void deb(float x) { cout << x << endl;}
void deb(double x) { cout << x << endl;}
void deb(long double x) { cout << x << endl;}
void deb(char x) {cout << '\'' << x << '\'' << endl;}
void deb(string s) { cout << '\"' << s << '\"' << endl;}
void deb(vector<ll> v) { for (auto x : v) cout << x << ' '; cout << endl;}
void deb(bool bb) {cout << (bb ? "True" : "False") << endl;}
void deb(map<ll, ll> mpp) {for (auto x : mpp) cout << x.first << ' ' << x.second << endl;}
void deb(pair<ll, ll> pp) { cout << "{" << pp.first << "," << pp.second << "}" << endl;}

ll gcd(ll x, ll y) {
	if (x == 0) return y;
	return gcd((ll) y % x, (ll) x);
}

template<typename T>
#define o_set(T) tree<T, null_type,less_equal<T>, rb_tree_tag,tree_order_statistics_node_update>
//member functions :
//1. order_of_key(k) : number of elements strictly lesser than k
//2. find_by_order(k) : k-th element in the set
// MULTISET ==> less_equal
// SET ==> less

ll powM(ll x, ll y, ll m) {
	ll ans = 1, r = 1;
	x %= m;
	while (r > 0 && r <= y) {
		if (r & y) {
			ans *= x;
			ans %= m;
		}
		r <<= 1;
		x *= x;
		x %= m;
	}
	return ans;
}

map<long long, long long> factorize(long long n) {
	map<long long, long long> ans;
	for (long long i = 2; i * i <= n; i++) {
		while (n % i == 0) {
			ans[i]++;
			n /= i;
		}
	}
	if (n > 1) {
		ans[n]++;
		n = 1;
	}
	return ans;
}

ll countBits(ll n) {
	ll count = 0;
	while (n)
	{
		count++;
		n >>= 1;
	}
	return count;
}

ll isP(ll n) {
	if (n == 0 || n == 1)
		return 0;
	for (ll i = 2; i <= sqrt(n); i++) {
		if (n % i == 0)
			return 0;
	}
	return 1;
}

/* **********END OF TEMPLATE*********** */
ll min(ll a, ll b) {
	if (a < b)
		return a;
	return b;
}

ll min(ll a, ll b, ll c) {
	return min(a, min(b, c));
}
ll min(ll a, ll b, ll c, ll d) {
	return min(min(a, d), min(b, c));
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	CASE(t) {
		ll n;
		cin >> n;
		// ll ar[n][n] = {};
		vi v(n);
		vector<pair<ll, ll>> ggs;
		ll seq = 1;
		for (ll i = 0 ; i < n; i++) {
			cin >> v[i];
			v[i];
		}
		ll prev = v[0];
		for (ll i = 1; i < n; i++) {
			if (v[i] == prev) {
				seq++;
			}
			else {
				ggs.pb({seq, prev});
				prev = v[i];
				seq = 1;
			}
		}
		ll ans = 0;
		ggs.pb({seq, prev});

		for (auto x : ggs) {
			ll e = x.first;
			ans += (e * (e - 1) * (e - 2) * (e - 3)) / 24;
		}
		for (ll i = 0; i < (ll)ggs.size(); i++) {
			vector<pair<ll, ll>> waiting, waiting2;

			waiting2.clear();
			waiting.clear();
			for (ll j = i + 1; j < (ll)ggs.size(); j++) {
				if (ggs[j].second != ggs[i].second) {
					waiting.pb(ggs[j]);
				}
				else {
					ll e = ggs[i].first + ggs[j].first;
					ans += (e * (e - 1) * (e - 2) * (e - 3)) / 24;
					e = ggs[i].first;
					ans -= (e * (e - 1) * (e - 2) * (e - 3)) / 24;
					e = ggs[j].first;
					ans -= (e * (e - 1) * (e - 2) * (e - 3)) / 24;

					for (ll k = j + 1; k < (ll)ggs.size(); k++) {
						waiting2.pb(ggs[k]);
					}
					ll tmp = 0;
					for (auto x : waiting) {
						for (auto y : waiting2) {
							if (x.second == y.second)
								tmp += (((x.first) * (y.first)) * (ggs[i].first) * (ggs[j].first));
						}
					}
					ans += tmp;
					waiting2.clear();
					waiting.clear();

					break;
				}
			}
		}
		cout << ans << endl;
		// for (ll i = 1 ; i < n; i++) {
		// 	// at position i
		// 	for (ll j = 0; j < n; j++) {
		// 		ar[i][j] += ar[i - 1][j];
		// 	}
		// }
		// for (ll i = 0 ; i < n; i++) {
		// 	for (ll j = 0; j < n; j++) {
		// 		cout << ar[i][j] << ' ';
		// 	}
		// 	cout << endl;
		// }
		// ll ans = 0;
		// for (ll i = 0; i < n; i++) {
		// 	for (ll j = i + 1; j < n; j++) {
		// 		// i ...  j
		// 		// x y.x  y
		// 		// cout << i << ' ' << j << ' ' << ar[j][v[i]] << " " << ar[i][v[i]] << " " << ar[j][v[j]]  << " " << ar[i][v[j]] << endl;

		// 		if (i + 3 <= j && ar[j][v[i]] > ar[i][v[i]] && ar[j][v[j]] >= ar[i][v[j]] + 2) {
		// 			cout << i << ' ' << j << endl;
		// 			ans++;
		// 			// ans += ((ar[j][v[i]] - ar[i][v[i]]) * ( ar[j][v[j]] - ar[i][v[j]]));
		// 		}
		// 	}

		// }
		// cout << ans << endl;

	}
	return 0;

}


#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
/********************************************************************/
#define M1 1000000007
#define M2 998244353
const double pi = 3.14159265;

#define ll long long
#define pll pair<ll,ll>

#define forr(i, n) for(ll i=0;i<n;i++)
#define FOR(x) for(ll kk = 0;kk <(x);kk++)

#define pb push_back
#define ppb pop_back
#define mp make_pair
#define MT make_tuple
#define V(a) vector<a>
#define vi vector<ll>

#define CASE(t) ll t; cin>>t; while(t--)

#define endl '\n'
#define space ' '
#define enter cout<<endl
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl

#define all(x) x.begin(), x.end()
#define sortall(x) sort(all(x))
#define collect(v, n) for(ll i = 0; i < n;i++){cin>>v[i];}

#define deci(n) fixed << setprecision(n)		//sets no of digits to show after decimal
#define setcount(n) __builtin_popcount(n)       //the number of ones in the int
#define setcountll(n) __builtin_popcountll(n)   //the number of ones in the ll
#define parity(n) __builtin_parity(n)			//the parity (even or odd) of the number of ones
#define clz(n) __builtin_clz(n)					//the number of zeros at the beginning of the number
#define clzll(n) __builtin_clzll(n)				//the number of zeros at the beginning of the ll
#define ctz(n) __builtin_ctz(n)					//the number of zeros at the end of the number

void deb(ll x) { cout << x << endl;}
void deb(int x) { cout << x << endl;}
void deb(unsigned int x) { cout << x << endl;}
void deb(unsigned ll x) { cout << x << endl;}
void deb(float x) { cout << x << endl;}
void deb(double x) { cout << x << endl;}
void deb(long double x) { cout << x << endl;}
void deb(char x) {cout << '\'' << x << '\'' << endl;}
void deb(string s) { cout << '\"' << s << '\"' << endl;}
void deb(vector<ll> v) { for (auto x : v) cout << x << ' '; cout << endl;}
void deb(bool bb) {cout << (bb ? "True" : "False") << endl;}
void deb(map<ll, ll> mpp) {for (auto x : mpp) cout << x.first << ' ' << x.second << endl;}
void deb(pair<ll, ll> pp) { cout << "{" << pp.first << "," << pp.second << "}" << endl;}

ll gcd(ll x, ll y) {
	if (x == 0) return y;
	return gcd((ll) y % x, (ll) x);
}

template<typename T>
#define o_set(T) tree<T, null_type,less_equal<T>, rb_tree_tag,tree_order_statistics_node_update>
//member functions :
//1. order_of_key(k) : number of elements strictly lesser than k
//2. find_by_order(k) : k-th element in the set
// MULTISET ==> less_equal
// SET ==> less

ll powM(ll x, ll y, ll m) {
	ll ans = 1, r = 1;
	x %= m;
	while (r > 0 && r <= y) {
		if (r & y) {
			ans *= x;
			ans %= m;
		}
		r <<= 1;
		x *= x;
		x %= m;
	}
	return ans;
}

map<long long, long long> factorize(long long n) {
	map<long long, long long> ans;
	for (long long i = 2; i * i <= n; i++) {
		while (n % i == 0) {
			ans[i]++;
			n /= i;
		}
	}
	if (n > 1) {
		ans[n]++;
		n = 1;
	}
	return ans;
}

ll countBits(ll n) {
	ll count = 0;
	while (n)
	{
		count++;
		n >>= 1;
	}
	return count;
}

ll isP(ll n) {
	if (n == 0 || n == 1)
		return 0;
	for (ll i = 2; i <= sqrt(n); i++) {
		if (n % i == 0)
			return 0;
	}
	return 1;
}

/* **********END OF TEMPLATE*********** */
ll min(ll a, ll b) {
	if (a < b)
		return a;
	return b;
}

ll min(ll a, ll b, ll c) {
	return min(a, min(b, c));
}
ll min(ll a, ll b, ll c, ll d) {
	return min(min(a, d), min(b, c));
}

/*
	* Build Command: *
	g++ -std=c++14 -Wshadow -Wall -o c c.cpp -O2 -Wno-unused-result
*/

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	CASE(t) {
		ll n;
		cin >> n;
		vi v(n);
		ll ar[n][n] = {};
		collect(v, n);
		for (ll i = 0; i < n; i++) {
			v[i]--;
			ar[i][v[i]]++;
		}
		ll pre[n][n] = {}, suf[n][n] = {};
		pre[0][v[0]]++;
		suf[n - 1][v[n - 1]]++;
		for (ll i = 1; i < n; i++) {
			for (ll j = 0; j < n; j++) {
				pre[i][j] += pre[i - 1][j];
				pre[i][j] += ar[i][j];
			}
		}
		for (ll i =  n - 2; i >= 0; i--) {
			for (ll j = 0; j < n; j++) {
				suf[i][j] += suf[i + 1][j];
				suf[i][j] += ar[i][j];
			}
		}
		// for (ll i = 0; i < n; i++) {
		// 	for (auto x : pre[i])
		// 		cout << x << ' ';
		// 	cout << endl;
		// }
		// cout << "***\n";
		// for (ll i = 0; i < n; i++) {
		// 	for (auto x : suf[i])
		// 		cout << x << ' ';
		// 	cout << endl;
		// }
		// cout << "***\n";

		ll ans = 0;
		for (ll i  = 1 ; i < n; i++) {
			for (ll j = i + 1; j < n - 1; j++) {
				ans += (pre[i - 1][v[j]]) * (suf[j + 1][v[i]]);
			}
		}
		cout << ans << endl;
	}
	return 0;

}


#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
/********************************************************************/
#define M1 1000000007
#define M2 998244353
const double pi = 3.14159265;

#define ll long long
#define pll pair<ll,ll>

#define forr(i, n) for(ll i=0;i<n;i++)
#define FOR(x) for(ll kk = 0;kk <(x);kk++)

#define pb push_back
#define ppb pop_back
#define mp make_pair
#define MT make_tuple
#define V(a) vector<a>
#define vi vector<ll>

#define CASE(t) ll t; cin>>t; while(t--)

#define endl '\n'
#define space ' '
#define enter cout<<endl
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl

#define all(x) x.begin(), x.end()
#define sortall(x) sort(all(x))
#define collect(v, n) for(ll i = 0; i < n;i++){cin>>v[i];}

#define deci(n) fixed << setprecision(n)		//sets no of digits to show after decimal
#define setcount(n) __builtin_popcount(n)       //the number of ones in the int
#define setcountll(n) __builtin_popcountll(n)   //the number of ones in the ll
#define parity(n) __builtin_parity(n)			//the parity (even or odd) of the number of ones
#define clz(n) __builtin_clz(n)					//the number of zeros at the beginning of the number
#define clzll(n) __builtin_clzll(n)				//the number of zeros at the beginning of the ll
#define ctz(n) __builtin_ctz(n)					//the number of zeros at the end of the number

void deb(ll x) { cout << x << endl;}
void deb(int x) { cout << x << endl;}
void deb(unsigned int x) { cout << x << endl;}
void deb(unsigned ll x) { cout << x << endl;}
void deb(float x) { cout << x << endl;}
void deb(double x) { cout << x << endl;}
void deb(long double x) { cout << x << endl;}
void deb(char x) {cout << '\'' << x << '\'' << endl;}
void deb(string s) { cout << '\"' << s << '\"' << endl;}
void deb(vector<ll> v) { for (auto x : v) cout << x << ' '; cout << endl;}
void deb(bool bb) {cout << (bb ? "True" : "False") << endl;}
void deb(map<ll, ll> mpp) {for (auto x : mpp) cout << x.first << ' ' << x.second << endl;}
void deb(pair<ll, ll> pp) { cout << "{" << pp.first << "," << pp.second << "}" << endl;}

ll gcd(ll x, ll y) {
	if (x == 0) return y;
	return gcd((ll) y % x, (ll) x);
}

template<typename T>
#define o_set(T) tree<T, null_type,less_equal<T>, rb_tree_tag,tree_order_statistics_node_update>
//member functions :
//1. order_of_key(k) : number of elements strictly lesser than k
//2. find_by_order(k) : k-th element in the set
// MULTISET ==> less_equal
// SET ==> less

ll powM(ll x, ll y, ll m) {
	ll ans = 1, r = 1;
	x %= m;
	while (r > 0 && r <= y) {
		if (r & y) {
			ans *= x;
			ans %= m;
		}
		r <<= 1;
		x *= x;
		x %= m;
	}
	return ans;
}

map<long long, long long> factorize(long long n) {
	map<long long, long long> ans;
	for (long long i = 2; i * i <= n; i++) {
		while (n % i == 0) {
			ans[i]++;
			n /= i;
		}
	}
	if (n > 1) {
		ans[n]++;
		n = 1;
	}
	return ans;
}

ll countBits(ll n) {
	ll count = 0;
	while (n)
	{
		count++;
		n >>= 1;
	}
	return count;
}

ll isP(ll n) {
	if (n == 0 || n == 1)
		return 0;
	for (ll i = 2; i <= sqrt(n); i++) {
		if (n % i == 0)
			return 0;
	}
	return 1;
}

/* **********END OF TEMPLATE*********** */
ll min(ll a, ll b) {
	if (a < b)
		return a;
	return b;
}

ll min(ll a, ll b, ll c) {
	return min(a, min(b, c));
}
ll min(ll a, ll b, ll c, ll d) {
	return min(min(a, d), min(b, c));
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	CASE(t) {
		ll n;
		cin >> n;
		ll ar[n][n] = {};
		vi v(n);
		vector<pair<ll, ll>> ggs;
		ll seq = 1;
		for (ll i = 0 ; i < n; i++) {
			cin >> v[i];
			v[i]--;
			ar[i][v[i]]++;
		}
		ll prev = v[0];
		for (ll i = 1; i < n; i++) {
			if (v[i] == prev) {
				seq++;
			}
			else {
				ggs.pb({seq, prev});
				prev = v[i];
				seq = 1;
			}
		}
		ll ans = 0;
		ggs.pb({seq, prev});

		for (auto x : ggs) {
			ll e = x.first;
			ans += (e * (e - 1) * (e - 2) * (e - 3)) / 24;
			// cout << x.first << ' ' << x.second << endl;
		}
		for (ll i = 0; i < (ll)ggs.size(); i++) {
			ll curr_q = ggs[i].first;
			ll curr_val = ggs[i].second;
			vector<pair<ll, ll>> waiting, waiting2;
			for (ll j = i + 1; j < (ll)ggs.size(); j++) {
				if (ggs[j].second != curr_val) {
					waiting.pb(ggs[j]);
				}
				else {
					for (ll k = j + 1; k < (ll)ggs.size(); k++) {
						waiting2.pb(ggs[k]);
					}
					ll tmp = 0;
					for (auto x : waiting) {
						for (auto y : waiting2) {
							if (x.second == y.second)
								tmp += (((x.first) * (y.first)) * (ggs[i].first) * (ggs[j].first));
						}
					}
					ans += tmp;
					break;
					waiting2.clear();
					waiting.clear();
				}
			}
		}
		cout << ans << endl;
		// for (ll i = 1 ; i < n; i++) {
		// 	// at position i
		// 	for (ll j = 0; j < n; j++) {
		// 		ar[i][j] += ar[i - 1][j];
		// 	}
		// }
		// for (ll i = 0 ; i < n; i++) {
		// 	for (ll j = 0; j < n; j++) {
		// 		cout << ar[i][j] << ' ';
		// 	}
		// 	cout << endl;
		// }
		// ll ans = 0;
		// for (ll i = 0; i < n; i++) {
		// 	for (ll j = i + 1; j < n; j++) {
		// 		// i ...  j
		// 		// x y.x  y
		// 		// cout << i << ' ' << j << ' ' << ar[j][v[i]] << " " << ar[i][v[i]] << " " << ar[j][v[j]]  << " " << ar[i][v[j]] << endl;

		// 		if (i + 3 <= j && ar[j][v[i]] > ar[i][v[i]] && ar[j][v[j]] >= ar[i][v[j]] + 2) {
		// 			cout << i << ' ' << j << endl;
		// 			ans++;
		// 			// ans += ((ar[j][v[i]] - ar[i][v[i]]) * ( ar[j][v[j]] - ar[i][v[j]]));
		// 		}
		// 	}

		// }
		// cout << ans << endl;

	}
	return 0;

}


#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")

#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
/********************************************************************/
#define M1 1000000007
#define M2 998244353
const double pi = 3.14159265;

#define ll long long
#define pll pair<ll,ll>

#define forr(i, n) for(ll i=0;i<n;i++)
#define FOR(x) for(ll kk = 0;kk <(x);kk++)

#define pb push_back
#define ppb pop_back
#define mp make_pair
#define MT make_tuple
#define V(a) vector<a>
#define vi vector<ll>

#define CASE(t) ll t; cin>>t; while(t--)

#define endl '\n'
#define space ' '
#define enter cout<<endl
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl

#define all(x) x.begin(), x.end()
#define sortall(x) sort(all(x))
#define collect(v, n) for(ll i = 0; i < n;i++){cin>>v[i];}

#define deci(n) fixed << setprecision(n)		//sets no of digits to show after decimal
#define setcount(n) __builtin_popcount(n)       //the number of ones in the int
#define setcountll(n) __builtin_popcountll(n)   //the number of ones in the ll
#define parity(n) __builtin_parity(n)			//the parity (even or odd) of the number of ones
#define clz(n) __builtin_clz(n)					//the number of zeros at the beginning of the number
#define clzll(n) __builtin_clzll(n)				//the number of zeros at the beginning of the ll
#define ctz(n) __builtin_ctz(n)					//the number of zeros at the end of the number

void deb(ll x) { cout << x << endl;}
void deb(int x) { cout << x << endl;}
void deb(unsigned int x) { cout << x << endl;}
void deb(unsigned ll x) { cout << x << endl;}
void deb(float x) { cout << x << endl;}
void deb(double x) { cout << x << endl;}
void deb(long double x) { cout << x << endl;}
void deb(char x) {cout << '\'' << x << '\'' << endl;}
void deb(string s) { cout << '\"' << s << '\"' << endl;}
void deb(vector<ll> v) { for (auto x : v) cout << x << ' '; cout << endl;}
void deb(bool bb) {cout << (bb ? "True" : "False") << endl;}
void deb(map<ll, ll> mpp) {for (auto x : mpp) cout << x.first << ' ' << x.second << endl;}
void deb(pair<ll, ll> pp) { cout << "{" << pp.first << "," << pp.second << "}" << endl;}

ll gcd(ll x, ll y) {
	if (x == 0) return y;
	return gcd((ll) y % x, (ll) x);
}

template<typename T>
#define o_set(T) tree<T, null_type,less_equal<T>, rb_tree_tag,tree_order_statistics_node_update>
//member functions :
//1. order_of_key(k) : number of elements strictly lesser than k
//2. find_by_order(k) : k-th element in the set
// MULTISET ==> less_equal
// SET ==> less

ll powM(ll x, ll y, ll m) {
	ll ans = 1, r = 1;
	x %= m;
	while (r > 0 && r <= y) {
		if (r & y) {
			ans *= x;
			ans %= m;
		}
		r <<= 1;
		x *= x;
		x %= m;
	}
	return ans;
}

map<long long, long long> factorize(long long n) {
	map<long long, long long> ans;
	for (long long i = 2; i * i <= n; i++) {
		while (n % i == 0) {
			ans[i]++;
			n /= i;
		}
	}
	if (n > 1) {
		ans[n]++;
		n = 1;
	}
	return ans;
}

ll countBits(ll n) {
	ll count = 0;
	while (n)
	{
		count++;
		n >>= 1;
	}
	return count;
}

ll isP(ll n) {
	if (n == 0 || n == 1)
		return 0;
	for (ll i = 2; i <= sqrt(n); i++) {
		if (n % i == 0)
			return 0;
	}
	return 1;
}

/* **********END OF TEMPLATE*********** */
ll min(ll a, ll b) {
	if (a < b)
		return a;
	return b;
}

ll min(ll a, ll b, ll c) {
	return min(a, min(b, c));
}
ll min(ll a, ll b, ll c, ll d) {
	return min(min(a, d), min(b, c));
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	CASE(t) {
		ll n;
		cin >> n;
		// ll ar[n][n] = {};
		vi v(n);
		vector<pair<ll, ll>> ggs;
		ll ar[n] = {};
		ll seq = 1;
		for (ll i = 0 ; i < n; i++) {
			cin >> v[i];
			ar[v[i] - 1]++;
		}
		ll prev = v[0];
		for (ll i = 1; i < n; i++) {
			if (v[i] == prev) {
				seq++;
			}
			else {
				ggs.pb({seq, prev});
				prev = v[i];
				seq = 1;
			}
		}
		ll ans = 0;
		ggs.pb({seq, prev});
		// for (ll i = 0; i < ggs.size(); i++)
		// 	cout << i << ' ' << ggs[i].first << ' ' << ggs[i].second << endl;
		// cout << "***" << endl;
		for (ll i = 0; i < n; i++) {
			ll e = ar[i];
			ans += (e * (e - 1) * (e - 2) * (e - 3)) / 24;
		}
		for (ll i = 0; i < (ll)ggs.size(); i++) {
			vector<pair<ll, ll>> waiting, waiting2;
			for (ll j = i + 1; j < (ll)ggs.size(); j++) {
				if (ggs[j].second != ggs[i].second) {
					waiting.pb(ggs[j]);
				}
				else {
					for (ll k = j + 1; k < (ll)ggs.size(); k++) {
						waiting2.pb(ggs[k]);
					}
					ll tmp = 0;
					for (auto x : waiting) {
						for (auto y : waiting2) {
							if (x.second == y.second && x.second != ggs[i].second) {
								// cout << i << ' ' << j << ' ' << x.second << ' ' << ggs[i].second << ' ' << (((x.first) * (y.first)) * (ggs[i].first) * (ggs[j].first)) << endl;
								tmp += (((x.first) * (y.first)) * (ggs[i].first) * (ggs[j].first));
							}
						}
					}
					ans += tmp;
					waiting2.clear();
					waiting.pb(ggs[j]);
				}
			}

			waiting2.clear();
			waiting.clear();
		}
		cout << ans << endl;
		// for (ll i = 1 ; i < n; i++) {
		// 	// at position i
		// 	for (ll j = 0; j < n; j++) {
		// 		ar[i][j] += ar[i - 1][j];
		// 	}
		// }
		// for (ll i = 0 ; i < n; i++) {
		// 	for (ll j = 0; j < n; j++) {
		// 		cout << ar[i][j] << ' ';
		// 	}
		// 	cout << endl;
		// }
		// ll ans = 0;
		// for (ll i = 0; i < n; i++) {
		// 	for (ll j = i + 1; j < n; j++) {
		// 		// i ...  j
		// 		// x y.x  y
		// 		// cout << i << ' ' << j << ' ' << ar[j][v[i]] << " " << ar[i][v[i]] << " " << ar[j][v[j]]  << " " << ar[i][v[j]] << endl;

		// 		if (i + 3 <= j && ar[j][v[i]] > ar[i][v[i]] && ar[j][v[j]] >= ar[i][v[j]] + 2) {
		// 			cout << i << ' ' << j << endl;
		// 			ans++;
		// 			// ans += ((ar[j][v[i]] - ar[i][v[i]]) * ( ar[j][v[j]] - ar[i][v[j]]));
		// 		}
		// 	}

		// }
		// cout << ans << endl;

	}
	return 0;

}


#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")

#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
/********************************************************************/
#define M1 1000000007
#define M2 998244353
const double pi = 3.14159265;

#define ll int
#define pll pair<ll,ll>

#define forr(i, n) for(ll i=0;i<n;i++)
#define FOR(x) for(ll kk = 0;kk <(x);kk++)

#define pb push_back
#define ppb pop_back
#define mp make_pair
#define MT make_tuple
#define V(a) vector<a>
#define vi vector<ll>

#define CASE(t) ll t; cin>>t; while(t--)

#define endl '\n'
#define space ' '
#define enter cout<<endl
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl

#define all(x) x.begin(), x.end()
#define sortall(x) sort(all(x))
#define collect(v, n) for(ll i = 0; i < n;i++){cin>>v[i];}

#define deci(n) fixed << setprecision(n)		//sets no of digits to show after decimal
#define setcount(n) __builtin_popcount(n)       //the number of ones in the int
#define setcountll(n) __builtin_popcountll(n)   //the number of ones in the ll
#define parity(n) __builtin_parity(n)			//the parity (even or odd) of the number of ones
#define clz(n) __builtin_clz(n)					//the number of zeros at the beginning of the number
#define clzll(n) __builtin_clzll(n)				//the number of zeros at the beginning of the ll
#define ctz(n) __builtin_ctz(n)					//the number of zeros at the end of the number

// void deb(ll x) { cout << x << endl;}
// void deb(int x) { cout << x << endl;}
// void deb(unsigned int x) { cout << x << endl;}
// void deb(unsigned ll x) { cout << x << endl;}
// void deb(float x) { cout << x << endl;}
// void deb(double x) { cout << x << endl;}
// void deb(long double x) { cout << x << endl;}
// void deb(char x) {cout << '\'' << x << '\'' << endl;}
// void deb(string s) { cout << '\"' << s << '\"' << endl;}
// void deb(vector<ll> v) { for (auto x : v) cout << x << ' '; cout << endl;}
// void deb(bool bb) {cout << (bb ? "True" : "False") << endl;}
// void deb(map<ll, ll> mpp) {for (auto x : mpp) cout << x.first << ' ' << x.second << endl;}
// void deb(pair<ll, ll> pp) { cout << "{" << pp.first << "," << pp.second << "}" << endl;}

ll gcd(ll x, ll y) {
	if (x == 0) return y;
	return gcd((ll) y % x, (ll) x);
}

template<typename T>
#define o_set(T) tree<T, null_type,less_equal<T>, rb_tree_tag,tree_order_statistics_node_update>
//member functions :
//1. order_of_key(k) : number of elements strictly lesser than k
//2. find_by_order(k) : k-th element in the set
// MULTISET ==> less_equal
// SET ==> less

ll powM(ll x, ll y, ll m) {
	ll ans = 1, r = 1;
	x %= m;
	while (r > 0 && r <= y) {
		if (r & y) {
			ans *= x;
			ans %= m;
		}
		r <<= 1;
		x *= x;
		x %= m;
	}
	return ans;
}

map<long long, long long> factorize(long long n) {
	map<long long, long long> ans;
	for (long long i = 2; i * i <= n; i++) {
		while (n % i == 0) {
			ans[i]++;
			n /= i;
		}
	}
	if (n > 1) {
		ans[n]++;
		n = 1;
	}
	return ans;
}

ll countBits(ll n) {
	ll count = 0;
	while (n)
	{
		count++;
		n >>= 1;
	}
	return count;
}

ll isP(ll n) {
	if (n == 0 || n == 1)
		return 0;
	for (ll i = 2; i <= sqrt(n); i++) {
		if (n % i == 0)
			return 0;
	}
	return 1;
}

/* **********END OF TEMPLATE*********** */
ll min(ll a, ll b) {
	if (a < b)
		return a;
	return b;
}

ll min(ll a, ll b, ll c) {
	return min(a, min(b, c));
}
ll min(ll a, ll b, ll c, ll d) {
	return min(min(a, d), min(b, c));
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	CASE(t) {
		ll n;
		cin >> n;
		vi v(n);
		vector<pair<ll, ll>> ggs;
		ll ar[n] = {};
		ll seq = 1;
		for (ll i = 0 ; i < n; i++) {
			cin >> v[i];
			ar[v[i] - 1]++;
		}
		ll prev = v[0];
		for (ll i = 1; i < n; i++) {
			if (v[i] == prev) {
				seq++;
			}
			else {
				ggs.pb({seq, prev});
				prev = v[i];
				seq = 1;
			}
		}
		ll ans = 0;
		ggs.pb({seq, prev});
		for (ll i = 0; i < n; i++) {
			ll e = ar[i];
			ans += (e * (e - 1) * (e - 2) * (e - 3)) / 24;
		}
		for (ll i = 0; i < (ll)ggs.size(); i++) {
			vector<pair<ll, ll>> waiting, waiting2;
			for (ll j = i + 1; j < (ll)ggs.size(); j++) {
				if (ggs[j].second != ggs[i].second) {
					waiting.pb(ggs[j]);
				}
				else {
					for (ll k = j + 1; k < (ll)ggs.size(); k++) {
						waiting2.pb(ggs[k]);
					}
					ll tmp = 0;
					for (auto x : waiting) {
						for (auto y : waiting2) {
							if (x.second == y.second && x.second != ggs[i].second) {
								// cout << i << ' ' << j << ' ' << x.second << ' ' << ggs[i].second << ' ' << (((x.first) * (y.first)) * (ggs[i].first) * (ggs[j].first)) << endl;
								tmp += (((x.first) * (y.first)) * (ggs[i].first) * (ggs[j].first));
							}
						}
					}
					ans += tmp;
					waiting2.clear();
					waiting.pb(ggs[j]);
				}
			}

			waiting2.clear();
			waiting.clear();
		}
		cout << ans << endl;

	}
	return 0;

}


#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")

#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
/********************************************************************/
#define M1 1000000007
#define M2 998244353
const double pi = 3.14159265;

#define ll int
#define pll pair<ll,ll>

#define forr(i, n) for(ll i=0;i<n;i++)
#define FOR(x) for(ll kk = 0;kk <(x);kk++)

#define pb push_back
#define ppb pop_back
#define mp make_pair
#define MT make_tuple
#define V(a) vector<a>
#define vi vector<ll>

#define CASE(t) ll t; cin>>t; while(t--)

#define endl '\n'
#define space ' '
#define enter cout<<endl
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl

#define all(x) x.begin(), x.end()
#define sortall(x) sort(all(x))
#define collect(v, n) for(ll i = 0; i < n;i++){cin>>v[i];}

#define deci(n) fixed << setprecision(n)		//sets no of digits to show after decimal
#define setcount(n) __builtin_popcount(n)       //the number of ones in the int
#define setcountll(n) __builtin_popcountll(n)   //the number of ones in the ll
#define parity(n) __builtin_parity(n)			//the parity (even or odd) of the number of ones
#define clz(n) __builtin_clz(n)					//the number of zeros at the beginning of the number
#define clzll(n) __builtin_clzll(n)				//the number of zeros at the beginning of the ll
#define ctz(n) __builtin_ctz(n)					//the number of zeros at the end of the number

// void deb(ll x) { cout << x << endl;}
// void deb(int x) { cout << x << endl;}
// void deb(unsigned int x) { cout << x << endl;}
// void deb(unsigned ll x) { cout << x << endl;}
// void deb(float x) { cout << x << endl;}
// void deb(double x) { cout << x << endl;}
// void deb(long double x) { cout << x << endl;}
// void deb(char x) {cout << '\'' << x << '\'' << endl;}
// void deb(string s) { cout << '\"' << s << '\"' << endl;}
// void deb(vector<ll> v) { for (auto x : v) cout << x << ' '; cout << endl;}
// void deb(bool bb) {cout << (bb ? "True" : "False") << endl;}
// void deb(map<ll, ll> mpp) {for (auto x : mpp) cout << x.first << ' ' << x.second << endl;}
// void deb(pair<ll, ll> pp) { cout << "{" << pp.first << "," << pp.second << "}" << endl;}

ll gcd(ll x, ll y) {
	if (x == 0) return y;
	return gcd((ll) y % x, (ll) x);
}

template<typename T>
#define o_set(T) tree<T, null_type,less_equal<T>, rb_tree_tag,tree_order_statistics_node_update>
//member functions :
//1. order_of_key(k) : number of elements strictly lesser than k
//2. find_by_order(k) : k-th element in the set
// MULTISET ==> less_equal
// SET ==> less

ll powM(ll x, ll y, ll m) {
	ll ans = 1, r = 1;
	x %= m;
	while (r > 0 && r <= y) {
		if (r & y) {
			ans *= x;
			ans %= m;
		}
		r <<= 1;
		x *= x;
		x %= m;
	}
	return ans;
}

map<long long, long long> factorize(long long n) {
	map<long long, long long> ans;
	for (long long i = 2; i * i <= n; i++) {
		while (n % i == 0) {
			ans[i]++;
			n /= i;
		}
	}
	if (n > 1) {
		ans[n]++;
		n = 1;
	}
	return ans;
}

ll countBits(ll n) {
	ll count = 0;
	while (n)
	{
		count++;
		n >>= 1;
	}
	return count;
}

ll isP(ll n) {
	if (n == 0 || n == 1)
		return 0;
	for (ll i = 2; i <= sqrt(n); i++) {
		if (n % i == 0)
			return 0;
	}
	return 1;
}

/* **********END OF TEMPLATE*********** */
ll min(ll a, ll b) {
	if (a < b)
		return a;
	return b;
}

ll min(ll a, ll b, ll c) {
	return min(a, min(b, c));
}
ll min(ll a, ll b, ll c, ll d) {
	return min(min(a, d), min(b, c));
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	CASE(t) {
		ll n;
		cin >> n;
		vi v(n);
		vector<pair<ll, ll>> ggs;
		ll ar[n] = {};
		ll seq = 1;
		for (ll i = 0 ; i < n; i++) {
			cin >> v[i];
			ar[v[i] - 1]++;
		}
		ll prev = v[0];
		for (ll i = 1; i < n; i++) {
			if (v[i] == prev) {
				seq++;
			}
			else {
				ggs.pb({seq, prev});
				prev = v[i];
				seq = 1;
			}
		}
		long long ans = 0;
		ggs.pb({seq, prev});
		for (ll i = 0; i < n; i++) {
			long long e = ar[i];
			ans += ((long long)e * (long long)(e - 1) * (long long)(e - 2) * (long long)(e - 3)) / 24;
		}
		for (ll i = 0; i < (ll)ggs.size(); i++) {
			vector<pair<ll, ll>> waiting, waiting2;
			for (ll j = i + 1; j < (ll)ggs.size(); j++) {
				if (ggs[j].second != ggs[i].second) {
					waiting.pb(ggs[j]);
				}
				else {
					for (ll k = j + 1; k < (ll)ggs.size(); k++) {
						waiting2.pb(ggs[k]);
					}
					long long tmp = 0;
					for (auto x : waiting) {
						for (auto y : waiting2) {
							if (x.second == y.second && x.second != ggs[i].second) {
								// cout << i << ' ' << j << ' ' << x.second << ' ' << ggs[i].second << ' ' << (((x.first) * (y.first)) * (ggs[i].first) * (ggs[j].first)) << endl;
								tmp += (long long)(((long long)(x.first) * (long long)(y.first)) * (long long) (ggs[i].first) * (long long)(ggs[j].first));
							}
						}
					}
					ans += tmp;
					waiting2.clear();
					waiting.pb(ggs[j]);
				}
			}

			waiting2.clear();
			waiting.clear();
		}
		cout << ans << endl;

	}
	return 0;

}


#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
/********************************************************************/
#define M1 1000000007
#define M2 998244353
const double pi = 3.14159265;

#define ll long long
#define pll pair<ll,ll>

#define forr(i, n) for(ll i=0;i<n;i++)
#define FOR(x) for(ll kk = 0;kk <(x);kk++)

#define pb push_back
#define ppb pop_back
#define mp make_pair
#define MT make_tuple
#define V(a) vector<a>
#define vi vector<ll>

#define CASE(t) ll t; cin>>t; while(t--)

#define endl '\n'
#define space ' '
#define enter cout<<endl
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl

#define all(x) x.begin(), x.end()
#define sortall(x) sort(all(x))
#define collect(v, n) for(ll i = 0; i < n;i++){cin>>v[i];}

#define deci(n) fixed << setprecision(n)		//sets no of digits to show after decimal
#define setcount(n) __builtin_popcount(n)       //the number of ones in the int
#define setcountll(n) __builtin_popcountll(n)   //the number of ones in the ll
#define parity(n) __builtin_parity(n)			//the parity (even or odd) of the number of ones
#define clz(n) __builtin_clz(n)					//the number of zeros at the beginning of the number
#define clzll(n) __builtin_clzll(n)				//the number of zeros at the beginning of the ll
#define ctz(n) __builtin_ctz(n)					//the number of zeros at the end of the number

void deb(ll x) { cout << x << endl;}
void deb(int x) { cout << x << endl;}
void deb(unsigned int x) { cout << x << endl;}
void deb(unsigned ll x) { cout << x << endl;}
void deb(float x) { cout << x << endl;}
void deb(double x) { cout << x << endl;}
void deb(long double x) { cout << x << endl;}
void deb(char x) {cout << '\'' << x << '\'' << endl;}
void deb(string s) { cout << '\"' << s << '\"' << endl;}
void deb(vector<ll> v) { for (auto x : v) cout << x << ' '; cout << endl;}
void deb(bool bb) {cout << (bb ? "True" : "False") << endl;}
void deb(map<ll, ll> mpp) {for (auto x : mpp) cout << x.first << ' ' << x.second << endl;}
void deb(pair<ll, ll> pp) { cout << "{" << pp.first << "," << pp.second << "}" << endl;}

ll gcd(ll x, ll y) {
	if (x == 0) return y;
	return gcd((ll) y % x, (ll) x);
}

template<typename T>
#define o_set(T) tree<T, null_type,less_equal<T>, rb_tree_tag,tree_order_statistics_node_update>
//member functions :
//1. order_of_key(k) : number of elements strictly lesser than k
//2. find_by_order(k) : k-th element in the set
// MULTISET ==> less_equal
// SET ==> less

ll powM(ll x, ll y, ll m) {
	ll ans = 1, r = 1;
	x %= m;
	while (r > 0 && r <= y) {
		if (r & y) {
			ans *= x;
			ans %= m;
		}
		r <<= 1;
		x *= x;
		x %= m;
	}
	return ans;
}

map<long long, long long> factorize(long long n) {
	map<long long, long long> ans;
	for (long long i = 2; i * i <= n; i++) {
		while (n % i == 0) {
			ans[i]++;
			n /= i;
		}
	}
	if (n > 1) {
		ans[n]++;
		n = 1;
	}
	return ans;
}

ll countBits(ll n) {
	ll count = 0;
	while (n)
	{
		count++;
		n >>= 1;
	}
	return count;
}

ll isP(ll n) {
	if (n == 0 || n == 1)
		return 0;
	for (ll i = 2; i <= sqrt(n); i++) {
		if (n % i == 0)
			return 0;
	}
	return 1;
}

/* **********END OF TEMPLATE*********** */
ll min(ll a, ll b) {
	if (a < b)
		return a;
	return b;
}

ll min(ll a, ll b, ll c) {
	return min(a, min(b, c));
}
ll min(ll a, ll b, ll c, ll d) {
	return min(min(a, d), min(b, c));
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	CASE(t) {
		ll n;
		cin >> n;
		ll ar[n][n] = {};
		vi v(n);
		vector<pair<ll, ll>> ggs;
		ll seq = 1;
		for (ll i = 0 ; i < n; i++) {
			cin >> v[i];
			v[i]--;
			ar[i][v[i]]++;
		}
		ll prev = v[0];
		for (ll i = 1; i < n; i++) {
			if (v[i] == prev) {
				seq++;
			}
			else {
				ggs.pb({seq, prev});
				prev = v[i];
				seq = 1;
			}
		}
		ll ans = 0;
		ggs.pb({seq, prev});

		for (auto x : ggs) {
			ll e = x.first;
			ans += (e * (e - 1) * (e - 2) * (e - 3)) / 24;
			// cout << x.first << ' ' << x.second << endl;
		}
		for (ll i = 0; i < (ll)ggs.size(); i++) {
			ll curr_q = ggs[i].first;
			ll curr_val = ggs[i].second;
			vector<pair<ll, ll>> waiting, waiting2;
			for (ll j = i + 1; j < (ll)ggs.size(); j++) {
				if (ggs[j].second != curr_val) {
					waiting.pb(ggs[j]);
				}
				else {

					ll e = ggs[i].first + ggs[j].first;
					ans += (e * (e - 1) * (e - 2) * (e - 3)) / 24;
					e = ggs[i].first;
					ans -= (e * (e - 1) * (e - 2) * (e - 3)) / 24;
					e = ggs[j].first;
					ans -= (e * (e - 1) * (e - 2) * (e - 3)) / 24;

					for (ll k = j + 1; k < (ll)ggs.size(); k++) {
						waiting2.pb(ggs[k]);
					}
					ll tmp = 0;
					for (auto x : waiting) {
						for (auto y : waiting2) {
							if (x.second == y.second)
								tmp += (((x.first) * (y.first)) * (ggs[i].first) * (ggs[j].first));
						}
					}
					ans += tmp;
					break;
					waiting2.clear();
					waiting.clear();
				}
			}
		}
		cout << ans << endl;
		// for (ll i = 1 ; i < n; i++) {
		// 	// at position i
		// 	for (ll j = 0; j < n; j++) {
		// 		ar[i][j] += ar[i - 1][j];
		// 	}
		// }
		// for (ll i = 0 ; i < n; i++) {
		// 	for (ll j = 0; j < n; j++) {
		// 		cout << ar[i][j] << ' ';
		// 	}
		// 	cout << endl;
		// }
		// ll ans = 0;
		// for (ll i = 0; i < n; i++) {
		// 	for (ll j = i + 1; j < n; j++) {
		// 		// i ...  j
		// 		// x y.x  y
		// 		// cout << i << ' ' << j << ' ' << ar[j][v[i]] << " " << ar[i][v[i]] << " " << ar[j][v[j]]  << " " << ar[i][v[j]] << endl;

		// 		if (i + 3 <= j && ar[j][v[i]] > ar[i][v[i]] && ar[j][v[j]] >= ar[i][v[j]] + 2) {
		// 			cout << i << ' ' << j << endl;
		// 			ans++;
		// 			// ans += ((ar[j][v[i]] - ar[i][v[i]]) * ( ar[j][v[j]] - ar[i][v[j]]));
		// 		}
		// 	}

		// }
		// cout << ans << endl;

	}
	return 0;

}


#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")

#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
/********************************************************************/
#define M1 1000000007
#define M2 998244353
const double pi = 3.14159265;

#define ll int
#define pll pair<ll,ll>

#define forr(i, n) for(ll i=0;i<n;i++)
#define FOR(x) for(ll kk = 0;kk <(x);kk++)

#define pb push_back
#define ppb pop_back
#define mp make_pair
#define MT make_tuple
#define V(a) vector<a>
#define vi vector<ll>

#define CASE(t) ll t; cin>>t; while(t--)

#define endl '\n'
#define space ' '
#define enter cout<<endl
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl

#define all(x) x.begin(), x.end()
#define sortall(x) sort(all(x))
#define collect(v, n) for(ll i = 0; i < n;i++){cin>>v[i];}

#define deci(n) fixed << setprecision(n)		//sets no of digits to show after decimal
#define setcount(n) __builtin_popcount(n)       //the number of ones in the int
#define setcountll(n) __builtin_popcountll(n)   //the number of ones in the ll
#define parity(n) __builtin_parity(n)			//the parity (even or odd) of the number of ones
#define clz(n) __builtin_clz(n)					//the number of zeros at the beginning of the number
#define clzll(n) __builtin_clzll(n)				//the number of zeros at the beginning of the ll
#define ctz(n) __builtin_ctz(n)					//the number of zeros at the end of the number

// void deb(ll x) { cout << x << endl;}
// void deb(int x) { cout << x << endl;}
// void deb(unsigned int x) { cout << x << endl;}
// void deb(unsigned ll x) { cout << x << endl;}
// void deb(float x) { cout << x << endl;}
// void deb(double x) { cout << x << endl;}
// void deb(long double x) { cout << x << endl;}
// void deb(char x) {cout << '\'' << x << '\'' << endl;}
// void deb(string s) { cout << '\"' << s << '\"' << endl;}
// void deb(vector<ll> v) { for (auto x : v) cout << x << ' '; cout << endl;}
// void deb(bool bb) {cout << (bb ? "True" : "False") << endl;}
// void deb(map<ll, ll> mpp) {for (auto x : mpp) cout << x.first << ' ' << x.second << endl;}
// void deb(pair<ll, ll> pp) { cout << "{" << pp.first << "," << pp.second << "}" << endl;}

ll gcd(ll x, ll y) {
	if (x == 0) return y;
	return gcd((ll) y % x, (ll) x);
}

template<typename T>
#define o_set(T) tree<T, null_type,less_equal<T>, rb_tree_tag,tree_order_statistics_node_update>
//member functions :
//1. order_of_key(k) : number of elements strictly lesser than k
//2. find_by_order(k) : k-th element in the set
// MULTISET ==> less_equal
// SET ==> less

ll powM(ll x, ll y, ll m) {
	ll ans = 1, r = 1;
	x %= m;
	while (r > 0 && r <= y) {
		if (r & y) {
			ans *= x;
			ans %= m;
		}
		r <<= 1;
		x *= x;
		x %= m;
	}
	return ans;
}

map<long long, long long> factorize(long long n) {
	map<long long, long long> ans;
	for (long long i = 2; i * i <= n; i++) {
		while (n % i == 0) {
			ans[i]++;
			n /= i;
		}
	}
	if (n > 1) {
		ans[n]++;
		n = 1;
	}
	return ans;
}

ll countBits(ll n) {
	ll count = 0;
	while (n)
	{
		count++;
		n >>= 1;
	}
	return count;
}

ll isP(ll n) {
	if (n == 0 || n == 1)
		return 0;
	for (ll i = 2; i <= sqrt(n); i++) {
		if (n % i == 0)
			return 0;
	}
	return 1;
}

/* **********END OF TEMPLATE*********** */
ll min(ll a, ll b) {
	if (a < b)
		return a;
	return b;
}

ll min(ll a, ll b, ll c) {
	return min(a, min(b, c));
}
ll min(ll a, ll b, ll c, ll d) {
	return min(min(a, d), min(b, c));
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	CASE(t) {
		ll n;
		cin >> n;
		vi v(n);
		vector<pair<ll, ll>> ggs;
		ll ar[n] = {};
		ll seq = 1;
		for (ll i = 0 ; i < n; i++) {
			cin >> v[i];
			ar[v[i] - 1]++;
		}
		ll prev = v[0];
		for (ll i = 1; i < n; i++) {
			if (v[i] == prev) {
				seq++;
			}
			else {
				ggs.pb({seq, prev});
				prev = v[i];
				seq = 1;
			}
		}
		long long ans = 0;
		ggs.pb({seq, prev});
		for (ll i = 0; i < n; i++) {
			ll e = ar[i];
			ans += (e * (e - 1) * (e - 2) * (e - 3)) / 24;
		}
		for (ll i = 0; i < (ll)ggs.size(); i++) {
			vector<pair<ll, ll>> waiting, waiting2;
			for (ll j = i + 1; j < (ll)ggs.size(); j++) {
				if (ggs[j].second != ggs[i].second) {
					waiting.pb(ggs[j]);
				}
				else {
					for (ll k = j + 1; k < (ll)ggs.size(); k++) {
						waiting2.pb(ggs[k]);
					}
					long long tmp = 0;
					for (auto x : waiting) {
						for (auto y : waiting2) {
							if (x.second == y.second && x.second != ggs[i].second) {
								// cout << i << ' ' << j << ' ' << x.second << ' ' << ggs[i].second << ' ' << (((x.first) * (y.first)) * (ggs[i].first) * (ggs[j].first)) << endl;
								tmp += (long long)(((long long)(x.first) * (long long)(y.first)) * (long long) (ggs[i].first) * (long long)(ggs[j].first));
							}
						}
					}
					ans += tmp;
					waiting2.clear();
					waiting.pb(ggs[j]);
				}
			}

			waiting2.clear();
			waiting.clear();
		}
		cout << ans << endl;

	}
	return 0;

}


#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")

#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
/********************************************************************/
#define M1 1000000007
#define M2 998244353
const double pi = 3.14159265;

#define ll int
#define pll pair<ll,ll>

#define forr(i, n) for(ll i=0;i<n;i++)
#define FOR(x) for(ll kk = 0;kk <(x);kk++)

#define pb push_back
#define ppb pop_back
#define mp make_pair
#define MT make_tuple
#define V(a) vector<a>
#define vi vector<ll>

#define CASE(t) ll t; cin>>t; while(t--)

#define endl '\n'
#define space ' '
#define enter cout<<endl
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl

#define all(x) x.begin(), x.end()
#define sortall(x) sort(all(x))
#define collect(v, n) for(ll i = 0; i < n;i++){cin>>v[i];}

#define deci(n) fixed << setprecision(n)		//sets no of digits to show after decimal
#define setcount(n) __builtin_popcount(n)       //the number of ones in the int
#define setcountll(n) __builtin_popcountll(n)   //the number of ones in the ll
#define parity(n) __builtin_parity(n)			//the parity (even or odd) of the number of ones
#define clz(n) __builtin_clz(n)					//the number of zeros at the beginning of the number
#define clzll(n) __builtin_clzll(n)				//the number of zeros at the beginning of the ll
#define ctz(n) __builtin_ctz(n)					//the number of zeros at the end of the number

void deb(ll x) { cout << x << endl;}
void deb(int x) { cout << x << endl;}
void deb(unsigned int x) { cout << x << endl;}
void deb(unsigned ll x) { cout << x << endl;}
void deb(float x) { cout << x << endl;}
void deb(double x) { cout << x << endl;}
void deb(long double x) { cout << x << endl;}
void deb(char x) {cout << '\'' << x << '\'' << endl;}
void deb(string s) { cout << '\"' << s << '\"' << endl;}
void deb(vector<ll> v) { for (auto x : v) cout << x << ' '; cout << endl;}
void deb(bool bb) {cout << (bb ? "True" : "False") << endl;}
void deb(map<ll, ll> mpp) {for (auto x : mpp) cout << x.first << ' ' << x.second << endl;}
void deb(pair<ll, ll> pp) { cout << "{" << pp.first << "," << pp.second << "}" << endl;}

ll gcd(ll x, ll y) {
	if (x == 0) return y;
	return gcd((ll) y % x, (ll) x);
}

template<typename T>
#define o_set(T) tree<T, null_type,less_equal<T>, rb_tree_tag,tree_order_statistics_node_update>
//member functions :
//1. order_of_key(k) : number of elements strictly lesser than k
//2. find_by_order(k) : k-th element in the set
// MULTISET ==> less_equal
// SET ==> less

ll powM(ll x, ll y, ll m) {
	ll ans = 1, r = 1;
	x %= m;
	while (r > 0 && r <= y) {
		if (r & y) {
			ans *= x;
			ans %= m;
		}
		r <<= 1;
		x *= x;
		x %= m;
	}
	return ans;
}

map<long long, long long> factorize(long long n) {
	map<long long, long long> ans;
	for (long long i = 2; i * i <= n; i++) {
		while (n % i == 0) {
			ans[i]++;
			n /= i;
		}
	}
	if (n > 1) {
		ans[n]++;
		n = 1;
	}
	return ans;
}

ll countBits(ll n) {
	ll count = 0;
	while (n)
	{
		count++;
		n >>= 1;
	}
	return count;
}

ll isP(ll n) {
	if (n == 0 || n == 1)
		return 0;
	for (ll i = 2; i <= sqrt(n); i++) {
		if (n % i == 0)
			return 0;
	}
	return 1;
}

/* **********END OF TEMPLATE*********** */
ll min(ll a, ll b) {
	if (a < b)
		return a;
	return b;
}

ll min(ll a, ll b, ll c) {
	return min(a, min(b, c));
}
ll min(ll a, ll b, ll c, ll d) {
	return min(min(a, d), min(b, c));
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	CASE(t) {
		ll n;
		cin >> n;
		vi v(n);
		vector<pair<ll, ll>> ggs;
		ll ar[n] = {};
		ll seq = 1;
		for (ll i = 0 ; i < n; i++) {
			cin >> v[i];
			ar[v[i] - 1]++;
		}
		ll prev = v[0];
		for (ll i = 1; i < n; i++) {
			if (v[i] == prev) {
				seq++;
			}
			else {
				ggs.pb({seq, prev});
				prev = v[i];
				seq = 1;
			}
		}
		ll ans = 0;
		ggs.pb({seq, prev});
		for (ll i = 0; i < n; i++) {
			ll e = ar[i];
			ans += (e * (e - 1) * (e - 2) * (e - 3)) / 24;
		}
		for (ll i = 0; i < (ll)ggs.size(); i++) {
			vector<pair<ll, ll>> waiting, waiting2;
			for (ll j = i + 1; j < (ll)ggs.size(); j++) {
				if (ggs[j].second != ggs[i].second) {
					waiting.pb(ggs[j]);
				}
				else {
					for (ll k = j + 1; k < (ll)ggs.size(); k++) {
						waiting2.pb(ggs[k]);
					}
					ll tmp = 0;
					for (auto x : waiting) {
						for (auto y : waiting2) {
							if (x.second == y.second && x.second != ggs[i].second) {
								// cout << i << ' ' << j << ' ' << x.second << ' ' << ggs[i].second << ' ' << (((x.first) * (y.first)) * (ggs[i].first) * (ggs[j].first)) << endl;
								tmp += (((x.first) * (y.first)) * (ggs[i].first) * (ggs[j].first));
							}
						}
					}
					ans += tmp;
					waiting2.clear();
					waiting.pb(ggs[j]);
				}
			}

			waiting2.clear();
			waiting.clear();
		}
		cout << ans << endl;

	}
	return 0;

}


#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
/********************************************************************/
#define M1 1000000007
#define M2 998244353
const double pi = 3.14159265;

#define ll long long
#define pll pair<ll,ll>

#define forr(i, n) for(ll i=0;i<n;i++)
#define FOR(x) for(ll kk = 0;kk <(x);kk++)

#define pb push_back
#define ppb pop_back
#define mp make_pair
#define MT make_tuple
#define V(a) vector<a>
#define vi vector<ll>

#define CASE(t) ll t; cin>>t; while(t--)

#define endl '\n'
#define space ' '
#define enter cout<<endl
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl

#define all(x) x.begin(), x.end()
#define sortall(x) sort(all(x))
#define collect(v, n) for(ll i = 0; i < n;i++){cin>>v[i];}

#define deci(n) fixed << setprecision(n)		//sets no of digits to show after decimal
#define setcount(n) __builtin_popcount(n)       //the number of ones in the int
#define setcountll(n) __builtin_popcountll(n)   //the number of ones in the ll
#define parity(n) __builtin_parity(n)			//the parity (even or odd) of the number of ones
#define clz(n) __builtin_clz(n)					//the number of zeros at the beginning of the number
#define clzll(n) __builtin_clzll(n)				//the number of zeros at the beginning of the ll
#define ctz(n) __builtin_ctz(n)					//the number of zeros at the end of the number

void deb(ll x) { cout << x << endl;}
void deb(int x) { cout << x << endl;}
void deb(unsigned int x) { cout << x << endl;}
void deb(unsigned ll x) { cout << x << endl;}
void deb(float x) { cout << x << endl;}
void deb(double x) { cout << x << endl;}
void deb(long double x) { cout << x << endl;}
void deb(char x) {cout << '\'' << x << '\'' << endl;}
void deb(string s) { cout << '\"' << s << '\"' << endl;}
void deb(vector<ll> v) { for (auto x : v) cout << x << ' '; cout << endl;}
void deb(bool bb) {cout << (bb ? "True" : "False") << endl;}
void deb(map<ll, ll> mpp) {for (auto x : mpp) cout << x.first << ' ' << x.second << endl;}
void deb(pair<ll, ll> pp) { cout << "{" << pp.first << "," << pp.second << "}" << endl;}

ll gcd(ll x, ll y) {
	if (x == 0) return y;
	return gcd((ll) y % x, (ll) x);
}

template<typename T>
#define o_set(T) tree<T, null_type,less_equal<T>, rb_tree_tag,tree_order_statistics_node_update>
//member functions :
//1. order_of_key(k) : number of elements strictly lesser than k
//2. find_by_order(k) : k-th element in the set
// MULTISET ==> less_equal
// SET ==> less

ll powM(ll x, ll y, ll m) {
	ll ans = 1, r = 1;
	x %= m;
	while (r > 0 && r <= y) {
		if (r & y) {
			ans *= x;
			ans %= m;
		}
		r <<= 1;
		x *= x;
		x %= m;
	}
	return ans;
}

map<long long, long long> factorize(long long n) {
	map<long long, long long> ans;
	for (long long i = 2; i * i <= n; i++) {
		while (n % i == 0) {
			ans[i]++;
			n /= i;
		}
	}
	if (n > 1) {
		ans[n]++;
		n = 1;
	}
	return ans;
}

ll countBits(ll n) {
	ll count = 0;
	while (n)
	{
		count++;
		n >>= 1;
	}
	return count;
}

ll isP(ll n) {
	if (n == 0 || n == 1)
		return 0;
	for (ll i = 2; i <= sqrt(n); i++) {
		if (n % i == 0)
			return 0;
	}
	return 1;
}

/* **********END OF TEMPLATE*********** */
ll min(ll a, ll b) {
	if (a < b)
		return a;
	return b;
}

ll min(ll a, ll b, ll c) {
	return min(a, min(b, c));
}
ll min(ll a, ll b, ll c, ll d) {
	return min(min(a, d), min(b, c));
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	CASE(t) {
		ll n;
		cin >> n;
		// ll ar[n][n] = {};
		vi v(n);
		vector<pair<ll, ll>> ggs;
		ll ar[n] = {};
		ll seq = 1;
		for (ll i = 0 ; i < n; i++) {
			cin >> v[i];
			ar[v[i] - 1]++;
		}
		ll prev = v[0];
		for (ll i = 1; i < n; i++) {
			if (v[i] == prev) {
				seq++;
			}
			else {
				ggs.pb({seq, prev});
				prev = v[i];
				seq = 1;
			}
		}
		ll ans = 0;
		ggs.pb({seq, prev});
		for (ll i = 0; i < n; i++) {
			ll e = ar[i];
			ans += (e * (e - 1) * (e - 2) * (e - 3)) / 24;
		}
		for (ll i = 0; i < (ll)ggs.size(); i++) {
			vector<pair<ll, ll>> waiting, waiting2;
			for (ll j = i + 1; j < (ll)ggs.size(); j++) {
				if (ggs[j].second != ggs[i].second) {
					waiting.pb(ggs[j]);
				}
				else {

					for (ll k = j + 1; k < (ll)ggs.size(); k++) {
						waiting2.pb(ggs[k]);
					}
					// waiting2.pb(ggs[j]);
					ll tmp = 0;
					for (auto x : waiting) {
						for (auto y : waiting2) {
							if (x.second == y.second)
								tmp += (((x.first) * (y.first)) * (ggs[i].first) * (ggs[j].first));
						}
					}
					ans += tmp;
					waiting2.clear();
					waiting.clear();
					break;
				}
			}

			waiting2.clear();
			waiting.clear();
		}
		cout << ans << endl;
		// for (ll i = 1 ; i < n; i++) {
		// 	// at position i
		// 	for (ll j = 0; j < n; j++) {
		// 		ar[i][j] += ar[i - 1][j];
		// 	}
		// }
		// for (ll i = 0 ; i < n; i++) {
		// 	for (ll j = 0; j < n; j++) {
		// 		cout << ar[i][j] << ' ';
		// 	}
		// 	cout << endl;
		// }
		// ll ans = 0;
		// for (ll i = 0; i < n; i++) {
		// 	for (ll j = i + 1; j < n; j++) {
		// 		// i ...  j
		// 		// x y.x  y
		// 		// cout << i << ' ' << j << ' ' << ar[j][v[i]] << " " << ar[i][v[i]] << " " << ar[j][v[j]]  << " " << ar[i][v[j]] << endl;

		// 		if (i + 3 <= j && ar[j][v[i]] > ar[i][v[i]] && ar[j][v[j]] >= ar[i][v[j]] + 2) {
		// 			cout << i << ' ' << j << endl;
		// 			ans++;
		// 			// ans += ((ar[j][v[i]] - ar[i][v[i]]) * ( ar[j][v[j]] - ar[i][v[j]]));
		// 		}
		// 	}

		// }
		// cout << ans << endl;

	}
	return 0;

}


#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
/********************************************************************/
#define M1 1000000007
#define M2 998244353
const double pi = 3.14159265;

#define ll long long
#define pll pair<ll,ll>

#define forr(i, n) for(ll i=0;i<n;i++)
#define FOR(x) for(ll kk = 0;kk <(x);kk++)

#define pb push_back
#define ppb pop_back
#define mp make_pair
#define MT make_tuple
#define V(a) vector<a>
#define vi vector<ll>

#define CASE(t) ll t; cin>>t; while(t--)

#define endl '\n'
#define space ' '
#define enter cout<<endl
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl

#define all(x) x.begin(), x.end()
#define sortall(x) sort(all(x))
#define collect(v, n) for(ll i = 0; i < n;i++){cin>>v[i];}

#define deci(n) fixed << setprecision(n)		//sets no of digits to show after decimal
#define setcount(n) __builtin_popcount(n)       //the number of ones in the int
#define setcountll(n) __builtin_popcountll(n)   //the number of ones in the ll
#define parity(n) __builtin_parity(n)			//the parity (even or odd) of the number of ones
#define clz(n) __builtin_clz(n)					//the number of zeros at the beginning of the number
#define clzll(n) __builtin_clzll(n)				//the number of zeros at the beginning of the ll
#define ctz(n) __builtin_ctz(n)					//the number of zeros at the end of the number

void deb(ll x) { cout << x << endl;}
void deb(int x) { cout << x << endl;}
void deb(unsigned int x) { cout << x << endl;}
void deb(unsigned ll x) { cout << x << endl;}
void deb(float x) { cout << x << endl;}
void deb(double x) { cout << x << endl;}
void deb(long double x) { cout << x << endl;}
void deb(char x) {cout << '\'' << x << '\'' << endl;}
void deb(string s) { cout << '\"' << s << '\"' << endl;}
void deb(vector<ll> v) { for (auto x : v) cout << x << ' '; cout << endl;}
void deb(bool bb) {cout << (bb ? "True" : "False") << endl;}
void deb(map<ll, ll> mpp) {for (auto x : mpp) cout << x.first << ' ' << x.second << endl;}
void deb(pair<ll, ll> pp) { cout << "{" << pp.first << "," << pp.second << "}" << endl;}

ll gcd(ll x, ll y) {
	if (x == 0) return y;
	return gcd((ll) y % x, (ll) x);
}

template<typename T>
#define o_set(T) tree<T, null_type,less_equal<T>, rb_tree_tag,tree_order_statistics_node_update>
//member functions :
//1. order_of_key(k) : number of elements strictly lesser than k
//2. find_by_order(k) : k-th element in the set
// MULTISET ==> less_equal
// SET ==> less

ll powM(ll x, ll y, ll m) {
	ll ans = 1, r = 1;
	x %= m;
	while (r > 0 && r <= y) {
		if (r & y) {
			ans *= x;
			ans %= m;
		}
		r <<= 1;
		x *= x;
		x %= m;
	}
	return ans;
}

map<long long, long long> factorize(long long n) {
	map<long long, long long> ans;
	for (long long i = 2; i * i <= n; i++) {
		while (n % i == 0) {
			ans[i]++;
			n /= i;
		}
	}
	if (n > 1) {
		ans[n]++;
		n = 1;
	}
	return ans;
}

ll countBits(ll n) {
	ll count = 0;
	while (n)
	{
		count++;
		n >>= 1;
	}
	return count;
}

ll isP(ll n) {
	if (n == 0 || n == 1)
		return 0;
	for (ll i = 2; i <= sqrt(n); i++) {
		if (n % i == 0)
			return 0;
	}
	return 1;
}

/* **********END OF TEMPLATE*********** */
ll min(ll a, ll b) {
	if (a < b)
		return a;
	return b;
}

ll min(ll a, ll b, ll c) {
	return min(a, min(b, c));
}
ll min(ll a, ll b, ll c, ll d) {
	return min(min(a, d), min(b, c));
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	CASE(t) {
		ll n;
		cin >> n;
		// ll ar[n][n] = {};
		vi v(n);
		vector<pair<ll, ll>> ggs;
		ll ar[n] = {};
		ll seq = 1;
		for (ll i = 0 ; i < n; i++) {
			cin >> v[i];
			ar[v[i] - 1]++;
		}
		ll prev = v[0];
		for (ll i = 1; i < n; i++) {
			if (v[i] == prev) {
				seq++;
			}
			else {
				ggs.pb({seq, prev});
				prev = v[i];
				seq = 1;
			}
		}
		ll ans = 0;
		ggs.pb({seq, prev});

		// for (auto x : ggs) {
		// 	ll e = x.first;
		// 	ans += (e * (e - 1) * (e - 2) * (e - 3)) / 24;
		// }
		for (ll i = 0; i < n; i++) {
			ll e = ar[i];
			ans += (e * (e - 1) * (e - 2) * (e - 3)) / 24;
		}
		for (ll i = 0; i < (ll)ggs.size(); i++) {
			vector<pair<ll, ll>> waiting, waiting2;

			// waiting.pb(ggs[i]);

			for (ll j = i + 1; j < (ll)ggs.size(); j++) {
				if (ggs[j].second != ggs[i].second) {
					waiting.pb(ggs[j]);
				}
				else {
					ll e = ggs[i].first + ggs[j].first;
					ans += (e * (e - 1) * (e - 2) * (e - 3)) / 24;
					e = ggs[i].first;
					ans -= (e * (e - 1) * (e - 2) * (e - 3)) / 24;
					e = ggs[j].first;
					ans -= (e * (e - 1) * (e - 2) * (e - 3)) / 24;

					for (ll k = j + 1; k < (ll)ggs.size(); k++) {
						waiting2.pb(ggs[k]);
					}
					// waiting2.pb(ggs[j]);
					ll tmp = 0;
					for (auto x : waiting) {
						for (auto y : waiting2) {
							if (x.second == y.second)
								tmp += (((x.first) * (y.first)) * (ggs[i].first) * (ggs[j].first));
						}
					}
					ans += tmp;
					waiting2.clear();
					waiting.clear();
					break;
				}
			}

			waiting2.clear();
			waiting.clear();
		}
		cout << ans << endl;
		// for (ll i = 1 ; i < n; i++) {
		// 	// at position i
		// 	for (ll j = 0; j < n; j++) {
		// 		ar[i][j] += ar[i - 1][j];
		// 	}
		// }
		// for (ll i = 0 ; i < n; i++) {
		// 	for (ll j = 0; j < n; j++) {
		// 		cout << ar[i][j] << ' ';
		// 	}
		// 	cout << endl;
		// }
		// ll ans = 0;
		// for (ll i = 0; i < n; i++) {
		// 	for (ll j = i + 1; j < n; j++) {
		// 		// i ...  j
		// 		// x y.x  y
		// 		// cout << i << ' ' << j << ' ' << ar[j][v[i]] << " " << ar[i][v[i]] << " " << ar[j][v[j]]  << " " << ar[i][v[j]] << endl;

		// 		if (i + 3 <= j && ar[j][v[i]] > ar[i][v[i]] && ar[j][v[j]] >= ar[i][v[j]] + 2) {
		// 			cout << i << ' ' << j << endl;
		// 			ans++;
		// 			// ans += ((ar[j][v[i]] - ar[i][v[i]]) * ( ar[j][v[j]] - ar[i][v[j]]));
		// 		}
		// 	}

		// }
		// cout << ans << endl;

	}
	return 0;

}


#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
/********************************************************************/
#define M1 1000000007
#define M2 998244353
const double pi = 3.14159265;

#define ll long long
#define pll pair<ll,ll>

#define forr(i, n) for(ll i=0;i<n;i++)
#define FOR(x) for(ll kk = 0;kk <(x);kk++)

#define pb push_back
#define ppb pop_back
#define mp make_pair
#define MT make_tuple
#define V(a) vector<a>
#define vi vector<ll>

#define CASE(t) ll t; cin>>t; while(t--)

#define endl '\n'
#define space ' '
#define enter cout<<endl
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl

#define all(x) x.begin(), x.end()
#define sortall(x) sort(all(x))
#define collect(v, n) for(ll i = 0; i < n;i++){cin>>v[i];}

#define deci(n) fixed << setprecision(n)		//sets no of digits to show after decimal
#define setcount(n) __builtin_popcount(n)       //the number of ones in the int
#define setcountll(n) __builtin_popcountll(n)   //the number of ones in the ll
#define parity(n) __builtin_parity(n)			//the parity (even or odd) of the number of ones
#define clz(n) __builtin_clz(n)					//the number of zeros at the beginning of the number
#define clzll(n) __builtin_clzll(n)				//the number of zeros at the beginning of the ll
#define ctz(n) __builtin_ctz(n)					//the number of zeros at the end of the number

void deb(ll x) { cout << x << endl;}
void deb(int x) { cout << x << endl;}
void deb(unsigned int x) { cout << x << endl;}
void deb(unsigned ll x) { cout << x << endl;}
void deb(float x) { cout << x << endl;}
void deb(double x) { cout << x << endl;}
void deb(long double x) { cout << x << endl;}
void deb(char x) {cout << '\'' << x << '\'' << endl;}
void deb(string s) { cout << '\"' << s << '\"' << endl;}
void deb(vector<ll> v) { for (auto x : v) cout << x << ' '; cout << endl;}
void deb(bool bb) {cout << (bb ? "True" : "False") << endl;}
void deb(map<ll, ll> mpp) {for (auto x : mpp) cout << x.first << ' ' << x.second << endl;}
void deb(pair<ll, ll> pp) { cout << "{" << pp.first << "," << pp.second << "}" << endl;}

ll gcd(ll x, ll y) {
	if (x == 0) return y;
	return gcd((ll) y % x, (ll) x);
}

template<typename T>
#define o_set(T) tree<T, null_type,less_equal<T>, rb_tree_tag,tree_order_statistics_node_update>
//member functions :
//1. order_of_key(k) : number of elements strictly lesser than k
//2. find_by_order(k) : k-th element in the set
// MULTISET ==> less_equal
// SET ==> less

ll powM(ll x, ll y, ll m) {
	ll ans = 1, r = 1;
	x %= m;
	while (r > 0 && r <= y) {
		if (r & y) {
			ans *= x;
			ans %= m;
		}
		r <<= 1;
		x *= x;
		x %= m;
	}
	return ans;
}

map<long long, long long> factorize(long long n) {
	map<long long, long long> ans;
	for (long long i = 2; i * i <= n; i++) {
		while (n % i == 0) {
			ans[i]++;
			n /= i;
		}
	}
	if (n > 1) {
		ans[n]++;
		n = 1;
	}
	return ans;
}

ll countBits(ll n) {
	ll count = 0;
	while (n)
	{
		count++;
		n >>= 1;
	}
	return count;
}

ll isP(ll n) {
	if (n == 0 || n == 1)
		return 0;
	for (ll i = 2; i <= sqrt(n); i++) {
		if (n % i == 0)
			return 0;
	}
	return 1;
}

/* **********END OF TEMPLATE*********** */
ll min(ll a, ll b) {
	if (a < b)
		return a;
	return b;
}

ll min(ll a, ll b, ll c) {
	return min(a, min(b, c));
}
ll min(ll a, ll b, ll c, ll d) {
	return min(min(a, d), min(b, c));
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	CASE(t) {
		ll n;
		cin >> n;
		// ll ar[n][n] = {};
		vi v(n);
		vector<pair<ll, ll>> ggs;
		ll ar[n] = {};
		ll seq = 1;
		for (ll i = 0 ; i < n; i++) {
			cin >> v[i];
			ar[v[i] - 1]++;
		}
		ll prev = v[0];
		for (ll i = 1; i < n; i++) {
			if (v[i] == prev) {
				seq++;
			}
			else {
				ggs.pb({seq, prev});
				prev = v[i];
				seq = 1;
			}
		}
		ll ans = 0;
		ggs.pb({seq, prev});
		// for (ll i = 0; i < ggs.size(); i++)
		// 	cout << i << ' ' << ggs[i].first << ' ' << ggs[i].second << endl;
		// cout << "***" << endl;
		for (ll i = 0; i < n; i++) {
			ll e = ar[i];
			ans += (e * (e - 1) * (e - 2) * (e - 3)) / 24;
		}
		for (ll i = 0; i < (ll)ggs.size(); i++) {
			vector<pair<ll, ll>> waiting, waiting2;
			for (ll j = i + 1; j < (ll)ggs.size(); j++) {
				if (ggs[j].second != ggs[i].second) {
					waiting.pb(ggs[j]);
				}
				else {
					for (ll k = j + 1; k < (ll)ggs.size(); k++) {
						waiting2.pb(ggs[k]);
					}
					ll tmp = 0;
					for (auto x : waiting) {
						for (auto y : waiting2) {
							if (x.second == y.second && x.second != ggs[i].second) {
								// cout << i << ' ' << j << ' ' << x.second << ' ' << ggs[i].second << ' ' << (((x.first) * (y.first)) * (ggs[i].first) * (ggs[j].first)) << endl;
								tmp += (((x.first) * (y.first)) * (ggs[i].first) * (ggs[j].first));
							}
						}
					}
					ans += tmp;
					waiting2.clear();
					waiting.pb(ggs[j]);
				}
			}

			waiting2.clear();
			waiting.clear();
		}
		cout << ans << endl;
		// for (ll i = 1 ; i < n; i++) {
		// 	// at position i
		// 	for (ll j = 0; j < n; j++) {
		// 		ar[i][j] += ar[i - 1][j];
		// 	}
		// }
		// for (ll i = 0 ; i < n; i++) {
		// 	for (ll j = 0; j < n; j++) {
		// 		cout << ar[i][j] << ' ';
		// 	}
		// 	cout << endl;
		// }
		// ll ans = 0;
		// for (ll i = 0; i < n; i++) {
		// 	for (ll j = i + 1; j < n; j++) {
		// 		// i ...  j
		// 		// x y.x  y
		// 		// cout << i << ' ' << j << ' ' << ar[j][v[i]] << " " << ar[i][v[i]] << " " << ar[j][v[j]]  << " " << ar[i][v[j]] << endl;

		// 		if (i + 3 <= j && ar[j][v[i]] > ar[i][v[i]] && ar[j][v[j]] >= ar[i][v[j]] + 2) {
		// 			cout << i << ' ' << j << endl;
		// 			ans++;
		// 			// ans += ((ar[j][v[i]] - ar[i][v[i]]) * ( ar[j][v[j]] - ar[i][v[j]]));
		// 		}
		// 	}

		// }
		// cout << ans << endl;

	}
	return 0;

}


#pragma GCC optimize("Ofast")
#pragma GCC target("avv,axx2,fma")
#pragma GCC optimization("unroll-loops")

#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
/********************************************************************/
#define M1 1000000007
#define M2 998244353
const double pi = 3.14159265;

#define ll long long
#define pll pair<ll,ll>

#define forr(i, n) for(ll i=0;i<n;i++)
#define FOR(x) for(ll kk = 0;kk <(x);kk++)

#define pb push_back
#define ppb pop_back
#define mp make_pair
#define MT make_tuple
#define V(a) vector<a>
#define vi vector<ll>

#define CASE(t) ll t; cin>>t; while(t--)

#define endl '\n'
#define space ' '
#define enter cout<<endl
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl

#define all(x) x.begin(), x.end()
#define sortall(x) sort(all(x))
#define collect(v, n) for(ll i = 0; i < n;i++){cin>>v[i];}

#define deci(n) fixed << setprecision(n)		//sets no of digits to show after decimal
#define setcount(n) __builtin_popcount(n)       //the number of ones in the int
#define setcountll(n) __builtin_popcountll(n)   //the number of ones in the ll
#define parity(n) __builtin_parity(n)			//the parity (even or odd) of the number of ones
#define clz(n) __builtin_clz(n)					//the number of zeros at the beginning of the number
#define clzll(n) __builtin_clzll(n)				//the number of zeros at the beginning of the ll
#define ctz(n) __builtin_ctz(n)					//the number of zeros at the end of the number

void deb(ll x) { cout << x << endl;}
void deb(int x) { cout << x << endl;}
void deb(unsigned int x) { cout << x << endl;}
void deb(unsigned ll x) { cout << x << endl;}
void deb(float x) { cout << x << endl;}
void deb(double x) { cout << x << endl;}
void deb(long double x) { cout << x << endl;}
void deb(char x) {cout << '\'' << x << '\'' << endl;}
void deb(string s) { cout << '\"' << s << '\"' << endl;}
void deb(vector<ll> v) { for (auto x : v) cout << x << ' '; cout << endl;}
void deb(bool bb) {cout << (bb ? "True" : "False") << endl;}
void deb(map<ll, ll> mpp) {for (auto x : mpp) cout << x.first << ' ' << x.second << endl;}
void deb(pair<ll, ll> pp) { cout << "{" << pp.first << "," << pp.second << "}" << endl;}

ll gcd(ll x, ll y) {
	if (x == 0) return y;
	return gcd((ll) y % x, (ll) x);
}

template<typename T>
#define o_set(T) tree<T, null_type,less_equal<T>, rb_tree_tag,tree_order_statistics_node_update>
//member functions :
//1. order_of_key(k) : number of elements strictly lesser than k
//2. find_by_order(k) : k-th element in the set
// MULTISET ==> less_equal
// SET ==> less

ll powM(ll x, ll y, ll m) {
	ll ans = 1, r = 1;
	x %= m;
	while (r > 0 && r <= y) {
		if (r & y) {
			ans *= x;
			ans %= m;
		}
		r <<= 1;
		x *= x;
		x %= m;
	}
	return ans;
}

map<long long, long long> factorize(long long n) {
	map<long long, long long> ans;
	for (long long i = 2; i * i <= n; i++) {
		while (n % i == 0) {
			ans[i]++;
			n /= i;
		}
	}
	if (n > 1) {
		ans[n]++;
		n = 1;
	}
	return ans;
}

ll countBits(ll n) {
	ll count = 0;
	while (n)
	{
		count++;
		n >>= 1;
	}
	return count;
}

ll isP(ll n) {
	if (n == 0 || n == 1)
		return 0;
	for (ll i = 2; i <= sqrt(n); i++) {
		if (n % i == 0)
			return 0;
	}
	return 1;
}

/* **********END OF TEMPLATE*********** */
ll min(ll a, ll b) {
	if (a < b)
		return a;
	return b;
}

ll min(ll a, ll b, ll c) {
	return min(a, min(b, c));
}
ll min(ll a, ll b, ll c, ll d) {
	return min(min(a, d), min(b, c));
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	CASE(t) {
		ll n;
		cin >> n;
		// ll ar[n][n] = {};
		vi v(n);
		vector<pair<ll, ll>> ggs;
		ll ar[n] = {};
		ll seq = 1;
		for (ll i = 0 ; i < n; i++) {
			cin >> v[i];
			ar[v[i] - 1]++;
		}
		ll prev = v[0];
		for (ll i = 1; i < n; i++) {
			if (v[i] == prev) {
				seq++;
			}
			else {
				ggs.pb({seq, prev});
				prev = v[i];
				seq = 1;
			}
		}
		ll ans = 0;
		ggs.pb({seq, prev});
		// for (ll i = 0; i < ggs.size(); i++)
		// 	cout << i << ' ' << ggs[i].first << ' ' << ggs[i].second << endl;
		// cout << "***" << endl;
		for (ll i = 0; i < n; i++) {
			ll e = ar[i];
			ans += (e * (e - 1) * (e - 2) * (e - 3)) / 24;
		}
		for (ll i = 0; i < (ll)ggs.size(); i++) {
			vector<pair<ll, ll>> waiting, waiting2;
			for (ll j = i + 1; j < (ll)ggs.size(); j++) {
				if (ggs[j].second != ggs[i].second) {
					waiting.pb(ggs[j]);
				}
				else {
					for (ll k = j + 1; k < (ll)ggs.size(); k++) {
						waiting2.pb(ggs[k]);
					}
					ll tmp = 0;
					for (auto x : waiting) {
						for (auto y : waiting2) {
							if (x.second == y.second && x.second != ggs[i].second) {
								// cout << i << ' ' << j << ' ' << x.second << ' ' << ggs[i].second << ' ' << (((x.first) * (y.first)) * (ggs[i].first) * (ggs[j].first)) << endl;
								tmp += (((x.first) * (y.first)) * (ggs[i].first) * (ggs[j].first));
							}
						}
					}
					ans += tmp;
					waiting2.clear();
					waiting.pb(ggs[j]);
				}
			}

			waiting2.clear();
			waiting.clear();
		}
		cout << ans << endl;
		// for (ll i = 1 ; i < n; i++) {
		// 	// at position i
		// 	for (ll j = 0; j < n; j++) {
		// 		ar[i][j] += ar[i - 1][j];
		// 	}
		// }
		// for (ll i = 0 ; i < n; i++) {
		// 	for (ll j = 0; j < n; j++) {
		// 		cout << ar[i][j] << ' ';
		// 	}
		// 	cout << endl;
		// }
		// ll ans = 0;
		// for (ll i = 0; i < n; i++) {
		// 	for (ll j = i + 1; j < n; j++) {
		// 		// i ...  j
		// 		// x y.x  y
		// 		// cout << i << ' ' << j << ' ' << ar[j][v[i]] << " " << ar[i][v[i]] << " " << ar[j][v[j]]  << " " << ar[i][v[j]] << endl;

		// 		if (i + 3 <= j && ar[j][v[i]] > ar[i][v[i]] && ar[j][v[j]] >= ar[i][v[j]] + 2) {
		// 			cout << i << ' ' << j << endl;
		// 			ans++;
		// 			// ans += ((ar[j][v[i]] - ar[i][v[i]]) * ( ar[j][v[j]] - ar[i][v[j]]));
		// 		}
		// 	}

		// }
		// cout << ans << endl;

	}
	return 0;

}


#include <bits/stdc++.h>

using namespace std;

const int mxN = 3e3 + 2;
int vals[mxN];
int r[1000000];

int compress(pair<int, int> a)
{
    return a * mxN + b;
}

void solve()
{
    int n;
    cin >> n;

    for (int i = 1; i <= n; i++)
        cin >> vals[i];

    map<pair<int, int>, int> l;

    for (int i = 2; i <= n; i++)
    {
        for (int j = i + 1; j <= n; j++)
        {
            r[make_pair(vals[i], vals[j])]++;
        }
    }

    long long ret = 0;

    for (int split = 2; split <= n - 2; split++)
    {
        l.clear();
        // update
        for (int i = 1; i < split; i++)
        {
            l[make_pair(vals[i], vals[split])]++;
        }

        for (int i = split + 1; i <= n; i++)
        {
            r[compress(make_pair(vals[split], vals[i]))]--;
        }

        for (auto& p : l)
        {
            ret += p.second * r[compress(p.first)];
        }
    }

    cout << ret << endl;
}

int main()
{
    int t = 1;
    cin >> t;

    while(t--) solve();
}


#include <bits/stdc++.h>

using namespace std;

const int mxN = 3e3 + 2;
int vals[mxN];
int r[10000000];

int n;
int compress(pair<int, int> a)
{
    return a.first * (n + 2) + a.second;
}

void solve()
{
    cin >> n;

    memset(r, 0, (n + 2) * (n + 2));

    for (int i = 1; i <= n; i++)
        cin >> vals[i];

    map<pair<int, int>, int> l;

    for (int i = 2; i <= n; i++)
    {
        for (int j = i + 1; j <= n; j++)
        {
            r[compress(make_pair(vals[i], vals[j]))]++;
            //cout << "adding: " << compress(make_pair(vals[i], vals[j])) << endl;
        }
    }

    long long ret = 0;

    for (int split = 2; split <= n - 2; split++)
    {
        l.clear();
        // update
        for (int i = 1; i < split; i++)
        {
            l[make_pair(vals[i], vals[split])]++;
        }

        for (int i = split + 1; i <= n; i++)
        {
            r[compress(make_pair(vals[split], vals[i]))]--;
        }

        for (auto& p : l)
        {
            ret += p.second * r[compress(p.first)];
        }
    }

    cout << ret << endl;
}

int main()
{
    int t = 1;
    cin >> t;

    while(t--) solve();
}


#include <bits/stdc++.h>

using namespace std;

const int mxN = 3e3 + 2;
int vals[mxN];
int r[1000000];

int compress(pair<int, int> a)
{
    return a.first * mxN + a.second;
}

void solve()
{
    int n;
    cin >> n;

    for (int i = 1; i <= n; i++)
        cin >> vals[i];

    map<pair<int, int>, int> l;

    for (int i = 2; i <= n; i++)
    {
        for (int j = i + 1; j <= n; j++)
        {
            r[compress(make_pair(vals[i], vals[j]))]++;
        }
    }

    long long ret = 0;

    for (int split = 2; split <= n - 2; split++)
    {
        l.clear();
        // update
        for (int i = 1; i < split; i++)
        {
            l[make_pair(vals[i], vals[split])]++;
        }

        for (int i = split + 1; i <= n; i++)
        {
            r[compress(make_pair(vals[split], vals[i]))]--;
        }

        for (auto& p : l)
        {
            ret += p.second * r[compress(p.first)];
        }
    }

    cout << ret << endl;
}

int main()
{
    int t = 1;
    cin >> t;

    while(t--) solve();
}


#include <bits/stdc++.h>

using namespace std;

const int mxN = 3e3 + 2;
int vals[mxN];

void solve()
{
    int n;
    cin >> n;

    for (int i = 1; i <= n; i++)
        cin >> vals[i];

    map<pair<int, int>, int> l, r;

    for (int i = 2; i <= n; i++)
    {
        for (int j = i + 1; j <= n; j++)
        {
            r[make_pair(vals[i], vals[j])]++;
        }
    }

    long long ret = 0;

    for (int split = 2; split <= n - 2; split++)
    {
        l.clear();
        // update
        for (int i = 1; i < split; i++)
        {
            l[make_pair(vals[i], vals[split])]++;
        }

        for (int i = split + 1; i <= n; i++)
        {
            r[make_pair(vals[split], vals[i])]--;
        }

        for (auto& p : l)
        {
            ret += p.second * r[p.first];
        }
    }

    cout << ret << endl;
}

int main()
{
    int t = 1;
    cin >> t;

    while(t--) solve();
}


#include <bits/stdc++.h>

using namespace std;

const int mxN = 3e3 + 2;
int vals[mxN];
int r[10000000];

int n;
int compress(pair<int, int> a)
{
    return a.first * (n + 1) + a.second;
}

void solve()
{
    cin >> n;

    int addEnd = (n + 3) * (n + 3);
    memset(r, 0, addEnd);

    for (int i = 1; i <= n; i++)
        cin >> vals[i];

    map<pair<int, int>, int> l;

    for (int i = 2; i <= n; i++)
    {
        for (int j = i + 1; j <= n; j++)
        {
            r[compress(make_pair(vals[i], vals[j]))]++;
            //cout << "adding: " << compress(make_pair(vals[i], vals[j])) << endl;
        }
    }

    long long ret = 0;

    for (int split = 2; split <= n - 2; split++)
    {
        l.clear();
        // update
        for (int i = 1; i < split; i++)
        {
            l[make_pair(vals[i], vals[split])]++;
        }

        for (int i = split + 1; i <= n; i++)
        {
            r[compress(make_pair(vals[split], vals[i]))]--;
        }

        for (auto& p : l)
        {
            ret += p.second * r[compress(p.first)];
        }
    }

    cout << ret << endl;
}

int main()
{
    int t = 1;
    cin >> t;

    while(t--) solve();
}


#include <bits/stdc++.h>

using namespace std;

const int mxN = 3e3 + 2;
int vals[mxN];
int r[10000000];

int compress(pair<int, int> a)
{
    return a.first * mxN + a.second;
}

void solve()
{
    int n;
    cin >> n;

    for (int i = 1; i <= n; i++)
        cin >> vals[i];

    map<pair<int, int>, int> l;

    for (int i = 2; i <= n; i++)
    {
        for (int j = i + 1; j <= n; j++)
        {
            r[compress(make_pair(vals[i], vals[j]))]++;
        }
    }

    long long ret = 0;

    for (int split = 2; split <= n - 2; split++)
    {
        l.clear();
        // update
        for (int i = 1; i < split; i++)
        {
            l[make_pair(vals[i], vals[split])]++;
        }

        for (int i = split + 1; i <= n; i++)
        {
            r[compress(make_pair(vals[split], vals[i]))]--;
        }

        for (auto& p : l)
        {
            ret += p.second * r[compress(p.first)];
        }
    }

    cout << ret << endl;
}

int main()
{
    int t = 1;
    cin >> t;

    while(t--) solve();
}


#include <bits/stdc++.h>

using namespace std;

const int mxN = 3e3 + 2;
int vals[mxN];
int r[10000000];

int n;
int compress(pair<int, int> a)
{
    return a.first * (n + 1) + a.second;
}

void solve()
{
    cin >> n;

    memset(r, 0, (n + 3) * (n + 3));

    for (int i = 1; i <= n; i++)
        cin >> vals[i];

    map<pair<int, int>, int> l;

    for (int i = 2; i <= n; i++)
    {
        for (int j = i + 1; j <= n; j++)
        {
            r[compress(make_pair(vals[i], vals[j]))]++;
            //cout << "adding: " << compress(make_pair(vals[i], vals[j])) << endl;
        }
    }

    long long ret = 0;

    for (int split = 2; split <= n - 2; split++)
    {
        l.clear();
        // update
        for (int i = 1; i < split; i++)
        {
            l[make_pair(vals[i], vals[split])]++;
        }

        for (int i = split + 1; i <= n; i++)
        {
            r[compress(make_pair(vals[split], vals[i]))]--;
        }

        for (auto& p : l)
        {
            ret += p.second * r[compress(p.first)];
        }
    }

    cout << ret << endl;
}

int main()
{
    int t = 1;
    cin >> t;

    while(t--) solve();
}


#include <bits/stdc++.h>

using namespace std;

const int mxN = 3e3 + 2;
int vals[mxN];
int r[10000000];

int n;
int compress(pair<int, int> a)
{
    return a.first * (n + 1) + a.second;
}

void solve()
{
    cin >> n;

    int addEnd = (n + 4) * (n + 4);
    fill(r, r + addEnd, 0);

    for (int i = 1; i <= n; i++)
        cin >> vals[i];

    map<pair<int, int>, int> l;

    for (int i = 2; i <= n; i++)
    {
        for (int j = i + 1; j <= n; j++)
        {
            r[compress(make_pair(vals[i], vals[j]))]++;
            //cout << "adding: " << compress(make_pair(vals[i], vals[j])) << endl;
        }
    }

    long long ret = 0;

    for (int split = 2; split <= n - 2; split++)
    {
        l.clear();
        // update
        for (int i = 1; i < split; i++)
        {
            l[make_pair(vals[i], vals[split])]++;
        }

        for (int i = split + 1; i <= n; i++)
        {
            r[compress(make_pair(vals[split], vals[i]))]--;
        }

        for (auto& p : l)
        {
            ret += p.second * r[compress(p.first)];
        }
    }

    cout << ret << endl;
}

int main()
{
    int t = 1;
    cin >> t;

    while(t--) solve();
}


#include <bits/stdc++.h>

using namespace std;

const int mxN = 3e3 + 2;
int vals[mxN];
int r[10000000];

int n;
int compress(pair<int, int> a)
{
    return a.first * (n + 1) + a.second;
}

void solve()
{
    cin >> n;

    fill(r, r + 10000000, 0);

    for (int i = 1; i <= n; i++)
        cin >> vals[i];

    map<pair<int, int>, int> l;

    for (int i = 2; i <= n; i++)
    {
        for (int j = i + 1; j <= n; j++)
        {
            r[compress(make_pair(vals[i], vals[j]))]++;
            //cout << "adding: " << compress(make_pair(vals[i], vals[j])) << endl;
        }
    }

    long long ret = 0;

    for (int split = 2; split <= n - 2; split++)
    {
        l.clear();
        // update
        for (int i = 1; i < split; i++)
        {
            l[make_pair(vals[i], vals[split])]++;
        }

        for (int i = split + 1; i <= n; i++)
        {
            r[compress(make_pair(vals[split], vals[i]))]--;
        }

        for (auto& p : l)
        {
            ret += p.second * r[compress(p.first)];
        }
    }

    cout << ret << endl;
}

int main()
{
    int t = 1;
    cin >> t;

    while(t--) solve();
}


#include <bits/stdc++.h>

using namespace std;

const int mxN = 3e3 + 2;
int vals[mxN];
int r[10000000];

int n;
int compress(pair<int, int> a)
{
    return a.first * (n + 1) + a.second;
}

void solve()
{
    cin >> n;

    int addEnd = (n + 3) * (n + 3);
    memset(r, 0, sizeof(int) * addEnd);

    for (int i = 1; i <= n; i++)
        cin >> vals[i];

    map<pair<int, int>, int> l;

    for (int i = 2; i <= n; i++)
    {
        for (int j = i + 1; j <= n; j++)
        {
            r[compress(make_pair(vals[i], vals[j]))]++;
            //cout << "adding: " << compress(make_pair(vals[i], vals[j])) << endl;
        }
    }

    long long ret = 0;

    for (int split = 2; split <= n - 2; split++)
    {
        l.clear();
        // update
        for (int i = 1; i < split; i++)
        {
            l[make_pair(vals[i], vals[split])]++;
        }

        for (int i = split + 1; i <= n; i++)
        {
            r[compress(make_pair(vals[split], vals[i]))]--;
        }

        for (auto& p : l)
        {
            ret += p.second * r[compress(p.first)];
        }
    }

    cout << ret << endl;
}

int main()
{
    int t = 1;
    cin >> t;

    while(t--) solve();
}


#include <bits/stdc++.h>

using namespace std;

const int mxN = 3e3 + 2;
int vals[mxN];
int r[10000000];

int n;
int compress(pair<int, int> a)
{
    return a.first * (n + 1) + a.second;
}

void solve()
{
    cin >> n;

    int addEnd = (n + 3) * (n + 3);
    fill(r, r + addEnd, 0);

    for (int i = 1; i <= n; i++)
        cin >> vals[i];

    map<pair<int, int>, int> l;

    for (int i = 2; i <= n; i++)
    {
        for (int j = i + 1; j <= n; j++)
        {
            r[compress(make_pair(vals[i], vals[j]))]++;
            //cout << "adding: " << compress(make_pair(vals[i], vals[j])) << endl;
        }
    }

    long long ret = 0;

    for (int split = 2; split <= n - 2; split++)
    {
        l.clear();
        // update
        for (int i = 1; i < split; i++)
        {
            l[make_pair(vals[i], vals[split])]++;
        }

        for (int i = split + 1; i <= n; i++)
        {
            r[compress(make_pair(vals[split], vals[i]))]--;
        }

        for (auto& p : l)
        {
            ret += p.second * r[compress(p.first)];
        }
    }

    cout << ret << endl;
}

int main()
{
    int t = 1;
    cin >> t;

    while(t--) solve();
}


#include <bits/stdc++.h>
using namespace std;
#define MAX_N 3001

void solve(){
    int n;
    cin >> n;
    array<int, MAX_N> a;
    array<int, MAX_N> cnt_right;
    array<int, MAX_N> cnt_left;
    cnt_right.fill(0);
    cnt_left.fill(0);
    for(int i = 0;i < n;i++) cin >> a[i];
    long long int ans = 0;
    for(int i = 0;i < n - 1;i++){
        cnt_right.fill(0);
        for(int j = n - 1;j > i;j--){
            ans += cnt_left[a[j]] * cnt_right[a[i]];
            cnt_right[a[j]]++;
        }
        cnt_left[a[i]]++;
    }
    cout << ans << endl;
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    cin >> n;
    while(n--) solve();
}


#include <bits/stdc++.h>
using namespace std;
#define MAX_N 3000

void solve(){
    int n;
    cin >> n;
    array<int, MAX_N> a;
    array<int, MAX_N> cnt_right;
    array<int, MAX_N> cnt_left;
    cout << cnt_right[2] << endl;
    for(int i = 0;i < n;i++) cin >> a[i];
    int ans = 0;
    for(int i = 0;i < n - 1;i++){
        cnt_right.fill(0);
        for(int j = n - 1;j > i;j--){
            ans += cnt_left[a[j]] * cnt_right[a[i]];
            cnt_right[a[j]]++;
        }
        cnt_left[a[i]]++;
    }
    cout << ans << endl;
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    cin >> n;
    while(n--) solve();
}


#include <bits/stdc++.h>
using namespace std;
#define MAX_N 3000

void solve(){
    int n;
    cin >> n;
    array<int, MAX_N> a;
    array<int, MAX_N> cnt_right;
    array<int, MAX_N> cnt_left;
    for(int i = 0;i < n;i++) cin >> a[i];
    int ans = 0;
    for(int i = 0;i < n - 1;i++){
        cnt_right.fill(0);
        for(int j = n - 1;j > i;j--){
            ans += cnt_left[a[j]] * cnt_right[a[i]];
            cnt_right[a[j]]++;
        }
        cnt_left[a[i]]++;
    }
    cout << ans << endl;
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    cin >> n;
    while(n--) solve();
}


#include <bits/stdc++.h>
using namespace std;
#define MAX_N 3000

void solve(){
    int n;
    cin >> n;
    array<int, MAX_N> a;
    array<int, MAX_N> cnt_right;
    array<int, MAX_N> cnt_left;
    cnt_right.fill(0);
    cnt_left.fill(0);
    for(int i = 0;i < n;i++) cin >> a[i];
    int ans = 0;
    for(int i = 0;i < n - 1;i++){
        cnt_right.fill(0);
        for(int j = n - 1;j > i;j--){
            ans += cnt_left[a[j]] * cnt_right[a[i]];
            cnt_right[a[j]]++;
        }
        cnt_left[a[i]]++;
    }
    cout << ans << endl;
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    cin >> n;
    while(n--) solve();
}


#include <bits/stdc++.h>
using namespace std;
#define MAX_N 3000

void solve(){
    int n;
    cin >> n;
    array<int, MAX_N> a;
    array<int, MAX_N> cnt_right;
    array<int, MAX_N> cnt_left;
    cnt_right.fill(0);
    cnt_left.fill(0);
    for(int i = 0;i < n;i++) cin >> a[i];
    long long int ans = 0;
    for(int i = 0;i < n - 1;i++){
        cnt_right.fill(0);
        for(int j = n - 1;j > i;j--){
            ans += cnt_left[a[j]] * cnt_right[a[i]];
            cnt_right[a[j]]++;
        }
        cnt_left[a[i]]++;
    }
    cout << ans << endl;
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    cin >> n;
    while(n--) solve();
}


#include <bits/stdc++.h>
using namespace std;
#define en '\n'
#define Online 1
#define yes cout << "YES" << en
#define no  cout << "NO" << en
#define mems(arr,VAL)     memset(arr,VAL,sizeof(arr))
#define ll long long
#define vll vector<ll>
#define fo(i, a, b) for(ll i = a; i < b; i++)
#define fb(i, a, b) for(ll i = a; i > b; i--)
#define fv(it,v) for(auto it=v.begin();it<v.end();it++)
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define in insert
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define sz(a) int(a.size())
#define pll pair<ll, ll>
#define plll pair<ll, pll>
#define vpll vector<pll>
#define vplll vector<plll>
#define lbnd lower_bound
#define ubnd upper_bound
#define bs   binary_search
#define mp map<ll,ll>
#define gcd(a,b)    __gcd((a),(b))
#define lcm(a,b)    ((a)*(b)) / gcd((a),(b))
#define tle 1000000007
template <typename Arg1>
void pn(Arg1&& arg1)
{ cout<<arg1<<"\n";}
template <typename Arg1, typename... Args>
void pn(Arg1&& arg1, Args&&... args)
{ cout<<arg1<<"\n"; pn(args...); }

template <typename Arg1>
void ps(Arg1&& arg1)
{ cout<<arg1<<" ";}
template <typename Arg1, typename... Args>
void ps(Arg1&& arg1, Args&&... args)
{ cout<<arg1<<" "; ps(args...); }

template <typename Arg1>
void pvs(Arg1&& arg1, ll n)
{ for(ll i=0;i<n;i++)ps(arg1[i]); }
template <typename Arg1>
void pvn(Arg1&& arg1, ll n)
{ for(ll i=0;i<n;i++)pn(arg1[i]); }

template <typename Arg1>
void bug(Arg1&& arg1)
{ cout<<"->"<<arg1<<endl; }
void solve(){
      ll y,z,n,p,x,k,l,sum=0,flag=0,t,ans=0;
      string s,s1="",s2;
      cin>>n;
      vll a(n);
      fo(i,0,n)
      cin>>a[i];
      fo(i,0,n){
            fb(j,n-1,i+2){
                  ans=0,sum=0;
                  fo(k,i+1,j){
                        if(a[i]==a[j]){
                              if(a[k]==a[i])
                              ans++;
                              continue;
                        }
                        if(a[k]==a[j]){
                              flag+=(sum*ans);
                              ans=0;
                              sum++;
                        }
                        else if(a[k]==a[i]){
                              if(sum>0)
                              ans++;
                        }
                  }
                  if(sum==0)
                  {
                        flag+=(ans*(ans-1))/2;
                  }
                  flag+=(sum*ans);
            }
      }
      pn(flag);
}
int main()
{
      // #ifdef Online
      // freopen("input.txt", "r", stdin);
      // freopen("output.txt", "w", stdout);
      // #endif
      ios_base::sync_with_stdio(false);
      cin.tie(NULL);
      int test=1,i=1;
      cin>>test;
      while(test--){
            // cout<<"Case #"<<i++<<": ";
            solve();
      }

      return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define en '\n'
#define Online 1
#define yes cout << "YES" << en
#define no  cout << "NO" << en
#define mems(arr,VAL)     memset(arr,VAL,sizeof(arr))
#define ll long long
#define vll vector<ll>
#define fo(i, a, b) for(ll i = a; i < b; i++)
#define fb(i, a, b) for(ll i = a; i > b; i--)
#define fv(it,v) for(auto it=v.begin();it<v.end();it++)
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define in insert
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define sz(a) int(a.size())
#define pll pair<ll, ll>
#define plll pair<ll, pll>
#define vpll vector<pll>
#define vplll vector<plll>
#define lbnd lower_bound
#define ubnd upper_bound
#define bs   binary_search
#define mp map<ll,ll>
#define gcd(a,b)    __gcd((a),(b))
#define lcm(a,b)    ((a)*(b)) / gcd((a),(b))
#define tle 1000000007
template <typename Arg1>
void pn(Arg1&& arg1)
{ cout<<arg1<<"\n";}
template <typename Arg1, typename... Args>
void pn(Arg1&& arg1, Args&&... args)
{ cout<<arg1<<"\n"; pn(args...); }

template <typename Arg1>
void ps(Arg1&& arg1)
{ cout<<arg1<<" ";}
template <typename Arg1, typename... Args>
void ps(Arg1&& arg1, Args&&... args)
{ cout<<arg1<<" "; ps(args...); }

template <typename Arg1>
void pvs(Arg1&& arg1, ll n)
{ for(ll i=0;i<n;i++)ps(arg1[i]); }
template <typename Arg1>
void pvn(Arg1&& arg1, ll n)
{ for(ll i=0;i<n;i++)pn(arg1[i]); }

template <typename Arg1>
void bug(Arg1&& arg1)
{ cout<<"->"<<arg1<<endl; }
void solve(){
      ll y,z,n,p,x,k,l,sum=0,flag=0,t,ans=0;
      string s,s1="",s2;
      cin>>n;
      vll a(n);
      fo(i,0,n)
      cin>>a[i];
      unordered_map<ll,ll>m,m1;
      fo(i,0,n){
            m1.clear();
            m1[a[n-1]]++;
            fb(j,n-2,i){
                  ans+=m[a[j]]*m1[a[i]];
                  m1[a[j]]++;
            }
            m[a[i]]++;
      }
      pn(ans);
}
int main()
{
      // #ifdef Online
      // freopen("input.txt", "r", stdin);
      // freopen("output.txt", "w", stdout);
      // #endif
      ios_base::sync_with_stdio(false);
      cin.tie(NULL);
      int test=1,i=1;
      cin>>test;
      while(test--){
            // cout<<"Case #"<<i++<<": ";
            solve();
      }

      return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define en '\n'
#define Online 1
#define yes cout << "YES" << en
#define no  cout << "NO" << en
#define mems(arr,VAL)     memset(arr,VAL,sizeof(arr))
#define ll long long
#define vll vector<ll>
#define fo(i, a, b) for(ll i = a; i < b; i++)
#define fb(i, a, b) for(ll i = a; i > b; i--)
#define fv(it,v) for(auto it=v.begin();it<v.end();it++)
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define in insert
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define sz(a) int(a.size())
#define pll pair<ll, ll>
#define plll pair<ll, pll>
#define vpll vector<pll>
#define vplll vector<plll>
#define lbnd lower_bound
#define ubnd upper_bound
#define bs   binary_search
#define mp map<ll,ll>
#define gcd(a,b)    __gcd((a),(b))
#define lcm(a,b)    ((a)*(b)) / gcd((a),(b))
#define tle 1000000007
template <typename Arg1>
void pn(Arg1&& arg1)
{ cout<<arg1<<"\n";}
template <typename Arg1, typename... Args>
void pn(Arg1&& arg1, Args&&... args)
{ cout<<arg1<<"\n"; pn(args...); }

template <typename Arg1>
void ps(Arg1&& arg1)
{ cout<<arg1<<" ";}
template <typename Arg1, typename... Args>
void ps(Arg1&& arg1, Args&&... args)
{ cout<<arg1<<" "; ps(args...); }

template <typename Arg1>
void pvs(Arg1&& arg1, ll n)
{ for(ll i=0;i<n;i++)ps(arg1[i]); }
template <typename Arg1>
void pvn(Arg1&& arg1, ll n)
{ for(ll i=0;i<n;i++)pn(arg1[i]); }

template <typename Arg1>
void bug(Arg1&& arg1)
{ cout<<"->"<<arg1<<endl; }
void solve(){
      ll y,z,n,p,x,k,l,sum=0,flag=0,t,ans=0;
      string s,s1="",s2;
      cin>>n;
      vll a(n);
      fo(i,0,n)
      cin>>a[i];
      fo(i,0,n){
            fb(j,n-1,-1){
                  ans=0,sum=0;
                  fo(k,i+1,j){
                        if(a[i]==a[j]){
                              if(a[k]==a[i])
                              ans++;
                              continue;
                        }
                        if(a[k]==a[j]){
                              flag+=(sum*ans);
                              ans=0;
                              sum++;
                        }
                        else if(a[k]==a[i]){
                              if(sum>0)
                              ans++;
                        }
                  }
                  if(sum==0)
                  {
                        flag+=(ans*(ans-1))/2;
                  }
                  flag+=(sum*ans);
            }
      }
      pn(flag);
}
int main()
{
      // #ifdef Online
      // freopen("input.txt", "r", stdin);
      // freopen("output.txt", "w", stdout);
      // #endif
      ios_base::sync_with_stdio(false);
      cin.tie(NULL);
      int test=1,i=1;
      cin>>test;
      while(test--){
            // cout<<"Case #"<<i++<<": ";
            solve();
      }

      return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define en '\n'
#define Online 1
#define yes cout << "YES" << en
#define no  cout << "NO" << en
#define mems(arr,VAL)     memset(arr,VAL,sizeof(arr))
#define ll long long
#define vll vector<ll>
#define fo(i, a, b) for(ll i = a; i < b; i++)
#define fb(i, a, b) for(ll i = a; i > b; i--)
#define fv(it,v) for(auto it=v.begin();it<v.end();it++)
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define in insert
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define sz(a) int(a.size())
#define pll pair<ll, ll>
#define plll pair<ll, pll>
#define vpll vector<pll>
#define vplll vector<plll>
#define lbnd lower_bound
#define ubnd upper_bound
#define bs   binary_search
#define mp map<ll,ll>
#define gcd(a,b)    __gcd((a),(b))
#define lcm(a,b)    ((a)*(b)) / gcd((a),(b))
#define tle 1000000007
template <typename Arg1>
void pn(Arg1&& arg1)
{ cout<<arg1<<"\n";}
template <typename Arg1, typename... Args>
void pn(Arg1&& arg1, Args&&... args)
{ cout<<arg1<<"\n"; pn(args...); }

template <typename Arg1>
void ps(Arg1&& arg1)
{ cout<<arg1<<" ";}
template <typename Arg1, typename... Args>
void ps(Arg1&& arg1, Args&&... args)
{ cout<<arg1<<" "; ps(args...); }

template <typename Arg1>
void pvs(Arg1&& arg1, ll n)
{ for(ll i=0;i<n;i++)ps(arg1[i]); }
template <typename Arg1>
void pvn(Arg1&& arg1, ll n)
{ for(ll i=0;i<n;i++)pn(arg1[i]); }

template <typename Arg1>
void bug(Arg1&& arg1)
{ cout<<"->"<<arg1<<endl; }
void solve(){
      ll y,z,n,p,x,k,l,sum=0,flag=0,t,ans=0;
      string s,s1="",s2;
      cin>>n;
      vll a(n);
      fo(i,0,n)
      cin>>a[i];
      fo(i,0,n){
            fb(j,n-1,i-1){
                  ans=0,sum=0;
                  fo(k,i+1,j){
                        if(a[i]==a[j]){
                              if(a[k]==a[i])
                              ans++;
                              continue;
                        }
                        if(a[k]==a[j]){
                              flag+=(sum*ans);
                              ans=0;
                              sum++;
                        }
                        else if(a[k]==a[i]){
                              if(sum>0)
                              ans++;
                        }
                  }
                  if(sum==0)
                  {
                        flag+=(ans*(ans-1))/2;
                  }
                  flag+=(sum*ans);
            }
      }
      pn(flag);
}
int main()
{
      // #ifdef Online
      // freopen("input.txt", "r", stdin);
      // freopen("output.txt", "w", stdout);
      // #endif
      ios_base::sync_with_stdio(false);
      cin.tie(NULL);
      int test=1,i=1;
      cin>>test;
      while(test--){
            // cout<<"Case #"<<i++<<": ";
            solve();
      }

      return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define en '\n'
#define Online 1
#define yes cout << "YES" << en
#define no  cout << "NO" << en
#define mems(arr,VAL)     memset(arr,VAL,sizeof(arr))
#define ll long long
#define vll vector<ll>
#define fo(i, a, b) for(ll i = a; i < b; i++)
#define fb(i, a, b) for(ll i = a; i > b; i--)
#define fv(it,v) for(auto it=v.begin();it<v.end();it++)
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define in insert
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define sz(a) int(a.size())
#define pll pair<ll, ll>
#define plll pair<ll, pll>
#define vpll vector<pll>
#define vplll vector<plll>
#define lbnd lower_bound
#define ubnd upper_bound
#define bs   binary_search
#define mp map<ll,ll>
#define gcd(a,b)    __gcd((a),(b))
#define lcm(a,b)    ((a)*(b)) / gcd((a),(b))
#define tle 1000000007
template <typename Arg1>
void pn(Arg1&& arg1)
{ cout<<arg1<<"\n";}
template <typename Arg1, typename... Args>
void pn(Arg1&& arg1, Args&&... args)
{ cout<<arg1<<"\n"; pn(args...); }

template <typename Arg1>
void ps(Arg1&& arg1)
{ cout<<arg1<<" ";}
template <typename Arg1, typename... Args>
void ps(Arg1&& arg1, Args&&... args)
{ cout<<arg1<<" "; ps(args...); }

template <typename Arg1>
void pvs(Arg1&& arg1, ll n)
{ for(ll i=0;i<n;i++)ps(arg1[i]); }
template <typename Arg1>
void pvn(Arg1&& arg1, ll n)
{ for(ll i=0;i<n;i++)pn(arg1[i]); }

template <typename Arg1>
void bug(Arg1&& arg1)
{ cout<<"->"<<arg1<<endl; }
void solve(){
      ll y,z,n,p,x,k,l,sum=0,flag=0,t,ans=0;
      string s,s1="",s2;
      cin>>n;
      vll a(n);
      fo(i,0,n) cin>>a[i];
      unordered_map<ll,ll>m,m1;
      fo(i,0,n){
            m1.clear();
            fb(j,n-1,i) ans+=m[a[j]]*m1[a[i]],m1[a[j]]++;
            m[a[i]]++;
      }
      pn(ans);
}
int main()
{
      // #ifdef Online
      // freopen("input.txt", "r", stdin);
      // freopen("output.txt", "w", stdout);
      // #endif
      ios_base::sync_with_stdio(false);
      cin.tie(NULL);
      int test=1,i=1;
      cin>>test;
      while(test--){
            // cout<<"Case #"<<i++<<": ";
            solve();
      }

      return 0;
}