Little chef has just been introduced to the world of numbers! While experimenting with addition and multiplication operations, the little chef came up with the following problem:

Given an array A of non-negative integers, how many pairs of indices i and j exist such that  A[i]*A[j] > A[i]+A[j]  where  i < j .

Now being a learner, little chef isn't able to solve this problem efficiently and hence turns to you for help. 

-----Input-----
First line of input contains an integer T denoting the number of test cases. For each test case, the first line contains an integer N denoting the number of integers in the array. The next line contains N space separated integers where the ith integer represents A[i]. 
Note : There may be trailing spaces on each line of input. 

-----Output-----
For each test, print the required number of pairs in a single line.

-----Constraints-----
-  1 ≤ T ≤ 10 
-  2 ≤ N ≤ 100000 (105) 
-  0 ≤ A[i] ≤ 1000000 (106)

-----Example-----
Input:
2
3
3 4 5
4
1 1 1 1

Output:
3
0

-----Explanation-----

Example case 1.

All pairs of numbers satisfy the criteria. Total number of pairs equals 3.

Example case 2.

No pair of numbers satisfy the criteria.

Source: codechef | Difficulty: MEDIUM
Tags: Combinatorics

Mathematics


Solutions:
def nc2(n):
	if n < 2:
		return 0
	return n * (n - 1) // 2

def solve2(A):
	ones = twos = zeros = 0
	for i in A:
		if i == 1:
			ones += 1
		elif i == 2:
			twos += 1
		elif i == 0:
			zeros += 1
	n = len(A)
	tot = nc2(len(A))
	onepairs = nc2(ones)
	onecombos = (n - ones) * ones
	zeropairs = nc2(zeros)
	zerocombos = (n - zeros - ones) * zeros
	twopairs = nc2(twos)
	return tot - (onepairs + onecombos + twopairs + zeropairs + zerocombos)
t = int(input())
for _ in range(t):
	n = int(input())
	A = list(map(int, input().split()))
	print(solve2(A))


for t in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	(a, b) = (0, 0)
	for i in range(n):
		if l[i] > 2:
			a += 1
		elif l[i] == 2:
			b += 1
	print(int(a * (a - 1) / 2 + a * b))


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	arr.sort()
	var0 = arr.count(0)
	var1 = arr.count(1)
	var2 = arr.count(2)
	new = n - var1 - var0
	res = new * (new - 1) // 2
	print(res - var2 * (var2 - 1) // 2)


import math
test = int(input())
for _ in range(test):
	n = int(input())
	array = list(map(int, input().split()))
	count = n - array.count(1) - array.count(0) - array.count(2)
	print(count * (count - 1) // 2 + array.count(2) * count)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	c1 = 0
	c2 = 0
	for j in a:
		if j >= 2:
			c1 += 1
		if j == 2:
			c2 += 1
	print(c1 * (c1 - 1) // 2 - c2 * (c2 - 1) // 2)


T = int(input())
ans = []
for _ in range(T):
	N = int(input())
	A = [int(i) for i in input().split()]
	C2 = 0
	C = 0
	for i in range(N):
		if A[i] == 2:
			C2 += 1
		if A[i] > 2:
			C += 1
	ans.append(C2 * C + C * (C - 1) // 2)
for i in ans:
	print(i)


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	n1 = 0
	n2 = 0
	for i in arr:
		if i == 2:
			n2 += 1
		elif i > 2:
			n1 += 1
		else:
			continue
	print((n1 - 1) * n1 // 2 + n1 * n2)


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	c1 = 0
	c2 = 0
	for i in l:
		if i == 2:
			c2 = c2 + 1
		elif i > 2:
			c1 = c1 + 1
	print(int(c2 * c1 + c1 * (c1 - 1) / 2))


for _ in range(int(input())):
	n = int(input())
	lis = list(map(int, input().split()))
	a1 = 0
	a2 = 0
	for i in lis:
		if i > 2:
			a1 += 1
		elif i == 2:
			a2 += 1
	print(a2 * a1 + a1 * (a1 - 1) // 2)


from collections import defaultdict
for _ in range(int(input())):
	n = int(input())
	ls = [int(x) for x in input().split()]
	n -= ls.count(1) + ls.count(0)
	twos = ls.count(2)
	rest = twos * (n - twos) + (n - twos) * (n - twos - 1) // 2
	print(rest)


n = int(input())
for i in range(n):
	a = int(input())
	l = list(map(int, input().split()))
	m = 0
	n = 0
	for j in l:
		if j > 2:
			m += 1
		elif j == 2:
			n += 1
	print(m * n + m * (m - 1) // 2)


def solve(n, arr):
	cnt2 = 0
	cnt = 0
	for i in range(n):
		if arr[i] == 2:
			cnt2 += 1
		elif arr[i] > 2:
			cnt += 1
	return cnt2 * cnt + cnt * (cnt - 1) // 2
for _ in range(int(input())):
	n = int(input())
	a = [int(j) for j in input().split()]
	print(solve(n, a))


for t in range(int(input())):
	N = int(input())
	A = list(map(int, input().split()))
	c1 = 0
	c2 = 0
	for i in range(N):
		if A[i] != 0 and A[i] != 1:
			c1 += 1
		if A[i] == 2:
			c2 += 1
	a = (c1 - 1) * c1 / 2
	b = c2 * (c2 - 1) / 2
	print(int(a - b))


def solution():
	n = int(input())
	arr = list(map(int, input().split()))
	(one, two, zero) = (0, 0, 0)
	for x in arr:
		if x == 0:
			zero += 1
		if x == 1:
			one += 1
		if x == 2:
			two += 1
	n -= one + zero
	ans = n * (n - 1) / 2
	ans -= two * (two - 1) / 2
	print(int(ans))
for _ in range(int(input())):
	solution()


t = int(input())
for i in range(t):
	N = int(input())
	A = list(map(int, input().split()))
	CG = 0
	C2 = 0
	for i in range(N):
		if A[i] > 2:
			CG += 1
		elif A[i] == 2:
			C2 += 1
	print(CG * C2 + CG * (CG - 1) // 2)


for _ in range(int(input().strip())):
	n = int(input().strip())
	nums = [int(x) for x in input().strip().split(' ')]
	count2 = nums.count(2)
	count = 0
	for num in nums:
		if num > 2:
			count += 1
	print(count * (count - 1) // 2 + count2 * count)


for _ in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	n -= a.count(0)
	n -= a.count(1)
	p = a.count(2)
	n -= p
	print(n * (n - 1) // 2 + p * n)


for _ in range(int(input())):
	(c, z, o, t) = (0, 0, 0, 0)
	n = int(input())
	a = list(map(int, input().split()))
	for x in a:
		if x > 2:
			z += 1
		elif x == 2:
			t += 1
	c += z * t + z * (z - 1) // 2
	print(c)


for tc in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	p = []
	z = []
	for i in range(n):
		if a[i] <= 0:
			z.append(a[i])
		elif a[i] >= 2:
			p.append(a[i])
	b = len(z) * (len(z) - 1) // 2 - z.count(0) * (z.count(0) - 1) // 2
	c = len(p) * (len(p) - 1) // 2 - p.count(2) * (p.count(2) - 1) // 2
	print(b + c)


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	a = l.count(0)
	b = l.count(1)
	c = l.count(2)
	n -= a + b
	ans = n * (n - 1) / 2
	ans -= c * (c - 1) / 2
	print(int(ans))


for t in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	no = 0
	two = 0
	for i in a:
		if i >= 2:
			no += 1
			if i == 2:
				two += 1
	print(no * (no - 1) // 2 - two * (two - 1) // 2)


t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	count2 = arr.count(2)
	countpos = len(arr) - count2 - arr.count(0) - arr.count(1)
	print(count2 * countpos + countpos * (countpos - 1) // 2)


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().lstrip().split()))
	arr.sort()
	count = 0
	rep = 1
	for i in range(n - 1):
		if arr[i] * arr[i + 1] > arr[i] + arr[i + 1]:
			count += (n - (i + 1)) * rep
			rep = 1
		if arr[i] == arr[i + 1] and arr[i] == 2:
			rep += 1
	print(count)


from bisect import bisect_right as upper_bound
t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	l.sort()
	ones = upper_bound(l, 1)
	twos = upper_bound(l, 2) - ones
	rest = n - ones - twos
	total = max(0, rest * (rest - 1) // 2) + twos * rest
	print(total)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	c2 = 0
	c = 0
	for i in l:
		if i == 2:
			c2 += 1
		if i > 2:
			c += 1
	print(c2 * c + c * (c - 1) // 2)


for i in range(int(input())):
	v = int(input())
	m = list(map(int, input().split()))
	l = v - m.count(0) - m.count(1) - m.count(2)
	print(l * m.count(2) + l * (l - 1) // 2)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	count_2 = 0
	for i in range(n):
		if arr[i] == 1 or arr[i] == 0:
			n -= 1
		elif arr[i] == 2:
			count_2 += 1
	ans = n * (n - 1) // 2
	eli_2 = count_2 * (count_2 - 1) // 2
	ans -= eli_2
	print(ans)


from functools import reduce
t = int(input())
l = []
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	(c2, c) = (a.count(2), reduce(lambda count, i: count + (i > 2), a, 0))
	l.append(c2 * c + c * (c - 1) // 2)
for _ in l:
	print(_)


a = int(input())
for i in range(a):
	b = int(input())
	arr = [int(x) for x in input().split()]
	one = 0
	two = 0
	zero = 0
	for r in arr:
		if r == 1:
			one += 1
		elif r == 2:
			two += 1
		elif r == 0:
			zero += 1
	result = len(arr) * (len(arr) - 1) // 2
	if result < 0:
		result = 0
	if one > 0:
		result -= one * (one - 1) // 2
		result -= one * (len(arr) - one)
	if two > 0:
		result -= two * (two - 1) // 2
	if zero > 0:
		result -= zero * (zero - 1) // 2
		result -= zero * (len(arr) - zero) - zero * one
	print(int(result))


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	n -= l.count(0)
	p = l.count(2)
	k = l.count(1)
	n -= k
	ans = n * (n - 1) // 2
	if p > 1:
		ans -= (p - 1) * p // 2
	print(ans)


try:
	for i in range(int(input())):
		n = int(input())
		l = list(map(int, input().split()))
		k = []
		for i in l:
			if i > 1:
				k.append(i)
		two = k.count(2)
		c2 = len(k) - two
		print(two * c2 + c2 * (c2 - 1) // 2)
except:
	pass


T = int(input())
for i in range(T):
	n = int(input())
	l = sorted(list(map(int, input().split())))
	a = l.count(1)
	b = l.count(2)
	g = l.count(0)
	c = n - (a + b + g)
	e = int(c * (c - 1) / 2)
	d = c * b
	f = d + e
	print(f)


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().strip().split()))
	count1 = 0
	count2 = 0
	for i in range(n):
		if l[i] == 0 or l[i] == 1:
			count1 += 1
		if l[i] == 2:
			count2 += 1
	temp1 = n - count1
	temp2 = 0
	if count2 >= 2:
		temp2 = count2
	print(temp1 * (temp1 - 1) // 2 - temp2 * (temp2 - 1) // 2)


t = int(input())
res = []
for i in range(t):
	n = int(input())
	arr = [int(i) for i in input().split()]
	num_2 = 0
	num = 0
	for j in range(len(arr)):
		if arr[j] == 2:
			num_2 += 1
		if arr[j] > 2:
			num += 1
	res.append(num_2 * num + num * (num - 1) // 2)
for z in res:
	print(z)


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	c2 = 0
	cg = 0
	for ele in arr:
		if ele > 2:
			cg += 1
		elif ele == 2:
			c2 += 1
	print(c2 * cg + cg * (cg - 1) // 2)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	c = t = 0
	for i in range(n):
		if a[i] == 2:
			t += 1
			c += 1
		elif a[i] > 1:
			c += 1
	print(int(c * (c - 1) / 2 - t * (t - 1) / 2))


for _ in range(int(input())):
	n = int(input())
	A = list(map(int, input().split()))
	ans = ones = twos = 0
	for i in range(n):
		if A[i] == 1 or A[i] == 0:
			ones += 1
		elif A[i] == 2:
			twos += 1
	n = n - ones
	ct2 = 0
	if twos > 1:
		ct2 = twos
	ans = n * (n - 1) // 2 - ct2 * (ct2 - 1) // 2
	print(ans)


from sys import stdin, stdout
for _ in range(int(stdin.readline())):
	N = int(stdin.readline())
	L = list(map(int, stdin.readline().split()))
	c1 = c2 = 0
	for i in L:
		if i > 2:
			c1 += 1
		elif i == 2:
			c2 += 1
	print((c1 ** 2 + 2 * c1 * c2 - c1) // 2)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	total = sum(map(lambda x: x > 1, a))
	two = a.count(2)
	ans = total * (total - 1) // 2 - two * (two - 1) // 2
	print(ans)


for _ in range(int(input())):
	n = int(input())
	arr = [int(i) for i in input().split()]
	arr.sort()
	count0 = 0
	count1 = 0
	count2 = 0
	for k in range(n):
		if arr[k] > 0:
			break
		elif arr[k] == 0:
			count0 += 1
	for i in range(k, n):
		if arr[i] > 1:
			break
		elif arr[i] == 1:
			count1 += 1
	for j in range(i, n):
		if arr[j] > 2:
			break
		elif arr[j] == 2:
			count2 += 1
	ans = n * (n - 1) // 2
	l = count1 * (n - count0) - (count1 + 1) * count1 // 2 + count2 * (count2 - 1) // 2 + count0 * n - (count0 + 1) * count0 // 2
	print(ans - l)


for tc in range(int(input())):
	n = int(input())
	li = list(map(int, input().split()))
	c2 = li.count(2)
	c = n - li.count(1) - li.count(0) - li.count(2)
	print(c2 * c + c * (c - 1) // 2)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	s = sum((1 for el in l if el > 2))
	c = l.count(2)
	print(c * s + s * (s - 1) // 2)


T = int(input())
for i in range(T):
	N = int(input())
	A = [int(x) for x in input().split()]
	(x, y) = (0, 0)
	for i in A:
		if i == 1 or i == 0:
			x += 1
		elif i == 2:
			y += 1
	print((N - x) * (N - x - 1) // 2 - y * (y - 1) // 2)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = a.count(2)
	c = len([i for i in a if i > 1])
	d = 0
	for i in range(n - 1):
		if a[i] == 2:
			b -= 1
			c -= 1
			d += c - b
		elif a[i] > 2:
			c -= 1
			d += c
	print(d)


t = int(input())
for _ in range(t):
	n = int(input())
	L = list(map(int, input().split()))
	a = L.count(0)
	b = L.count(1)
	c = L.count(2)
	n = n - a - b
	ans = n * (n - 1) // 2
	print(ans - c * (c - 1) // 2)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	(zero, one, two) = (0, 0, 0)
	for i in l:
		if i == 0:
			zero += 1
		elif i == 1:
			one += 1
		elif i == 2:
			two += 1
	total = n - one - zero - two
	total = two * total + total * (total - 1) // 2
	print(total)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	c = 0
	c1 = 0
	for i in arr:
		if i > 2:
			c += 1
		elif i == 2:
			c1 += 1
	print(c1 * c + c * (c - 1) // 2)


from collections import Counter
T = int(input())
for _ in range(T):
	n = int(input())
	a = list(map(int, input().split()))
	d = dict(Counter(a))
	distinct = 0
	two = 0
	for k in d.keys():
		if k != 0 and k != 1 and (k != 2):
			distinct = distinct + d[k]
		if k == 2:
			two = two + d[k]
	res = distinct * two + distinct * (distinct - 1) // 2
	print(res)


for _ in range(int(input())):
	N = int(input())
	arr = list(map(int, input().split()))
	x = 0
	y = 0
	for i in arr:
		if i == 2:
			x += 1
		if i > 2:
			y += 1
	print(x * y + y * (y - 1) // 2)


from collections import defaultdict
for _ in range(int(input())):
	n = int(input())
	ls = [int(x) for x in input().split()]
	twos = 0
	rest = 0
	for i in range(n):
		if ls[i] == 0 or ls[i] == 1:
			continue
		elif ls[i] == 2:
			twos += 1
		else:
			rest += 1
	ans = twos * rest + rest * (rest - 1) // 2
	print(ans)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	t_2 = 0
	tn_2 = 0
	for i in range(len(l)):
		if l[i] > 2:
			tn_2 += 1
		elif l[i] == 2:
			t_2 += 1
	ans = tn_2 * (tn_2 - 1) / 2
	ans += t_2 * tn_2
	print(int(ans))


for _ in range(int(input())):
	n = int(input())
	s = sorted(list(map(int, input().split())))
	val = 0
	for (count, i) in enumerate(s):
		if i > 1:
			s = s[count:]
			n = n - count
			val = i
			break
	if val == 0:
		print(0)
	elif val == 2:
		count = s.count(2)
		print(n * (n - 1) // 2 - count * (count - 1) // 2)
	else:
		print(n * (n - 1) // 2)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	c2 = arr.count(2)
	cg = len([i for i in arr if i > 1])
	ans = 0
	for i in range(len(arr)):
		if arr[i] == 2:
			c2 -= 1
			cg -= 1
			ans += cg - c2
		elif arr[i] > 2:
			cg -= 1
			ans += cg
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	c2 = 0
	cG = 0
	for element in arr:
		if element > 2:
			cG += 1
		elif element == 2:
			c2 += 1
	print(c2 * cG + cG * (cG - 1) // 2)


from bisect import bisect_right as upper_bound
t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	zero = l.count(0)
	one = l.count(1)
	two = l.count(2)
	rem = n - zero - one - two
	print(two * rem + rem * (rem - 1) // 2)


def nc2(n):
	return n * (n - 1) // 2
for __ in range(int(input())):
	n = int(input().strip())
	a = list(map(int, input().strip().split()))
	total = sum(map(lambda x: x > 1, a))
	two = a.count(2)
	print(nc2(total) - nc2(two))


t = int(input())
for _ in range(t):
	n = int(input())
	list1 = list(map(int, input().strip().split()))
	count1 = 0
	count2 = 0
	for val in list1:
		if val == 0 or val == 1:
			count1 += 1
		elif val == 2:
			count2 += 1
	temp1 = n - count1
	temp2 = 0
	if count2 >= 2:
		temp2 = count2
	print(temp1 * (temp1 - 1) // 2 - temp2 * (temp2 - 1) // 2)


a = int(input())
for _ in range(a):
	b = int(input())
	c = list(map(int, input().split()))
	count = 0
	count2 = 0
	for i in c:
		if i == 2:
			count2 += 1
		elif i > 2:
			count += 1
	print(int(count * count2 + count * (count - 1) / 2))


for t in range(0, int(input())):
	n = int(input())
	arr = list(map(int, input().split()))[:n]
	zero = arr.count(0)
	one = arr.count(1)
	two = arr.count(2)
	rem = n - (zero + one + two)
	print(two * rem + rem * (rem - 1) // 2)


t = int(input())
for i in range(t):
	a = int(input())
	b = list(map(int, input().split()))
	c = 0
	c1 = 0
	for j in b:
		if j > 2:
			c += 1
		elif j == 2:
			c1 += 1
	print(c1 * c + c * (c - 1) // 2)


from collections import Counter
t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	l = [i for i in l if i != 1 and i != 0]
	c = len(l)
	if c == 0 or c == 1:
		print(0)
	else:
		co = Counter(l)
		h = co[2]
		print(c * (c - 1) // 2 - h * (h - 1) // 2)


for _ in range(int(input())):
	leng = int(input())
	ints = list(map(int, input().split()))
	ints = sorted(ints)
	ans = 0
	ans2 = 0
	for i in range(leng):
		if ints[i] > 2:
			ans += 1
		elif ints[i] == 2:
			ans2 += 1
	print(ans * ans2 + ans * (ans - 1) // 2)


tests = int(input())
for i in range(tests):
	length = int(input())
	arr = list(map(int, input().split()))
	total = length * (length - 1) // 2
	remove = arr.count(1)
	remove += arr.count(0)
	two_count = arr.count(2)
	total -= (length - remove) * remove
	total -= remove * (remove - 1) // 2
	total -= two_count * (two_count - 1) // 2
	print(total)


t = int(input())
for _ in range(t):
	n = int(input())
	l = [int(x) for x in input().split()]
	z = l.count(0)
	x = l.count(1)
	c = l.count(2)
	v = len(l) - z - x
	b = (v - 1) * v // 2
	n = (c - 1) * c // 2
	print(b - n)


from bisect import *
from collections import *
from sys import stdin, stdout
from queue import *
from itertools import *
from heapq import *
from random import *
from statistics import *
from math import *
import operator
inn = stdin.readline
out = stdout.write
for i in range(int(inn())):
	n = int(inn())
	a = list(map(int, inn().split()))
	d = defaultdict(list)
	s = 0
	for i in range(n):
		if a[i] == 1 or a[i] == 0 or a[i] == 2:
			d[a[i]].append(i)
	for i in range(n):
		if a[i] == 0 or a[i] == 1:
			continue
		if a[i] == 2:
			k = len(d[0]) - bisect(d[0], i)
			k1 = len(d[1]) - bisect(d[1], i)
			k2 = len(d[2]) - bisect(d[2], i)
			s += n - i - 1 - (k + k1 + k2)
			continue
		k = len(d[0]) - bisect(d[0], i)
		k1 = len(d[1]) - bisect(d[1], i)
		s += n - i - 1 - (k + k1)
	print(s)


T = int(input())
for _ in range(T):
	N = int(input())
	A = list(map(int, input().split()))
	(ans, ans1) = (0, 0)
	for i in range(N):
		if A[i] > 2:
			ans += 1
		elif A[i] == 2:
			ans1 += 1
	print(ans * ans1 + ans * (ans - 1) // 2)


def solve():
	n = int(input())
	l = list(map(int, input().split()))
	t_2 = 0
	tn_2 = 0
	for i in range(len(l)):
		if l[i] > 2:
			tn_2 += 1
		elif l[i] == 2:
			t_2 += 1
	ans = tn_2 * (tn_2 - 1) / 2
	ans += t_2 * tn_2
	print(int(ans))

def __starting_point():
	t = int(input())
	while t != 0:
		solve()
		t -= 1
__starting_point()


t = int(input())
for _ in range(t):
	n = int(input())
	l1 = list(map(int, input().split()))
	l2 = [i for i in l1 if i > 1]
	n = len(l2)
	tc = n * (n - 1) // 2
	if l2.count(2) > 1:
		n1 = l1.count(2)
		tc1 = n1 * (n1 - 1) // 2
		tc = tc - tc1
	print(tc)


def comb(n, r):
	value = n * (n - 1) // r
	return max(0, value)
for test_cases in range(int(input())):
	n = int(input())
	nums = list(map(int, input().split()))
	i = n - 1
	value = 0
	ans = 0
	two_counts = 0
	other_count = 0
	for i in nums:
		if i == 2:
			two_counts += 1
		if i > 2:
			other_count += 1
	value = comb(two_counts + other_count, 2)
	seln_2 = comb(two_counts, 2)
	ans = value - max(0, seln_2)
	ans = max(ans, 0)
	print(ans)