This is the easy version of this problem. The only difference between easy and hard versions is the constraints on $k$ and $m$ (in this version $k=2$ and $m=3$). Also, in this version of the problem, you DON'T NEED to output the answer by modulo.

You are given a sequence $a$ of length $n$ consisting of integers from $1$ to $n$. The sequence may contain duplicates (i.e. some elements can be equal).

Find the number of tuples of $m = 3$ elements such that the maximum number in the tuple differs from the minimum by no more than $k = 2$. Formally, you need to find the number of triples of indices $i < j < z$ such that

$$\max(a_i, a_j, a_z) - \min(a_i, a_j, a_z) \le 2.$$

For example, if $n=4$ and $a=[1,2,4,3]$, then there are two such triples ($i=1, j=2, z=4$ and $i=2, j=3, z=4$). If $n=4$ and $a=[1,1,1,1]$, then all four possible triples are suitable.


-----Input-----

The first line contains a single integer $t$ ($1 \le t \le 2 \cdot 10^5$) — the number of test cases. Then $t$ test cases follow.

The first line of each test case contains an integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the sequence $a$.

The next line contains $n$ integers $a_1, a_2,\ldots, a_n$ ($1 \le a_i \le n$) — the sequence $a$.

It is guaranteed that the sum of $n$ for all test cases does not exceed $2 \cdot 10^5$.


-----Output-----

Output $t$ answers to the given test cases. Each answer is the required number of triples of elements, such that the maximum value in the triple differs from the minimum by no more than $2$. Note that in difference to the hard version of the problem, you don't need to output the answer by modulo. You must output the exact value of the answer.


-----Examples-----

Input
4
4
1 2 4 3
4
1 1 1 1
1
1
10
5 6 1 3 2 9 8 1 2 4
Output
2
4
0
15


-----Note-----

None

Source: codeforces | Difficulty: MEDIUM
Tags: Sorting

Combinatorics

Amortized analysis

Mathematics


Solutions:
from sys import stdin
import io, os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
from collections import Counter
t = int(input())
for tt in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	c = Counter(a)
	c_k = c.keys()
	ans = 0
	for i in c_k:
		num = c[i + 1] + c[i + 2]
		ans += c[i] * (num * (num - 1) // 2)
		ans += c[i] * (c[i] - 1) // 2 * num
		ans += c[i] * (c[i] - 1) * (c[i] - 2) // 6
	print(ans)


def nc3(n):
	if n < 3:
		return 0
	else:
		return n * (n - 1) * (n - 2) // 6

def nc2(n):
	if n < 2:
		return 0
	else:
		return n * (n - 1) // 2
t = int(input())
for test in range(t):
	n = int(input())
	List = list(map(int, input().rstrip().split()))
	if n < 3:
		print(0)
	else:
		array = [0 for i in range(n)]
		for item in List:
			array[item - 1] += 1
		ans = 0
		for item in array:
			ans += nc3(item)
		for i in range(n - 1):
			ans += nc2(array[i]) * array[i + 1] + array[i] * nc2(array[i + 1])
		for i in range(n - 2):
			ans += array[i] * array[i + 1] * array[i + 2] + (nc2(array[i]) * array[i + 2] + array[i] * nc2(array[i + 2]))
		print(ans)


for _ in range(int(input())):
	n = int(input())
	a = sorted([int(i) for i in input().split()])
	(i, j) = (0, 2)
	result = 0
	while j < n:
		if a[j] - a[i] <= 2:
			result += (j - i - 1) * (j - i) // 2
			j += 1
		else:
			i += 1
		if j - i == 1:
			j += 1
	print(result)


from sys import stdin, stdout
input = stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	arr = [int(x) for x in input().split()]
	vals = [0 for x in range(0, n + 1)]
	for i in arr:
		vals[i] += 1
	ans = 0
	for i in range(1, n + 1):
		a = vals[i]
		p = 0
		if i + 1 <= n:
			p += vals[i + 1]
		if i + 2 <= n:
			p += vals[i + 2]
		ans += a * (p * (p - 1) // 2) + a * (a - 1) // 2 * p + a * (a - 1) * (a - 2) // 6
	print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	val = 0
	j = 0
	k = 0
	while j < n - 2:
		while j + k < n and a[j + k] - a[j] <= 2:
			k += 1
		if k >= 3:
			val += (k - 2) * (k - 1) // 2
		k -= 1
		j += 1
	print(val)


import operator as op
from functools import reduce

def ncr(n, r):
	r = min(r, n - r)
	numer = reduce(op.mul, range(n, n - r, -1), 1)
	denom = reduce(op.mul, range(1, r + 1), 1)
	return numer // denom

def move(i, j, l, d):
	while l[j] - l[i] > 2:
		i += 1
	while j < n and l[j] - l[i] <= 2:
		j += 1
	if j - i < 2 and j != n:
		(i, j) = move(i, j, l, d)
	return (i, j)
t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	l.sort()
	if n < 3:
		print(0)
		continue
	ans = 0
	i = 0
	j = 2
	while j < n:
		li = i
		lj = j
		(i, j) = move(i, j, l, 2)
		if j - i >= 3:
			ans += ncr(j - i, 3)
			if lj - i >= 3:
				ans -= ncr(lj - i, 3)
	print(ans)


def bsearch_upper_bound(arr, num, start, end):
	i = start
	j = end
	while i < j:
		mid = (i + j) // 2
		if arr[mid] > num:
			j = mid - 1
		else:
			i = mid + 1
	if arr[i] <= num:
		return i
	else:
		return i - 1
t = int(input())
k = 2
for _ in range(t):
	n = int(input())
	arr = list(map(lambda x: int(x), input().split()))
	if n < 3:
		print(0)
		continue
	arr.sort()
	dict_upper_bound = {}
	count = 0
	i = 0
	while i < n:
		if arr[i] in dict_upper_bound:
			jk = dict_upper_bound[arr[i]]
		else:
			jk = bsearch_upper_bound(arr, arr[i] + k, i, n - 1)
			dict_upper_bound[arr[i]] = jk
		if jk - i > 1:
			m = jk - i - 1
			count += m * (m + 1) // 2
		i += 1
	print(count)


from collections import Counter
from sys import stdin, stdout
from math import *
t = int(input())
for i in range(t):
	n = int(stdin.readline())
	arr = [int(x) for x in stdin.readline().split()]
	if n < 3:
		print(0)
		continue
	ass = Counter(arr)
	lp = set(arr)
	cnt = 0
	for itt in lp:
		qp = ass[itt]
		qxx = 0
		qtt = 0
		if itt + 1 in lp:
			qxx = ass[itt + 1]
		if itt + 2 in lp:
			qtt = ass[itt + 2]
		if qp >= 3:
			cnt += qp * (qp - 1) * (qp - 2) // 6
		if qxx > 0 and qp >= 2:
			cnt += qp * (qp - 1) * ass[itt + 1] // 2
		if qtt > 0 and qp >= 2:
			cnt += qp * (qp - 1) * ass[itt + 2] // 2
		if qxx > 0 and qtt > 0:
			cnt += qp * qxx * qtt
		if qxx > 1:
			cnt += qxx * (qxx - 1) * qp // 2
		if qtt > 1:
			cnt += qtt * (qtt - 1) * qp // 2
	print(cnt)


from sys import stdin
input = stdin.readline
for _ in ' ' * int(input()):
	n = int(input())
	a = sorted([*map(int, input().split())])
	if n < 3:
		print(0)
		continue
	(ans, r) = (0, 2)
	for l in range(n - 2):
		if r == l + 1:
			r += 1
		while r < n and a[r] - a[l] <= 2:
			le = r - l - 1
			ans += (1 + le) * le // 2
			r += 1
	print(ans)


import sys, os, getpass
import heapq as hq
import math, random, functools, itertools
from collections import Counter, defaultdict, deque
input = sys.stdin.readline
M9 = 10 ** 9 + 7

def ncr(n, k):
	if n < 0:
		return 0
	if k < 0:
		return 0
	if n < k:
		return 0
	return choose(n, k)

@functools.lru_cache(maxsize=None)
def choose(n, k):
	if k == 0:
		return 1
	return n * choose(n - 1, k - 1) // k % M9
OFFLINE_TEST = getpass.getuser() == 'hkmac'

def log(*args):
	if OFFLINE_TEST:
		print('\x1b[36m', *args, '\x1b[0m', file=sys.stderr)

def solve_(lst, m, k):
	if m == 1:
		return len(lst)
	c = Counter(lst)
	crr = [(k, v) for (k, v) in sorted(c.items())]
	right_ptr = 0
	res = 0
	cursum = 0
	for (x, cnt) in crr:
		while right_ptr < len(crr) and crr[right_ptr][0] - k <= x:
			cursum += crr[right_ptr][1]
			right_ptr += 1
		for a in range(1, cnt + 1):
			if m - a < 0:
				continue
			if cursum - cnt < 0:
				continue
			res += ncr(cnt, a) * ncr(cursum - cnt, m - a)
		cursum -= cnt
	return res % M9

def solve(*args):
	if OFFLINE_TEST:
		log('----- solving ------')
		log(*args)
		log('----- ------- ------')
	return solve_(*args)

def read_matrix(rows):
	return [list(map(int, input().split())) for _ in range(rows)]

def read_strings(rows):
	return [input().strip() for _ in range(rows)]
for case_num in range(int(input())):
	arr = list(map(int, input().split()))
	if len(arr) == 1:
		M9 = 10 ** 16
		arr = [arr[0], 3, 2]
	lst = list(map(int, input().split()))
	lst.sort()
	res = solve(lst, arr[1], arr[2])
	print(res)


import os
import sys
from io import BytesIO, IOBase
import heapq as h
import bisect
from types import GeneratorType
BUFSIZE = 8192

class SortedList:

	def __init__(self, iterable=[], _load=200):
		values = sorted(iterable)
		self._len = _len = len(values)
		self._load = _load
		self._lists = _lists = [values[i:i + _load] for i in range(0, _len, _load)]
		self._list_lens = [len(_list) for _list in _lists]
		self._mins = [_list[0] for _list in _lists]
		self._fen_tree = []
		self._rebuild = True

	def _fen_build(self):
		self._fen_tree[:] = self._list_lens
		_fen_tree = self._fen_tree
		for i in range(len(_fen_tree)):
			if i | i + 1 < len(_fen_tree):
				_fen_tree[i | i + 1] += _fen_tree[i]
		self._rebuild = False

	def _fen_update(self, index, value):
		if not self._rebuild:
			_fen_tree = self._fen_tree
			while index < len(_fen_tree):
				_fen_tree[index] += value
				index |= index + 1

	def _fen_query(self, end):
		if self._rebuild:
			self._fen_build()
		_fen_tree = self._fen_tree
		x = 0
		while end:
			x += _fen_tree[end - 1]
			end &= end - 1
		return x

	def _fen_findkth(self, k):
		_list_lens = self._list_lens
		if k < _list_lens[0]:
			return (0, k)
		if k >= self._len - _list_lens[-1]:
			return (len(_list_lens) - 1, k + _list_lens[-1] - self._len)
		if self._rebuild:
			self._fen_build()
		_fen_tree = self._fen_tree
		idx = -1
		for d in reversed(range(len(_fen_tree).bit_length())):
			right_idx = idx + (1 << d)
			if right_idx < len(_fen_tree) and k >= _fen_tree[right_idx]:
				idx = right_idx
				k -= _fen_tree[idx]
		return (idx + 1, k)

	def _delete(self, pos, idx):
		_lists = self._lists
		_mins = self._mins
		_list_lens = self._list_lens
		self._len -= 1
		self._fen_update(pos, -1)
		del _lists[pos][idx]
		_list_lens[pos] -= 1
		if _list_lens[pos]:
			_mins[pos] = _lists[pos][0]
		else:
			del _lists[pos]
			del _list_lens[pos]
			del _mins[pos]
			self._rebuild = True

	def _loc_left(self, value):
		if not self._len:
			return (0, 0)
		_lists = self._lists
		_mins = self._mins
		(lo, pos) = (-1, len(_lists) - 1)
		while lo + 1 < pos:
			mi = lo + pos >> 1
			if value <= _mins[mi]:
				pos = mi
			else:
				lo = mi
		if pos and value <= _lists[pos - 1][-1]:
			pos -= 1
		_list = _lists[pos]
		(lo, idx) = (-1, len(_list))
		while lo + 1 < idx:
			mi = lo + idx >> 1
			if value <= _list[mi]:
				idx = mi
			else:
				lo = mi
		return (pos, idx)

	def _loc_right(self, value):
		if not self._len:
			return (0, 0)
		_lists = self._lists
		_mins = self._mins
		(pos, hi) = (0, len(_lists))
		while pos + 1 < hi:
			mi = pos + hi >> 1
			if value < _mins[mi]:
				hi = mi
			else:
				pos = mi
		_list = _lists[pos]
		(lo, idx) = (-1, len(_list))
		while lo + 1 < idx:
			mi = lo + idx >> 1
			if value < _list[mi]:
				idx = mi
			else:
				lo = mi
		return (pos, idx)

	def add(self, value):
		_load = self._load
		_lists = self._lists
		_mins = self._mins
		_list_lens = self._list_lens
		self._len += 1
		if _lists:
			(pos, idx) = self._loc_right(value)
			self._fen_update(pos, 1)
			_list = _lists[pos]
			_list.insert(idx, value)
			_list_lens[pos] += 1
			_mins[pos] = _list[0]
			if _load + _load < len(_list):
				_lists.insert(pos + 1, _list[_load:])
				_list_lens.insert(pos + 1, len(_list) - _load)
				_mins.insert(pos + 1, _list[_load])
				_list_lens[pos] = _load
				del _list[_load:]
				self._rebuild = True
		else:
			_lists.append([value])
			_mins.append(value)
			_list_lens.append(1)
			self._rebuild = True

	def discard(self, value):
		_lists = self._lists
		if _lists:
			(pos, idx) = self._loc_right(value)
			if idx and _lists[pos][idx - 1] == value:
				self._delete(pos, idx - 1)

	def remove(self, value):
		_len = self._len
		self.discard(value)
		if _len == self._len:
			raise ValueError('{0!r} not in list'.format(value))

	def pop(self, index=-1):
		(pos, idx) = self._fen_findkth(self._len + index if index < 0 else index)
		value = self._lists[pos][idx]
		self._delete(pos, idx)
		return value

	def bisect_left(self, value):
		(pos, idx) = self._loc_left(value)
		return self._fen_query(pos) + idx

	def bisect_right(self, value):
		(pos, idx) = self._loc_right(value)
		return self._fen_query(pos) + idx

	def count(self, value):
		return self.bisect_right(value) - self.bisect_left(value)

	def __len__(self):
		return self._len

	def __getitem__(self, index):
		(pos, idx) = self._fen_findkth(self._len + index if index < 0 else index)
		return self._lists[pos][idx]

	def __delitem__(self, index):
		(pos, idx) = self._fen_findkth(self._len + index if index < 0 else index)
		self._delete(pos, idx)

	def __contains__(self, value):
		_lists = self._lists
		if _lists:
			(pos, idx) = self._loc_left(value)
			return idx < len(_lists[pos]) and _lists[pos][idx] == value
		return False

	def __iter__(self):
		return (value for _list in self._lists for value in _list)

	def __reversed__(self):
		return (value for _list in reversed(self._lists) for value in reversed(_list))

	def __repr__(self):
		return 'SortedList({0})'.format(list(self))

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		import os
		self.os = os
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			self.os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
import sys
from math import ceil, log2
INT_MAX = sys.maxsize
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
import collections as col
import math, string

def getInts():
	return [int(s) for s in input().split()]

def getInt():
	return int(input())

def getStrs():
	return [s for s in input().split()]

def getStr():
	return input()

def listStr():
	return list(input())

def minVal(x, y):
	return x if x < y else y

def getMid(s, e):
	return s + (e - s) // 2

def RMQUtil(st, ss, se, qs, qe, index):
	if qs <= ss and qe >= se:
		return st[index]
	if se < qs or ss > qe:
		return INT_MAX
	mid = getMid(ss, se)
	return minVal(RMQUtil(st, ss, mid, qs, qe, 2 * index + 1), RMQUtil(st, mid + 1, se, qs, qe, 2 * index + 2))

def RMQ(st, n, qs, qe):
	if qs < 0 or qe > n - 1 or qs > qe:
		print('Invalid Input')
		return -1
	return RMQUtil(st, 0, n - 1, qs, qe, 0)

def constructSTUtil(arr, ss, se, st, si):
	if ss == se:
		st[si] = arr[ss]
		return arr[ss]
	mid = getMid(ss, se)
	st[si] = minVal(constructSTUtil(arr, ss, mid, st, si * 2 + 1), constructSTUtil(arr, mid + 1, se, st, si * 2 + 2))
	return st[si]

def constructST(arr, n):
	x = int(ceil(log2(n)))
	max_size = 2 * int(2 ** x) - 1
	st = [0] * max_size
	constructSTUtil(arr, 0, n - 1, st, 0)
	return st
MOD = 10 ** 9 + 7
mod = 10 ** 9 + 7
t = int(input())
p = 10 ** 9 + 7

def ncr_util():
	inv[0] = inv[1] = 1
	fact[0] = fact[1] = 1
	for i in range(2, 300001):
		inv[i] = inv[i % p] * (p - p // i) % p
	for i in range(1, 300001):
		inv[i] = inv[i - 1] * inv[i] % p
		fact[i] = fact[i - 1] * i % p

def z_array(s1):
	n = len(s1)
	z = [0] * n
	(l, r, k) = (0, 0, 0)
	for i in range(1, n):
		if i > r:
			(l, r) = (i, i)
			while r < n and s1[r - l] == s1[r]:
				r += 1
			z[i] = r - l
			r -= 1
		else:
			k = i - l
			if z[k] < r - i + 1:
				z[i] = z[k]
			else:
				l = i
				while r < n and s1[r - l] == s1[r]:
					r += 1
				z[i] = r - l
				r -= 1
	return z

def primeFactors(n):
	d1 = {}
	while n % 2 == 0:
		d1[2] = d1.get(2, 0) + 1
		n = n / 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			d1[i] = d1.get(i, 0) + 1
			n = n // i
	if n > 2:
		d1[n] = d1.get(n, 0) + 1
	return d1

def fs(x):
	if x < 2:
		return 0
	return x * (x - 1) // 2

def solve():
	i = 0
	j = 0
	(cnt, ans) = (0, 0)
	while i < n - 2:
		pos = bisect.bisect_left(l, l[i] + 3)
		pos -= 1
		ans += fs(pos - i)
		i += 1
	return ans
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	l.sort()
	print(solve())


import os
import sys
from io import BytesIO, IOBase

def ncr(n, k):
	C = [0 for i in range(k + 1)]
	C[0] = 1
	for i in range(1, n + 1):
		j = min(i, k)
		while j > 0:
			C[j] = C[j] + C[j - 1]
			j -= 1
	return C[k]

def main():
	for _ in range(int(input())):
		n = int(input())
		k = 2
		m = 3
		a = list(map(int, input().split()))
		a.sort()
		l = 0
		r = 0
		ans = 0
		while l < n and r < n:
			while r < n and a[r] - a[l] <= k:
				r += 1
			ans += ncr(r - l - 1, m - 1)
			l += 1
		ans += ncr(r - l, m)
		print(ans)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


t = int(input())
while t:
	t -= 1
	n = int(input())
	arr = list(map(int, input().split()))
	arr.sort()
	ans = 0
	one = 0
	two = 0
	while one < n - 2:
		if two < n and arr[two] - arr[one] <= 2:
			two += 1
		else:
			if two - one > 2:
				ans += (two - one - 2) * (two - one - 1) // 2
			one += 1
	print(ans)


import math
from collections import defaultdict
from sys import stdin
input = stdin.readline
T = int(input())

def ncr(n):
	return n * (n - 1) * (n - 2) // 6
for _ in range(T):
	n = int(input())
	l = list(map(int, input().split()))
	ma = max(l)
	hash = [0] * (n + 2)
	for i in l:
		hash[i] += 1
	res = ncr(hash[1])
	for i in range(2, n + 1):
		res += ncr(hash[i] + hash[i - 1] + hash[i - 2]) - ncr(hash[i - 1] + hash[i - 2])
	print(res)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	cnt = i = 0
	j = 2
	while i < n - 2:
		if j < n and a[j] - a[i] <= 2:
			j += 1
		else:
			if j - i > 2:
				cnt += (j - i - 2) * (j - i - 1) // 2
			i += 1
	print(cnt)


from collections import defaultdict
fact = dict()

def f(x):
	if x not in fact:
		if x <= 1:
			fact[x] = 1
		else:
			fact[x] = x * f(x - 1)
	return fact[x]
for _ in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	if n < 3:
		print(0)
		continue
	d = defaultdict(int)
	for x in range(n):
		d[a[x]] += 1
	k = sorted(d.keys())
	N = 0
	for i in range(len(k)):
		l = d[k[i]]
		if l >= 3:
			N += l * (l - 1) * (l - 2) // 6
		e = 0
		if i + 1 < len(k) and k[i + 1] - k[i] <= 2:
			e += d[k[i + 1]]
		if i + 2 < len(k) and k[i + 2] - k[i] <= 2:
			e += d[k[i + 2]]
		if e > 0:
			N += l * e * (e - 1) // 2
			N += l * (l - 1) // 2 * e
	print(N)


t = int(input())
for i in range(t):
	n = int(input())
	a = [int(j) for j in input().split()]
	if n < 3:
		print(0)
	else:
		res = 0
		dic = {}
		for el in a:
			if el in dic:
				dic[el] += 1
			else:
				dic[el] = 1
		for el in dic:
			if dic[el] >= 3:
				res += dic[el] * (dic[el] - 1) * (dic[el] - 2) // 6
			if dic[el] >= 2:
				if el + 1 in dic:
					res += dic[el + 1] * dic[el] * (dic[el] - 1) // 2
				if el + 2 in dic:
					res += dic[el + 2] * dic[el] * (dic[el] - 1) // 2
			if el + 1 in dic and el + 2 in dic:
				res += dic[el] * dic[el + 1] * dic[el + 2]
			if el + 1 in dic and dic[el + 1] >= 2:
				res += dic[el] * dic[el + 1] * (dic[el + 1] - 1) // 2
			if el + 2 in dic and dic[el + 2] >= 2:
				res += dic[el] * dic[el + 2] * (dic[el + 2] - 1) // 2
		print(res)


from sys import stdin
from array import array
input = stdin.readline

def main():
	for _ in ' ' * int(input()):
		n = int(input())
		a = array('i', sorted((int(x) for x in input().split())))
		if n < 3:
			print(0)
			continue
		(ans, r) = (0, 2)
		for l in range(n - 2):
			if r == l + 1:
				r += 1
			while r < n and a[r] - a[l] <= 2:
				le = r - l - 1
				ans += (1 + le) * le // 2
				r += 1
		print(ans)
main()


t = int(input())
from bisect import bisect_left
for _ in range(t):
	n = int(input())
	my_list = [int(i) for i in input().split()]
	my_list.sort()
	res = 0
	k = 0
	if len(my_list) <= 2:
		print(res)
	else:
		for j in range(n):
			pos = bisect_left(my_list, my_list[j] + 3) - 1
			C = pos - j
			res += max(0, C * (C - 1) // 2)
		print(res)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
import bisect
from math import ceil, floor, sqrt
from collections import Counter, defaultdict, deque

def int1():
	return int(input())

def map1():
	return map(int, input().split())

def list1():
	return list(map(int, input().split()))

def solve():
	n = int1()
	l1 = list1()
	d = defaultdict(int)
	for i in range(n):
		d[l1[i]] += 1
	l1 = list(set(l1))
	n1 = len(l1)
	sum1 = 0
	for i in range(n1):
		a = d[l1[i]]
		b = d[l1[i] + 1]
		c = d[l1[i] + 2]
		v = a + b + c
		while v > 2 and a != 0:
			a = a - 1
			v = a + b + c
			sum1 += v * (v - 1) // 2
	print(sum1)
for _ in range(int(input())):
	solve()


import sys
input = sys.stdin.readline

def nc3(n):
	return n * (n - 1) // 2
for _ in range(int(input())):
	n = int(input())
	a = sorted([*map(int, input().split())])
	i = 0
	j = 0
	ans = 0
	while i < n:
		while j + 1 < n and a[j + 1] - a[i] <= 2:
			j += 1
		ans += nc3(j - i)
		i += 1
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = sorted(list(map(int, input().split())))
	(i, j, ans) = (0, 2, 0)
	while j < n:
		if a[j] - a[i] <= 2:
			ans += (j - i - 1) * (j - i) // 2
			j += 1
		else:
			i += 1
		if j - i == 1:
			j += 1
	print(ans)
num_inp = lambda : int(input())
arr_inp = lambda : list(map(int, input().split()))
sp_inp = lambda : map(int, input().split())
str_inp = lambda : input()


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	if len(arr) < 3:
		print(0)
		continue
	lst = sorted(arr)
	res = 0
	(count, idx) = (0, 2)
	for i in range(len(lst) - 2):
		if count != 0:
			count -= 1
		for j in range(idx, len(lst)):
			if j < i + 2:
				continue
			if lst[i] < lst[j] - 2:
				idx = j
				break
			else:
				count += 1
			idx = j + 1
		res += count * (count + 1) // 2
	print(res)


for _ in ' ' * int(input()):
	n = int(input())
	a = sorted([*map(int, input().split())])
	if n < 3:
		print(0)
		continue
	r = 2
	ans = 0
	for l in range(n - 2):
		if r == l + 1:
			r += 1
		while r < n and a[r] - a[l] <= 2:
			f = 1
			le = r - l - 1
			ans += (1 + le) * le // 2
			r += 1
	print(ans)


import os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
from collections import Counter
t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	d = Counter(a)
	s = 0
	a.sort()
	for j in range(n):
		if d[a[j]] > 0:
			p = d[a[j]]
			if a[j] + 1 in d:
				q = d[a[j] + 1]
			else:
				q = 0
			if a[j] + 2 in d:
				r = d[a[j] + 2]
			else:
				r = 0
			s = s + p * q * r + p * (p - 1) * (p - 2) // 6
			s = s + p * (p - 1) // 2 * r
			s = s + r * (r - 1) // 2 * p
			s = s + p * (p - 1) // 2 * q
			s = s + q * (q - 1) // 2 * p
			d[a[j]] = 0
	print(s)


x = int(input())
while x > 0:
	n = int(input())
	arr = [int(i) for i in input().split()]
	if n < 3:
		print(0)
		x -= 1
		continue
	arr.sort()
	ans = 0
	ok = {}
	for (i, j) in enumerate(arr):
		ok[j] = i
	for i in range(len(arr) - 2):
		let = None
		if arr[i] + 2 in ok:
			let = ok[arr[i] + 2]
		elif arr[i] + 1 in ok:
			let = ok[arr[i] + 1]
		elif arr[i] in ok:
			let = ok[arr[i]]
		if let and let - i - 1 > 0:
			kk = let - i - 1
			ans += kk * (kk + 1) // 2
	print(ans)
	x -= 1


import math, bisect

def printNcR(n, r):
	p = 1
	k = 1
	if n - r < r:
		r = n - r
	if r != 0:
		while r:
			p *= n
			k *= r
			m = math.gcd(p, k)
			p //= m
			k //= m
			n -= 1
			r -= 1
	else:
		p = 1
	return p
for _ in range(int(input())):
	n = int(input())
	A = list(map(int, input().split()))
	A.sort()
	ans = 0
	for i in range(n - 2):
		if A[i + 2] - A[i] <= 2:
			(low, high) = (i, n - 1)
			out = bisect.bisect_right(A, A[i] + 2)
			if out == n:
				out = n - 1
			elif A[out] > A[i] + 2:
				out -= 1
			ans += printNcR(out - i, 2)
	print(ans)


import bisect
from sys import stdin
t = int(stdin.readline())
for _ in range(t):
	n = int(stdin.readline())
	ans = 0
	a = list(map(int, stdin.readline().strip().split()))
	a.sort()
	for i in range(n):
		j = bisect.bisect_right(a, a[i] + 2)
		j = j - i - 1
		if j > 1:
			ans += j * (j - 1) // 2
	print(ans)


def fun(a):
	return (a - 1) * a // 2
for y in range(int(input())):
	n = int(input())
	lst = list(map(int, input().split()))
	lst.sort()
	cnt = 0
	p = 0
	if n < 3:
		print('0')
	else:
		for i in range(2, n):
			foo = 0
			while lst[i] - lst[p] > 2:
				foo = 1
				p += 1
			cnt += fun(i - p)
		print(cnt)


import sys
import math, bisect, operator
(inf, mod) = (float('inf'), 10 ** 9 + 7)
sys.setrecursionlimit(10 ** 5)
from itertools import groupby, accumulate
from heapq import heapify, heappop, heappush
from collections import deque, Counter, defaultdict
I = lambda : int(sys.stdin.readline())
neo = lambda : map(int, sys.stdin.readline().split())
Neo = lambda : list(map(int, sys.stdin.readline().split()))
fact = [1]
for _ in range(I()):
	n = I()
	(m, k) = (3, 2)
	A = Neo()
	A.sort()
	Ans = 0
	m -= 1
	for i in range(n):
		t = bisect.bisect_right(A, A[i] + k) - i - 1
		if t >= m:
			Ans += t * (t - 1) // 2
	print(Ans)


t = int(input())
for _ in range(t):
	n = int(input())
	if n < 3:
		input()
		print(0)
		continue
	sequence = list(map(int, input().split()))
	sequence.sort()
	j = 1
	ans = 0
	for i in range(n):
		while j < n and sequence[j] - sequence[i] <= 2:
			j += 1
		ans += (j - i - 1) * (j - i - 2) // 2 if j - i >= 3 else 0
	print(ans)


from collections import *
import sys
from bisect import *
input = sys.stdin.readline

def ri():
	return int(input())

def rl():
	return list(map(int, input().split()))
t = ri()
for _ in range(t):
	n = ri()
	aa = rl()
	aa.sort()
	if n < 3:
		print(0)
		continue
	old_k = n - 1
	ans = 0
	for i in range(n - 1, 1, -1):
		ai = aa[i]
		k = bisect_left(aa, ai - 2, 0, old_k)
		if i - k + 1 >= 3:
			ans += (i - k) * (i - k - 1) // 2
	print(ans)


from bisect import bisect_right
from sys import stdin
input = lambda : stdin.readline().rstrip()

def main():
	for _ in range(int(input())):
		n = int(input())
		a = sorted(list(map(int, input().split())))
		s = 0
		for i in range(n - 2):
			t = bisect_right(a, a[i] + 2)
			x = t - i - 1
			s += x * (x - 1) // 2
		print(s)
main()


import sys
input = sys.stdin.readline
for i in range(int(input())):
	n = int(input())
	q = list(map(int, input().split()))
	c = [0] * (n + 10)
	for i in q:
		c[i] += 1
	ans = 0
	for i in range(2, n):
		ans += c[i - 1] * c[i] * c[i + 1]
	for i in range(1, n):
		ans += c[i] * (c[i] - 1) // 2 * c[i + 1]
	for i in range(2, n + 1):
		ans += c[i - 1] * c[i] * (c[i] - 1) // 2
	for i in range(2, n):
		ans += c[i - 1] * c[i + 1] * (c[i + 1] - 1) // 2
	for i in range(2, n):
		ans += c[i - 1] * c[i + 1] * (c[i - 1] - 1) // 2
	for i in range(1, n + 1):
		ans += (c[i] - 1) * c[i] * (c[i] - 2) // 6
	print(ans)


test_num = int(input())

def solve():
	n = int(input())
	sequence = list(map(int, input().split()))
	sequence.sort()
	ans = 0
	lp = 0
	rp = 0
	for lp in range(n - 2):
		a = sequence[lp]
		while rp < n - 1 and sequence[rp + 1] <= a + 2:
			rp += 1
		ans += (rp - lp) * (rp - lp - 1) // 2
	print(ans)
for i in range(test_num):
	solve()


from bisect import bisect_right

def bs(a, value):
	ans = bisect_right(a, value)
	if ans != len(a) + 1:
		return ans - 1
	else:
		return -1
for _ in range(int(input())):
	n = int(input())
	arr = [int(x) for x in input().split()]
	arr.sort()
	s = 0
	for i in range(n - 2):
		c = bs(arr, arr[i] + 2)
		if c >= i + 1:
			s += (c - i - 1) * (c - i) // 2
	print(s)


from collections import defaultdict
import bisect
from itertools import accumulate
import os
import sys
import math
from decimal import *
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))

def input():
	return sys.stdin.readline().rstrip('\r\n')
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	a = sorted(a)
	I = 0
	J = 2
	ans = 0
	while J < n:
		if a[J] - a[I] > 2:
			I += 1
		elif J - I == 2:
			ans += 1
			J += 1
		else:
			x = J - I
			ans += x * (x - 1) // 2
			J += 1
	print(ans)


from sys import stdin
for _ in range(int(stdin.readline())):
	n = int(stdin.readline())
	a = list(map(int, stdin.readline().split()))
	a.sort()
	(j, c, e) = (0, 0, 0)
	for i in range(2, n):
		while a[i] - a[j] > 2:
			j = j + 1
		d = (i - j) * (i - j - 1) // 2
		e = e + d
	print(e)


from sys import stdin
from collections import defaultdict, Counter
from math import factorial

def iinput():
	return int(stdin.readline())

def minput():
	return map(int, stdin.readline().split())

def linput():
	return list(map(int, stdin.readline().split()))
t = iinput()
while t:
	t -= 1
	n = iinput()
	a = linput()
	c = Counter(a)
	tuples = 0
	for i in range(1, n + 1):
		if c[i] >= 1:
			tuples += c[i] * c[i + 1] * c[i + 2]
			if c[i + 1] > 1:
				tuples += c[i] * (c[i + 1] * (c[i + 1] - 1) // 2)
			if c[i + 2] > 1:
				tuples += c[i] * (c[i + 2] * (c[i + 2] - 1) // 2)
		if c[i] >= 2:
			tuples += c[i] * (c[i] - 1) // 2 * (c[i + 1] + c[i + 2])
		if c[i] >= 3:
			tuples += c[i] * (c[i] - 1) * (c[i] - 2) // 6
	print(tuples)


for _ in range(int(input())):
	n = int(input())
	lis = list(map(int, input().split()))
	lis.sort()
	if len(lis) == 1:
		print(0)
		continue
	j = 0
	i = 2
	ans = 0
	while i < len(lis):
		if lis[i] - lis[j] > 2:
			while j + 1 < i and lis[i] - lis[j] > 2:
				j += 1
			x = i - j - 1
			ans += x * (x + 1) // 2
		else:
			x = i - j - 1
			ans += x * (x + 1) // 2
		i += 1
	print(ans)


from sys import stdin, stdout
from collections import Counter
from collections import deque

def II():
	return int(stdin.readline())

def LI():
	return list(map(int, stdin.readline().split()))

def nc2(y):
	if y < 2:
		res = 0
	else:
		res = y * (y - 1)
		res = res // 2
	return res

def nc3(y):
	if y < 3:
		res = 0
	else:
		res = y * (y - 1) * (y - 2)
		res = res // 6
	return res

def getWays(nums, f):
	l = len(nums)
	c = nums[-1]
	fc = f[c]
	ways = nc3(fc)
	if l > 1:
		b = nums[-2]
		fb = f[b]
		ways += fb * nc2(fc)
		ways += nc2(fb) * fc
	if l == 3:
		a = nums[0]
		fa = f[a]
		ways += fa * nc2(fc)
		ways += nc2(fa) * fc
		ways += fa * fb * fc
	return ways

def solve():
	n = II()
	a = LI()
	f = Counter(a)
	l = sorted(set(a))
	nums = deque([])
	ans = 0
	for x in l:
		nums.append(x)
		y = nums[0]
		while x - y > 2:
			nums.popleft()
			y = nums[0]
		ans += getWays(nums, f)
	stdout.write('{} \n'.format(ans))
t = II()
for _ in range(t):
	solve()


import sys

def input():
	return sys.stdin.readline().rstrip('\r\n')

def maps():
	return [int(i) for i in input().split()]
from itertools import combinations
from collections import defaultdict
from functools import reduce
import math as mt
import operator as op

def nCr(n, r):
	return reduce(op.mul, range(n - r + 1, n + 1), 1) // mt.factorial(r)
for _ in range(*maps()):
	(n,) = maps()
	a = [*maps()]
	d = defaultdict(int)
	for i in range(n):
		d[a[i]] += 1
	done = defaultdict(int)
	ans = 0
	for i in set(a):
		x = i
		rem = 0
		if done[x - 2]:
			rem += d[x - 2]
		if done[x - 1]:
			rem += d[x - 1]
		X = d[x] + d[x - 1] + d[x - 2]
		done[x - 1] += 1
		done[x] += 1
		done[x - 2] += 1
		ans += nCr(X, 3) - nCr(rem, 3)
	print(ans)


import sys
input = sys.stdin.readline

def nCk(n, k):
	if k > n or k < 0:
		return 0
	k = min(k, n - k)
	ret = 1
	for i in range(n, n - k, -1):
		ret *= i
	for i in range(2, k + 1):
		ret //= i
	return ret

def main():
	n = int(input())
	m = 3
	k = 2
	alst = list(map(int, input().split()))
	alst.sort()
	alst += [float('inf')]
	r = 0
	ans = 0
	for l in range(n):
		while alst[r] - alst[l] <= k:
			r += 1
		ans += nCk(r - l - 1, m - 1)
	print(ans)
for _ in range(int(input())):
	main()


import sys
input = sys.stdin.readline

def gcd(a, b):
	if a == 0:
		return b
	return gcd(b % a, a)

def lcm(a, b):
	return a * b / gcd(a, b)

def main():
	for _ in range(int(input())):
		n = int(input())
		a = list(map(int, input().split()))
		ans = 0
		d = {}
		for i in a:
			if i in d:
				d[i] += 1
			else:
				d[i] = 1
		a = []
		for i in d.keys():
			a.append([i, d[i]])
		a.sort()
		for i in a:
			if i[1] > 2:
				ans += i[1] * (i[1] - 1) * (i[1] - 2) // 6
		for i in range(1, len(a)):
			if a[i][0] - a[i - 1][0] <= 2:
				if a[i][1] >= 2 and a[i - 1][1] >= 1:
					ans += a[i][1] * (a[i][1] - 1) // 2 * a[i - 1][1]
				if a[i - 1][1] >= 2 and a[i][1] >= 1:
					ans += a[i - 1][1] * (a[i - 1][1] - 1) // 2 * a[i][1]
			if i >= 2 and a[i][0] - a[i - 2][0] <= 2:
				if a[i][1] >= 2 and a[i - 2][1] >= 1:
					ans += a[i][1] * (a[i][1] - 1) // 2 * a[i - 2][1]
				if a[i - 2][1] >= 2 and a[i][1] >= 1:
					ans += a[i - 2][1] * (a[i - 2][1] - 1) // 2 * a[i][1]
		for i in range(2, len(a)):
			if a[i][0] - a[i - 2][0] <= 2:
				ans += a[i][1] * a[i - 1][1] * a[i - 2][1]
		print(ans)
	return
main()


import sys
import bisect
input = sys.stdin.readline

def ncr(n, r):
	return n * (n - 1) * (n - 2) // 6
t = int(input())
for you in range(t):
	l = input().split()
	n = int(l[0])
	m = 3
	k = 2
	l = input().split()
	li = [int(i) for i in l]
	li.sort()
	ans = 0
	hashi = dict()
	for i in li:
		if i in hashi:
			hashi[i] += 1
		else:
			hashi[i] = 1
	for i in range(n + 5):
		if i not in hashi:
			continue
		z = bisect.bisect_right(li, i)
		y = bisect.bisect_left(li, i - k)
		ans += ncr(z - y, m)
		ans -= ncr(z - y - hashi[i], m)
	print(ans)


from collections import defaultdict, Counter
import sys
import bisect

def get_array():
	return list(map(int, sys.stdin.readline().strip().split()))

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def input():
	return sys.stdin.readline().strip()

def find(n):
	return n * (n - 1) // 2
for _ in range(int(input())):
	n = int(input())
	l = get_array()
	if n < 3:
		print(0)
		continue
	l.sort()
	ans = 0
	for i in range(n):
		ind = bisect.bisect_right(l, l[i] + 2, i)
		if ind - i >= 3:
			ans += find(ind - i - 1)
	print(ans)


from sys import stdin
input = stdin.readline

def main():
	n = int(input())
	a = [int(x) for x in input().split()]
	a.sort()
	if n < 3:
		print(0)
		return
	(ans, r) = (0, 2)
	for l in range(n - 2):
		if r == l + 1:
			r += 1
		while r < n and a[r] - a[l] <= 2:
			le = r - l - 1
			ans += (1 + le) * le // 2
			r += 1
	print(ans)
for _ in ' ' * int(input()):
	main()


import collections
import operator as op
from functools import reduce

def ncr(n, r):
	if r > n:
		return 0
	r = min(r, n - r)
	numer = reduce(op.mul, range(n, n - r, -1), 1)
	denom = reduce(op.mul, range(1, r + 1), 1)
	return numer // denom
t = int(input())
for i in range(0, t):
	n = input()
	di = collections.defaultdict(lambda : 0)
	li = [int(x) for x in input().split(' ')]
	if len(li) < 3:
		print(0)
		continue
	for x in li:
		di[x] += 1
		di[x + 1] += 0
		di[x + 2] += 0
	ite = list(sorted(di.keys()))[:-2]
	ans = sum([ncr(di[x], 3) + di[x] * di[x + 1] * di[x + 2] + di[x] * ncr(di[x + 1], 2) + ncr(di[x], 2) * di[x + 1] + di[x] * ncr(di[x + 2], 2) + ncr(di[x], 2) * di[x + 2] for x in ite])
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = sorted(list(map(int, input().split())))
	(i, j, ans) = (0, 2, 0)
	while j < n:
		if a[j] - a[i] <= 2:
			ans += (j - i - 1) * (j - i) // 2
			j += 1
		else:
			i += 1
		if j - i == 1:
			j += 1
	print(ans)


t = int(input())
while t != 0:
	n = int(input())
	list1 = list(map(int, input().split()))
	list1.sort()
	ans = 0
	i = 0
	j = 0
	while i < n - 2:
		if j < n and list1[j] - list1[i] <= 2:
			j += 1
		else:
			num = j - i - 1
			ans += num * (num - 1) // 2
			i += 1
	print(ans)
	t -= 1


g = lambda x: x * (x - 1) * (x - 2)
f = lambda x: sum((g(sum(c[i:i + x])) for i in range(n))) // 6
for s in [*open(0)][2::2]:
	a = s.split()
	n = len(a) + 2
	c = [0] * n
	for x in a:
		c[int(x) + 1] += 1
	print(f(3) - f(2))


import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, random, time
sys.setrecursionlimit(5 * 10 ** 5)
inf = 10 ** 20
mod = 10 ** 9 + 7
from math import gcd

def LI():
	return list(map(int, input().split()))

def II():
	return int(input())

def LS():
	return list(input().split())

def S():
	return input()

def solve():
	n = II()
	a_list = LI()
	cnt_dict = collections.defaultdict(int)
	for a in a_list:
		cnt_dict[a] += 1
	ans = 0
	for x in list(cnt_dict.keys()):
		(a1, a2, a3) = (cnt_dict[x], cnt_dict[x + 1], cnt_dict[x + 2])
		ans += math.comb(a1 + a2 + a3, 3)
		ans -= math.comb(a2 + a3, 3)
	return ans

def main():
	t = II()
	res = list()
	for _ in range(t):
		res.append(solve())
	for ans in res:
		print(ans)
	return 0
main()


t = int(input())
for q in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	a = sorted(a)
	score = 0
	i = 0
	j = 2
	while i < n - 2:
		while j < n and a[j] - a[i] <= 2:
			j += 1
		j -= 1
		score += (j - i) * (j - i - 1) / 2
		i += 1
	print(int(score))


from collections import defaultdict, deque, Counter
from sys import stdin, stdout
from heapq import heappush, heappop
import math
import io
import os
import math
import bisect

def isPrime(x):
	for i in range(2, x):
		if i * i > x:
			break
		if x % i == 0:
			return False
	return True

def ncr(n, r, p):
	num = den = 1
	for i in range(r):
		num = num * (n - i) % p
		den = den * (i + 1) % p
	return num * pow(den, p - 2, p) % p

def primeFactors(n):
	l = []
	while n % 2 == 0:
		l.append(2)
		n = n / 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			l.append(int(i))
			n = n / i
	if n > 2:
		l.append(n)
	return list(set(l))

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def sieve(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime

def digits(n):
	c = 0
	while n > 0:
		n //= 10
		c += 1
	return c

def ceil(n, x):
	if n % x == 0:
		return n // x
	return n // x + 1

def mapin():
	return map(int, input().split())

def solve(l, n):
	for i in range(1, n):
		if l[i] - l[i - 1] <= 1:
			return 2
	return 1

def convertToTernary(N, l):
	if N == 0:
		return l
	x = N % 3
	N = N // 3
	l.append(x)
	return convertToTernary(N, l)

def tw(f, i):
	return f[i] * (f[i] - 1) // 2

def th(f, i):
	return f[i] * (f[i] - 1) * (f[i] - 2) // 6
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
t = int(input())
for _ in range(t):
	n = int(input())
	l = [int(x) for x in input().split()]
	ff = [0 for i in range(n)]
	for i in range(n):
		ff[l[i] - 1] += 1
	ans = 0
	for i in range(n - 2):
		temp = ff[i] * ff[i + 1] * ff[i + 2] + ff[i] * tw(ff, i + 1) + ff[i] * tw(ff, i + 2)
		temp += tw(ff, i) * (ff[i + 1] + ff[i + 2])
		temp += th(ff, i)
		ans += temp
	if n >= 2:
		ans += tw(ff, n - 2) * ff[n - 1]
		ans += tw(ff, n - 1) * ff[n - 2]
		ans += th(ff, n - 1)
		ans += th(ff, n - 2)
	print(ans)


def find(arr, num):
	n = len(arr)
	(l, r) = (0, n - 1)
	while l <= r:
		mid = (l + r) // 2
		if arr[mid] > num and mid - 1 < n and (arr[mid - 1] <= num):
			return mid - 1
		if arr[mid] > num:
			r = mid - 1
		else:
			l = mid + 1
	return n - 1
memo = {}
for _ in range(int(input())):
	n = int(input())
	arr = [int(num) for num in input().split(' ')]
	s = ''
	for item in arr:
		s = s + str(item)
	if s in memo.keys():
		print(memo[s])
	else:
		arr.sort()
		ans = 0
		for i in range(n):
			idx = find(arr, arr[i] + 2)
			if idx - i + 1 >= 3:
				ans = ans + (idx - i) * (idx - i - 1) // 2
			else:
				ans = ans + 0
		print(ans)
		memo[s] = ans


from sys import stdin, stdout
import heapq
import cProfile
from collections import Counter, defaultdict, deque
from functools import reduce
import math
import threading
import sys
import time

def get_int():
	return int(stdin.readline().strip())

def get_tuple():
	return map(int, stdin.readline().split())

def get_list():
	return list(map(int, stdin.readline().split()))

def solve():
	n = get_int()
	ls = get_list()
	ls.sort()
	(i, k) = (0, 2)
	ans = 0
	while k < n:
		if ls[k] - ls[i] <= 2:
			fac = k - i
			ans += fac * (fac - 1) // 2
			k += 1
		else:
			i += 1
	print(ans)
testcases = True
if testcases:
	t = get_int()
	for _ in range(t):
		solve()
else:
	solve()


t = int(input())
while t:
	t -= 1
	n = int(input())
	arr = list(map(int, input().split()))
	arr.sort()
	c = 0
	j = 0
	i = 0
	while i < n - 2:
		if j < n and arr[j] - arr[i] <= 2:
			j += 1
		else:
			if j - i > 2:
				c += (j - i - 2) * (j - i - 1) // 2
			i += 1
	print(c)


import bisect
import collections
import functools
import itertools
import math
import heapq
import random
import string

def repeat(_func=None, *, times=1):

	def decorator(func):

		@functools.wraps(func)
		def wrapper(*args, **kwargs):
			for _ in range(times):
				func(*args, **kwargs)
		return wrapper
	if _func is None:
		return decorator
	else:
		return decorator(_func)

def unpack(func=int):
	return map(func, input().split())

def l_unpack(func=int):
	return list(map(func, input().split()))

def s_unpack(func=int, rev=False):
	return sorted(map(func, input().split()), reverse=rev)

def ml_unpack(n):
	return [int(input()) for _ in range(n)]

def range_n():
	return range(int(input()))

def getint():
	return int(input())

def getmatrix(rows):
	return [list(map(int, input().split())) for _ in range(rows)]

def display_matrix(mat):
	for i in range(len(mat)):
		print(mat[i])

def factor(n):
	d = collections.defaultdict(int)
	while not n % 2:
		d[2] += 1
		n //= 2
	i = 3
	while i * i <= n:
		while not n % i:
			d[i] += 1
			n //= i
		i += 2
	if n > 1:
		d[n] += 1
	return d

@repeat(times=getint())
def main():
	n = getint()
	(m, k) = (3, 2)
	arr = l_unpack()
	if n < 3:
		print(0)
		return
	cnt = 0
	hleft = collections.defaultdict(int)
	hright = collections.Counter(arr)
	hleft[arr[0]] += 1
	hright[arr[0]] -= 1
	for i in range(1, n - 1):
		x = arr[i]
		hright[x] -= 1
		left_arr = [hleft[x - i] for i in range(-k, k + 1)]
		right_arr = [hright[x - i] for i in range(-k, k + 1)]
		right_arr += [-x for x in right_arr]
		for i in range(1, len(right_arr)):
			right_arr[i] += right_arr[i - 1]
		for (i, val) in enumerate(left_arr):
			cnt += val * right_arr[k + i]
		hleft[x] += 1
	print(cnt)
main()


T = int(input())
a = [t * (t + 1) // 2 for t in range(200001)]
s = ''
for i in range(T):
	n = int(input())
	lis = sorted(map(int, input().split()))
	(ans, k, to, l) = (0, 1, -1, len(lis))
	for j in range(l - 2):
		to = lis[j] + 2
		while k < l and lis[k] <= to:
			k += 1
		else:
			t = k - j - 2
		if t < 1:
			continue
		ans += a[t]
	s += f'{ans}\n'
print(s, end='')


import sys
from math import *
import collections
import bisect

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def get_list():
	return list(map(int, sys.stdin.readline().strip().split()))

def get_string():
	return sys.stdin.readline().strip()
for t in range(int(input())):
	n = int(input())
	arr = get_list()
	if n <= 2:
		print(0)
		continue
	counter = collections.Counter(arr)
	ans = 0
	for i in range(1, n + 1):
		if counter[i] >= 3:
			ans += counter[i] * (counter[i] - 1) * (counter[i] - 2) // 6
		if counter[i] >= 2 and counter[i + 1] >= 1:
			ans += counter[i] * (counter[i] - 1) * counter[i + 1] // 2
		if counter[i] >= 2 and counter[i + 2] >= 1:
			ans += counter[i] * (counter[i] - 1) * counter[i + 2] // 2
		if counter[i] >= 1 and counter[i + 1] >= 2:
			ans += counter[i + 1] * (counter[i + 1] - 1) * counter[i] // 2
		if counter[i] >= 1 and counter[i + 2] >= 2:
			ans += counter[i + 2] * (counter[i + 2] - 1) * counter[i] // 2
		if counter[i] >= 1 and counter[i + 1] >= 1 and (counter[i + 2] >= 1):
			ans += counter[i] * counter[i + 1] * counter[i + 2]
	print(ans)


import sys

def ncr(ret):
	return ret * (ret - 1) // 2
for _ in range(int(sys.stdin.readline())):
	n = int(sys.stdin.readline())
	a = list(map(int, sys.stdin.readline().split()))
	a.sort()
	a.append(200009)
	d = {}
	b = list(set(a))
	b.sort()
	for i in range(n):
		if a[i] not in d:
			d[a[i]] = [i, 0]
		if a[i + 1] != a[i]:
			d[a[i]][1] = i
	i = 0
	j = 0
	ans = 0
	while i < n - 2:
		x = a[i] + 2
		if x in d:
			ret = d[x][1] - i
			ans += ncr(ret)
		else:
			if j + 1 < n:
				while b[j + 1] - a[i] <= 2:
					j += 1
			ret = d[b[j]][1] - i
			if ret >= 2:
				ans += ncr(ret)
		i += 1
	sys.stdout.write(str(ans) + '\n')


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	ar = list(map(int, input().split()))
	dic = {}
	for i in range(1, n + 1):
		dic[i] = 0
	for i in ar:
		dic[i] += 1
	ans = 0
	for i in range(1, n + 1):
		coun = dic[i]
		if coun >= 3:
			ans += coun * (coun - 1) * (coun - 2) // 6
	for i in range(2, n):
		ans += dic[i - 1] * dic[i] * dic[i + 1]
	for i in range(1, n):
		coun1 = dic[i]
		coun2 = dic[i + 1]
		ans += coun1 * (coun2 * (coun2 - 1) // 2)
		ans += coun2 * (coun1 * (coun1 - 1) // 2)
	for i in range(1, n - 1):
		coun1 = dic[i]
		coun2 = dic[i + 2]
		ans += coun1 * (coun2 * (coun2 - 1) // 2)
		ans += coun2 * (coun1 * (coun1 - 1) // 2)
	print(ans)


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	a = sorted(a)
	ans = 0
	(i, j) = (0, 2)
	while j < n:
		if a[j] - a[i] <= 2:
			ans += (j - i - 1) * (j - i) // 2
			j += 1
		else:
			i += 1
		if j - i == 1:
			j += 1
	print(ans)


import math
import string
import random
import sys
from random import randrange
from collections import deque
from collections import defaultdict
from bisect import bisect, bisect_right, bisect_left

def data():
	return sys.stdin.readline().strip()

def mdata():
	return list(map(int, data().split()))

def outl(var):
	sys.stdout.write(' '.join(map(str, var)) + '\n')

def out(var):
	sys.stdout.write(str(var) + '\n')

def getFac(n):
	if n < 2:
		return 0
	return (n - 1) * n // 2
for _ in range(int(input())):
	n = int(data())
	arr = sorted(mdata())
	ans = 0
	for i in range(n):
		pos = bisect_right(arr, arr[i] + 2) - 1
		ans += getFac(pos - i)
	out(ans)


import os
from io import BytesIO, IOBase
import sys
from collections import defaultdict, deque, Counter
from math import sqrt, pi, ceil, log, inf, gcd, floor
from itertools import combinations, permutations
from bisect import *
from fractions import Fraction
from heapq import *

def main():
	for _ in range(int(input())):
		(n, k) = (int(input()), 2)
		a = sorted(map(int, input().split()))
		(i, j) = (0, 2)
		ans = 0
		while j < n and i < n:
			while j < n and a[j] - a[i] > k:
				i += 1
			if j - i + 1 >= 2 and a[j] - a[i] <= k:
				ans += (j - i) * (j - i - 1) // 2
			j += 1
		print(ans)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	k = 2
	m = 3
	check = [0] * (n + 1)
	for i in arr:
		check[i] += 1
	tot = 0
	for i in range(1, n - 1):
		x = check[i]
		y = check[i + 1]
		z = check[i + 2]
		tot += x * y * z + x * (y * (y - 1)) // 2 + x * (z * (z - 1)) // 2 + x * (x - 1) // 2 * (y + z) + x * (x - 1) * (x - 2) // 6
	x = check[n - 1]
	y = check[n]
	tot += x * (y * (y - 1)) // 2 + x * (x - 1) * (x - 2) // 6 + y * (y - 1) * (y - 2) // 6 + x * (x - 1) // 2 * y
	print(tot)


import bisect

def combin(n, k):
	if k > n // 2:
		k = n - k
	x = 1
	y = 1
	i = n - k + 1
	while i <= n:
		x = x * i // y
		y += 1
		i += 1
	return x
for _ in range(int(input())):
	n = int(input())
	a = sorted(list(map(int, input().split())))
	if n < 3:
		print(0)
		continue
	c = 0
	for w in range(1, n):
		i = n - w
		if i < 2:
			break
		b = bisect.bisect_left(a, a[i] - 2)
		p = i - b - 1
		if p >= 1:
			c += p * (p + 1) // 2
	print(c)


import sys, itertools
tc = int(sys.stdin.readline())
for _ in range(tc):
	n = int(sys.stdin.readline())
	arr = list(map(int, sys.stdin.readline().split()))
	val = max(arr)
	cnt = [0] * (val + 1)
	vis = [False] * (val + 1)
	ans = 0
	for i in arr:
		cnt[i] += 1
		vis[i] = True
	for i in range(1, len(cnt)):
		if vis[i]:
			if cnt[i] >= 3:
				ans += cnt[i] * (cnt[i] - 1) * (cnt[i] - 2) // 6
			if cnt[i] >= 2:
				if i + 2 <= val and vis[i + 2]:
					ans += cnt[i] * (cnt[i] - 1) // 2 * cnt[i + 2]
				if i + 1 <= val and vis[i + 1]:
					ans += cnt[i] * (cnt[i] - 1) // 2 * cnt[i + 1]
			if cnt[i] >= 1:
				if i + 1 <= val and i + 2 <= val and vis[i + 2] and vis[i + 1]:
					ans += cnt[i] * cnt[i + 1] * cnt[i + 2]
				if i + 1 <= val and vis[i + 1]:
					ans += cnt[i] * (cnt[i + 1] * (cnt[i + 1] - 1) // 2)
				if i + 2 <= val and vis[i + 2]:
					ans += cnt[i] * (cnt[i + 2] * (cnt[i + 2] - 1) // 2)
	print(ans)


import operator
import sys
from collections import Counter
from functools import partial
from itertools import accumulate
from math import factorial
SAMPLE_IN = 'sample.in'
SAMPLE_ANS = 'sample.ans'

def read_int(f) -> int:
	return int(f.readline())

def read_ints(f) -> list:
	return [int(x) for x in f.readline().split()]

def read_case(f=sys.stdin) -> tuple:
	read_int(f)
	nums = read_ints(f)
	return nums

def binom2(n):
	return n * (n - 1) // 2

def binom3(n):
	return n * (n - 1) * (n - 2) // 6

def solve(nums):
	n = len(nums)
	c = Counter(nums)
	return sum((c[i] * (c[i + 1] * c[i + 2] + binom2(c[i + 1]) + binom2(c[i + 2])) + binom2(c[i]) * (c[i + 1] + c[i + 2]) + binom3(c[i]) for i in range(1, n + 1) if i in c))

def make_submission(fin=sys.stdin, fout=sys.stdout):
	output = partial(print, file=fout)
	n_cases = int(fin.readline())
	for t in range(1, n_cases + 1):
		case = read_case(fin)
		answer = solve(case)
		output(answer)
import os
if os.getenv('GCJ_LOCAL'):
	with open(SAMPLE_IN) as s:
		make_submission(s)
else:
	make_submission()


import sys
import bisect, string, math, time, functools, random, fractions
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
from itertools import permutations, combinations, groupby
rep = range
R = range

def Golf():
	(n, *t) = map(int, open(0).read().split())

def I():
	return int(input())

def S_():
	return input()

def IS():
	return input().split()

def LS():
	return [i for i in input().split()]

def MI():
	return map(int, input().split())

def LI():
	return [int(i) for i in input().split()]

def LI_():
	return [int(i) - 1 for i in input().split()]

def NI(n):
	return [int(input()) for i in range(n)]

def NI_(n):
	return [int(input()) - 1 for i in range(n)]

def NLI(n):
	return [[int(i) for i in input().split()] for i in range(n)]

def NLI_(n):
	return [[int(i) - 1 for i in input().split()] for i in range(n)]

def StoLI():
	return [ord(i) - 97 for i in input()]

def ItoS(n):
	return chr(n + 97)

def LtoS(ls):
	return ''.join([chr(i + 97) for i in ls])

def RA():
	return map(int, open(0).read().split())

def RLI(n=8, a=1, b=10):
	return [random.randint(a, b) for i in range(n)]

def RI(a=1, b=10):
	return random.randint(a, b)

def INP():
	N = 9
	n = random.randint(1, N)
	m = random.randint(1, n * n)
	A = [random.randint(1, n) for i in range(m)]
	B = [random.randint(1, n) for i in range(m)]
	G = [[] for i in range(n)]
	RG = [[] for i in range(n)]
	for i in range(m):
		(a, b) = (A[i] - 1, B[i] - 1)
		if a == b:
			continue
		G[a] += ((b, 1),)
		RG[b] += ((a, 1),)
	return (n, m, G, RG)

def Rtest(T):
	(case, err) = (0, 0)
	for i in range(T):
		inp = INP()
		a1 = naive(*inp)
		a2 = solve(*inp)
		if a1 == a2:
			print(inp)
			print('naive', a1)
			print('solve', a2)
			err += 1
		case += 1
	print('Tested', case, 'case with', err, 'errors')

def GI(V, E, ls=None, Directed=False, index=1):
	org_inp = []
	g = [[] for i in range(V)]
	FromStdin = True if ls == None else False
	for i in range(E):
		if FromStdin:
			inp = LI()
			org_inp.append(inp)
		else:
			inp = ls[i]
		if len(inp) == 2:
			(a, b) = inp
			c = 1
		else:
			(a, b, c) = inp
		if index == 1:
			a -= 1
			b -= 1
		aa = (a, c)
		bb = (b, c)
		g[a].append(bb)
		if not Directed:
			g[b].append(aa)
	return (g, org_inp)

def GGI(h, w, search=None, replacement_of_found='.', mp_def={'#': 1, '.': 0}, boundary=1):
	mp = [boundary] * (w + 2)
	found = {}
	for i in R(h):
		s = input()
		for char in search:
			if char in s:
				found[char] = (i + 1) * (w + 2) + s.index(char) + 1
				mp_def[char] = mp_def[replacement_of_found]
		mp += [boundary] + [mp_def[j] for j in s] + [boundary]
	mp += [boundary] * (w + 2)
	return (h + 2, w + 2, mp, found)

def TI(n):
	return GI(n, n - 1)

def accum(ls):
	rt = [0]
	for i in ls:
		rt += [rt[-1] + i]
	return rt

def bit_combination(n, base=2):
	rt = []
	for tb in R(base ** n):
		s = [tb // base ** bt % base for bt in R(n)]
		rt += [s]
	return rt

def gcd(x, y):
	if y == 0:
		return x
	if x % y == 0:
		return y
	while x % y != 0:
		(x, y) = (y, x % y)
	return y

def YN(x):
	print(['NO', 'YES'][x])

def Yn(x):
	print(['No', 'Yes'][x])

def show(*inp, end='\n'):
	if show_flg:
		print(*inp, end=end)
mo = 10 ** 9 + 7
inf = float('inf')
FourNb = [(-1, 0), (1, 0), (0, 1), (0, -1)]
EightNb = [(-1, 0), (1, 0), (0, 1), (0, -1), (1, 1), (-1, -1), (1, -1), (-1, 1)]
compas = dict(zip('WENS', FourNb))
cursol = dict(zip('LRUD', FourNb))
l_alp = string.ascii_lowercase
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
input = lambda : sys.stdin.readline().rstrip()
show_flg = False
show_flg = True

class Comb:

	def __init__(self, n):
		return

	def fact(self, n):
		return self.fac[n]

	def invf(self, n):
		return self.inv[n]

	def comb(self, x, y):
		if y < 0 or y > x:
			return 0
		return x * (x - 1) // 2

class Bit:

	def __init__(self, n, init=None):
		self.size = n
		self.m = len(bin(self.size)) - 2
		self.arr = [0] * (2 ** self.m + 1)
		self.el = [0] * (2 ** self.m + 1)
		if init != None:
			for i in range(len(init)):
				self.add(i, init[i])
				self.el[i] = init[i]

	def __str__(self):
		a = [self.sum(i + 1) - self.sum(i) for i in range(self.size)]
		return str(a)

	def add(self, i, x):
		if not 0 < i <= self.size:
			return NotImplemented
		self.el[i] += x
		while i <= self.size:
			self.arr[i] += x
			i += i & -i
		return

	def sum(self, i):
		if not 0 <= i <= self.size:
			return NotImplemented
		rt = 0
		while i > 0:
			rt += self.arr[i]
			i -= i & -i
		return rt

	def __getitem__(self, key):
		return self.el[key]

	def __setitem__(self, key, value):
		self.add(key, value - self.sum(key + 1) + self.sum(key))

	def l_bound(self, w):
		if w <= 0:
			return 0
		x = 0
		k = 2 ** self.m
		while k > 0:
			if x + k <= self.size and self.arr[x + k] < w:
				w -= self.arr[x + k]
				x += k
			k >>= 1
		return x + 1

	def u_bound(self, w):
		if w <= 0:
			return 0
		x = 0
		k = 2 ** self.m
		while k > 0:
			if x + k <= self.size and self.arr[x + k] <= w:
				w -= self.arr[x + k]
				x += k
			k >>= 1
		return x + 1

class Bit0(Bit):

	def add(self, j, x):
		super().add(j + 1, x)

	def l_bound(self, w):
		return max(super().l_bound(w) - 1, 0)

	def u_bound(self, w):
		return max(super().u_bound(w) - 1, 0)

class Multiset(Bit0):

	def __init__(self, max_v):
		super().__init__(max_v)

	def insert(self, x):
		super().add(x, 1)

	def find(self, x):
		return super().l_bound(super().sum(x))

	def __str__(self):
		return str(self.arr)

def compress(L):
	dc = {v: i for (i, v) in enumerate(sorted(set(L)))}
	return [dc[i] for i in L]
ans = 0
cm = Comb(3 * 10 ** 5)
for _ in range(I()):
	n = I()
	k = 2
	m = 3
	a = sorted(LI())
	bt = Bit0(n + 2)
	ans = 0
	for i in range(n):
		c = a[i]
		x = bt.sum(min(c + k + 1, n + 1)) - bt.sum(c)
		y = bt.sum(min(c + 1, n + 1)) - bt.sum(max(c - k, 0))
		z = bt.sum(min(c + 1, n + 1)) - bt.sum(max(c, 0))
		ans += cm.comb(x, 2) + cm.comb(y, 2) - cm.comb(z, 2)
		bt.add(c, 1)
	print(ans)


def main():
	for _ in ' ' * int(input()):
		n = int(input())
		a = [int(x) for x in input().split()]
		a.sort()
		if n < 3:
			print(0)
			continue
		(ans, r) = (0, 2)
		for l in range(n - 2):
			if r == l + 1:
				r += 1
			while r < n and a[r] - a[l] <= 2:
				le = r - l - 1
				ans += (1 + le) * le // 2
				r += 1
		print(ans)
main()


import math
from math import gcd, sqrt, floor, factorial, ceil
from bisect import bisect_left, bisect_right
import bisect
import sys
from sys import stdin, stdout
import os
sys.setrecursionlimit(pow(10, 7))
import collections
from collections import defaultdict, Counter
from statistics import median
input = stdin.readline
print = stdout.write
from queue import Queue
inf = float('inf')

def numbers_in_between(a, b):
	return b - a - 1

def nC2(a):
	if a <= 0:
		return 0
	return a * (a - 1) // 2
t = int(input())
for i in range(t):
	n = int(input())
	if n <= 2:
		input()
		print('0' + '\n')
		continue
	l = list(map(int, input().split()))
	l.sort()
	it1 = 0
	count = 0
	while it1 < n:
		number = l[it1]
		index = bisect_left(l, number + 3)
		index -= 1
		count += nC2(index - it1)
		it1 += 1
	print(str(count) + '\n')


import sys
for _ in range(int(input())):
	n = int(sys.stdin.readline())
	nums = list(map(int, sys.stdin.readline().split()))
	nums.sort()
	answer = 0
	idx = 0
	before = 0
	for i in range(n - 2):
		if nums[i] == before:
			if idx - i >= 2:
				answer += (idx - i) * (idx - i - 1) // 2
			continue
		else:
			before = nums[i]
			idx = i
		for j in range(i + 1, n):
			if nums[j] - nums[i] > 2:
				idx = j - 1
				break
		else:
			idx = j
		if idx - i >= 2:
			answer += (idx - i) * (idx - i - 1) // 2
	print(answer)


t = int(input())
for i in range(t):
	n = int(input())
	a = sorted(list(map(int, input().split())))
	high = 2
	low = 0
	summa = 0
	while high < n:
		if a[high] - a[low] <= 2:
			summa += (high - low - 1) * (high - low) // 2
			high += 1
		else:
			low += 1
		if high - low == 1:
			high += 1
	print(summa)


ans = []
import bisect
x_test = 1
if x_test == 0:
	x_test = 1
else:
	x_test = int(input())

def _main():

	def f(x):
		if x < 1:
			return 0
		return x * (x + 1) // 2
	for _ in range(x_test):
		n = int(input())
		a = list(map(int, input().split()))
		if n < 3:
			ans.append(0)
		else:
			a.sort()
			result = 0
			for i in range(2, n):
				left_value = a[i] - 2
				j = bisect.bisect_left(a, left_value)
				x = i - j - 1
				result += f(x)
			ans.append(result)
if x_test == 200000:
	ans = [0] * 200000
else:
	_main()
ans = str(ans)
lls = len(ans)
_ans = ans[1:lls - 1].replace(', ', '\n')
print(_ans)


import bisect
import sys
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	arr.sort()
	ans = 0
	for i in range(n):
		y = arr[i] + 2
		id = bisect.bisect(arr, y)
		ans += max((id - i - 1) * (id - i - 2) // 2, 0)
	print(ans)


from bisect import bisect
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	ans = 0
	for i in range(n):
		b = bisect(a, a[i] + 2)
		ans += (b - i - 1) * (b - i - 2) // 2
	print(ans)


import sys
input = sys.stdin.readline

def solution(n, arr):
	left = 0
	right = 0
	sol = 0
	diff = 2
	arr.sort()

	def k_C_2(num):
		return num * (num - 1) // 2
	while left < n - 2:
		while right <= n - 1 and arr[left] + diff >= arr[right]:
			right += 1
		sol += k_C_2(right - left - 1)
		left += 1
	print(sol)
T = int(input())
for t in range(T):
	n = int(input())
	arr = list(map(int, input().split()))
	solution(n, arr)


t = int(input())
rs = []
import math

def nCr(n, r):
	ts = 1
	f = math.factorial
	ms = f(r)
	for i in range(n - r + 1, n + 1):
		ts *= i
	return int(ts / ms)
for i in range(t):
	_ = int(input())
	arr = input()
	a = [int(item) for item in arr.split(' ')]
	a = list(sorted(a))
	c = 0
	end = 0
	for i in range(len(a)):
		if a[i] == a[i - 1] and i > 0:
			continue
		j = i
		while True:
			if j < len(a) - 1:
				if a[j + 1] - a[i] <= 2:
					j += 1
				else:
					break
			if j == len(a) - 1:
				break
		x = j - i + 1
		if x >= 3:
			c += nCr(x, 3)
		if end > i:
			x = end - i + 1
			if x >= 3:
				c -= nCr(x, 3)
		end = j
	rs.append(c)
for i in rs:
	print(int(i))


import sys
import io
import math
import collections
import decimal
import itertools
import bisect
import heapq

def input():
	return sys.stdin.readline()[:-1]

def comb(n, k):
	m = 1
	if n < 2 * k:
		k = n - k
	for i in range(1, k + 1):
		m = m * (n - i + 1) // i
	return m

def solve(N, M, K, A):
	L = []
	n_max = 0
	for i in range(N - M + 1):
		j = bisect.bisect_right(A, A[i] + K)
		if j - i < M:
			continue
		n = j - i - 1
		r = M - 1
		L.append((n, r))
		n_max = max(n_max, n)
	v = 0
	for (n, r) in L:
		r = min(r, n - r)
		v += comb(n, r)
	return v

def main():
	T0 = int(input())
	for _ in range(T0):
		N = int(input())
		A = sorted(list(map(int, input().split())))
		ans = solve(N, 3, 2, A)
		print(ans)
main()


from sys import stdin

def read_ints():
	return list(map(int, stdin.readline().split()))
skoka = int(input())
last = [0] * skoka
for _ in range(skoka):
	n = int(input())
	arr = read_ints()
	if n <= 2:
		last[_] = 0
		continue
	am = [0] * n
	for i in range(n):
		am[arr[i] - 1] += 1
	ans = 0
	if am[0] >= 3:
		ans += am[0] * (am[0] - 1) * (am[0] - 2) // 6
	for i in range(1, n):
		nowBLYAT = am[i]
		if i == 1:
			if nowBLYAT >= 1:
				s = am[i - 1]
				ans += nowBLYAT * s * (s - 1) // 2
				if nowBLYAT >= 2:
					ans += nowBLYAT * (nowBLYAT - 1) // 2 * am[i - 1]
					if nowBLYAT >= 3:
						ans += nowBLYAT * (nowBLYAT - 1) * (nowBLYAT - 2) // 6
		elif nowBLYAT >= 1:
			s = am[i - 1] + am[i - 2]
			ans += nowBLYAT * s * (s - 1) // 2
			if nowBLYAT >= 2:
				ans += nowBLYAT * (nowBLYAT - 1) // 2 * (am[i - 1] + am[i - 2])
				if nowBLYAT >= 3:
					ans += nowBLYAT * (nowBLYAT - 1) * (nowBLYAT - 2) // 6
	last[_] = ans
print(*last, sep='\n')


p = ''
for _ in range(int(input())):
	n = int(input())
	a = sorted(map(int, input().split()))
	x = 0
	ans = 0
	for i in range(n):
		while a[x] + 2 < a[i]:
			x += 1
		o = i - x
		ans += o * (o - 1) // 2
	p += str(ans) + '\n'
print(p)


t = int(input())
lazyfac = [1, 1]

def upfac(lazyfac, n):
	while len(lazyfac) < n + 1:
		lazyfac.append(lazyfac[-1] * len(lazyfac))

def fac2(n):
	if n == 0:
		return 1
	else:
		return n * fac2(n - 1)

def choose(n, m):
	d1 = n - m
	d2 = m
	p1 = max(d1, d2)
	p2 = min(d1, d2)
	num = 1
	for i in range(p1 + 1, n + 1):
		num *= i
	return int(num / fac2(p2))
for test in range(t):
	n = int(input())
	m = 3
	k = 2
	a = [int(i) for i in input().split()]
	a.sort()
	ans = 0
	j = 0
	if m == 0:
		ans = n
	else:
		for i in range(n - m + 1):
			if j < n:
				while a[j] <= a[i] + k:
					j += 1
					if j == n:
						break
			if j >= i + m:
				ans += choose(j - i - 1, m - 1)
				ans = ans
	print(ans)


import sys
import math
from collections import defaultdict, deque
import bisect
input = sys.stdin.readline

def inar():
	return [int(el) for el in input().split()]

def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		arr = inar()
		arr.sort()
		ans = 0
		for i in range(n):
			el = arr[i] + 2
			ind = bisect.bisect_right(arr, el)
			if ind - i - 1 >= 2:
				num = ind - i - 1
				ans += num * (num - 1) // 2
		print(ans)
main()


import sys, os
from io import BytesIO, IOBase
import collections, itertools, bisect, heapq, math, string
from decimal import *
BUFSIZE = 8192
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def main():
	t = int(input())
	for i in range(t):
		n = int(input())
		a = list(map(int, input().split()))
		a.sort()
		(m, k) = (3, 2)
		ans = 0
		for j in range(n - m + 1):
			if j != 0 and a[j] == a[j - 1]:
				if pos != -1:
					cnt = pos - j - 1
					ans += cnt * (cnt - 1) // 2
				continue
			pos = -1
			for x in range(k + 1):
				index = bisect.bisect_right(a, a[j] + x)
				if index - j >= m and a[index - 1] == a[j] + x:
					pos = max(pos, index)
			if pos != -1:
				cnt = pos - j - 1
				ans += cnt * (cnt - 1) // 2
		print(ans)
main()


import sys, math, itertools
from collections import Counter, deque, defaultdict
from bisect import bisect_left, bisect_right
from heapq import heappop, heappush, heapify
mod = 10 ** 9 + 7
INF = float('inf')

def inp():
	return int(sys.stdin.readline())

def inpl():
	return list(map(int, sys.stdin.readline().split()))

def comb(n, r=3):
	return n * (n - 1) * (n - 2) // (r * (r - 1) * (r - 2))
for _ in range(inp()):
	n = inp()
	a = inpl()
	b = [0 for _ in range(n)]
	for x in a:
		b[x - 1] += 1
	res = 0
	pre = False
	for i in range(2, n):
		x = b[i]
		su = 0
		for j in range(i - 2, i + 1):
			su += b[j]
		if su < 3:
			pre = False
			continue
		res += comb(su, 3)
		if pre and su - x >= 3:
			res -= comb(su - x, 3)
		pre = True
	print(res)


import io, os, time, sys
from sys import stdin

def print(s):
	sys.stdout.write(str(s) + '\n')

def ncr(n):
	return n * (n - 1) * (n - 2) // 6
for _ in range(int(stdin.readline())):
	n = int(stdin.readline())
	l = list(map(int, stdin.readline().split()))
	ma = max(l)
	hash = [0] * (n + 2)
	for i in l:
		hash[i] += 1
	res = ncr(hash[1])
	for i in range(2, n + 1):
		res += ncr(hash[i] + hash[i - 1] + hash[i - 2]) - ncr(hash[i - 1] + hash[i - 2])
	print(res)


T = int(input())
for t in range(T):
	n = int(input())
	x = list(input().split())
	for i in range(n):
		x[i] = int(x[i])
	if n < 3:
		print(0)
		continue
	x.sort()
	finalsum = 0
	j = 2
	for i in range(n - 2):
		if i == 0:
			j = i + 2
			while j < n:
				if x[j] - x[i] > 2:
					j -= 1
					break
				j += 1
			if j == n:
				j -= 1
			c = j - i
			if c > 1:
				finalsum += (c - 1) * c // 2
		else:
			while j < n:
				if x[j] - x[i] > 2:
					j -= 1
					break
				j += 1
			if j == n:
				j -= 1
			c = j - i
			if c > 1:
				finalsum += (c - 1) * c // 2
	print(finalsum)


import sys, collections, heapq, math
readline = sys.stdin.readline

def solve(A):
	counter = collections.defaultdict(int)
	for (i, val) in enumerate(A):
		counter[val] += 1
	sorted_keys = sorted(counter.keys())
	ans = 0
	for i in range(len(sorted_keys)):
		original = counter[sorted_keys[i]]
		choices = 0
		j = i + 1
		while j < len(sorted_keys) and sorted_keys[j] - sorted_keys[i] <= 2:
			choices += counter[sorted_keys[j]]
			j += 1
		if original + choices >= 3:
			for val in range(1, min(original, 2) + 1):
				ans += math.comb(original, val) * math.comb(choices, 3 - val)
			if original >= 3:
				ans += math.comb(original, 3)
	return ans
t = int(readline())
for _ in range(t):
	n = int(readline())
	print(solve(list(map(int, readline().split()))))


import math
from heapq import heappush, heappop, heapify
import string
from collections import deque
from bisect import bisect, bisect_left, bisect_right, insort
import sys
input = sys.stdin.readline
S = lambda : input().rstrip()
I = lambda : int(S())
M = lambda : map(int, S().split())
L = lambda : list(M())
H = 1000000000 + 7
for _ in range(I()):
	n = I()
	l = L()
	a = [0] * (n + 2)
	for i in l:
		a[i - 1] += 1
	b = a[:-2]
	for i in range(1, n + 2):
		a[i] += a[i - 1]
	s = 0
	for i in range(n):
		if b[i] > 0:
			q = a[i + 2] - a[i]
			s += b[i] * math.comb(q, 2) + math.comb(b[i], 2) * q + math.comb(b[i], 3)
	print(s)


import sys, bisect
input = sys.stdin.readline
mod = 1
m = 3
k = 2
L = []
mv = 2 * 10 ** 5 + 1
for i in range(2, mv + 1):
	val = i * (i - 1) // 2
	L.append(val)
T = int(input())
for _ in range(T):
	n = int(input())
	A = list(map(int, input().split()))
	A.sort()
	ans = 0
	for i in range(n):
		mv = A[i]
		ind = bisect.bisect_right(A, mv + k)
		v = ind - i - m
		if v >= 0:
			ans = ans + L[v]
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	if n < 3:
		print(0)
		continue
	a.sort()
	(p1, p2, tot) = (0, 2, 0)
	while p1 < n - 2:
		while p2 < n and a[p2] - a[p1] <= 2:
			p2 += 1
		dif = p2 - p1 - 1
		tot += dif * (dif - 1) // 2
		p1 += 1
	print(tot)


from __future__ import division, print_function
from fractions import Fraction
import sys
import os
from io import BytesIO, IOBase
from functools import cmp_to_key
from heapq import *
from math import gcd, factorial, floor, ceil, sqrt
from copy import deepcopy
from collections import deque
from bisect import bisect_left as bl
from bisect import bisect_right as br
from bisect import bisect
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
from types import GeneratorType

def iterative(f, stack=[]):

	def wrapped_func(*args, **kwargs):
		if stack:
			return f(*args, **kwargs)
		to = f(*args, **kwargs)
		while True:
			if type(to) is GeneratorType:
				stack.append(to)
				to = next(to)
				continue
			stack.pop()
			if not stack:
				break
			to = stack[-1].send(to)
		return to
	return wrapped_func

def inp():
	return sys.stdin.readline().rstrip('\r\n')

def out(var):
	sys.stdout.write(str(var))

def lis():
	return list(map(int, inp().split()))

def stringlis():
	return list(map(str, inp().split()))

def sep():
	return map(int, inp().split())

def strsep():
	return map(str, inp().split())

def testcase(t):
	for pp in range(t):
		solve(pp)

def google(p):
	print('Case #' + str(p) + ': ', end='')

def lcm(a, b):
	return a * b // gcd(a, b)

def power(x, y, p):
	y %= p - 1
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def ncr(n, r):
	return factorial(n) // (factorial(r) * factorial(max(n - r, 1)))

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True
inf = pow(10, 20)
mod = 10 ** 9 + 7

def do(n):
	return n * (n - 1) * (n - 2) // 6

def solve(case):
	n = int(inp())
	a = lis()
	mx = max(a) + 10
	cnt = [0] * mx
	for i in range(n):
		cnt[a[i]] += 1
	ans = do(cnt[1] + cnt[2] + cnt[3])
	for i in range(4, mx):
		ans += do(cnt[i] + cnt[i - 1] + cnt[i - 2])
		ans -= do(cnt[i - 1] + cnt[i - 2])
	print(ans)
testcase(int(inp()))


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	l.sort()
	ans = 0
	i = 0
	j = 2
	while j < n:
		if l[j] - l[i] <= 2:
			r = j - i - 1
			ans += r * (r + 1) // 2
			j += 1
		else:
			i += 1
		if j - i == 1:
			j += 1
	print(ans)


for _ in range(int(input())):
	n = int(input())
	l = [*map(int, input().split())]
	l.sort()
	a = b = res = 0
	while a < n - 2:
		if b < n and l[b] - l[a] <= 2:
			b += 1
			continue
		else:
			if b - a > 2:
				res += (b - a - 2) * (b - a - 1) // 2
			a += 1
	print(res)


from collections import Counter as C
from operator import itemgetter
from math import ceil, sqrt, gcd, factorial, comb
from collections import deque
from bisect import bisect_right as br
from collections import defaultdict as dd
d = dd(lambda : 0)
from sys import stdin

def get_ints():
	return list(map(int, stdin.readline().strip().split()))
t = int(stdin.readline())
for _ in range(t):
	n = int(stdin.readline())
	l = sorted(get_ints())
	ans = 0
	for i in range(n):
		x = br(l, l[i] + 2)
		a = x - i - 1
		ans += a * (a - 1) // 2
	print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	a = [int(i) for i in input().split(' ')]
	a.sort()
	count = 0
	j = 0
	for i in range(n - 2):
		while j + 1 < n:
			if a[j + 1] - a[i] <= 2:
				j += 1
			else:
				break
		if j - i >= 2:
			count += (j - i) * (j - i - 1) // 2
	print(count)


import bisect
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	arr = sorted(arr)
	j = 1
	ans = 0
	for i in range(n - 2):
		while j < n and arr[j] - arr[i] <= 2:
			j += 1
		k1 = max(0, j - i - 1)
		ans += k1 * (k1 - 1) // 2
	print(ans)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def inp():
	return sys.stdin.readline().rstrip()

def mpint():
	return map(int, inp().split(' '))

def itg():
	return int(inp())

def comb3(n):
	return n * (n - 1) * (n - 2) // 6

def comb2(n):
	return n * (n - 1) >> 1
from collections import Counter

def solve():
	n = itg()
	counter = Counter(mpint())
	ans = 0
	for a in range(n + 5):
		b = counter[a + 1]
		c = counter[a + 2]
		a = counter[a]
		ans += comb3(a)
		ans += a * comb2(b)
		ans += comb2(a) * b
		ans += a * comb2(c)
		ans += comb2(a) * c
		ans += a * b * c
	return ans

def main():
	for _ in range(itg()):
		print(solve())
DEBUG = 0
URL = 'https://codeforces.com/contest/1462/problem/E1'
if DEBUG == 1:
	import requests
	from ACgenerator.Y_Test_Case_Runner import TestCaseRunner
	runner = TestCaseRunner(main, URL)
	inp = runner.input_stream
	print = runner.output_stream
	runner.checking()
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
	if DEBUG == 2:
		_print = print

		def print(*args, **kwargs):
			_print(*args, **kwargs)
			sys.stdout.flush()
	main()


import os
import sys
from io import BytesIO, IOBase
import math
from collections import defaultdict, deque
from itertools import combinations
import random
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split(' ')))
	l.sort()
	j = 0
	ans = 0
	for i in range(n):
		while j < n and l[j] - l[i] <= 2:
			j += 1
		ans += max(0, (j - i - 1) * (j - i - 2) // 2)
	print(ans)


import math
import string
import random
import sys
from random import randrange
from collections import deque
from collections import defaultdict
from bisect import bisect, bisect_right, bisect_left

def data():
	return sys.stdin.readline().strip()

def mdata():
	return list(map(int, data().split()))

def outl(var):
	sys.stdout.write(' '.join(map(str, var)) + '\n')

def out(var):
	sys.stdout.write(str(var) + '\n')

def fac(x):
	if x < 2:
		return 0
	return (x - 1) * x // 2
for t in range(int(data())):
	n = int(data())
	arr = sorted(mdata())
	ans = 0
	for i in range(n):
		b = bisect_right(arr, arr[i] + 2) - 1
		ans += fac(b - i)
	out(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	if a == [1]:
		print(0)
		continue
	count = [0 for i in range(n + 4)]
	st = set()
	for ele in a:
		count[ele] += 1
		st.add(ele)
	res = 0
	for x in st:
		ele = x
		res += count[ele] * (count[ele] - 1) * (count[ele] - 2) // 6
		res += count[ele] * (count[ele] - 1) // 2 * count[ele + 1]
		res += count[ele] * (count[ele] - 1) // 2 * count[ele + 2]
		res += count[x] * (count[x + 1] * (count[x + 1] - 1) // 2)
		res += count[x] * (count[x + 2] * (count[x + 2] - 1) // 2)
		res += count[x] * count[x + 1] * count[x + 2]
	print(res)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	size = len(a)
	a = sorted(a)
	(i, j) = (0, 2)
	count = 0
	while j < size:
		while j < size and a[j] - a[i] <= 2:
			count += (j - i - 1) * (j - i) // 2
			j += 1
		i += 1
	print(count)


import sys

def input():
	return sys.stdin.readline().strip()

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def list4d(a, b, c, d, e):
	return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]

def ceil(x, y=1):
	return int(-(-x // y))

def INT():
	return int(input())

def MAP():
	return map(int, input().split())

def LIST(N=None):
	return list(MAP()) if N is None else [INT() for i in range(N)]

def Yes():
	print('Yes')

def No():
	print('No')

def YES():
	print('YES')

def NO():
	print('NO')
INF = 10 ** 19
MOD = 10 ** 9 + 7

def nC2(n):
	return n * (n - 1) // 2

def nC3(n):
	return n * (n - 1) * (n - 2) // 6
for _ in range(INT()):
	N = INT()
	A = LIST()
	C = [0] * (N + 7)
	for a in A:
		C[a] += 1
	cnt = 0
	for i in range(1, N + 1):
		cnt += nC3(C[i])
		cnt += nC2(C[i]) * C[i + 1] + C[i] * nC2(C[i + 1])
		cnt += nC2(C[i]) * C[i + 2] + C[i] * nC2(C[i + 2])
		cnt += C[i] * C[i + 1] * C[i + 2]
	print(cnt)


from __future__ import division, print_function
import os
import sys
from io import BytesIO, IOBase
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip

def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		a = [int(x) for x in input().split()]
		a.sort()
		result = 0
		(l, r) = (0, 2)
		while r < n:
			while a[r] - a[l] > 2 and r - l + 1 > 3:
				l += 1
			dist = r - l
			if a[r] - a[l] <= 2 and dist >= 2:
				result += (dist - 1) * dist // 2
			r += 1
		print(result)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


import sys
try:
	(sys.stdin, sys.stdout) = (open('in.txt', 'r'), open('out.txt', 'w'))
except:
	pass
ii1 = lambda : int(sys.stdin.readline().strip())
is1 = lambda : sys.stdin.readline().strip()
iia = lambda : list(map(int, sys.stdin.readline().strip().split()))
isa = lambda : sys.stdin.readline().strip().split()
mod = int(1000000000.0 + 7)
from collections import *
from math import *
for _ in range(ii1()):
	n = ii1()
	arr = iia()
	dp = [0] * (n + 1)
	count = 0
	for i in arr:
		dp[i] += 1
	for i in range(1, n + 1):
		if dp[i] >= 3:
			v = dp[i]
			count += v * (v - 1) * (v - 2) // 6
	for i in range(1, n):
		if dp[i] >= 1 and dp[i + 1] >= 1 and (dp[i] + dp[i + 1] >= 3):
			(v, vv) = (dp[i], dp[i + 1])
			count += v * (vv * (vv - 1) // 2)
			count += vv * (v * (v - 1) // 2)
	for i in range(1, n - 1):
		if dp[i] >= 1 and dp[i + 1] >= 1 and (dp[i + 2] >= 1):
			count += dp[i] * dp[i + 1] * dp[i + 2]
		if dp[i] >= 1 and dp[i + 2] >= 1 and (dp[i] + dp[i + 2] >= 3):
			(v, vv) = (dp[i], dp[i + 2])
			count += v * (vv * (vv - 1) // 2)
			count += vv * (v * (v - 1) // 2)
	print(count)


import sys, os
import math
if os.path.exists('in.txt'):
	sys.stdin = open('in.txt', 'r')
from io import BytesIO, IOBase

def main():
	pass
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def MI():
	return map(int, input().split())

def out(var):
	sys.stdout.write(str(var) + '\n')
from collections import defaultdict
for _ in range(int(input())):
	n = int(input())
	d = defaultdict(lambda : 0)
	arr = list(map(int, input().split()))
	out = 0
	for i in arr:
		c = d[i] + d[i - 1] + d[i - 2]
		if c >= 2:
			out += c * (c - 1) // 2
		c = d[i - 1] + d[i] + d[i + 1]
		if c >= 2:
			out += c * (c - 1) // 2
			if d[i - 1] + d[i] >= 2:
				out -= (d[i - 1] + d[i]) * (d[i - 1] + d[i] - 1) // 2
		c = d[i + 1] + d[i] + d[i + 2]
		if c >= 2:
			out += c * (c - 1) // 2
			if d[i + 1] + d[i] >= 2:
				out -= (d[i + 1] + d[i]) * (d[i + 1] + d[i] - 1) // 2
		d[i] += 1
	print(out)


from math import comb
t = int(input())
for x in range(t):
	n = int(input())
	a = list(map(int, input().split(' ')))
	if n < 3:
		print(0)
	else:
		freq = dict()
		for el in a:
			if el in freq:
				freq[el] += 1
			else:
				freq[el] = 1
		ans = 0
		for i in range(1, n - 1):
			tup = 0
			if i == 1:
				for j in range(1, 4):
					if j in freq:
						tup += freq[j]
				if tup >= 3:
					ans += comb(tup, 3)
			else:
				used = 0
				for j in range(i, i + 3):
					if j in freq:
						tup += freq[j]
					if j != i + 2 and j in freq:
						used += freq[j]
				if tup >= 3:
					ans += comb(tup, 3)
				if used >= 3:
					ans -= comb(used, 3)
		print(ans)


import bisect
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	arr = sorted(arr)
	if n <= 2:
		print(0)
		continue
	ans = 0
	for i in range(n):
		k = bisect.bisect(arr, arr[i] + 2, i + 1, n)
		k1 = max(0, k - i - 1)
		ans += k1 * (k1 - 1) // 2
	print(ans)


R = lambda : map(int, input().split())
for _ in range(int(input())):
	n = int(input())
	arr = sorted(R())
	j = 0
	res = 0
	for i in range(2, n):
		while j < i and arr[i] - arr[j] > 2:
			j += 1
		res += (i - j) * (i - j - 1) // 2
	print(res)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from collections import Counter
import math

def nCk(n, k):
	res = 1
	for i in range(1, k + 1):
		res = res * (n - i + 1) // i
	return res
inf = 10 ** 20

def binary_high(arr, i):
	l = i + 1
	h = n - 1
	ans = -1
	while l <= h:
		mid = (l + h) // 2
		if arr[mid] - arr[i] <= 2:
			ans = mid
			l = mid + 1
		else:
			h = mid - 1
	return ans
for _ in range(int(input())):
	n = int(input())
	arr = sorted(list(map(int, input().split())))
	ans = 0
	for i in range(n - 2):
		cnt = binary_high(arr, i)
		if cnt != -1 and cnt - i >= 2:
			ans += (cnt - i) * (cnt - i - 1) // 2
	print(ans)


import sys
input = sys.stdin.readline
import math
import copy
for _ in range(int(input())):
	n = int(input())
	k = 2
	m = 3
	arr = list(map(int, input().split()))
	if n < m:
		print(0)
		continue
	arr.sort()
	dp = [0] * n
	if arr[2] - arr[0] <= k:
		dp[2] = 1
	index = 0
	for i in range(3, n):
		while arr[i] - arr[index] > k:
			index += 1
		dp[i] = dp[i - 1] + math.comb(i - index, m - 1)
	print(dp[n - 1])


def main():
	t = int(input())
	while t > 0:
		t -= 1
		n = int(input())
		a = list(map(int, input().split()))
		a = sorted(a)
		ans = 0
		left = 0
		right = 0
		while left < len(a) - 2:
			while right < len(a) and a[right] - a[left] <= 2:
				right += 1
			right -= 1
			if right > left + 1:
				ans += (right - left) * (right - left - 1) // 2
			left += 1
		print(ans)
main()


from sys import stdin
for _ in range(int(stdin.readline())):
	a = int(stdin.readline())
	b = sorted(map(int, stdin.readline().split()))
	ans = 0
	j = 0
	for i in range(0, a):
		while j <= i and b[i] - b[j] > 2:
			j += 1
		n = i - j
		ans += n * (n - 1) // 2
	print(ans)


import sys
from collections import Counter
import operator as op
from functools import reduce

def ncr(n, r):
	r = min(r, n - r)
	numer = reduce(op.mul, range(n, n - r, -1), 1)
	denom = reduce(op.mul, range(1, r + 1), 1)
	return numer // denom
t = [int(val) for val in sys.stdin.readline().split()][0]
m = 3
k = 2
for test in range(t):
	n = [int(val) for val in sys.stdin.readline().split()][0]
	a = [int(val) for val in sys.stdin.readline().split()]
	c = Counter(a)
	kv = [(key, val) for (key, val) in c.items()]
	kv.sort(key=lambda x: x[0])
	sind = 0
	count = 0
	while sind < len(kv):
		bind = sind + 1
		while bind < len(kv) and abs(kv[bind][0] - kv[sind][0]) <= k:
			bind += 1
		tots = 0
		for i in range(sind + 1, bind):
			tots += kv[i][1]
		if kv[sind][1] >= m:
			count += ncr(kv[sind][1], m)
		for use in range(1, min(m, kv[sind][1] + 1)):
			if use + tots >= m:
				count += ncr(kv[sind][1], use) * ncr(tots, m - use)
		sind += 1
	print(count)


for _ in ' ' * int(input()):
	n = int(input())
	a = sorted(map(int, input().split()))
	ans = 0
	cnt = 0
	if n < 3:
		print(0)
	else:
		for i in range(2, n):
			while a[i] - a[cnt] > 2:
				cnt += 1
			val = i - cnt
			ans += (val - 1) * val // 2
		print(ans)


import sys
input = sys.stdin.readline

def integer_list(lag=0):
	return [int(x) + lag for x in input().rstrip().split()]

def f(a, el):
	left = 0
	right = len(a) - 1
	while left < right:
		mid = (left + right) // 2
		if a[mid] >= el:
			right = mid
		else:
			left = mid + 1
	if a[left] != el:
		return (-1, -1, 0)
	else:
		LEFT = left
	left = 0
	right = len(a) - 1
	while left < right:
		mid = (left + right + 1) // 2
		if a[mid] <= el:
			left = mid
		else:
			right = mid - 1
	return (LEFT, right, right - LEFT + 1)
t = int(input())
for test in range(t):
	n = int(input())
	a = integer_list()
	a = sorted(a)
	totale = 0
	for (j, el) in enumerate(a):
		(_, _, x) = f(a, el - 2)
		(_, _, y) = f(a, el - 1)
		(l, r, _) = f(a, el)
		(_, _, w) = f(a, el + 1)
		(_, _, p) = f(a, el + 2)
		totale += x * (r - j) + y * (r - j + w) + (j - l) * (r - j + w + p)
	print(totale)


import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction
import copy
import time
starttime = time.time()
mod = int(pow(10, 9) + 7)
mod2 = 998244353

def data():
	return sys.stdin.readline().strip()

def out(*var, end='\n'):
	sys.stdout.write(' '.join(map(str, var)) + end)

def L():
	return list(sp())

def sl():
	return list(ssp())

def sp():
	return map(int, data().split())

def ssp():
	return map(str, data().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [l1d(n, val) for j in range(m)]
try:
	sys.stdin = open('input.txt', 'r')
except:
	pass

def pmat(A):
	for ele in A:
		print(ele, end='\n')
for _ in range(L()[0]):
	n = L()[0]
	k = 2
	m = 3
	A = L()
	A.sort()
	ans = 0
	for i in range(n):
		x = br(A, A[i] + k)
		ans += (x - i - 1) * (x - i - 2) // 2
	print(ans)
endtime = time.time()


import sys
input = sys.stdin.readline
from bisect import bisect_right as br
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	l.sort()
	s = 0
	for i in range(n):
		d = br(l, l[i] + 2)
		c = d - i
		if c >= 3:
			s += (c - 1) * (c - 2) // 2
	print(s)


T = int(input())
for _ in range(T):
	n = int(input())
	s = list(map(int, input().split()))
	if n < 3:
		print(0)
		continue
	s.sort()
	dic = {}
	ans = 0
	for i in s:
		dic[i] = dic.get(i, 0) + 1
		dic[i + 1] = dic.get(i + 1, 0)
		dic[i + 2] = dic.get(i + 2, 0)
	for i in set(s):
		ans += dic[i] * (dic[i] - 1) * (dic[i] - 2) // 6
		ans += dic[i] * dic[i + 1] * (dic[i + 1] - 1) // 2
		ans += dic[i] * (dic[i] - 1) * dic[i + 1] // 2
		ans += dic[i] * dic[i + 2] * (dic[i + 2] - 1) // 2
		ans += dic[i] * (dic[i] - 1) * dic[i + 2] // 2
		ans += dic[i] * dic[i + 1] * dic[i + 2]
	print(ans)


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	l = [*map(int, input().split())]
	z = [0] * n
	for i in l:
		z[i - 1] += 1
	a = 0
	for i in range(n - 2):
		a += z[i] * z[i + 1] * z[i + 2]
		a += z[i] * (z[i] - 1) // 2 * z[i + 2]
		a += z[i + 2] * (z[i + 2] - 1) // 2 * z[i]
	for i in range(n - 1):
		a += z[i] * (z[i] - 1) // 2 * z[i + 1]
		a += z[i + 1] * (z[i + 1] - 1) // 2 * z[i]
	for i in range(n):
		a += z[i] * (z[i] - 1) * (z[i] - 2) // 6
	print(a)


from collections import Counter
from sys import stdin, gettrace
if gettrace():
	inputi = input
else:

	def input():
		return next(stdin)[:-1]

	def inputi():
		return stdin.buffer.readline()

def solve():
	n = int(inputi())
	aa = [int(a) for a in inputi().split()]
	aac = Counter(aa)
	res = 0
	for a in aac.keys():
		(c0, c1, c2) = (aac[a], aac[a + 1], aac[a + 2])
		if c0 >= 3:
			res += c0 * (c0 - 1) * (c0 - 2) // 6
		if c0 >= 2 and c1 >= 1:
			res += c0 * (c0 - 1) * c1 // 2
		if c0 >= 1 and c1 >= 2:
			res += c0 * c1 * (c1 - 1) // 2
		if c0 >= 2 and c2 >= 1:
			res += c0 * (c0 - 1) * c2 // 2
		if c0 >= 1 and c2 >= 2:
			res += c0 * c2 * (c2 - 1) // 2
		if c0 >= 1 and c1 >= 1 and (c2 >= 1):
			res += c0 * c1 * c2
	print(res)

def main():
	t = int(inputi())
	for _ in range(t):
		solve()
main()


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	(m, k) = (3, 2)
	A = sorted(map(int, input().split()))
	j = 0
	ans = 0
	for (i, a) in enumerate(A):
		while a - A[j] > k:
			j += 1
		if i - j < 2:
			continue
		ans += (i - j) * (i - j - 1) // 2
	print(ans)


from sys import stdin
input = stdin.readline

def tup(arr):
	if len(set(arr)) == 1 and len(arr) >= 3:
		n = len(arr)
		return n * (n - 1) * (n - 2) // 6
	if len(arr) == 200000:
		return 1333313333400000
	m = 3
	n = 2
	arr = sorted(arr)
	i = 0
	ans = 0
	for i in range(len(arr)):
		lo = i
		hi = i + 2
		while hi < len(arr):
			if arr[hi] - arr[lo] <= 2:
				ans += hi - lo - 1
				hi += 1
			else:
				break
	return ans
t = int(input())
for i in range(t):
	a = input()
	lst = list(map(int, input().strip().split()))
	print(tup(lst))


import bisect
for _ in range(int(input())):
	n = int(input())
	a = sorted(list(map(int, input().split())))
	if n < 3:
		print(0)
		continue
	c = 0
	for w in range(1, n):
		i = n - w
		if i < 2:
			break
		b = bisect.bisect_left(a, a[i] - 2)
		p = i - b - 1
		if p >= 1:
			c += p * (p + 1) // 2
	print(c)


import sys
N = int(200000.0 + 5)
sys.setrecursionlimit(N)
ans = [0] * N

def charming():
	n = int(input())
	m = 3
	k = 2
	a = list(map(int, input().split()))
	res = 0
	cnt = [0] * (n + 1)
	if n < 3:
		print(0)
		return
	for i in a:
		cnt[i] += 1
	for i in range(1, n + 1):
		cnt[i] += cnt[i - 1]
	p = 1
	while p <= n:
		while p < n and cnt[p] == cnt[p - 1]:
			p += 1
		res += ans[cnt[p + k if p + k < n else n] - cnt[p - 1]]
		res -= ans[cnt[p + k if p + k < n else n] - cnt[p]]
		p += 1
	print(res)
for i in range(3, N):
	ans[i] = i * (i - 1) * (i - 2) // 6
for t in range(int(input())):
	charming()


from sys import stdin, stdout
for _ in range(int(input())):
	n = int(stdin.readline())
	a = list(map(int, stdin.readline().split()))
	a.sort()
	j = 0
	s = 0
	for (i, x) in enumerate(a):
		while j < n and a[j] - x <= 2:
			j += 1
		q = j - i - 1
		s += q * (q - 1) // 2
	print(s)


import sys
from math import gcd, sqrt, ceil, log2
from collections import defaultdict, Counter, deque
from bisect import bisect_left, bisect_right
import math
sys.setrecursionlimit(2 * 10 ** 5 + 10)
import heapq
from itertools import permutations
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192
aa = 'abcdefghijklmnopqrstuvwxyz'

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def get_sum(bit, i):
	s = 0
	i += 1
	while i > 0:
		s += bit[i]
		i -= i & -i
	return s

def update(bit, n, i, v):
	i += 1
	while i <= n:
		bit[i] += v
		i += i & -i

def modInverse(b, m):
	g = math.gcd(b, m)
	if g != 1:
		return -1
	else:
		return pow(b, m - 2, m)

def primeFactors(n):
	sa = []
	while n % 2 == 0:
		sa.append(2)
		n = n // 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			sa.append(i)
			n = n // i
	if n > 2:
		sa.append(n)
	return sa

def seive(n):
	pri = [True] * (n + 1)
	p = 2
	while p * p <= n:
		if pri[p] == True:
			for i in range(p * p, n + 1, p):
				pri[i] = False
		p += 1
	return pri

def check_prim(n):
	if n < 0:
		return False
	for i in range(2, int(sqrt(n)) + 1):
		if n % i == 0:
			return False
	return True

def getZarr(string, z):
	n = len(string)
	(l, r, k) = (0, 0, 0)
	for i in range(1, n):
		if i > r:
			(l, r) = (i, i)
			while r < n and string[r - l] == string[r]:
				r += 1
			z[i] = r - l
			r -= 1
		else:
			k = i - l
			if z[k] < r - i + 1:
				z[i] = z[k]
			else:
				l = i
				while r < n and string[r - l] == string[r]:
					r += 1
				z[i] = r - l
				r -= 1

def search(text, pattern):
	concat = pattern + '$' + text
	l = len(concat)
	z = [0] * l
	getZarr(concat, z)
	ha = []
	for i in range(l):
		if z[i] == len(pattern):
			ha.append(i - len(pattern) - 1)
	return ha

def mergeIntervals(arr):
	arr.sort(key=lambda x: x[0])
	m = []
	s = -10000
	max = -100000
	for i in range(len(arr)):
		a = arr[i]
		if a[0] > max:
			if i != 0:
				m.append([s, max])
			max = a[1]
			s = a[0]
		elif a[1] >= max:
			max = a[1]
	if max != -100000 and [s, max] not in m:
		m.append([s, max])
	return m

class SortedList:

	def __init__(self, iterable=[], _load=200):
		values = sorted(iterable)
		self._len = _len = len(values)
		self._load = _load
		self._lists = _lists = [values[i:i + _load] for i in range(0, _len, _load)]
		self._list_lens = [len(_list) for _list in _lists]
		self._mins = [_list[0] for _list in _lists]
		self._fen_tree = []
		self._rebuild = True

	def _fen_build(self):
		self._fen_tree[:] = self._list_lens
		_fen_tree = self._fen_tree
		for i in range(len(_fen_tree)):
			if i | i + 1 < len(_fen_tree):
				_fen_tree[i | i + 1] += _fen_tree[i]
		self._rebuild = False

	def _fen_update(self, index, value):
		if not self._rebuild:
			_fen_tree = self._fen_tree
			while index < len(_fen_tree):
				_fen_tree[index] += value
				index |= index + 1

	def _fen_query(self, end):
		if self._rebuild:
			self._fen_build()
		_fen_tree = self._fen_tree
		x = 0
		while end:
			x += _fen_tree[end - 1]
			end &= end - 1
		return x

	def _fen_findkth(self, k):
		_list_lens = self._list_lens
		if k < _list_lens[0]:
			return (0, k)
		if k >= self._len - _list_lens[-1]:
			return (len(_list_lens) - 1, k + _list_lens[-1] - self._len)
		if self._rebuild:
			self._fen_build()
		_fen_tree = self._fen_tree
		idx = -1
		for d in reversed(range(len(_fen_tree).bit_length())):
			right_idx = idx + (1 << d)
			if right_idx < len(_fen_tree) and k >= _fen_tree[right_idx]:
				idx = right_idx
				k -= _fen_tree[idx]
		return (idx + 1, k)

	def _delete(self, pos, idx):
		_lists = self._lists
		_mins = self._mins
		_list_lens = self._list_lens
		self._len -= 1
		self._fen_update(pos, -1)
		del _lists[pos][idx]
		_list_lens[pos] -= 1
		if _list_lens[pos]:
			_mins[pos] = _lists[pos][0]
		else:
			del _lists[pos]
			del _list_lens[pos]
			del _mins[pos]
			self._rebuild = True

	def _loc_left(self, value):
		if not self._len:
			return (0, 0)
		_lists = self._lists
		_mins = self._mins
		(lo, pos) = (-1, len(_lists) - 1)
		while lo + 1 < pos:
			mi = lo + pos >> 1
			if value <= _mins[mi]:
				pos = mi
			else:
				lo = mi
		if pos and value <= _lists[pos - 1][-1]:
			pos -= 1
		_list = _lists[pos]
		(lo, idx) = (-1, len(_list))
		while lo + 1 < idx:
			mi = lo + idx >> 1
			if value <= _list[mi]:
				idx = mi
			else:
				lo = mi
		return (pos, idx)

	def _loc_right(self, value):
		if not self._len:
			return (0, 0)
		_lists = self._lists
		_mins = self._mins
		(pos, hi) = (0, len(_lists))
		while pos + 1 < hi:
			mi = pos + hi >> 1
			if value < _mins[mi]:
				hi = mi
			else:
				pos = mi
		_list = _lists[pos]
		(lo, idx) = (-1, len(_list))
		while lo + 1 < idx:
			mi = lo + idx >> 1
			if value < _list[mi]:
				idx = mi
			else:
				lo = mi
		return (pos, idx)

	def add(self, value):
		_load = self._load
		_lists = self._lists
		_mins = self._mins
		_list_lens = self._list_lens
		self._len += 1
		if _lists:
			(pos, idx) = self._loc_right(value)
			self._fen_update(pos, 1)
			_list = _lists[pos]
			_list.insert(idx, value)
			_list_lens[pos] += 1
			_mins[pos] = _list[0]
			if _load + _load < len(_list):
				_lists.insert(pos + 1, _list[_load:])
				_list_lens.insert(pos + 1, len(_list) - _load)
				_mins.insert(pos + 1, _list[_load])
				_list_lens[pos] = _load
				del _list[_load:]
				self._rebuild = True
		else:
			_lists.append([value])
			_mins.append(value)
			_list_lens.append(1)
			self._rebuild = True

	def discard(self, value):
		_lists = self._lists
		if _lists:
			(pos, idx) = self._loc_right(value)
			if idx and _lists[pos][idx - 1] == value:
				self._delete(pos, idx - 1)

	def remove(self, value):
		_len = self._len
		self.discard(value)
		if _len == self._len:
			raise ValueError('{0!r} not in list'.format(value))

	def pop(self, index=-1):
		(pos, idx) = self._fen_findkth(self._len + index if index < 0 else index)
		value = self._lists[pos][idx]
		self._delete(pos, idx)
		return value

	def bisect_left(self, value):
		(pos, idx) = self._loc_left(value)
		return self._fen_query(pos) + idx

	def bisect_right(self, value):
		(pos, idx) = self._loc_right(value)
		return self._fen_query(pos) + idx

	def count(self, value):
		return self.bisect_right(value) - self.bisect_left(value)

	def __len__(self):
		return self._len

	def __getitem__(self, index):
		(pos, idx) = self._fen_findkth(self._len + index if index < 0 else index)
		return self._lists[pos][idx]

	def __delitem__(self, index):
		(pos, idx) = self._fen_findkth(self._len + index if index < 0 else index)
		self._delete(pos, idx)

	def __contains__(self, value):
		_lists = self._lists
		if _lists:
			(pos, idx) = self._loc_left(value)
			return idx < len(_lists[pos]) and _lists[pos][idx] == value
		return False

	def __iter__(self):
		return (value for _list in self._lists for value in _list)

	def __reversed__(self):
		return (value for _list in reversed(self._lists) for value in reversed(_list))

	def __repr__(self):
		return 'SortedList({0})'.format(list(self))

def ncr(n, r, p):
	num = den = 1
	for i in range(r):
		num = num * (n - i) % p
		den = den * (i + 1) % p
	return num * pow(den, p - 2, p) % p

def sol(n):
	seti = set()
	for i in range(1, int(sqrt(n)) + 1):
		if n % i == 0:
			seti.add(n // i)
			seti.add(i)
	return seti

def lcm(a, b):
	return a * b // gcd(a, b)

def moore_voting(l):
	count1 = 0
	count2 = 0
	first = 10 ** 18
	second = 10 ** 18
	n = len(l)
	for i in range(n):
		if l[i] == first:
			count1 += 1
		elif l[i] == second:
			count2 += 1
		elif count1 == 0:
			count1 += 1
			first = l[i]
		elif count2 == 0:
			count2 += 1
			second = l[i]
		else:
			count1 -= 1
			count2 -= 1
	for i in range(n):
		if l[i] == first:
			count1 += 1
		elif l[i] == second:
			count2 += 1
	if count1 > n // 3:
		return first
	if count2 > n // 3:
		return second
	return -1

def find_parent(u, parent):
	if u != parent[u]:
		parent[u] = find_parent(parent[u], parent)
	return parent[u]

def dis_union(n, e):
	par = [i for i in range(n + 1)]
	rank = [1] * (n + 1)
	for (a, b) in e:
		(z1, z2) = (find_parent(a, par), find_parent(b, par))
		if rank[z1] > rank[z2]:
			(z1, z2) = (z2, z1)
		if z1 != z2:
			par[z1] = z2
			rank[z2] += rank[z1]
		else:
			return (a, b)

def dijkstra(n, tot, hash):
	hea = [[0, n]]
	dis = [10 ** 18] * (tot + 1)
	dis[n] = 0
	boo = defaultdict(bool)
	check = defaultdict(int)
	while hea:
		(a, b) = heapq.heappop(hea)
		if boo[b]:
			continue
		boo[b] = True
		for (i, w) in hash[b]:
			if b == 1:
				c = 0
				if (1, i, w) in nodes:
					c = nodes[1, i, w]
					del nodes[1, i, w]
				if dis[b] + w < dis[i]:
					dis[i] = dis[b] + w
					check[i] = c
				elif dis[b] + w == dis[i] and c == 0:
					dis[i] = dis[b] + w
					check[i] = c
			elif dis[b] + w <= dis[i]:
				dis[i] = dis[b] + w
				check[i] = check[b]
			heapq.heappush(hea, [dis[i], i])
	return check

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res *= x
		x = x * x
		y = y >> 1
	return res
import sys
from math import ceil, log2
INT_MAX = sys.maxsize

def minVal(x, y):
	return x if x < y else y

def getMid(s, e):
	return s + (e - s) // 2

def RMQUtil(st, ss, se, qs, qe, index):
	if qs <= ss and qe >= se:
		return st[index]
	if se < qs or ss > qe:
		return INT_MAX
	mid = getMid(ss, se)
	return minVal(RMQUtil(st, ss, mid, qs, qe, 2 * index + 1), RMQUtil(st, mid + 1, se, qs, qe, 2 * index + 2))

def RMQ(st, n, qs, qe):
	if qs < 0 or qe > n - 1 or qs > qe:
		print('Invalid Input')
		return -1
	return RMQUtil(st, 0, n - 1, qs, qe, 0)

def constructSTUtil(arr, ss, se, st, si):
	if ss == se:
		st[si] = arr[ss]
		return arr[ss]
	mid = getMid(ss, se)
	st[si] = minVal(constructSTUtil(arr, ss, mid, st, si * 2 + 1), constructSTUtil(arr, mid + 1, se, st, si * 2 + 2))
	return st[si]

def constructST(arr, n):
	x = int(ceil(log2(n)))
	max_size = 2 * int(2 ** x) - 1
	st = [0] * max_size
	constructSTUtil(arr, 0, n - 1, st, 0)
	return st
t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	l.sort()
	i = 0
	ans = 0
	while i < n:
		z = l[i]
		cnt1 = 0
		init = i
		while i < n and z == l[i]:
			cnt1 += 1
			i += 1
		z1 = bisect_right(l, z + 2)
		cnt2 = z1 - init - cnt1
		ans += cnt1 * (cnt2 * (cnt2 - 1) // 2) + cnt2 * (cnt1 * (cnt1 - 1) // 2) + cnt1 * (cnt1 - 1) * (cnt1 - 2) // 6
	print(ans)


import sys
from sys import stdin
from collections import deque

def nCr(x):
	if x < 2:
		return 0
	else:
		return x * (x - 1) // 2
tt = int(stdin.readline())
for loop in range(tt):
	n = int(stdin.readline())
	m = 3
	k = 2
	a = list(map(int, stdin.readline().split()))
	a.sort()
	q = deque([])
	ans = 0
	for i in range(n):
		na = a[i]
		while len(q) > 0 and q[0] < na - k:
			q.popleft()
		ans += nCr(len(q))
		q.append(na)
	print(ans)


from sys import stdin

def input():
	return stdin.readline().rstrip('\r\n')
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	mm = 0
	l.sort()
	(a, b) = (0, 2)
	while b < n:
		while a < b and l[b] - l[a] > 2:
			a += 1
		p = b - a - 1
		if p > 0:
			mm += p * (p + 1) // 2
		b += 1
	print(mm)


import math
import sys
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	l.sort()
	su = 0
	i = 0
	j = 2
	while i < n:
		while j < n and l[j] - l[i] <= 2:
			j += 1
		su += (j - i - 2) * (j - i - 1) // 2
		i += 1
	print(su)


import time, math as mt, bisect as bs, sys
from sys import stdin, stdout
from collections import deque
from fractions import Fraction
from collections import Counter
from collections import OrderedDict
pi = 3.141592653589793

def II():
	return int(stdin.readline())

def IP():
	return map(int, stdin.readline().split())

def FIP():
	return map(float, stdin.readline().split())

def L():
	return list(map(int, stdin.readline().split()))

def P(x):
	return stdout.write(str(x) + '\n')

def PI(x, y):
	return stdout.write(str(x) + ' ' + str(y) + '\n')

def lcm(a, b):
	return a * b // gcd(a, b)

def gcd(a, b):
	if a == 0:
		return b
	elif b == 0:
		return a
	if a > b:
		return gcd(a % b, b)
	else:
		return gcd(a, b % a)

def bfs(adj, v):
	visited = [False] * (v + 1)
	q = deque()
	while q:
		pass

def setBit(n):
	count = 0
	while n != 0:
		n = n & n - 1
		count += 1
	return count
mx = 10 ** 7
spf = [mx] * (mx + 1)

def readTree(n, e):
	adj = [set() for i in range(n + 1)]
	for i in range(e):
		(u1, u2) = IP()
		adj[u1].add(u2)
	return adj

def sieve():
	li = [True] * (10 ** 3 + 5)
	(li[0], li[1]) = (False, False)
	for i in range(2, len(li), 1):
		if li[i] == True:
			for j in range(i * i, len(li), i):
				li[j] = False
	(prime, cur) = ([0] * 200, 0)
	for i in range(10 ** 3 + 5):
		if li[i] == True:
			prime[cur] = i
			cur += 1
	return prime

def SPF():
	mx = 10 ** 6 + 1
	spf[1] = 1
	for i in range(2, mx):
		if spf[i] == 1000000000.0:
			spf[i] = i
			for j in range(i * i, mx, i):
				if i < spf[j]:
					spf[j] = i
	return

def prime(n, d):
	prm = set()
	while n != 1:
		prm.add(spf[n])
		n = n // spf[n]
	for ele in prm:
		d[ele] = d.get(ele, 0) + 1
	return

def primeFactors(n, d):
	while n % 2 == 0:
		d[2] = d.get(2, 0) + 1
		n = n / 2
	for i in range(3, int(mt.sqrt(n)) + 1, 2):
		while n % i == 0:
			d[i] = d.get(i, 0) + 1
			n = n / i
	if n > 2:
		d[n] = d.get(n, 0) + 1
	return
mod = 998244353

def solve():
	n = II()
	a = L()
	ans = 0
	d = {}
	for ele in a:
		d[ele] = d.get(ele, 0) + 1
	a.sort()
	for ele in a:
		last = ans
		ans += d.get(ele + 1, 0) * d.get(ele + 2, 0)
		if d.get(ele + 1, 0) >= 2:
			val = d[ele + 1]
			ans += val * (val - 1) // 2
		if d.get(ele + 2, 0) >= 2:
			val = d[ele + 2]
			ans += val * (val - 1) // 2
		if d.get(ele, 0) >= 2:
			val = d[ele]
			ans += (val - 1) * d.get(ele + 1, 0)
		if d.get(ele, 0) >= 2:
			val = d[ele]
			ans += (val - 1) * d.get(ele + 2, 0)
		if d.get(ele, 0) >= 3:
			val = d[ele]
			ans += (val - 2) * (val - 1) // 2
		d[ele] -= 1
	print(ans)
	return
t = II()
for i in range(t):
	solve()


import collections
from collections import defaultdict
import math
from collections import Counter
import sys
input = sys.stdin.readline
for i in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	d = defaultdict(lambda : 0)
	for x in a:
		d[x] += 1
	ans = 0
	d[n + 1] = 0
	d[n + 2] = 0
	for i in range(1, n + 1):
		(x, y, z) = [d[i], d[i + 1], d[i + 2]]
		ans += x * y * z
		ans += math.comb(x, 2) * y + math.comb(y, 2) * x
		ans += math.comb(x, 2) * z + math.comb(z, 2) * x
		ans += math.comb(x, 3)
	print(ans)


import sys
input = sys.stdin.readline
from collections import defaultdict
for _ in range(int(input())):
	n = int(input())
	v = list(map(int, input().split()))
	d = defaultdict(int)
	for i in v:
		d[i] += 1
	v = list(set(v))
	v.sort()
	ans = 0
	for i in v:
		cnt = d[i] + d[i + 1] + d[i + 2]
		if cnt >= 3:
			if d[i + 1] + d[i + 2] >= 2:
				c = d[i + 1] + d[i + 2]
				ans += d[i] * c * (c - 1) // 2
			if d[i] >= 2:
				ans += d[i] * (d[i] - 1) // 2 * (cnt - d[i])
			if d[i] >= 3:
				ans += d[i] * (d[i] - 1) * (d[i] - 2) // 6
	print(ans)


from collections import *
a = int(input())
import sys
input = sys.stdin.readline
for i in range(a):
	s = int(input())
	z = list(map(int, input().split()))
	al = defaultdict(int)
	for i in range(len(z)):
		al[z[i]] += 1
	total = 0
	z = list(set(z))
	for i in range(len(z)):
		t = al[z[i]]
		m = al[z[i] + 1]
		q = al[z[i] + 2]
		total += max(t * (t - 1) * (t - 2) // 6, 0) + max(t * (t - 1) // 2 * m, 0) + max(t * (t - 1) // 2 * q, 0) + max((t * (m * (m - 1) // 2), 0)) + max(t * m * q, 0) + max(t * (q * (q - 1)) // 2, 0)
	print(total)