"You must lift the dam. With a lever. I will give it to you.

You must block the canal. With a rock. I will not give the rock to you." 

Danik urgently needs rock and lever! Obviously, the easiest way to get these things is to ask Hermit Lizard for them.

Hermit Lizard agreed to give Danik the lever. But to get a stone, Danik needs to solve the following task.

You are given a positive integer $n$, and an array $a$ of positive integers. The task is to calculate the number of such pairs $(i,j)$ that $i<j$ and $a_i$ $\&$ $a_j \ge a_i \oplus a_j$, where $\&$ denotes the bitwise AND operation, and $\oplus$ denotes the bitwise XOR operation.

Danik has solved this task. But can you solve it?


-----Input-----

Each test contains multiple test cases.

The first line contains one positive integer $t$ ($1 \le t \le 10$) denoting the number of test cases. Description of the test cases follows.

The first line of each test case contains one positive integer $n$ ($1 \le n \le 10^5$) — length of the array.

The second line contains $n$ positive integers $a_i$ ($1 \le a_i \le 10^9$) — elements of the array.

It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.


-----Output-----

For every test case print one non-negative integer — the answer to the problem.


-----Example-----
Input
5
5
1 4 3 7 10
3
1 1 1
4
6 2 5 3
2
2 4
1
1

Output
1
3
2
0
0



-----Note-----

In the first test case there is only one pair: $(4,7)$: for it $4$ $\&$ $7 = 4$, and $4 \oplus 7 = 3$.

In the second test case all pairs are good.

In the third test case there are two pairs: $(6,5)$ and $(2,3)$.

In the fourth test case there are no good pairs.

Source: codeforces | Difficulty: EASY
Tags: Bit manipulation

Mathematics


Solutions:
from math import log
for _ in range(int(input())):
	n = int(input())
	arr = [0] * 102
	dq = list(map(int, input().split()))
	ans = 0
	for i in dq:
		arr[int(log(i, 2))] += 1
	for i in arr:
		ans += i * (i - 1) // 2
	print(ans)


import math
nbTest = int(input())

def findHighest2Pow(n):
	return int(math.log(n, 2))
for k in range(nbTest):
	n = int(input())
	T = list(map(int, input().split(' ')))
	sol = 0
	dic = {}
	for k in range(0, n):
		pow2 = findHighest2Pow(T[k])
		if pow2 in dic.keys():
			sol += dic[pow2]
			dic[pow2] += 1
		else:
			dic[pow2] = 1
	print(sol)


import math
from collections import defaultdict
for i1 in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	if n == 1:
		print(0)
		continue
	dict = defaultdict(int)
	for i in l:
		dict[int(math.log(i, 2))] += 1
	ans = 0
	for i in dict.values():
		ans += i * (i - 1) // 2
	print(ans)


from collections import defaultdict
for _ in range(int(input())):
	n = int(input())
	a = [len(bin(int(i))[2:]) for i in input().split()]
	cnt = [0] * (max(a) + 1)
	for i in a:
		cnt[i] += 1
	ans = 0
	for i in cnt:
		ans += i * (i - 1) // 2
	print(ans)


import math
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	cnt = 0
	mp = {}
	for i in a:
		x = math.floor(math.log2(i))
		if x in mp.keys():
			mp[x] += 1
		else:
			mp[x] = 1
	for i in mp:
		cnt += mp[i] * (mp[i] - 1) // 2
	print(cnt)


from sys import *
input = stdin.readline
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	d = {}
	for em in a:
		index = -1
		while em > 0:
			em = em // 2
			index += 1
		d[index] = d.get(index, 0) + 1
	sn = 0
	for (key, value) in d.items():
		sn += value * (value - 1) // 2
	stdout.write(str(sn) + '\n')


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()))
		b = [bin(x)[2:].rjust(32, '0') for x in a]
		s = 0
		v = [False] * n
		for j in range(32):
			one = 0
			for k in range(n):
				if not v[k] and b[k][j] == '1':
					one += 1
					v[k] = True
			s += one * (one - 1) // 2
		print(s)
main()


from math import *
from cmath import *
from itertools import *
from decimal import *
from fractions import *
for _ in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	p = [0] * 35
	for x in a:
		cnt = 34
		while x >> cnt == 0:
			cnt -= 1
		p[cnt] += 1
	cnt = 0
	for x in p:
		cnt += x * (x - 1) // 2
	print(cnt)


from collections import Counter
from math import log2
for _ in range(int(input())):
	n = int(input())
	a = [*map(lambda x: int(log2(int(x))), input().split())]
	b = Counter(a)
	c = 0
	for i in b.values():
		c += i * (i - 1) // 2
	print(c)


for _ in range(int(input())):
	n = int(input())
	a = map(int, input().split())
	s = 0
	b = [0] * 30
	for i in a:
		c = 0
		p = 1
		while p <= i:
			c += 1
			p *= 2
		b[c - 1] += 1
	for i in b:
		s += i * (i - 1) // 2
	print(s)


import os
import sys
from io import BytesIO, IOBase
import threading
from bisect import bisect_right
from math import gcd, log
from collections import Counter
from pprint import pprint

def main():
	n = int(input())
	arr = list(map(int, input().split()))
	cnt = [0] * 50
	ans = 0
	for i in range(n):
		l = len(bin(arr[i])[2:])
		ans += cnt[l]
		cnt[l] += 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')
for _ in range(int(input())):
	main()


from math import log2
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	mylist = [0 for i in range(50)]
	for i in a:
		if i != 0:
			value = int(log2(i)) + 1
		else:
			value = 0
		mylist[value] += 1
	ans = 0
	for i in mylist:
		ans += i * (i - 1) // 2
	print(ans)


mod = 10 ** 9 + 7

def f(n):
	ret = 0
	while n > 0:
		ret += 1
		n >>= 1
	return ret

def solve():
	n = int(input())
	a = list(map(int, input().split()))
	d = {}
	ans = 0
	for i in range(n):
		x = f(a[i])
		if x in d:
			ans += d[x]
			d[x] += 1
		else:
			d[x] = 1
	print(ans)
t = int(input())
while t > 0:
	solve()
	t -= 1


def lftmost(x):
	if x == 0:
		return -1
	i = 32
	while not 1 << i & x:
		i -= 1
	return i
q = int(input())
while q:
	q -= 1
	n = int(input())
	a = [int(x) for x in input().split()]
	x = [[] for _ in range(32)]
	for i in a:
		if x != 0:
			x[lftmost(i)].append(i)

	def prs(i):
		return i * (i - 1) // 2
	ans = 0
	for i in x:
		ans += prs(len(i))
	print(ans)


import sys
import math
from collections import defaultdict, deque
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())
		dic = defaultdict(int)
		arr = inar()
		for i in range(n):
			lol = 0
			count = 0
			for j in range(0, 33):
				count = 1 << j
				if count & arr[i]:
					lol = count
			dic[lol] += 1
		ans = 0
		for i in dic.keys():
			ans += dic[i] * (dic[i] - 1) // 2
		print(ans)
main()


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


from math import *
from bisect import bisect_left as bl
from bisect import bisect_right as br
from collections import Counter as ctr
from collections import deque as dq
from array import array
from re import search
inp = lambda : int(input())
li = lambda : list(map(int, input().split()))
lb = lambda : list(map(int, input()))
ls = lambda : list(input())
arr = lambda a: array('i', a)
bi = lambda n: bin(n).replace('0b', '')
yn = lambda f: print('NYOE S'[f::2])
sbstr = lambda a, s: search('.*'.join(a), s)

def solve(__=1):
	for _ in range(__):
		n = inp()
		a = li()
		t = [0] * 31
		for i in range(n):
			j = int(log(a[i], 2))
			t[j] += 1
		ans = 0
		for i in t:
			ans += i * (i - 1)
		print(ans // 2)
solve(inp())


import math
t = int(input())
for case in range(t):
	n = int(input())
	a = [int(x) for x in input().split(' ')]
	d = {}
	cnt = 0
	for x in a:
		b = math.floor(math.log(x, 2))
		if b in d.keys():
			d[b] += 1
		else:
			d[b] = 1
	for v in d.values():
		if v > 1:
			cnt += v * (v - 1) // 2
	print(cnt)


from math import *
from bisect import *
from collections import *
from random import *
from decimal import *
import sys
input = sys.stdin.readline

def inp():
	return int(input())

def st():
	return input().rstrip('\n')

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

def ma():
	return map(int, input().split())
t = int(input())
while t:
	t -= 1
	n = inp()
	a = lis()
	b = [0] * 30
	for i in a:
		z = bin(i)[2:]
		z = z.zfill(30)
		for j in range(30):
			if z[j] == '1':
				b[j] += 1
				break
	s = 0
	for i in b:
		s += i * (i - 1) // 2
	print(s)


import sys
input = sys.stdin.readline
import io, os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
(inp, ip) = (lambda : int(input()), lambda : [int(w) for w in input().split()])
for _ in range(inp()):
	n = inp()
	x = ip()
	ct = [0] * 32
	ans = 0
	for i in range(n - 1, -1, -1):
		bn = bin(x[i])[2:].zfill(32)
		for j in range(32):
			if bn[j] == '1':
				ans += ct[j]
				ct[j] += 1
				break
	print(ans)


import sys
import os
from math import sqrt, log, log2, log10, gcd, floor, pow, sin, cos, tan, pi, inf, factorial
from copy import copy, deepcopy
import bisect
from sys import exit, stdin, stdout
from collections import Counter, defaultdict, deque
from itertools import permutations
import heapq
sys.setrecursionlimit(10000)
mod = 1000000007
iinp = lambda : int(sys.stdin.readline())
inp = lambda : sys.stdin.readline().strip()
strl = lambda : list(inp().strip().split(' '))
intl = lambda : list(map(int, inp().split(' ')))
flol = lambda : list(map(float, inp().split(' ')))
flush = lambda : stdout.flush()

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	p = int(sqrt(n))
	for i in range(5, p + 1, 6):
		if n % i == 0 or n % (i + 2) == 0:
			return False
	return True

def solve():
	n = iinp()
	ans = 0
	arr = intl()
	d = {}
	for i in range(n):
		b = bin(arr[i])[2:]
		if len(b) in d.keys():
			d[len(b)] += 1
		else:
			d[len(b)] = 1
	ans = 0
	for i in d.keys():
		if d[i] >= 2:
			ans += d[i] * (d[i] - 1) // 2
	stdout.write(str(ans) + '\n')
for _ in range(iinp()):
	solve()


game = int(input())
for f in range(game):
	n = int(input())
	a = list(map(int, input().split()))
	a = sorted(a)
	c = 0
	count = 0
	li = []
	for value in a:
		c = bin(value)
		li.append(len(c))
	for i in range(36):
		le = li.count(i)
		if le > 1:
			count += le * (le - 1) // 2
		else:
			continue
	print(count)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	bitcount = [0 for i in range(32)]
	for x in arr:
		bit = -1
		for i in range(32):
			if x >> i & 1:
				bit = i
		if bit >= 0:
			bitcount[bit] += 1
	ans = 0
	for x in bitcount:
		ans += x * (x - 1) // 2
	print(ans)


import math
for case in range(int(input())):
	n = int(input())
	nums = list(map(int, input().split()))
	bits = {}
	count = 0
	new = []
	for num in nums:
		cur = int(math.log(num, 2))
		new.append(cur)
		if cur in bits:
			count += bits[cur]
			bits[cur] += 1
		else:
			bits[cur] = 1
	print(count)


for _ in range(int(input())):
	n = int(input())
	lst = list(map(int, input().split()))
	d = {}
	for ele in lst:
		msbl = len(bin(ele)) - 2
		if msbl in d:
			d[msbl] += 1
		else:
			d[msbl] = 1
	res = 0
	for k in d:
		e = d[k]
		res += e * (e - 1) / 2
	print(int(res))


from sys import stdin, stdout
from math import log2
test = int(stdin.readline())
for _ in range(test):
	n = int(stdin.readline())
	a = [int(x) for x in stdin.readline().split()]
	temp = [0 for i in range(35)]
	for i in a:
		temp[int(log2(i)) + 1] += 1
	ans = 0
	for i in temp:
		ans += i * (i - 1) // 2
	print(ans)


import sys
input = sys.stdin.readline
t = int(input())
for tests in range(t):
	n = int(input())
	A = list(map(int, input().split()))
	X = [0] * 32
	for a in A:
		for j in range(32, -1, -1):
			if a & 1 << j != 0:
				X[j] += 1
				break
	ANS = 0
	for x in X:
		ANS += x * (x - 1) // 2
	print(ANS)


t = int(input())
while t:
	t -= 1
	n = int(input())
	l = list(map(int, input().split()))
	x = 70 * [0]
	for i in l:
		s = len(bin(i).replace('0b', ''))
		x[s] += 1
	sum = 0
	for i in x:
		sum += i * (i - 1) // 2
	print(sum)


from collections import defaultdict
t = int(input())
for i in range(t):
	n = input()
	dic = defaultdict(int)
	nums = input().split()
	for j in nums:
		dic[len(bin(int(j)))] += 1
	out = 0
	for j in dic.values():
		out += j * (j - 1) // 2
	print(out)


for t in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	result = 0
	counts = [0] * 31
	for i in arr:
		l = len(bin(i)[2:])
		counts[l] += 1
	for j in range(31):
		result = result + counts[j] * (counts[j] - 1) // 2
	print(result)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	d = {}
	for i in range(0, n):
		p = bin(a[i])[2:]
		z = len(p)
		if z not in d:
			d[z] = 1
		else:
			d[z] += 1
	count = 0
	for x in d:
		y = d[x]
		if y > 1:
			f = y * (y - 1) // 2
			count += f
	print(int(count))


import sys
t = int(input())
while t > 0:
	t -= 1
	n = int(sys.stdin.readline())
	input = sys.stdin.readline()
	ar = list(map(int, input.split()))
	ar.sort()
	ans = 0
	index = 0
	(current_start, finish) = (1, 1)
	while index < n:
		counter = 0
		while index < n and current_start <= ar[index] <= finish:
			counter += 1
			index += 1
		ans += counter * (counter - 1) // 2
		current_start = finish + 1
		finish = current_start * 2 - 1
	print(ans)


from collections import Counter

def solve(n, l):
	m = [bin(i)[2:] for i in l]
	z = [len(j) for j in m]
	if n == 1:
		return 0
	d = Counter(z)
	ans = 0
	for i in d:
		if d[i] == 1:
			ans += 0
		else:
			ans += d[i] * (d[i] - 1) // 2
	return ans
t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	print(solve(n, l))


def getPairs(arr):
	freq = {}
	for num in arr:
		x = str(bin(num))[2:]
		h = 32 - len(x)
		y = '0' * h + x
		if y not in freq:
			freq[y] = 0
		freq[y] += 1
	ans = 0
	for i in range(32):
		if len(freq) == 0:
			break
		num_ones = []
		x = 0
		for num in freq:
			if num[i] == '1':
				num_ones.append(num)
				x += freq[num]
		ans += x * (x - 1) / 2
		for num in num_ones:
			freq.pop(num)
	return int(ans)
t = int(input())
while t:
	t -= 1
	n = int(input())
	a = [int(x) for x in input().split()]
	pairs = getPairs(a)
	print(pairs)


import os
import sys
from io import BytesIO, IOBase

def main():
	for _ in range(int(input())):
		n = int(input())
		ans = 0
		arr = list(map(int, input().strip().split()))
		d = {}
		for i in range(n):
			b = bin(arr[i])[2:]
			if len(b) in d.keys():
				d[len(b)] += 1
			else:
				d[len(b)] = 1
		ans = 0
		for i in d.keys():
			if d[i] >= 2:
				ans += d[i] * (d[i] - 1) // 2
		sys.stdout.write(str(ans) + '\n')
	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')
main()


from math import log2 as l2
from collections import defaultdict as dd
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	c = 0
	b = dd(int)
	b[int(l2(l[0]))] = 1
	for i in range(1, n):
		x = int(l2(l[i]))
		c += b[x]
		b[x] += 1
	print(c)


for _ in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	g = [0] * 32
	for i in a:
		s = bin(i)
		s = s.replace('0b', '')
		s = s[::-1]
		s += '0' * (32 - len(s))
		s = s[::-1]
		p = s.find('1')
		if p >= 0:
			g[32 - p - 1] += 1
	s = 0
	for i in range(32):
		if g[i] > 1:
			s += g[i] * (g[i] - 1) // 2
	print(s)


test = int(input())

def solver(n):
	counter = 0
	while n > 0:
		counter = counter + 1
		n = n // 2
	return counter
for _ in range(test):
	n = int(input())
	a = [int(u) for u in input().split()]
	msb = [int(0) for _ in range(65)]
	for item in a:
		msb[solver(item)] += 1
	ans = 0
	for item in msb:
		ans += item * (item - 1) // 2
	print(ans)


t = int(input())

def findbit(x):
	t = 1 << 30
	while x < t:
		t = t >> 1
	return t
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	d = {}
	ans = 0
	for j in range(n):
		k = findbit(arr[j])
		if k not in d:
			d[k] = 0
		d[k] += 1
	for j in d:
		ans += (d[j] - 1) * d[j] // 2
	print(ans)


results = []
t = int(input())
for k in range(t):
	n = int(input())
	vals = input().split()
	A = [int(val) for val in vals]
	list_count = [0] * 32
	total = 0
	for a in A:
		highest = len(bin(a)) - 3
		list_count[highest] += 1
	for count in list_count:
		total += count * (count - 1) // 2
	results.append(total)
for result in results:
	print(result)


def f(l):
	n = len(l)
	return n * (n - 1) // 2
for i in range(int(input())):
	n = int(input())
	l = [int(x) for x in input().split()]
	l.sort()
	count = 0
	l2 = []
	l3 = []
	for i in range(n):
		if i + 1 <= n - 1:
			if len(bin(l[i]).replace('0b', '')) == len(bin(l[i + 1]).replace('0b', '')):
				l2.append(l[i])
			else:
				l2.append(l[i])
				l3.append(l2)
				l2 = []
		else:
			l2.append(l[i])
			l3.append(l2)
			l2 = []
	if l2 not in l3:
		l3.append(l2)
	for i in l3:
		count += f(i)
	print(count)


l = [2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144, 524288, 1048576, 2097152, 4194304, 8388608, 16777216, 33554432, 67108864, 134217728, 268435456, 536870912, 1073741824]
try:
	t = int(input())
	for i in range(t):
		n = int(input())
		a = list(map(int, input().split()))
		j = 0
		ans = 0
		k = 0
		a.sort()
		while j < n:
			cnt = 0
			while j < n and a[j] < l[k]:
				cnt += 1
				j += 1
			ans += cnt * (cnt - 1) // 2
			k += 1
		print(ans)
except:
	pass


from math import floor, log2
t = int(input())
ans = 0
for _ in range(t):
	f = [0] * 50
	n = int(input())
	a = [int(i) for i in input().split(' ')]
	for i in a:
		f[int(floor(log2(i)))] += 1
	ans = 0
	for i in f:
		ans += i * (i - 1) // 2
	print(int(ans))


import io, os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def f():
	n = int(input())
	A = [int(s) for s in input().split()]
	mask = [1 << i for i in range(29, -1, -1)]
	count = [0] * 30
	for a in A:
		for i in range(30):
			if mask[i] & a:
				count[i] += 1
				break
	ans = 0
	for c in count:
		ans += c * (c - 1) // 2
	print(ans)
for t in range(int(input())):
	f()


import math
t = int(input())
for _ in range(t):
	n = int(input())
	arr = [int(x) for x in input().split()]
	s = [int(0) for i in range(33)]
	for i in arr:
		k = int(math.log(i, 2))
		s[k] += 1
	ans = 0
	for i in s:
		if i >= 2:
			ans += i * (i - 1) // 2
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	k = 2
	val = 0
	dic = {}
	for i in range(0, n):
		while a[i] // k != 0:
			k *= 2
		if k in dic:
			dic[k] += 1
		else:
			dic[k] = 1
	count = 0
	for k in dic:
		t = dic[k]
		count += t * (t - 1) // 2
	print(count)


import sys
import math

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

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

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

def SI():
	return sys.stdin.readline().strip()
t = II()
for q in range(t):
	n = II()
	a = LI()
	d = [0] * 32
	for i in range(n):
		d[len(bin(a[i])[2:]) - 1] += 1
	ans = 0
	for i in range(32):
		ans += d[i] * (d[i] - 1) // 2
	print(ans)


import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush, nsmallest
from math import ceil, floor, gcd, fabs, factorial, fmod, sqrt, inf, log
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
from decimal import Decimal
mod = pow(10, 9) + 7
mod2 = 998244353

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

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

def outa(*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)]
for _ in range(int(data())):
	n = int(data())
	arr = l()
	dp = [0] * 33
	for i in arr:
		for j in range(32, -1, -1):
			if i & 1 << j:
				dp[j] += 1
				break
	answer = 0
	for i in range(32, -1, -1):
		answer += dp[i] * (dp[i] - 1) // 2
	out(answer)


from collections import Counter
t = int(input())
for _ in range(t):
	n = int(input())
	a = [len(bin(int(x))) for x in input().split()]
	c = Counter(a)
	ans = 0
	for (_, i) in c.items():
		ans += i * (i - 1) // 2
	print(ans)


import math

def first(n):
	return int(math.log2(n))
for t in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	count = 0
	d = {}
	for i in range(n):
		z = first(a[i])
		if z in d:
			d[z] += 1
		else:
			d[z] = 1
	for i in d:
		count += d[i] * (d[i] - 1) // 2
	print(count)


from sys import stdin
I = stdin.readline
for _ in range(int(I())):
	n = int(I())
	ar = list(map(int, I().split()))
	dic = {}
	for i in ar:
		bi = len(bin(i)[2:])
		if bi in dic:
			dic[bi] += 1
		else:
			dic[bi] = 1
	ans = 0
	for i in dic:
		ans += dic[i] * (dic[i] - 1) // 2
	print(ans)


from collections import defaultdict
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	sum1 = 0
	d = defaultdict(int)
	for i in range(n):
		x = len(bin(a[i])[2:])
		d[x] += 1
	for i in range(33):
		sum1 += d[i] * (d[i] - 1) // 2
	print(sum1)


import sys
try:
	fin = open('in')
except:
	fin = sys.stdin
input = fin.readline
from math import log2
from collections import *
for _ in range(int(input())):
	n = int(input())
	r = list(map(int, input().split()))
	r = [int(log2(x)) for x in r]
	c = Counter(r).values()
	print(sum((t * (t - 1) // 2 for t in c)))


import math
from collections import defaultdict
import functools as f
from itertools import permutations as p
t = 1
t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	l.sort()
	a = []
	for i in range(n):
		k = bin(l[i])[2:]
		k = str(k)
		a.append(len(k))
	a.sort()
	an = 1
	co = 0
	for i in range(len(a) - 1):
		if a[i] == a[i + 1]:
			an += 1
			if i == len(a) - 2:
				co += an * (an - 1) / 2
		else:
			co += an * (an - 1) / 2
			an = 1
	print(int(co))


t = int(input())
for _ in range(t):
	n = int(input())
	arr = [int(j) for j in input().split()]
	freq = [0] * 35
	ans = 0
	for x in arr:
		freq[len(bin(x))] += 1
	for x in freq:
		ans += x * (x - 1) // 2
	print(ans)


def rock(A):
	A = list(map(lambda x: bin(x)[2:], A))
	for i in range(len(A)):
		if len(A[i]) == 32:
			continue
		else:
			x = len(A[i])
			count = 32 - x
			s = '0' * count
			A[i] = s + A[i]
	count = 0
	for i in range(32):
		(count1, count2) = (0, 0)
		for j in range(len(A)):
			if not A[j]:
				continue
			if A[j][i] == '1':
				count2 += 1
				A[j] = ''
			else:
				count1 += 1
		count += count2 * (count2 - 1) // 2
	return count
t = int(input())
for i in range(t):
	n = int(input())
	A = [int(i) for i in input().split()][:n]
	print(rock(A))


def finput():
	return float(input())

def iinput():
	return int(input())

def liinput():
	return [int(_) for _ in input().split()]

def lfinput():
	return [float(_) for _ in input().split()]
from collections import defaultdict
T = iinput()
for _ in range(T):
	map = defaultdict(int)
	n = iinput()
	a = liinput()
	a = [str(bin(x))[2:] for x in a]
	for x in a:
		map[len(x)] += 1
	ans = 0
	for v in map.values():
		ans += v * (v - 1) // 2
	print(ans)


import math

def solve():
	n = int(input().strip())
	a = list(map(int, input().strip().split()))
	groups = [0 for i in range(100)]
	ans = 0
	for i in a:
		msb_pos = int(math.log2(i))
		ans += groups[msb_pos]
		groups[msb_pos] += 1
	print(ans)
t = int(input().strip())
for _ in range(t):
	solve()


from math import log, floor
from collections import defaultdict
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	mydick = defaultdict(lambda : 0)
	for i in range(n):
		mydick[floor(log(a[i], 2))] += 1
	ans = 0
	for (keys, values) in mydick.items():
		ans += values * (values - 1) // 2
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().strip().split()))[:n]
	visited = [False] * n
	total = 0
	for i in range(31, -1, -1):
		k = 1 << i
		chance = 0
		for (j, num) in enumerate(arr):
			if visited[j] == False and num & k:
				visited[j] = True
				chance += 1
		total += chance * (chance - 1) // 2
	print(total)


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')
t = int(input())
for _ in range(t):
	n = int(input())
	a = sorted(list(map(int, input().rstrip().split(' '))))
	answer = 0
	i = 0
	setbit = 0
	while i < n:
		current = 1 << setbit
		count = 0
		while i < n and a[i] < current:
			count += 1
			i += 1
		setbit += 1
		answer += count * (count - 1) // 2
	print(answer)


import math
t = int(input())
while t > 0:
	t -= 1
	d = {}
	n = int(input())
	l1 = [int(i) for i in input().split(' ')]
	for i in l1:
		k = int(math.log(i, 2))
		if d.get(k, 'bruh') == 'bruh':
			d[k] = 1
		else:
			d[k] += 1
	count = 0
	for i in d.values():
		count += i * (i - 1) / 2
	print(int(count))


t = int(input())
from math import log
while t > 0:
	t = t - 1
	n = int(input())
	a = input().split()
	b = [0] * 32
	for i in range(0, n):
		a[i] = int(a[i])
		a[i] = int(log(a[i], 2))
		b[a[i]] += 1
	ans = 0
	for i in b:
		ans = ans + i * i - i
	print(int(ans / 2))


for t in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	maximum = 0
	for i in range(n):
		a[i] = len(bin(a[i])) - 2
		maximum = max(maximum, a[i])
	counter = [0] * maximum
	for i in a:
		counter[i - 1] += 1
	result = 0
	for i in counter:
		result += i * (i - 1) // 2
	print(result)


t = int(input())
for test in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	d = {}
	for i in range(len(arr)):
		q = len(bin(arr[i]))
		if q in d:
			d[q] += 1
		else:
			d[q] = 1
	ct = 0
	for item in d:
		if d[item] > 1:
			c = d[item]
			ct += c * (c - 1) // 2
	print(ct)


def solve():
	dp = [0] * 40
	n = int(input())
	arr = [int(v) for v in input().split()]
	ans = 0
	for v in arr:
		for i in reversed(range(32)):
			if v & 1 << i:
				dp[i] += 1
				break
	for v in arr:
		for i in reversed(range(32)):
			if v & 1 << i:
				dp[i] -= 1
				ans += dp[i]
				break
	print(ans)
t = int(input())
for _ in range(t):
	solve()


for tt in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	vals = [0 for i in range(32)]
	for i in l:
		vals[len(bin(i)[2:])] += 1
	ans = 0
	for i in vals:
		ans += i * (i - 1) // 2
	print(ans)


ans_list = []
for i in range(int(input())):
	n = int(input())
	ans = 0
	list1 = list(map(int, input().split()))
	list2 = []
	a = 0
	list1.sort()
	for x in list1:
		while x >= 2 ** a:
			a += 1
		else:
			list2.append(a)
	m = list2[0]
	b = 0
	for x in list2:
		if x == m:
			b += 1
		else:
			m = x
			ans += (b - 1) * b // 2
			b = 1
	ans += (b - 1) * b // 2
	ans_list.append(ans)
print(*ans_list, sep='\n')


from collections import defaultdict

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

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

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

def solve():
	n = getint()
	arr = getints()
	endbits = defaultdict(lambda : 0)
	ans = 0
	for i in arr:
		l = len(bin(i))
		ans += endbits[l]
		endbits[l] += 1
	print(ans)
for t in range(getint()):
	solve()


def sulen(a):
	r = 0
	while a > 0:
		r += 1
		a //= 2
	return r
cnt = [0 for i in range(50)]
T = int(input())
for Tid in range(T):
	n = int(input())
	ls = list(map(int, input().split()))
	ans = 0
	for i in range(50):
		cnt[i] = 0
	for i in range(n):
		l = sulen(ls[i])
		ans += cnt[l]
		cnt[l] += 1
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = input().split()
	a = [int(x) for x in a]
	a = sorted(a)
	bin_len = len(bin(a[0]))
	res = 0
	cur = 1
	for i in range(1, n):
		new_len = len(bin(a[i]))
		if new_len == bin_len:
			cur += 1
		else:
			res += cur * (cur - 1) // 2
			cur = 1
			bin_len = new_len
	if cur > 0:
		res += cur * (cur - 1) // 2
	print(res)


gans = []
for _ in range(int(input())):
	n = int(input())
	u = list(map(int, input().split()))
	u.sort()
	w = []
	cur = 1
	for i in range(n):
		while cur * 2 <= u[i]:
			cur *= 2
		w.append(cur)
	ans = 0
	j = 0
	for i in range(n):
		while j < n and u[j] < w[i] * 2:
			j += 1
		if j > i:
			j -= 1
		ans += j - i
	gans.append(ans)
print('\n'.join(map(str, gans)))


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	input()
	count = [0] * 40
	for v in map(int, input().split()):
		curr = 0
		while v:
			v //= 2
			curr += 1
		count[curr] += 1
	out = 0
	for v in count:
		out += v * (v - 1) // 2
	print(out)


def msbit(n):
	if n == 0:
		return 0
	msb = 0
	n = n // 2
	while n > 0:
		n = n // 2
		msb += 1
	return 1 << msb
t = int(input())
for _ in range(t):
	n = int(input())
	xyz = {}
	ans = 0
	d = []
	l = list(map(int, input().split()))
	for i in l:
		d.append(msbit(i))
	for i in d:
		if i in xyz:
			xyz[i] += 1
		else:
			xyz[i] = 1
	for j in xyz.values():
		ans += j * (j - 1) // 2
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	for i in range(n):
		a[i] = int.bit_length(a[i])
	a.sort()
	a.append(0)
	k = 1
	sum = 0
	for i in range(1, n + 1):
		if a[i] == a[i - 1]:
			k += 1
		else:
			sum += k * (k - 1) // 2
			k = 1
	print(sum)


import math
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	arr = [0] * 32
	ans = 0
	for i in l:
		x = int(math.log(i, 2))
		arr[x] += 1
		ans += arr[x] - 1
	print(ans)


import sys
from collections import defaultdict
input = sys.stdin.readline

def getMSB(a):
	for i in range(32, -1, -1):
		if a & 1 << i:
			return i
	return 0
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	bucket = defaultdict(int)
	for i in arr:
		msb = getMSB(i)
		bucket[msb] += 1
	result = 0
	for key in bucket.keys():
		item = bucket[key]
		result += int(item * (item - 1) / 2)
	print(result)


from math import inf
from collections import defaultdict

def Left_most_setbit_position(n):
	p = 1 << 30
	i = 30
	while p:
		if p & n:
			return i
		p >>= 1
		i -= 1
	return -1

def main():
	for _ in range(int(input())):
		n = int(input())
		a = list(map(int, input().split()))
		d = defaultdict(int)
		ans = 0
		for i in range(n):
			p = Left_most_setbit_position(a[i])
			ans += d[p]
			d[p] += 1
		print(ans)
main()


import os
from sys import stdin, stdout

def solve(tc):
	n = int(stdin.readline().strip())
	seq = list(map(int, stdin.readline().split()))
	dp = [0 for i in range(30)]
	for num in seq:
		msb = 0
		for k in range(30):
			if num & 1 << k > 0:
				msb = k
		dp[msb] += 1
	ans = 0
	for i in range(30):
		if dp[i] >= 2:
			ans += dp[i] * (dp[i] - 1) // 2
	print(ans)
tcs = 1
tcs = int(stdin.readline().strip())
tc = 1
while tc <= tcs:
	solve(tc)
	tc += 1


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = []
	for j in range(n):
		z = len(bin(a[j])[2:])
		b.append(z)
	frq = {}
	for item in b:
		if item in frq:
			frq[item] += 1
		else:
			frq[item] = 1
	sum = 0
	for x in frq:
		u = frq[x]
		if u >= 2:
			sum += u * (u - 1) // 2
	print(sum)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	f = 0
	c = [0] * 50
	for i in range(n):
		g = len(bin(l[i])[2:])
		f += c[g]
		c[g] += 1
	print(f)


def rockAndLever(n, array):
	binArray = []
	for x in range(n):
		binArray.append(bin(array[x])[2:])
	count = 0
	d = {}
	for x in range(n):
		if len(binArray[x]) in d:
			d[len(binArray[x])] += 1
		else:
			d[len(binArray[x])] = 1
	for x in d:
		count += d[x] * (d[x] - 1) // 2
	return count
t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	print(rockAndLever(n, l))


for __ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	l1 = []
	for i in l:
		b = bin(i)
		b = b[2:]
		l1.append(len(b))
	d = {}
	for i in l1:
		d[i] = 0
	for i in l1:
		d[i] += 1
	ans = 0
	for i in d:
		if d[i] > 1:
			ans += d[i] * (d[i] - 1) // 2
	print(ans)


import math
for _ in range(int(input())):
	c = 0
	d = {}
	n = int(input())
	num = list(map(int, input().split()))
	for i in num:
		b = int(math.log2(i))
		if b in d:
			d[b] += 1
		else:
			d[b] = 1
	for i in d.values():
		c += i * (i - 1) // 2
	print(c)


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()]
	bl = []
	for a in aa:
		bl.append(a.bit_length())
	blc = Counter(bl)
	res = 0
	for c in blc.values():
		res += c * (c - 1)
	print(res // 2)

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


import math
t = int(input())
for _ in range(t):
	d = {}
	n = int(input())
	a = [int(x) for x in input().split()]
	for i in a:
		x = int(math.log2(i))
		if x in d:
			d[x] += 1
		else:
			d[x] = 1
	c = 0
	for i in d.keys():
		if d[i] > 1:
			c += d[i] * (d[i] - 1) // 2
	print(c)


import math
t = int(input())
while t:
	n = int(input())
	li = list(map(int, input().split()))
	dic = dict()
	for i in range(1, 35):
		dic[i] = 0
	for i in range(len(li)):
		a = len(bin(li[i])[2:])
		dic[a] += 1
	ans = 0
	for i in range(1, 35):
		if dic[i] > 1:
			ans += dic[i] * (dic[i] - 1) / 2
	print(math.floor(ans))
	t -= 1


from collections import defaultdict
for t in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	dd = defaultdict(lambda : 0)
	for i in range(n):
		s = bin(a[i])
		c = len(s) - 2
		dd[c] += 1
	ans = 0
	for j in dd.values():
		ans += j * (j - 1) // 2
	print(ans)


import sys
import math
import collections
import operator as op
from collections import deque
from math import gcd, inf, sqrt, pi, cos, sin, ceil, log2
from bisect import bisect_right, bisect_left
from functools import reduce
from sys import stdin, stdout, setrecursionlimit
setrecursionlimit(2 ** 20)

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 prime_factors(n):
	i = 2
	factors = []
	while i * i <= n:
		if n % i:
			i += 1
		else:
			n //= i
			factors.append(i)
	if n > 1:
		factors.append(n)
	return list(factors)

def sumDigits(no):
	return 0 if no == 0 else int(no % 10) + sumDigits(int(no / 10))
MOD = 1000000007
PMOD = 998244353
N = 10 ** 9 + 7
LOGN = 30
T = 1
T = int(stdin.readline())
for _ in range(T):
	n = int(stdin.readline())
	a = list(map(int, stdin.readline().rstrip().split()))
	b = []
	for i in range(n):
		b.append(len(bin(a[i])[2:]))
	d = {}
	for i in b:
		if i not in d:
			d[i] = 0
		d[i] += 1
	ans = 0
	for k in d:
		if d[k] > 1:
			ans += ncr(d[k], 2)
	print(ans)


t = int(input())
for i in range(0, t):
	n = int(input())
	l = list(map(int, input().split()))
	d = dict()
	for j in range(n):
		x = bin(l[j]).replace('0b', '')
		d.setdefault(len(x), 0)
		d[len(x)] += 1
	ans = 0
	for j in d:
		ans += d[j] * (d[j] - 1) // 2
	print(ans)


def msb(x):
	for i in reversed(range(31)):
		if 2 ** i & x:
			return i
	return 0
T = int(input().strip())
for t in range(T):
	n = int(input().strip())
	a = list(map(int, input().strip().split()))
	ctr = [0] * 30
	for i in a:
		ctr[msb(i)] += 1
	count = 0
	for i in ctr:
		count += i * (i - 1) // 2
	print(count)


t = int(input())
while t:
	n = int(input())
	a = [int(x) for x in input().split()]
	cnts = [0] * 100
	count = 0
	for val in a:
		count += cnts[val.bit_length()]
		cnts[val.bit_length()] += 1
	print(count)
	t -= 1


import math
t = int(input())
while t:
	n = int(input())
	l = list(map(int, input().split()))
	c = 0
	d = {}
	for i in range(n):
		k = math.floor(math.log2(l[i]))
		d[k] = d.get(k, 0) + 1
	for i in d:
		if d[i] > 1:
			c += d[i] * (d[i] - 1) // 2
	print(c)
	t -= 1


import math
for i in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	x = []
	for i in range(n):
		s = math.floor(math.log10(l[i]) / math.log10(2))
		x.append(s)
	d = {}
	for i in x:
		if i in d:
			d[i] += 1
		else:
			d[i] = 1
	f = 0
	for value in d:
		f += d[value] * (d[value] - 1) // 2
	print(f)


t = int(input())
lista = []
liczba = 1
zmienna = 1
while liczba <= 1073741823:
	lista.append(liczba)
	zmienna = 2 * zmienna
	liczba += zmienna
for _ in range(t):
	n = int(input())
	liczby = list(map(int, input().split()))
	liczby.sort()
	wynik = 0
	x = 0
	y = 0
	a = 0
	while x < n:
		if liczby[x] <= lista[y]:
			a += 1
			if x == n - 1:
				if a > 1:
					wynik += a * (a - 1) // 2
				break
			x += 1
		else:
			y += 1
			if a > 1:
				wynik += a * (a - 1) // 2
			a = 0
	print(wynik)


t = int(input())
for _ in range(t):
	n = int(input())
	l = [int(i) for i in input().split()]
	d = {}
	for i in l:
		count = 0
		while i != 0:
			i = i >> 1
			count += 1
		if count not in d:
			d[count] = 1
		else:
			d[count] += 1
	count = 0
	for i in d.keys():
		count = count + d[i] * (d[i] - 1) // 2
	print(count)


from collections import Counter
from math import floor as _floor, log2 as _log2
import sys as _sys

def main():
	t = int(input())
	for i_t in range(t):
		(n,) = _read_ints()
		a = tuple(_read_ints())
		result = find_answer(a)
		print(result)

def _read_line():
	result = _sys.stdin.readline()
	assert result[-1] == '\n'
	return result[:-1]

def _read_ints():
	return map(int, _read_line().split(' '))

def find_answer(sequence):
	first_bits_poses = tuple(map(_floor, map(_log2, sequence)))
	groups_counter = Counter(first_bits_poses)
	result = sum((group_members_n * (group_members_n - 1) // 2 for (first_bit_index, group_members_n) in groups_counter.items()))
	return result
main()


t = int(input())

def func(a):
	a = bin(a)
	return len(a) - 2
from collections import Counter
while t > 0:
	t -= 1
	n = int(input())
	count = 0
	x = [int(x) for x in input().split()]
	arr = []
	for item in x:
		arr.append(func(item))
	c = Counter(arr)
	ans = 0
	for item in c:
		if c[item] != 1:
			ans += c[item] * (c[item] - 1) // 2
	print(ans)


t = int(input())
for T in range(t):
	n = int(input())
	num = [0 for _ in range(100)]
	a = list(map(int, input().split()))
	for x in a:
		level = 0
		while x > 0:
			level += 1
			x //= 2
		num[level] += 1
	ans = 0
	for i in range(50):
		ans += (num[i] - 1) * num[i] // 2
	print(ans)


for a in [*open(0)][2::2]:
	(s, c) = (0, [0] * 50)
	for x in map(int, a.split()):
		l = len(bin(x)[2:])
		s += c[l]
		c[l] += 1
	print(s)


import sys
import math
import bisect
import heapq
from bisect import bisect_right
from sys import stdin, stdout

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

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

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

def get_list_of_list(n):
	return [list(map(int, sys.stdin.readline().strip().split())) for _ in range(n)]

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

def Solution(arr, n):
	bits = [0 for _ in range(33)]
	for v in arr:
		c = 0
		while v > 0:
			v = v // 2
			c += 1
		bits[c] += 1
	ans = 0
	for v in bits:
		if v > 1:
			ans += v * (v - 1) // 2
	print(ans)

def main():
	for _ in range(get_int()):
		n = get_int()
		arr = get_ints_in_list()
		Solution(arr, n)
main()


import os
import sys
from io import BytesIO, IOBase
import math

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 binary(n):
	return bin(n).replace('0b', '')

def decimal(s):
	return int(s, 2)

def pow2(n):
	p = 0
	while n > 1:
		n //= 2
		p += 1
	return 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(i)
			n = n / i
	if n > 2:
		l.append(int(n))
	return l

def isPrime(n):
	if n == 1:
		return False
	else:
		root = int(n ** 0.5)
		root += 1
		for i in range(2, root):
			if n % i == 0:
				return False
		return True

def maxPrimeFactors(n):
	maxPrime = -1
	while n % 2 == 0:
		maxPrime = 2
		n >>= 1
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			maxPrime = i
			n = n / i
	if n > 2:
		maxPrime = n
	return int(maxPrime)
for xyz in range(0, int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	p = []
	for i in l:
		p.append(pow2(i))
	f = [0] * 60
	for i in p:
		f[i] += 1
	a = 0
	for i in f:
		a += i * (i - 1) // 2
	print(a)


from collections import defaultdict

def func(n, arr):
	d = defaultdict(int)
	for i in arr:
		temp = len(bin(i)) - 2
		d[temp] += 1
	res = 0
	for (i, j) in d.items():
		res += j * (j - 1) // 2
	print(res)
	return
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	func(n, arr)


t = int(input())
results = []
for k in range(t):
	n = int(input())
	vals = input().split()
	A = [int(val) for val in vals]
	d = {}
	for a in A:
		highest = len(bin(a))
		if highest in d:
			d[highest] += 1
		else:
			d[highest] = 1
	total = 0
	for count in d.values():
		total += count * (count - 1) // 2
	results.append(total)
for result in results:
	print(result)


TC = int(input())
for __ in range(TC):
	n = int(input())
	a = list(map(int, input().split()))
	cnt = [0] * 31
	ans = 0
	for x in a:
		for i in range(30, -1, -1):
			if x >> i & 1 == 1:
				ans += cnt[i]
				cnt[i] += 1
				break
	print(ans)


from sys import stdin, stdout
from collections import defaultdict
input = stdin.readline
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	d = defaultdict(int)
	for i in l:
		d[len(bin(i)) - 2] += 1
	ans = 0
	for i in d.values():
		ans += i * (i - 1) // 2
	print(ans)


def getPow2(num):
	start = num
	res = 0
	while start > 1:
		res += 1
		start = start // 2
	return res
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	ansDict = {}
	for i in arr:
		pow2 = getPow2(i)
		if pow2 in ansDict:
			ansDict[pow2] += 1
		else:
			ansDict[pow2] = 1
	res = 0
	for i in ansDict:
		n = ansDict[i]
		res += n * (n - 1) // 2
	print(res)


T = int(input())
for _ in range(T):
	N = int(input())
	A = sorted([int(a) for a in input().split()])[::-1]
	(i, j) = (0, 0)
	ans = 0
	for k in range(30)[::-1]:
		ch = 1 << k
		while j < N and A[j] >= ch:
			j += 1
		ans += (j - i) * (j - i - 1) // 2
		i = j
	print(ans)


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	ar = list(map(int, input().split()))
	br = ar.copy()
	dic = {}
	for i in range(n):
		st = 0
		while ar[i]:
			ar[i] >>= 1
			st += 1
		if st in dic:
			dic[st] += 1
		else:
			dic[st] = 1
	ans = 0
	for i in dic:
		ans += dic[i] * (dic[i] - 1) // 2
	print(ans)


def solve():
	n = int(input())
	a = [int(x) for x in input().split()]
	cnt = [0 for _ in range(32)]
	ans = 0
	for x in a:
		bit = '{:0>32b}'.format(x).find('1')
		cnt[bit] += 1
	for bit in cnt:
		ans += int(bit * (bit - 1) / 2)
	print(ans)
t = int(input())
for _ in range(t):
	solve()


from math import log2
from functools import reduce

def readGenerator():
	while True:
		tokens = input().split(' ')
		for t in tokens:
			yield t
reader = readGenerator()

def readWord():
	return next(reader)

def readInt():
	return int(next(reader))

def readFloat():
	return float(next(reader))

def readLine():
	return input()

def solve(a):
	l = [0] * 33
	for i in a:
		l[int(log2(i))] += 1
	return reduce(lambda acc, x: acc + x * (x - 1) // 2, l, 0)
t = readInt()
for _ in range(0, t):
	n = readInt()
	a = [readInt() for _ in range(0, n)]
	print(solve(a))


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	temp = [0 for i in range(32)]
	ans = 0
	for i in a[::-1]:
		b = bin(i)[2:]
		if b[0] == '1':
			ans += temp[len(b) - 1]
		temp[len(b) - 1] += i >> len(b) - 1
	print(ans)


import math
from collections import defaultdict
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	d = defaultdict(int)
	ans = 0
	for i in l:
		temp = int(math.log(i, 2))
		val = 2 ** temp
		if val in d:
			ans += d[val]
		d[val] += 1
	print(ans)


import sys
import math
import bisect
from sys import stdin, stdout
from math import gcd, floor, sqrt, log2, ceil, pi, sin, cos, acos, atan
from collections import defaultdict as dd
from bisect import bisect_left as bl, bisect_right as br
from bisect import insort
from collections import Counter
from collections import deque
from heapq import heappush, heappop, heapify
from itertools import permutations, combinations
mod = int(1000000000.0) + 7
ip = lambda : int(stdin.readline())
inp = lambda : map(int, stdin.readline().split())
ips = lambda : stdin.readline().rstrip()
out = lambda x: stdout.write(str(x) + '\n')
t = ip()
for _ in range(t):
	n = ip()
	arr = list(inp())
	dic = dd(list)
	for i in arr:
		b = bin(i)[2:]
		last = len(b)
		dic[last].append(i)
	ans = 0
	for i in dic:
		val = len(dic[i])
		ans += val * (val - 1) // 2
	out(ans)


T = int(input())
for i in range(T):
	N = int(input())
	nums = list(map(int, input().strip().split()))[:N]
	nums = sorted(nums)
	true = 0
	ch = 0
	for k in range(1, N):
		if nums[k] & nums[k - 1] >= nums[k] ^ nums[k - 1]:
			true += 1
			ch += true
		else:
			true = 0
	print(ch)


from collections import defaultdict
for _ in range(int(input())):
	msb = {}
	n = int(input())
	lis = list(map(int, input().split()))
	for i in lis:
		binary = bin(i)[2:]
		msb[len(binary)] = msb.get(len(binary), 0) + 1
	sum1 = 0
	for i in msb:
		sum1 += msb[i] * (msb[i] - 1) // 2
	print(sum1)


for _ in range(int(input())):
	n = int(input())
	(*arr,) = map(int, input().split())
	max_bits = 32
	highest = [0] * max_bits
	for v in arr:
		bit_idx = 0
		while v:
			v >>= 1
			bit_idx += 1
		highest[bit_idx] += 1
	ans = 0
	for i in range(max_bits):
		ans += highest[i] * (highest[i] - 1) // 2
	print(ans)


from collections import defaultdict, Counter
from math import ceil, floor, log2, gcd
from itertools import permutations, combinations
from functools import reduce
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	po = defaultdict(int)
	for x in a:
		b = '{0:b}'.format(x)
		po[len(b)] += 1
	out = [v * (v - 1) // 2 for (k, v) in po.items()]
	print(sum(out))


t = int(input())
for I in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	lel = dict()
	ans = 0
	for i in a:
		t = 0
		while i > 0:
			i >>= 1
			t += 1
		if lel.get(t) is None:
			lel[t] = 1
		else:
			lel[t] += 1
	for i in lel.items():
		ans += i[1] * (i[1] - 1) // 2
	print(ans)


from math import floor, log2

def get_ans(n, arr):
	bs = [0 for _ in range(36)]
	for i in arr:
		bs[floor(log2(i))] += 1
	ans = 0
	for i in bs:
		if i != 0:
			ans += i * (i - 1) // 2
	return ans
t = int(input())
anss = []
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	anss.append(get_ans(n, arr))
for i in anss:
	print(i)


def countSwaps(A, n):
	A.sort()
	DP = [0] * n
	for i in range(1, n):
		if A[i] & A[i - 1] >= A[i] ^ A[i - 1]:
			DP[i] = DP[i - 1] + 1
	return sum(DP)
T = int(input())
while T:
	N = int(input())
	A = list(map(int, input().split()))
	print(countSwaps(A, N))
	T -= 1


import math

class InOut:

	def __init__(self):
		self.dp = [0] * 35

	def inp(self):
		self.n = int(input())
		self.lst = list(map(int, input().split()))

	def solution(self):
		self.ans = 0
		for i in range(len(self.lst)):
			self.div = int(math.log2(self.lst[i]))
			self.dp[self.div] += 1
		for i in range(33):
			self.ans += int(self.dp[i] * (self.dp[i] - 1) / 2)
		return self.ans
t = int(input())
while t > 0:
	t -= 1
	obj = InOut()
	obj.inp()
	print(obj.solution())


t = int(input())
while t > 0:
	t -= 1
	n = int(input())
	arr = list(map(int, input().split()))
	dic = {}
	for i in range(n):
		temp = bin(arr[i])[2:]
		l = len(temp)
		if l in dic:
			dic[l] += 1
		else:
			dic[l] = 1
	ans = 0
	for i in dic.values():
		ans += i * (i - 1) // 2
	print(ans)


import sys
readline = sys.stdin.readline
ns = lambda : readline().rstrip()
ni = lambda : int(readline().rstrip())
nm = lambda : map(int, readline().split())
nl = lambda : list(map(int, readline().split()))
from math import ceil, log2
for _ in range(ni()):
	n = ni()
	a = nl()
	if n == 1:
		print(0)
		continue
	ans = 0
	po = [[] for uu in range(32)]
	for i in a:
		ind = int(log2(i))
		po[ind].append(i)
	(i, j) = (0, 1)
	for ii in po:
		n = len(ii)
		if n < 1:
			continue
		ans += n * (n - 1) // 2
	print(ans)


from collections import Counter
import sys

def main():
	t = Iint()
	for _ in range(t):
		n = Iint()
		a = Ilist()
		count = Counter([ai.bit_length() for ai in a])
		ans = 0
		for v in count.values():
			ans += v * (v - 1) // 2
		print(ans)
en = enumerate
FIO = 10

def debug(*args, c=6):
	pass
	print('\x1b[3{}m'.format(c), *args, '\x1b[0m', file=sys.stderr)
BUFSIZE = 8 * 1024

def Istr():
	return input()

def Iint():
	return int(input())

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

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

def Plist(li, s=' '):
	print(s.join(map(str, li)))
if not FIO:
	if __name__ == '__main__':
		main()
		exit()
else:
	from io import BytesIO, IOBase
	import os

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')
main()


import sys, math
tc = int(sys.stdin.readline())
for _ in range(tc):
	n = int(sys.stdin.readline())
	arr = list(map(int, sys.stdin.readline().split()))
	res = dict().fromkeys([i for i in range(30)], 0)
	for i in arr:
		res[int(math.log2(i))] += 1
	ans = 0
	for i in res:
		if res[i] >= 2:
			ans += res[i] * (res[i] - 1) // 2
	print(ans)


from math import log, factorial
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	highestBitIndex = [0 for i in range(32)]
	for i in arr:
		index = int(log(i, 2))
		highestBitIndex[index] += 1
	c = 0
	for i in highestBitIndex:
		if i >= 2:
			f = i * (i - 1)
			c += f // 2
	print(int(c))


try:
	for t in range(int(input())):
		n = int(input())
		l = [int(x) for x in input().split()][:n]
		lis = [0] * 40
		for i in l:
			lis[i.bit_length()] += 1
		ans = 0
		for i in lis:
			ans = ans + i * (i - 1) // 2
		print(ans)
except:
	pass


for t in range(int(input())):
	n = int(input())
	a = input().split()
	for i in range(n):
		a[i] = int(a[i])
	a.sort()
	ans = 0
	i = 0
	st = 1
	while st <= a[n - 1]:
		st = st * 2
		cnt = 0
		while i < n and a[i] < st:
			cnt = cnt + 1
			i = i + 1
		ans = ans + cnt * (cnt - 1) // 2
	print(ans)


def lowerIndex(arr, n, x):
	l = 0
	h = n - 1
	while l <= h:
		mid = int((l + h) / 2)
		if arr[mid] >= x:
			h = mid - 1
		else:
			l = mid + 1
	return l

def upperIndex(arr, n, x):
	l = 0
	h = n - 1
	while l <= h:
		mid = int((l + h) / 2)
		if arr[mid] <= x:
			l = mid + 1
		else:
			h = mid - 1
	return h

def countInRange(arr, n, x, y):
	count = 0
	count = upperIndex(arr, n, y) - lowerIndex(arr, n, x) + 1
	return count
test = int(input())
for i in range(test):
	n = int(input())
	a = [int(x) for x in input().split(' ')]
	a.sort()
	ans = 0
	j = 0
	while True:
		if pow(2, j) >= a[-1]:
			temp = countInRange(a, n, pow(2, j), pow(2, j + 1) - 1)
			if temp > 1:
				ans += temp * (temp - 1) // 2
			break
		else:
			temp = countInRange(a, n, pow(2, j), pow(2, j + 1) - 1)
			if temp > 1:
				ans += temp * (temp - 1) // 2
		j += 1
	print(ans)


from collections import defaultdict
from math import log, ceil, floor
cases = int(input())
for _ in range(cases):
	n = int(input())
	c = list(map(int, input().split()))
	w = []
	for i in range(n):
		c[i] = floor(log(c[i]) / log(2))
	d = defaultdict(int)
	for i in range(len(c)):
		d[c[i]] += 1
	tot = 0
	for (key, values) in d.items():
		tot += values * (values - 1) // 2
	print(tot)


import os
import sys
import math
import heapq
from decimal import *
from io import BytesIO, IOBase
from collections import defaultdict, deque

def r():
	return int(input())

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

def rl():
	return list(map(int, input().split()))
for _ in range(r()):
	n = r()
	a = rl()
	b = defaultdict(int)
	for i in a:
		b[len(bin(i))] += 1
	ans = 0
	for (i, j) in b.items():
		ans += j * (j - 1) // 2
	print(ans)


from collections import defaultdict
for _ in range(int(input())):
	n = int(input())
	a = [*map(int, input().split())]
	l = defaultdict(int)
	for i in a:
		l[len(bin(i)) - 2] += 1
	l = sorted(l.items())
	ans = 0
	for i in range(len(l)):
		ans += l[i][1] * (l[i][1] - 1) // 2
	print(ans)


def countBits(n):
	count = 0
	while n:
		count += 1
		n >>= 1
	return count
for _ in range(int(input())):
	n = int(input())
	a = sorted(list(map(int, input().split())))
	ans = 0
	frequency = [0] * 32
	for i in range(0, len(a)):
		p = countBits(a[i])
		frequency[p] += 1
	ans = 0
	for i in range(0, len(frequency)):
		ans += frequency[i] * (frequency[i] - 1) // 2
	print(ans)


import sys
input = sys.stdin.buffer.readline
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	a = [len(bin(val)) - 2 for val in a]
	cnt = [0] * 35
	for val in a:
		cnt[val] += 1
	ans = 0
	for i in cnt:
		ans += i * (i - 1) // 2
	print(ans)


import sys
import os
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')
mod = 10 ** 9 + 7

def lcm(x, y):
	return x * y / gcd(x, y)

def fact(x, mod=mod):
	ans = 1
	for i in range(1, x + 1):
		ans = ans * i % mod
	return ans

def arr2D(n, m, default=0):
	lst = []
	for i in range(n):
		temp = [default] * m
		lst.append(temp)
	return lst

def sortDictV(x):
	return {k: v for (k, v) in sorted(x.items(), key=lambda item: item[1])}
from math import log, floor

def solve(n, lst):
	cnt = [0] * 31
	for i in range(n):
		cnt[floor(log(lst[i], 2))] += 1
	pairs = 0
	for i in range(31):
		pairs += cnt[i] * (cnt[i] - 1) // 2
	return pairs
for _ in range(int(input())):
	n = int(input())
	lst = list(map(int, input().split()))
	print(solve(n, lst))


import sys
import heapq
import copy
import math
import decimal
input = sys.stdin.readline

def inp():
	return int(input())

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

def insr():
	s = input()
	return list(s[:len(s) - 1])

def insr2():
	s = input()
	s = list(s[:len(s) - 1])
	s = [ord(s[i]) - ord('a') for i in range(len(s))]
	return s

def invr():
	return map(int, input().split())
ans = 0

def pr_list(a):
	print(*a, sep=' ')

def swap(a, b):
	temp = a
	a = b
	b = temp
	return [a, b]

def pri(a, b):
	s = '? ' + str(a) + ' ' + str(b)
	print(s)
	sys.stdout.flush()

def main():
	tests = inp()
	mod = 1000000007
	limit = 10 ** 18
	ans = 0
	stack = []
	hashm = {}
	arr = []
	heapq.heapify(arr)
	for test in range(tests):
		n = inp()
		a = inlt()
		hashm = [0 for i in range(33)]
		ans = 0

		def find_msb(val):
			itr = 0
			while val != 0:
				val = val // 2
				itr += 1
			return itr
		for i in range(n):
			k = find_msb(a[i])
			ans = ans + hashm[k]
			hashm[k] += 1
		print(ans)
main()


import collections
import math
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	d = collections.defaultdict(int)
	for el in l:
		x = math.floor(math.log(el, 2))
		d[x] += 1
	ans = 0
	for key in d.keys():
		x = d[key]
		ans += x * (x - 1) // 2
	print(ans)


import sys
t = int(sys.stdin.readline())
for _ in range(t):
	n = int(sys.stdin.readline())
	lst = list(map(int, sys.stdin.readline().split()))
	exc = n * (n - 1) // 2
	len_set = len(set(lst))
	if n > 1 and len_set == 1:
		print(exc)
		continue
	sums = 0
	lst.sort()
	c = 2
	cnt = 0
	while c <= lst[0]:
		c *= 2
	for i in range(n):
		if lst[i] < c:
			cnt += 1
		else:
			while c <= lst[i]:
				c *= 2
			sums += cnt * (cnt - 1) // 2
			cnt = 1
	sums += cnt * (cnt - 1) // 2
	print(sums)


from sys import stdin, stdout
import math
ans = []

def main():
	n = int(stdin.readline())
	combo = list(map(int, stdin.readline().split()))
	count = 0
	dictt = {}
	for item in combo:
		x = math.floor(math.log(item, 2) + 1)
		dictt[x] = dictt.get(x, 0) + 1
	for v in dictt.values():
		count += v * (v - 1) / 2
	ans.append(str(int(count)))
for _ in range(int(stdin.readline())):
	main()
stdout.write('\n'.join(ans))


from sys import stdin, stdout
from math import gcd, sqrt, factorial, pi, inf
from collections import deque, defaultdict
input = stdin.readline
R = lambda : map(int, input().split())
I = lambda : int(input())
S = lambda : input().rstrip('\n')
L = lambda : list(R())
P = lambda x: stdout.write(x)
lcm = lambda x, y: x * y // gcd(x, y)
hg = lambda x, y: (y + x - 1) // x * x
pw = lambda x: 1 if x == 1 else 1 + pw(x // 2)
chk = lambda x: chk(x // 2) if not x % 2 else True if x == 1 else False
sm = lambda x: (x ** 2 + x) // 2
N = 10 ** 9 + 7
for _ in range(I()):
	n = I()
	v = [0] * 50
	for i in R():
		j = 0
		while i != 1:
			i //= 2
			j += 1
		v[j] += 1
	ans = 0
	for i in v:
		ans += (i * i - i) // 2
	print(ans)


def fun(num):
	bt = 0
	for k in range(32):
		if num & 1 << k:
			bt = max(bt, k)
	return bt
for _ in range(int(input())):
	n = int(input())
	lst = list(map(int, input().split()))
	for x in range(len(lst)):
		lst[x] = fun(lst[x])
	ans = 0
	for k in range(32):
		x = lst.count(k)
		ans += x * (x - 1) // 2
	print(ans)


from sys import stdin
import math
A = int(stdin.readline())
for t in range(0, A):
	B = int(stdin.readline())
	C = list(map(int, stdin.readline().split()))
	D = list()
	for y in range(0, B):
		D.append(int(math.floor(math.log2(C[y]))))
	E = set(D)
	a = 0
	for u in E:
		x = D.count(u)
		a += x * (x - 1) / 2
	print(int(a))


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	l = l + [0, 0, 0, 0, 0]
	i = 0
	k = {}
	for j in range(0, 41):
		k[j] = 0
	while i <= n:
		c = 0
		m = l[i]
		while m:
			m >>= 1
			c += 1
		i += 1
		k[c] += 1
	ans = 0
	for i in range(1, 41):
		ans += int(k[i] * (k[i] - 1) / 2)
	print(ans)


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


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	d = {}
	for i in range(n):
		k = len(bin(a[i]))
		if k in d:
			d[k] += 1
		else:
			d[k] = 1
	ans = 0
	for i in d:
		ans += d[i] * (d[i] - 1) // 2
	print(ans)


POW2 = [1]
MAX_POW = 30
for i in range(MAX_POW):
	POW2.append(POW2[-1] * 2)

def get_input():
	return input().split(' ')

def get_int_input():
	return list(map(int, get_input()))

def nC2(x):
	x = int(x)
	return int(x * (x - 1) / 2)

def rock_and_lever(nums):
	count_of_pow2 = [0] * MAX_POW
	for i in nums:
		for p in range(MAX_POW + 1):
			if i < POW2[p]:
				count_of_pow2[p - 1] += 1
				break
	return sum([nC2(count) for count in count_of_pow2])

def main():
	T = get_int_input()[0]
	for _ in range(T):
		n = get_int_input()[0]
		nums = get_int_input()
		print(rock_and_lever(nums))
main()


def nCr(n, k):
	if k > n:
		return 0
	if k > n - k:
		k = n - k
	res = 1
	for i in range(k):
		res = res * (n - i)
		res = res / (i + 1)
	return int(res)
for _ in range(int(input())):
	N = int(input())
	a = list(map(int, input().split()))
	d = {}
	for i in range(len(a)):
		a[i] = bin(a[i])[2:]
	for i in a:
		if len(i) not in d:
			d[len(i)] = 1
		else:
			d[len(i)] += 1
	count = 0
	for (k, v) in d.items():
		count += nCr(v, 2)
	print(count)


from __future__ import division, print_function
from fractions import Fraction
import sys
import os
from io import BytesIO, IOBase
from heapq import *
from math import gcd, factorial, floor, ceil
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
mod = 10 ** 9 + 7

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 zerolist(n):
	return [0] * n

def nextline():
	out('\n')

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

def printlist(a):
	for p in range(0, len(a)):
		out(str(a[p]) + ' ')

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

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

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 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

def solve(case):
	n = int(inp())
	a = lis()
	ans = [0] * n
	for i in range(n):
		curr = bin(a[i])
		ans[i] = len(curr)
	cnt = {}
	for i in range(n):
		if ans[i] in cnt:
			cnt[ans[i]] += 1
		else:
			cnt[ans[i]] = 1
	ans = 0
	for i in cnt:
		ans += cnt[i] * (cnt[i] - 1) // 2
	print(ans)
testcase(int(inp()))


def ms(n):
	if n == 0:
		return 0
	msb = 0
	n = int(n / 2)
	while n > 0:
		n = int(n / 2)
		msb += 1
	return 1 << msb
for _ in range(int(input())):
	n = int(input())
	d = {}
	s = 0
	x = []
	l = list(map(int, input().split()))
	for i in l:
		x.append(ms(i))
	for i in x:
		if i in d:
			d[i] += 1
		else:
			d[i] = 1
	for j in d.values():
		d = j * (j - 1) // 2
		s = s + d
	print(s)


def solve(n, A):
	cnt = [0] * 32
	for i in range(n):
		bit = 0
		while 1 << bit * 1 <= A[i]:
			bit += 1
		cnt[bit] += 1
	ans = 0
	for i in range(32):
		ans += cnt[i] * (cnt[i] - 1) // 2
	return ans
t = int(input())
while t:
	res = []
	n = int(input())
	in_ = map(int, input().split(' '))
	for x in in_:
		res.append(int(x))
	print(solve(n, res))
	t -= 1


from collections import Counter
for z in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	sum = 0
	for i in range(n):
		l[i] = len(bin(l[i]))
	s = Counter(l)
	for i in s.values():
		if i > 1:
			sum += i * (i - 1) / 2
	print(int(sum))


N = int(input())
l = []
for i in range(N):
	n = int(input())
	f = map(int, input().split())
	f = list(f)
	d = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30]
	d2 = dict.fromkeys(d, 0)
	for k in range(n):
		f[k] = bin(f[k])
		g = len(f[k]) - 2
		d2[g] = d2[g] + 1
	sum = 0
	for item in d2.values():
		sum = sum + item * (item - 1) / 2
	sum = int(sum)
	l.append(sum)
for i in range(N):
	print(l[i])


import collections

def setBitNumber(n):
	if n == 0:
		return 0
	msb = 0
	n = int(n / 2)
	while n > 0:
		n = int(n / 2)
		msb += 1
	return 1 << msb
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	a_list = []
	result = 0
	for i in range(n):
		a_list.append(setBitNumber(l[i]))
	counter = collections.Counter(a_list)
	b = list(counter.values())
	for i in b:
		if i > 1:
			result += i * (i - 1) // 2
	print(result)


def sol():
	n = int(input())
	a = list(map(int, input().split()))
	res = 0
	for b in range(31, -1, -1):
		x = 1 << b
		cnt = 0
		for idx in range(n):
			if a[idx] & x:
				cnt += 1
				a[idx] = 0
		res += cnt * (cnt - 1) // 2
	print(res)

def sol_args(r, g, b, w):
	return

def main():
	T = int(input())
	for t in range(T):
		sol()
main()


from __future__ import division, print_function
import os
import sys
from io import BytesIO, IOBase

def main():

	def solve():

		def msbIdx(num):
			return num.bit_length() - 1
		n = int(input())
		frequency = [0] * 30
		for num in map(lambda x: int(x), input().split()):
			frequency[msbIdx(num)] += 1
		answer = 0
		for i in range(30):
			answer += frequency[i] * (frequency[i] - 1) // 2
		print(answer)
	t = int(input())
	for i in range(t):
		solve()
BUFFSIZE = 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, BUFFSIZE))
			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, BUFFSIZE))
			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')
input = lambda : sys.stdin.readline().rstrip('\r\n')

def print(*args, **kwargs):
	sep = kwargs.pop('sep', ' ')
	file = kwargs.pop('file', sys.stdout)
	atStart = True
	for x in args:
		if not atStart:
			file.write(sep)
		file.write(str(x))
		atStart = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
main()


t = int(input())
while t > 0:
	t -= 1
	n = int(input())
	a = list(map(int, input().split()))
	c = [0] * 100
	res = 0
	for i in a:
		c[len(bin(i))] += 1
	for i in c:
		res += i * (i - 1) // 2
	print(res)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	d = dict()
	for i in range(len(a)):
		a[i] = bin(a[i])[2:]
		if a[i][0] == '1':
			d[len(a[i])] = d.get(len(a[i]), 0) + 1
	count = 0
	for (i, j) in d.items():
		count += d[i] * (d[i] - 1) // 2
	print(count)


import math
for test in range(int(input())):
	pocet = int(input())
	a = list(map(int, input().split(' ')))
	l = [math.floor(math.log2(m)) for m in a]
	pocty = [l.count(x) for x in set(l)]
	res = 0
	for poc in pocty:
		res += poc * (poc - 1) / 2
	print(str(int(res)))


import math, sys, os, collections
if os.path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
for testcase in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	count = 0
	num = []
	for i in range(len(arr)):
		num.append(len(bin(arr[i])[2:]))
	dic = collections.defaultdict(int)
	for i in num:
		if i in dic:
			dic[i] += 1
		else:
			dic[i] = 0
	for (key, val) in dic.items():
		count += val * (val + 1) // 2
	print(count)


for _ in range(int(input())):
	n = int(input())
	lst = list(map(int, input().split(' ')))
	freq = [0] * 31
	for x in lst:
		freq[len(bin(x)[2:])] += 1
	ans = 0
	for x in freq:
		if x > 1:
			ans += x * (x - 1) // 2
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	bits = [0] * 31
	for i in range(n):
		t = 0
		for j in range(31):
			if 1 << j & a[i] > 0:
				t = j
		bits[t] += 1
	ans = 0
	for i in range(31):
		ans += bits[i] * (bits[i] - 1) // 2
	print(ans)


import sys
import os
import math
import copy
from math import gcd
from bisect import bisect
from io import BytesIO, IOBase
from math import sqrt, floor, factorial, gcd, log, ceil
from collections import deque, Counter, defaultdict
from itertools import permutations, combinations
from itertools import accumulate

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

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

def Lstr():
	return list(sys.stdin.readline().strip())

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

def Mstr():
	return map(str, sys.stdin.readline().strip().split())

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

def Hash():
	return dict()

def Mod():
	return 1000000007

def Ncr(n, r, p):
	return fact[n] * (ifact[r] * ifact[n - r] % p) % p

def Most_frequent(list):
	return max(set(list), key=list.count)

def Mat2x2(n):
	return [List() for _ in range(n)]

def Lcm(x, y):
	return x * y // gcd(x, y)

def dtob(n):
	return bin(n).replace('0b', '')

def btod(n):
	return int(n, 2)

def common(l1, l2):
	return set(l1).intersection(l2)

def solution():
	for _ in range(Int()):
		n = Int()
		a = List()
		c = 0
		ans = 0
		a.sort()
		for i in range(n - 1):
			if a[i] & a[i + 1] >= a[i] ^ a[i + 1]:
				c += 1
				ans += c
			else:
				c = 0
		print(ans)
solution()


from math import log2

def solve():
	n = int(input())
	a = [int(x) for x in input().split()]
	cnt = [0 for _ in range(32)]
	ans = 0
	for x in a:
		bit = int(log2(x))
		cnt[bit] += 1
	for bit in cnt:
		ans += int(bit * (bit - 1) / 2)
	print(ans)
t = int(input())
for _ in range(t):
	solve()


def setbit(val, pos):
	return val >> pos & 1
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	d = {}
	for j in arr:
		for i in range(31, -1, -1):
			if setbit(j, i):
				if i in d:
					d[i] += 1
				else:
					d[i] = 1
				break
	ans = 0
	for i in d:
		temp = d[i]
		ans += temp * (temp - 1) // 2
	print(ans)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	aux = [0] * 31
	for i in arr:
		for j in range(31):
			if 2 ** j > i:
				aux[j - 1] += 1
				break
	count = 0
	for j in aux:
		count += j * (j - 1)
	print(int(count / 2))


testcase = int(input())
for i in range(testcase):
	n = int(input())
	li = list(map(int, input().split()))[:n]
	d = {}
	for j in li:
		l = len(bin(j))
		if l in d:
			d[l] += 1
		else:
			d[l] = 1
	res = 0
	for k in d:
		res += d[k] * (d[k] - 1) // 2
	print(res)


def f(n):
	n = n
	iter = 0
	while n != 0:
		n = n >> 1
		iter += 1
	return iter
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	new = [0] * 30
	am = []
	for i in range(n):
		d = f(arr[i])
		if d != 0:
			new[d - 1] += 1
	c = 0
	for i in range(30):
		c += new[i] * (new[i] - 1) // 2
	print(c)


def countBits(n):
	count = 0
	while n:
		count += 1
		n >>= 1
	return count
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	d = {}
	for i in l:
		num = countBits(i)
		if num in d:
			d[num] += 1
		else:
			d[num] = 1
	ans = 0
	for i in d:
		ans += d[i] * (d[i] - 1) // 2
	print(ans)


from math import log
for _ in range(int(input())):
	n = int(input())
	lst = [int(i) for i in input().split()]
	b = [0] * 32
	for i in range(n):
		b[int(log(lst[i], 2))] += 1
	res = 0
	for i in b:
		res += i * (i - 1) // 2
	print(res)


from functools import cmp_to_key
from collections import deque
from heapq import heappush, heappop
from math import log, ceil
import sys
import os
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')
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))

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()

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

def ii():
	return int(inp())

def li(lag=0):
	l = list(map(int, inp().split()))
	if lag != 0:
		for i in range(len(l)):
			l[i] += lag
	return l

def mi(lag=0):
	matrix = list()
	for i in range(n):
		matrix.append(li(lag))
	return matrix

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

def print_list(lista, space=' '):
	print(space.join(map(str, lista)))

class UnionFind:

	def __init__(self, n):
		self.parent = list(range(n))
		self.size = [1] * n
		self.num_sets = n

	def find(self, a):
		to_update = []
		while a != self.parent[a]:
			to_update.append(a)
			a = self.parent[a]
		for b in to_update:
			self.parent[b] = a
		return self.parent[a]

	def merge(self, a, b):
		a = self.find(a)
		b = self.find(b)
		if a == b:
			return
		if self.size[a] < self.size[b]:
			(a, b) = (b, a)
		self.num_sets -= 1
		self.parent[b] = a
		self.size[a] += self.size[b]

	def set_size(self, a):
		return self.size[self.find(a)]

	def __len__(self):
		return self.num_sets

def bisect_left(a, x):
	left = 0
	right = len(a)
	while left < right:
		mid = (left + right) // 2
		if a[mid] < x:
			left = mid + 1
		else:
			right = mid
	return left

def bisect_right(a, x):
	left = 0
	right = len(a)
	while left < right:
		mid = (left + right) // 2
		if a[mid] > x:
			right = mid
		else:
			left = mid + 1
	return left

def bisect_elements(a, x):
	return bisect_right(a, x) - bisect_left(a, x)

def custom_sort(lista):

	def cmp(x, y):
		if x + y > y + x:
			return 1
		else:
			return -1
	return sorted(lista, key=cmp_to_key(cmp))
MOD = 10 ** 9 + 7
maxN = 10 ** 5
FACT = [0] * maxN

def add(x, y):
	return (x + y) % MOD

def multiply(x, y):
	return x * y % MOD

def power(x, y):
	if y == 0:
		return 1
	elif y % 2:
		return multiply(x, power(x, y - 1))
	else:
		a = power(x, y // 2)
		return multiply(a, a)

def inverse(x):
	return power(x, MOD - 2)

def divide(x, y):
	return multiply(x, inverse(y))

def allFactorials():
	FACT[0] = 1
	for i in range(1, maxN):
		FACT[i] = multiply(i, FACT[i - 1])

def coeffBinom(n, k):
	if n < k:
		return 0
	return divide(FACT[n], multiply(FACT[k], FACT[n - k]))

def primes(N):
	smallest_prime = [1] * (N + 1)
	prime = []
	smallest_prime[0] = 0
	smallest_prime[1] = 0
	for i in range(2, N + 1):
		if smallest_prime[i] == 1:
			prime.append(i)
			smallest_prime[i] = i
		j = 0
		while j < len(prime) and i * prime[j] <= N:
			smallest_prime[i * prime[j]] = min(prime[j], smallest_prime[i])
			j += 1
	return (prime, smallest_prime)

def gcd(a, b):
	a = abs(a)
	b = abs(b)
	(s, t, r) = (0, 1, b)
	(old_s, old_t, old_r) = (1, 0, a)
	while r != 0:
		quotient = old_r // r
		(old_r, r) = (r, old_r - quotient * r)
		(old_s, s) = (s, old_s - quotient * s)
		(old_t, t) = (t, old_t - quotient * t)
	return (old_r, old_s, old_t)

def create_graph(n, m, undirected=1, unweighted=1):
	graph = [[] for i in range(n)]
	if unweighted:
		for i in range(m):
			[x, y] = li(lag=-1)
			graph[x].append(y)
			if undirected:
				graph[y].append(x)
	else:
		for i in range(m):
			[x, y, w] = li(lag=-1)
			w += 1
			graph[x].append([y, w])
			if undirected:
				graph[y].append([x, w])
	return graph

def create_tree(n, unweighted=1):
	children = [[] for i in range(n)]
	if unweighted:
		for i in range(n - 1):
			[x, y] = li(lag=-1)
			children[x].append(y)
			children[y].append(x)
	else:
		for i in range(n - 1):
			[x, y, w] = li(lag=-1)
			w += 1
			children[x].append([y, w])
			children[y].append([x, w])
	return children

def create_edges(m, unweighted=0):
	edges = list()
	if unweighted:
		for i in range(m):
			edges.append(li(lag=-1))
	else:
		for i in range(m):
			[x, y, w] = li(lag=-1)
			w += 1
			edges.append([w, x, y])
	return edges

def dist(tree, n, A, B=-1):
	s = [[A, 0]]
	(massimo, massimo_nodo) = (0, 0)
	distanza = -1
	v = [-1] * n
	while s:
		(el, dis) = s.pop()
		if dis > massimo:
			massimo = dis
			massimo_nodo = el
		if el == B:
			distanza = dis
		for child in tree[el]:
			if v[child] == -1:
				v[child] = 1
				s.append([child, dis + 1])
	return (massimo, massimo_nodo, distanza)

def diameter(tree):
	(_, foglia, _) = dist(tree, n, 0)
	(diam, _, _) = dist(tree, n, foglia)
	return diam

def dfs(graph, n, A):
	v = [-1] * n
	s = [[A, 0]]
	v[A] = 0
	while s:
		(el, dis) = s.pop()
		for child in graph[el]:
			if v[child] == -1:
				v[child] = dis + 1
				s.append([child, dis + 1])
	return v

def bfs(graph, n, A):
	v = [-1] * n
	s = deque()
	s.append([A, 0])
	v[A] = 0
	while s:
		(el, dis) = s.popleft()
		for child in graph[el]:
			if v[child] == -1:
				v[child] = dis + 1
				s.append([child, dis + 1])
	return v

def connected(graph, n):
	v = dfs(graph, n, 0)
	for el in v:
		if el == -1:
			return False
	return True

def topological(graph, n):
	indegree = [0] * n
	for el in range(n):
		for child in graph[el]:
			indegree[child] += 1
	s = deque()
	for el in range(n):
		if indegree[el] == 0:
			s.append(el)
	order = []
	while s:
		el = s.popleft()
		order.append(el)
		for child in graph[el]:
			indegree[child] -= 1
			if indegree[child] == 0:
				s.append(child)
	if n == len(order):
		return (False, order)
	else:
		return (True, [])

def bipartite(graph, n):
	color = [-1] * n
	color[0] = 0
	s = [0]
	while s:
		el = s.pop()
		for child in graph[el]:
			if color[child] == color[el]:
				return False
			if color[child] == -1:
				s.append(child)
			color[child] = 1 - color[el]
	return True

def dijkstra(graph, n, A):
	dist = [float('inf') for i in range(n)]
	prev = [-1 for i in range(n)]
	dist[A] = 0
	pq = []
	heappush(pq, [0, A])
	while pq:
		[d_v, v] = heappop(pq)
		if d_v != dist[v]:
			continue
		for (to, w) in graph[v]:
			if dist[v] + w < dist[to]:
				dist[to] = dist[v] + w
				prev[to] = v
				heappush(pq, [dist[to], to])
	return (dist, prev)

def dijkstra_0_1(graph, n, A):
	dist = [float('inf') for i in range(n)]
	dist[A] = 0
	p = deque()
	p.append(A)
	while p:
		v = p.popleft()
		for (to, w) in graph[v]:
			if dist[v] + w < dist[to]:
				dist[to] = dist[v] + w
			if w == 1:
				q.append(to)
			else:
				q.appendleft(to)
	return dist

def floyd_warshall(graph, n):
	dist = [[float('inf') for _ in range(n)] for _ in range(n)]
	for i in range(n):
		dist[i][i] = 0
		for (child, d) in graph[i]:
			dist[i][child] = d
			dist[child][i] = d
	for k in range(n):
		for i in range(n):
			for j in range(j):
				dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j])
	return dist

def minimum_spanning_tree(edges, n):
	edges = sorted(edges)
	union_find = UnionFind(n)
	used_edges = list()
	for (w, x, y) in edges:
		if union_find.find(x) != union_find.find(y):
			union_find.merge(x, y)
			used_edges.append([w, x, y])
	return used_edges

def parents_children_root_unrooted_tree(tree, n, root=0):
	q = deque()
	visited = [0] * n
	parent = [-1] * n
	children = [[] for i in range(n)]
	q.append(root)
	while q:
		all_done = 1
		visited[q[0]] = 1
		for child in tree[q[0]]:
			if not visited[child]:
				all_done = 0
				q.appendleft(child)
		if all_done:
			for child in tree[q[0]]:
				if parent[child] == -1:
					parent[q[0]] = child
					children[child].append(q[0])
			q.popleft()
	return (parent, children)

def all_longest_path_passing_from_node(parent, children, n):
	q = deque()
	visited = [len(children[i]) for i in range(n)]
	downwards = [[0, 0] for i in range(n)]
	upward = [1] * n
	longest_path = [1] * n
	for i in range(n):
		if not visited[i]:
			q.append(i)
			downwards[i] = [1, 0]
	while q:
		node = q.popleft()
		if parent[node] != -1:
			visited[parent[node]] -= 1
			if not visited[parent[node]]:
				q.append(parent[node])
		else:
			root = node
		for child in children[node]:
			downwards[node] = sorted([downwards[node][0], downwards[node][1], downwards[child][0] + 1], reverse=True)[0:2]
	s = [node]
	while s:
		node = s.pop()
		if parent[node] != -1:
			if downwards[parent[node]][0] == downwards[node][0] + 1:
				upward[node] = 1 + max(upward[parent[node]], downwards[parent[node]][1])
			else:
				upward[node] = 1 + max(upward[parent[node]], downwards[parent[node]][0])
		longest_path[node] = downwards[node][0] + downwards[node][1] + upward[node] - min([downwards[node][0], downwards[node][1], upward[node]]) - 1
		for child in children[node]:
			s.append(child)
	return longest_path

def finding_ancestors(parent, queries, n):
	steps = int(ceil(log(n, 2)))
	ancestors = [[-1 for i in range(n)] for j in range(steps)]
	ancestors[0] = parent
	for i in range(1, steps):
		for node in range(n):
			if ancestors[i - 1][node] != -1:
				ancestors[i][node] = ancestors[i - 1][ancestors[i - 1][node]]
	result = []
	for (node, k) in queries:
		ans = node
		if k >= n:
			ans = -1
		i = 0
		while k > 0 and ans != -1:
			if k % 2:
				ans = ancestors[i][ans]
			k = k // 2
			i += 1
		result.append(ans)
	return result

def prefix_sum(arr):
	r = [0] * (len(arr) + 1)
	for (i, el) in enumerate(arr):
		r[i + 1] = r[i] + el
	return r

def nearest_from_the_left_smaller_elements(arr):
	n = len(arr)
	res = [-1] * n
	s = []
	for (i, el) in enumerate(arr):
		while s and s[-1] >= el:
			s.pop()
		if s:
			res[i] = s[-1]
		s.append(el)
	return res

def sliding_window_minimum(arr, k):
	res = []
	q = deque()
	for (i, el) in enumerate(arr):
		while q and arr[q[-1]] >= el:
			q.pop()
		q.append(i)
		while q and q[0] <= i - k:
			q.popleft()
		if i >= k - 1:
			res.append(arr[q[0]])
	return res

def f(n):
	return n * (n - 1) // 2
for test in range(ii()):
	n = ii()
	a = li()
	power = [0] * 40
	for el in a:
		c = 0
		while el > 0:
			c += 1
			el = el // 2
		power[c] += 1
	s = 0
	for el in power:
		s += f(el)
	print(s)


from collections import Counter

def solve(a, n):
	arr = []
	ans = 0
	for i in range(n):
		arr.append(len(bin(a[i])) - 2)
	p = Counter(arr)
	for i in p:
		ans += p[i] * (p[i] - 1) // 2
	return ans
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	print(solve(a, n))


import sys
import math

def master():
	(n, l) = (II(), LI())
	s = 64
	ans = 0
	for i in range(len(l)):
		l[i] = str(bin(l[i])).rjust(s, '0')
	for i in range(s):
		a = 0
		for j in range(len(l)):
			if l[j][i] == '1':
				a += 1
				l[j] = '0' * s
		ans += a * (a - 1) / 2
	print(int(ans))
	return

def main():
	_v_ = II()
	for ___ in range(_v_):
		master()
sys.setrecursionlimit(1000000)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep='\n')
p2S = lambda x: print(*x, sep=' ')

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

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

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

def LLI(rows_number):
	return [LI() for _ in range(rows_number)]

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


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


import os
import sys
import math
from io import BytesIO, IOBase
from fractions import Fraction
import collections
from itertools import permutations
from collections import defaultdict
from collections import deque
import threading
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')

class Factorial:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorials = [1, 1]
		self.invModulos = [0, 1]
		self.invFactorial_ = [1, 1]

	def calc(self, n):
		if n <= -1:
			print('Invalid argument to calculate n!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.factorials):
			return self.factorials[n]
		nextArr = [0] * (n + 1 - len(self.factorials))
		initialI = len(self.factorials)
		prev = self.factorials[-1]
		m = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * i % m
		self.factorials += nextArr
		return self.factorials[n]

	def inv(self, n):
		if n <= -1:
			print('Invalid argument to calculate n^(-1)')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		p = self.MOD
		pi = n % p
		if pi < len(self.invModulos):
			return self.invModulos[pi]
		nextArr = [0] * (n + 1 - len(self.invModulos))
		initialI = len(self.invModulos)
		for i in range(initialI, min(p, n + 1)):
			next = -self.invModulos[p % i] * (p // i) % p
			self.invModulos.append(next)
		return self.invModulos[pi]

	def invFactorial(self, n):
		if n <= -1:
			print('Invalid argument to calculate (n^(-1))!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.invFactorial_):
			return self.invFactorial_[n]
		self.inv(n)
		nextArr = [0] * (n + 1 - len(self.invFactorial_))
		initialI = len(self.invFactorial_)
		prev = self.invFactorial_[-1]
		p = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * self.invModulos[i % p] % p
		self.invFactorial_ += nextArr
		return self.invFactorial_[n]

class Combination:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorial = Factorial(MOD)

	def ncr(self, n, k):
		if k < 0 or n < k:
			return 0
		k = min(k, n - k)
		f = self.factorial
		return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	d = dict()
	for i in a:
		m = 0
		while i > 0:
			m += 1
			i //= 2
		if m in d:
			d[m] += 1
		else:
			d[m] = 1
	r = 0
	for i in d:
		r += d[i] * (d[i] - 1) // 2
	print(r)


from itertools import combinations as co
for _ in range(int(input())):
	n = int(input())
	m = list(map(int, input().split()))
	i = 2
	l = [0] * 31
	for i in m:
		if i < 2:
			l[0] += 1
		elif i < 4:
			l[1] += 1
		elif i < 8:
			l[2] += 1
		elif i < 16:
			l[3] += 1
		elif i < 32:
			l[4] += 1
		elif i < 64:
			l[5] += 1
		elif i < 128:
			l[6] += 1
		elif i < 256:
			l[7] += 1
		elif i < 512:
			l[8] += 1
		elif i < 1024:
			l[9] += 1
		elif i < 2048:
			l[10] += 1
		elif i < 4096:
			l[11] += 1
		elif i < 8192:
			l[12] += 1
		elif i < 16384:
			l[13] += 1
		elif i < 32768:
			l[14] += 1
		elif i < 65536:
			l[15] += 1
		elif i < 131072:
			l[16] += 1
		elif i < 262144:
			l[17] += 1
		elif i < 524288:
			l[18] += 1
		elif i < 1048576:
			l[19] += 1
		elif i < 2097152:
			l[20] += 1
		elif i < 4194304:
			l[21] += 1
		elif i < 8388608:
			l[22] += 1
		elif i < 16777216:
			l[23] += 1
		elif i < 33554432:
			l[24] += 1
		elif i < 67108864:
			l[25] += 1
		elif i < 134217728:
			l[26] += 1
		elif i < 268435456:
			l[27] += 1
		elif i < 536870912:
			l[28] += 1
		elif i < 1073741824:
			l[29] += 1
	s = 0
	for i in l:
		s += (i - 1) * i // 2
	print(s)


import math
for _ in range(int(input())):
	n = int(input())
	ls = [int(a) for a in input().split()]
	ls.sort()
	an = [0] * (math.ceil(math.log2(ls[-1])) + 1)
	for i in range(n):
		an[int(math.log2(ls[i]))] += 1
	ans = 0
	for a in an:
		ans += int(a * (a - 1) / 2)
	print(ans)


import math
for z in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	temp = []
	for i in range(n):
		temp.append(len(bin(a[i])))
	p = []
	for j in range(1, 35):
		p.append(j)
	ans = 0
	for ii in p:
		cnt = temp.count(ii)
		ans += int(cnt * (cnt - 1) / 2)
	print(ans)


def solve_case():
	n = int(input())
	a = list(map(int, input().split()))
	m = {}
	ans = 0
	for i in a:
		l = len(bin(i))
		c = m.get(l, 0)
		m[l] = c + 1
		ans += c
	return ans

def main():
	tests = int(input())
	for t in range(tests):
		print(solve_case())
main()


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


import math
for _ in range(int(input())):
	n = input()
	a = [int(i) for i in input().split()]
	d = {0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0, 10: 0, 11: 0, 12: 0, 13: 0, 14: 0, 15: 0, 16: 0, 17: 0, 18: 0, 19: 0, 20: 0, 21: 0, 22: 0, 23: 0, 24: 0, 25: 0, 26: 0, 27: 0, 28: 0, 29: 0, 30: 0}
	for k in a:
		d[int(math.log2(k))] += 1
		res = 0
	for k in d:
		res += math.comb(d[k], 2)
	print(res)


from sys import stdin
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, stdin.readline().rstrip().split(' ')))
	d = {}
	for i in range(n):
		a = bin(l[i])
		z = len(a) - 2
		if not d.get(z, 0):
			d[z] = 0
		d[z] += 1
	res = 0
	for i in d.values():
		res += (i - 1) * i // 2
	print(res)


import math
for _ in range(int(input())):
	n = int(input())
	a = [int(math.log(int(x), 2)) for x in input().split()]
	a.sort()
	b = [1]
	for i in range(1, n):
		if a[i - 1] == a[i]:
			b[-1] += 1
		else:
			b.append(1)
	ans = 0
	for i in b:
		if i * (i - 1) // 2 > 0:
			ans += i * (i - 1) // 2
	print(ans)


from collections import Counter

def pow(c):
	cc = 0
	while c > 1:
		c = c // 2
		cc += 1
	return cc
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	d = Counter(l)
	l1 = []
	for i in l:
		t = pow(i)
		l1.append(t)
	d = Counter(l1)
	c = 0
	for i in d:
		if d[i] > 1:
			kk = d[i] * (d[i] - 1) // 2
			c += kk
	print(c)


from collections import defaultdict
t = int(input())
for i in range(t):
	n = int(input())
	b = list(map(int, input().split()))
	c = []
	for j in b:
		k = len(bin(j)[2:])
		c.append(k)
	d = defaultdict(lambda : 0)
	for j in c:
		d[j] += 1
	ans = 0
	for j in set(c):
		ans += d[j] * (d[j] - 1) // 2
	print(ans)


import math
T = int(input())
for z in range(T):
	n = int(input())
	a = [int(x) for x in input().split()]
	m = max(a)
	size = int(math.log(m, 2))
	see = [0 for i in range(size + 1)]
	for e in a:
		i = int(math.log(e, 2))
		see[i] += 1
	count = 0
	for e in see:
		count += e * (e - 1) // 2
	print(count)


import math
t = int(input())
for z in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	counter = [0] * 30
	for i in range(n):
		counter[int(math.log(arr[i], 2))] += 1
	ans = 0
	for num in counter:
		if num >= 2:
			ans = ans + num * (num - 1) // 2
	print(ans)


import sys
input = sys.stdin.readline
t = int(input())
for t1 in range(t):
	n = int(input())
	l = list(map(int, input().split(' ')))
	d = {}
	for i in range(n):
		z = bin(l[i])[2:]
		ind = len(z)
		d[ind] = d.get(ind, 0) + 1
	ans = 0
	for i in range(n):
		z = bin(l[i])[2:]
		ind = len(z)
		ans += d.get(ind, 0) - 1
	print(ans // 2)


t = int(input())
for _ in range(t):
	n = int(input())
	nums = [int(val) for val in input().split()]
	total = 0
	repo = {}
	for num in nums:
		b = len(bin(num)[2:])
		if b in repo:
			repo[b] += 1
		else:
			repo[b] = 1
	for val in repo.values():
		total += val * (val - 1) // 2
	print(total)


def lb(n):
	x = 0
	while n > 0:
		n = n // 2
		x += 1
	return x
from collections import defaultdict

def count(x):
	return x * (x - 1) // 2
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = [lb(i) for i in a]
	d = defaultdict(int)
	for i in b:
		d[i] += 1
	s = 0
	for i in d:
		if d[i] != 1:
			s += count(d[i])
	print(s)


def pos_lsd(num):
	x = bin(num)[2:]
	return len(str(x))

def solution(arr):
	my_dict = {}
	result = 0
	for i in arr:
		idx = pos_lsd(i)
		if idx not in my_dict:
			my_dict[idx] = [i]
		else:
			my_dict[idx] += [i]
	for x in my_dict.values():
		if len(x) > 1:
			result += len(x) * (len(x) - 1) // 2
	return result
n = int(input())
for i in range(n):
	a = int(input())
	x = list(map(int, input().split()))
	print(solution(x))


import math
for h in range(int(input())):
	n = int(input())
	arr = list(map(int, input().strip().split()))
	mapi = {}
	for i in arr:
		p = int(math.log2(i))
		if p in mapi:
			mapi[p] += 1
		else:
			mapi[p] = 1
	ans = 0
	for i in mapi:
		ans += mapi[i] * (mapi[i] - 1) // 2
	print(ans)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	arr.sort()
	ans = 0
	t = []
	for i in range(n):
		t.append(len(bin(arr[i])[2:]))
	i = 0
	last = 0
	count = 0
	while i < n:
		if last == t[i]:
			count += 1
		else:
			ans += count * (count + 1) // 2
			last = t[i]
			count = 0
		i += 1
	ans += count * (count + 1) // 2
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	A = list(map(int, input().split()))
	A.sort()
	d = {}
	ans = 0
	for a in A:
		b = format(a, 'b')
		s = len(b)
		if s in d:
			ans += d[s]
			d[s] += 1
		else:
			d[s] = 1
	print(ans)


import math

def get_pairs(n):
	return n * (n - 1) // 2
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	pl = []
	for i in l:
		pl.append(int(math.log(i, 2)))
	pl.sort()
	d = {}
	for i in pl:
		if i in d:
			d[i] += 1
		else:
			d[i] = 1
	out = 0
	for i in d:
		out += get_pairs(d[i])
	print(out)


for _ in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	(bi, ans) = ([0] * 32, 0)
	for e in a:
		v = len(bin(e)[2:])
		ans += bi[v]
		bi[v] += 1
	print(ans)


import sys
inp = sys.stdin.buffer.readline
wrl = print
inar = lambda : list(map(int, inp().split()))
inin = lambda : int(inp())
inst = lambda : inp().decode().strip()
from math import log2
_T_ = inin()
for _t_ in range(_T_):
	n = inin()
	a = inar()
	k = [int(log2(i)) for i in a]
	freq = [0 for i in range(64)]
	for i in k:
		freq[i] += 1
	ans = 0
	for i in freq:
		ans += i * (i - 1) // 2
	print(ans)


from sys import stdin, stdout
import math, sys
from itertools import permutations, combinations
from collections import defaultdict, deque, OrderedDict, Counter
from os import path
import bisect as bi
import heapq
mod = 10 ** 9 + 7

def yes():
	print('YES')

def no():
	print('NO')
if path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')

	def inp():
		return int(input())

	def minp():
		return map(int, input().split())
else:

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

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

def msb(n):
	k = int(math.log(n, 2))
	return int(pow(2, k))
for _ in range(inp()):
	n = inp()
	a = list(minp())
	for i in range(n):
		a[i] = msb(a[i])
	d = Counter(a)
	count = 0
	for keys in d:
		if d[keys] >= 2:
			count += d[keys] * (d[keys] - 1) // 2
	print(count)


t = int(input())
for i in range(t):
	n = int(input())
	ans = 0
	a = list(map(int, input().strip().split(' ')))
	dic = [0 for _ in range(n + 55)]
	for j in a:
		p = bin(j)[2:]
		kp = len(p)
		kps = 34 - kp
		pp = ''
		for jp in range(kps):
			pp += '0'
		p = pp + p
		for k in range(len(p)):
			if p[k] == '1':
				dic[k] += 1
				break
	for j in dic:
		if j > 0:
			ans += j * (j - 1) // 2
	print(ans)


result = []
for a in range(int(input())):
	input()
	checkList = [bin(int(val)) for val in input().split()]
	list_count = [0] * 32
	correctCases = 0
	for a in checkList:
		list_count[len(a) - 3] += 1
	for c in list_count:
		correctCases += (c - 1) / 2 * c
	result.append(int(correctCases))
for p in result:
	print(p)


import sys
import os
from io import BytesIO, IOBase

def ii():
	return int(input())

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

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

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

def prr(a, sep=' '):
	print(sep.join(map(str, a)))

def ddi():
	return defaultdict(int)

def ddl():
	return defaultdict(list)

def ddd():
	return defaultdict(defaultdict(int))
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 defaultdict
from math import gcd, pi, inf
for _ in range(ii()):
	n = ii()
	a = li()
	res = 0
	cnt = [0 for i in range(32)]
	for x in a:
		cnt[len(bin(x)[2:])] += 1
	for x in cnt:
		res += x * (x - 1) // 2
	print(res)


import sys
from bisect import *
from collections import deque
pl = 1
from copy import *
if pl:
	input = sys.stdin.readline
else:
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('outpt.txt', 'w')

def li():
	return [int(xxx) for xxx in input().split()]

def fi():
	return int(input())

def si():
	return list(input().rstrip())

def mi():
	return map(int, input().split())
t = fi()
while t > 0:
	t -= 1
	n = fi()
	a = li()
	d = [0] * 40
	ans = 0
	for i in range(n):
		h = bin(a[i])[2:]
		ans += d[len(h) - 1]
		d[len(h) - 1] += 1
	print(ans)


from math import floor, log2
from collections import defaultdict as dd
import sys
input = sys.stdin.readline
t = int(input())
while t:
	d = dd(int)
	n = int(input())
	l = list(map(int, input().split()))
	ans = 0
	for ii in range(n - 1, -1, -1):
		i = l[ii]
		p = floor(log2(i))
		ans += d[p]
		d[p] += 1
	print(ans)
	t -= 1


import sys
import itertools as it
import math as mt
import collections as cc
input = sys.stdin.readline
I = lambda : list(map(int, input().split()))
for tc in range(int(input())):
	(n,) = I()
	l = I()
	temp = [0] * 32
	for i in l:
		for j in range(32, -1, -1):
			if i & 1 << j:
				temp[j] += 1
				break
	ans = 0
	for i in temp[::-1]:
		if 1:
			ans += i * (i - 1) // 2
	print(ans)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	arr = [len(bin(e)) - 2 for e in arr]
	m = {}
	acc = 0
	for e in arr:
		acc += m.get(e, 0)
		m[e] = m.get(e, 0) + 1
	print(acc)


from math import log
t = int(input())
for i in range(t):
	n = int(input())
	nums = list(map(int, input().split()))
	pairs = 0
	counts = [0] * 30
	for num in nums:
		counts[int(log(num, 2))] += 1
	for count in counts:
		if count:
			pairs += int(count * (count - 1) / 2)
	print(pairs)


for t in range(int(input())):
	num = int(input())
	arr = list(map(int, input().split()))
	b = []
	for i in range(num):
		x = bin(arr[i]).replace('0b', '')
		b.append(list(x))
	m = max(arr)
	x = bin(m).replace('0b', '')
	y = len(x)
	d = {}
	for i in range(y):
		d[i] = 0
	for i in range(len(b)):
		d[b[i].index('1') + y - len(b[i])] += 1
	count = 0
	for i in range(y):
		count += d[i] * (d[i] - 1) // 2
	print(count)


from math import log2, ceil
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	d = dict()
	ans = 0
	for x in a:
		msb = int(log2(x) + 1)
		c = d.get(msb, 0)
		ans += c
		d[msb] = c + 1
	print(ans)


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
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	dic = {}
	for d in arr:
		if len(bin(d)) not in dic:
			dic[len(bin(d))] = 0
		dic[len(bin(d))] += 1
	ans = 0
	for d in dic:
		if dic[d] > 1:
			ans += ncr(dic[d], 2)
	print(ans)


import math

def main():
	n = int(input())
	arr = list(map(int, input().split()))
	d = {}
	for i in range(n):
		c = arr[i]
		k = int(math.log(c, 2))
		if d.get(k):
			d[k] += 1
		else:
			d[k] = 1
	ans = 0
	for (key, value) in d.items():
		ans += value * (value - 1) // 2
	print(ans)
for _ in range(int(input())):
	main()


t = int(input())
for test in range(t):
	n = int(input())
	l = [int(i) for i in input().split(' ')]
	c = [0 for i in range(30)]
	for m in l:
		k = 1
		while m >= 2:
			m = m // 2
			k += 1
		c[k - 1] += 1
	p = [i * (i - 1) // 2 for i in c]
	print(sum(p))


import math

def setBitNumber(n):
	k = int(math.log(n, 2))
	return 2 ** k
t = 1
t = int(input())
while t:
	t -= 1
	n = int(input())
	b = list(map(int, input().split()))
	cnt = {}
	for el in b:
		cnt[setBitNumber(el)] = cnt.get(setBitNumber(el), 0) + 1
	ans = 0
	for val in cnt.values():
		ans += val * (val - 1) // 2
	print(ans)


import math
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	x = -1
	cnt = 1
	ans = 0
	for i in a:
		if int(math.log2(i)) == x:
			cnt += 1
		else:
			x = int(math.log2(i))
			ans += cnt * (cnt - 1) // 2
			cnt = 1
	ans += cnt * (cnt - 1) // 2
	print(ans)


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


from collections import Counter

def solve(n, l):
	ans = 0
	d = []
	for i in range(n):
		d.append(len(str(bin(l[i]))) - 2)
	c = Counter(d)
	for i in c.items():
		ans += i[1] * (i[1] - 1) // 2
	return ans
t = int(input())
for we in range(t):
	n = int(input())
	l = sorted(list(map(int, input().split())))
	print(solve(n, l))


from sys import stdin
input = stdin.readline

def gt():
	return list(map(int, input().split()))
cnt = [0] * 33
(t,) = gt()
while t:
	t -= 1
	cnt = [0] * 33
	(n,) = gt()
	ar = gt()
	for val in ar:
		cnt[val.bit_length()] += 1
	ans = 0
	for num in cnt:
		ans += num * (num - 1) // 2
	print(ans)


import math

def li():
	return list(map(int, input().split()))
t = int(input())
for _ in range(t):
	n = int(input())
	arr = li()
	ans = [0] * 30
	for i in arr:
		x = int(math.log(i, 2))
		ans[x] += 1
	fans = 0
	for i in ans:
		if i == 1:
			continue
		elif i == 2:
			fans += 1
		else:
			fans += i * (i - 1) // 2
	print(fans)


import math
t = int(input())
for j in range(0, t):
	n = int(input())
	l = list(map(int, input().split()))
	c = 0
	uff = {}
	for u in range(0, n):
		x = int(math.log(l[u], 2))
		if x in uff:
			uff[x] += 1
		else:
			uff[x] = 1
	for ii in uff:
		c += uff[ii] * (uff[ii] - 1) // 2
	print(c)


import math
m = int(input())
for _ in range(m):
	n = int(input())
	a = list(map(int, input().split()))
	counters = [0] * 32
	result = 0
	for i in range(n):
		family = int(math.log2(a[i]))
		result += counters[family]
		counters[family] += 1
	print(result)


import os
from io import BytesIO, IOBase
import sys
from collections import defaultdict, deque, Counter
from bisect import *

def main():
	for t in range(int(input())):
		n = int(input())
		a = list(map(int, input().split()))
		b = [0] * 30
		c = []
		for i in range(n):
			z = bin(a[i])[2:]
			c.append(len(z))
			b[c[-1] - 1] += 1
		p = 0
		for i in range(30):
			if b[i] != 0:
				p += b[i] * (b[i] - 1) // 2
		print(p)
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())
for _ in range(0, T):
	n = int(input())
	s = [int(x) for x in input().split()]
	pos = [0] * 45
	for i in range(0, len(s)):
		tt = bin(s[i])
		tt = tt[2:]
		pos[len(tt)] += 1
	ans = 0
	for i in range(0, len(pos)):
		val = pos[i] * (pos[i] - 1) // 2
		ans += val
	print(ans)


import sys, math
from collections import Counter, deque, defaultdict
mod = 10 ** 9 + 7
INF = float('inf')

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

def inpl():
	return list(map(int, sys.stdin.readline().split()))
for _ in range(inp()):
	n = inp()
	a = inpl()
	cnt = defaultdict(int)
	for x in a:
		ln = len(bin(x))
		cnt[ln] += 1
	res = 0
	for key in list(cnt):
		res += cnt[key] * (cnt[key] - 1) // 2
	print(res)


import math
t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	p = [0] * 32
	for i in range(n):
		k = int(math.log(l[i], 2))
		p[k] = p[k] + 1
	r = 0
	for i in range(len(p)):
		if p[i] > 1:
			r = r + p[i] * (p[i] - 1) // 2
	print(r)


import sys, collections, math

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

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

def input():
	return sys.stdin.readline().strip()
mod = 1000000007
for _ in range(int(input())):
	n = int(input())
	arr = get_array()
	b = [0] * 33
	for i in range(n):
		z = arr[i]
		c = 0
		while z:
			z >>= 1
			c += 1
		b[c] += 1
	ans = 0
	for i in b:
		ans += i * (i - 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')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
import math
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()))
mod = pow(10, 9) + 7

def solve():
	n = int1()
	l1 = list1()
	d = defaultdict(int)
	for i in range(n):
		v = bin(l1[i])[2:]
		d[len(v)] += 1
	sum1 = 0
	for i in d:
		if d[i] > 1:
			v = d[i] - 1
			sum1 += v * (v + 1) // 2
	print(sum1)
for _ in range(int(input())):
	solve()


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	pows = [2 ** i for i in range(31)]
	counts = [0 for i in range(31)]
	for x in l:
		for j in range(31):
			if x < pows[j]:
				counts[j - 1] += 1
				break
	summ = 0
	for i in range(31):
		if counts[i] > 1:
			summ += counts[i] * (counts[i] - 1) // 2
	print(summ)


def lowbit(x: int):
	return x & -x

def treearray_update(pos: int, x: int, array: list):
	while pos < len(array):
		array[pos] += x
		pos += lowbit(pos)

def treearray_getsum(pos: int, array: list) -> int:
	ans = 0
	while pos > 0:
		ans += array[pos]
		pos -= lowbit(pos)
	return ans

def calcinvs(array: list):
	d = {}
	for (k, v) in enumerate(sorted(array)):
		d[v] = k + 1
	treearray = [0 for i in range(1 + len(array))]
	invs = 0
	for i in array:
		invs += treearray_getsum(len(treearray) - 1, treearray) - treearray_getsum(d[i], treearray)
		treearray_update(d[i], 1, treearray)
	return invs
import math
t = int(input())
for _ in range(t):
	n = int(input())
	li = [int(i) for i in input().split()]
	d = {}
	for i in li:
		while lowbit(i) != i:
			i -= lowbit(i)
		d[i] = d.get(i, 0) + 1
	ans = 0
	for (k, v) in d.items():
		ans += v * (v - 1)
	print(ans >> 1)


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


from sys import stdin
input = stdin.readline
for _ in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	b = [0] * 40
	res = 0
	for x in a:
		b[len(bin(x)) - 2] += 1
	for x in b:
		if x < 2:
			continue
		res += x * (x - 1) // 2
	print(res)


t = int(input())
for _ in range(t):
	n = int(input())
	arr = [int(x) for x in input().split()]
	dict = {}
	ans = 0
	for i in arr:
		b = bin(i)[2:]
		if len(b) in dict:
			dict[len(b)] += 1
		else:
			dict[len(b)] = 1
	for i in dict:
		a = dict[i]
		ans += a * (a - 1) // 2
	print(ans)


import math

def getFirstSetBitPos(n):
	return math.log2(n & -n) + 1
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	vis = [0] * (n + 1)
	cnt = [0] * 33
	for i in range(32 + 1):
		for j in range(n):
			if vis[j] == 0 and 1 << 32 - i & arr[j]:
				cnt[32 - i] += 1
				vis[j] = 1
	ans = 0
	for i in range(32):
		if cnt[i] >= 2:
			ans += cnt[i] * (cnt[i] - 1) // 2
	print(ans)


import math
for _ in range(int(input())):
	n = int(input())
	A = [int(_) for _ in input().split()]
	D = dict()
	for _ in range(n):
		x = int(math.log(A[_], 2)) + 1
		if x in D:
			D[x] += 1
		else:
			D[x] = 1
	c = 0
	for _ in D:
		c += D[_] * (D[_] - 1) // 2
	print(c)


import sys
import math
import bisect
from sys import stdin, stdout
from math import gcd, floor, sqrt, log
from collections import defaultdict as dd
from bisect import bisect_left as bl, bisect_right as br
from collections import Counter
inp = lambda : int(input())
strng = lambda : input().strip()
jn = lambda x, l: x.join(map(str, l))
strl = lambda : list(input().strip())
mul = lambda : map(int, input().strip().split())
mulf = lambda : map(float, input().strip().split())
seq = lambda : list(map(int, input().strip().split()))
ceil = lambda x: int(x) if x == int(x) else int(x) + 1
ceildiv = lambda x, d: x // d if x % d == 0 else x // d + 1
flush = lambda : stdout.flush()
stdstr = lambda : stdin.readline()
stdint = lambda : int(stdin.readline())
stdpr = lambda x: stdout.write(str(x))
stdarr = lambda : map(int, stdstr().split())
mod = 1000000007
for _ in range(stdint()):
	n = stdint()
	arr = list(stdarr())
	c = Counter()
	for i in arr:
		bi = math.log(i, 2)
		if bi % 1 == 0:
			bi = int(bi) + 1
		else:
			bi = math.ceil(bi)
		c[bi] += 1
	res = 0
	for i in c:
		res += c[i] * (c[i] - 1) // 2
	print(res)


from collections import Counter, defaultdict as dft

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

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

def solve():
	n = int(input())
	arr = ml()
	res = 0
	dct = dft(int)
	vis = dft(int)
	for i in range(31, -1, -1):
		for ind in range(n):
			num = arr[ind]
			if ind not in vis and num & 1 << i:
				dct[i] += 1
				vis[ind] = 1
	for (key, val) in dct.items():
		if val > 1:
			res += val * (val - 1) // 2
	print(res)
t = int(input())
for _ in range(t):
	solve()


import math
t = int(input())
while t > 0:
	n = int(input())
	a = list(map(int, input().split()))
	count = [0] * 32
	res = 0
	for i in range(n):
		family = int(math.log2(a[i]))
		res += count[family]
		count[family] += 1
	print(res)
	t -= 1


from functools import reduce
import os
import sys
from collections import *
from math import *
from bisect import *
from heapq import *
from io import BytesIO, IOBase
input = lambda : sys.stdin.readline().rstrip('\r\n')

def value():
	return tuple(map(int, input().split()))

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

def sarr():
	return [int(i) for i in input()]

def starr():
	return [str(x) for x in input().split()]

def inn():
	return int(input())

def svalue():
	return tuple(map(str, input().split()))

def parr():
	return [value() for i in range(n)]
mo = 1000000007
inf = 1e+18
for _ in range(inn()):
	n = inn()
	a = arr()
	res = []
	d = defaultdict(int)
	for i in range(n):
		c = 0
		while a[i]:
			a[i] = a[i] >> 1
			c += 1
		d[c] += 1
		res += [c]
	ans = 0
	for i in d:
		val = d[i] * (d[i] - 1) // 2
		ans += val
	print(ans)


import sys
import math
mod = 10 ** 9 + 7
inf = float('inf')

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

def LI():
	return list(map(int, sys.stdin.readline().split()))
t = I()
for _ in range(t):
	n = I()
	a = LI()
	cnt = [0] * (max(a).bit_length() + 1)
	ans = 0
	for ai in a:
		ans += cnt[ai.bit_length()]
		cnt[ai.bit_length()] += 1
	print(ans)


from math import log2

def main():
	t = int(input())
	for i in range(t):
		n = int(input())
		values = list(map(int, input().split()))
		last = dict()
		ans = 0
		for i in values:
			v = int(log2(i))
			if v in last:
				ans += last[v]
				last[v] += 1
			else:
				last[v] = 1
		print(ans)
main()


import sys
LI = lambda : list(map(int, sys.stdin.readline().strip('\n').split()))
MI = lambda : map(int, sys.stdin.readline().strip('\n').split())
SI = lambda : sys.stdin.readline().strip('\n')
II = lambda : int(sys.stdin.readline().strip('\n'))
for _ in range(II()):
	n = II()
	a = LI()
	val = [0] * 35
	for v in a:
		b = bin(v)[2:]
		val[len(b) - b.index('1')] += 1
	ans = 0
	for i in range(35):
		if val[i]:
			ans += val[i] * (val[i] - 1) // 2
	print(ans)


N = int(input())
for j in range(N):
	n = int(input())
	a = list(map(int, input().split()))
	m = {}
	ans = 0
	for i in a:
		l = len(bin(i))
		c = m.get(l, 0)
		m[l] = c + 1
		ans += c
	print(ans)


from sys import stdin, stdout

class Input:

	def __init__(self):
		self.lines = stdin.read().splitlines()
		self.idx = 0

	def line(self):
		try:
			return self.lines[self.idx]
		finally:
			self.idx += 1

	def array(self, sep=' ', cast=int):
		return list(map(cast, self.line().split(sep=sep)))

	def known_tests(self):
		(num_of_cases,) = self.array()
		for case in range(num_of_cases):
			yield self

	def unknown_tests(self):
		while self.idx < len(self.lines):
			yield self

def problem_solver():

	def solver(inpt):
		(n,) = inpt.array()
		a = inpt.array()
		c = [0 for i in range(32)]
		ans = 0
		for x in a:
			hb = -1
			while x > 0:
				x >>= 1
				hb += 1
			ans += c[hb]
			c[hb] += 1
		print(ans)
	return solver
try:
	solver = problem_solver()
	for tc in Input().known_tests():
		solver(tc)
except Exception as e:
	import traceback
	traceback.print_exc(file=stdout)


import sys
import math
import collections
input = sys.stdin.readline

def inp():
	return int(input())

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

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())
t = inp()
for i in range(t):
	n = inp()
	l = inlt()
	d = collections.defaultdict(int)
	for x in l:
		d[math.floor(math.log2(x))] += 1
	s = 0
	for v in d.values():
		s += (v - 1) * v / 2
	print(int(s))


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	powers_of_2 = {}
	for i in a:
		power_of_2 = 0
		while i >= 2 ** power_of_2:
			power_of_2 += 1
		if power_of_2 not in powers_of_2:
			powers_of_2[power_of_2] = 1
		else:
			powers_of_2[power_of_2] += 1
	count = 0
	for key in powers_of_2:
		n = powers_of_2[key]
		count += n * (n - 1) // 2
	print(count)


import sys, math
input = lambda : sys.stdin.readline().rstrip('\r\n')
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	dic = {}
	for i in arr:
		x = int(math.log(i, 2))
		if x in dic:
			dic[x] += 1
		else:
			dic[x] = 1
	out = 0
	for i in dic:
		out += dic[i] * (dic[i] - 1) // 2
	print(out)


def msb(n):
	y = 0
	while 2 ** y <= n:
		y += 1
	return y
t = int(input())
while t:
	n = int(input())
	a = list(map(int, input().split()))
	a = sorted(a)
	q = 1
	ans = 0
	for x in range(n - 1):
		if msb(a[x]) == msb(a[x + 1]):
			q += 1
		else:
			ans = ans + (q - 1) * q // 2
			q = 1
	ans = ans + (q - 1) * q // 2
	print(ans)
	t -= 1


k = 1
c = []
for i in range(31):
	c.append(k)
	k *= 2
for _ in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	a.sort()
	b = [0] * 30
	j = 0
	for i in a:
		while c[j] <= i:
			j += 1
		b[j - 1] += 1
	ss = 0
	for i in b:
		if i > 1:
			ss += i * (i - 1) // 2
	print(ss)


from collections import Counter

def get_bit(X):
	for i in range(30, -1, -1):
		if X >> i & 1:
			return i

def solve():
	N = int(input())
	a = list(map(int, input().split()))
	b = dict(Counter([get_bit(v) for v in a]))
	rev = 0
	for (key, value) in b.items():
		rev += value * (N - value)
	rev //= 2
	ans = N * (N - 1) // 2 - rev
	print(ans)
	return

def main():
	T = int(input())
	for i in range(T):
		solve()
main()


for T in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	cnt = [0 for i in range(50)]
	ans = 0
	for i in range(n):
		now = 0
		x = a[i]
		while x > 0:
			x //= 2
			now += 1
		cnt[now] += 1
	ans = 0
	for i in range(33):
		ans += cnt[i] * (cnt[i] - 1) // 2
	print(ans)


t = int(input())
powers = [2 ** i for i in range(31)]
for i in range(t):
	n = int(input())
	counts = [0] * 31
	a = map(int, input().split())
	for x in a:
		for j in range(1, 31):
			if x < powers[j]:
				counts[j - 1] += 1
				break
	total = 0
	for count in counts:
		total += count * (count - 1) // 2
	print(total)


import math

def solve(n, arr):
	arr.sort()
	log_dict = dict()
	for j in range(int(math.log(arr[-1], 2)) + 1):
		log_dict[j] = 0
	for i in range(n):
		log = int(math.log(arr[i], 2))
		log_dict[log] += 1
	ans = 0
	for ele in log_dict:
		ans += log_dict[ele] * (log_dict[ele] - 1) // 2
	return ans
	pass
t = int(input())
ans = []
for i in range(t):
	n = int(input())
	arr = [int(x) for x in input().split()]
	ans.append(solve(n, arr))
for test in ans:
	print(test)


from sys import stdin
for _ in ' ' * int(stdin.readline()):
	s = int(stdin.readline())
	z = sorted(map(int, stdin.readline().split()))
	i = 0
	(ans, _1) = (0, 0)
	r = lambda x: x * (x - 1) // 2
	while i < s and z[i] == 1:
		_1 += 1
		i += 1
	ans += r(_1)
	p = 2
	while i < s:
		_1 = 0
		while i < s and p <= z[i] <= 2 * p - 1:
			_1 += 1
			i += 1
		ans += r(_1)
		p *= 2
	print(ans)


import sys
from collections import defaultdict
input = sys.stdin.readline
from math import log2
for _ in range(int(input())):
	n = int(input())
	lis = list(map(int, input().split()))
	c = 0
	d = defaultdict(int)
	for i in lis:
		d[int(log2(i)) + 1] += 1
	for i in d:
		c += d[i] * (d[i] - 1) // 2
	print(c)


for t in range(int(input())):
	n = int(input())
	a = list(map(int, input().strip().split()))[:n]
	b = []
	for i in range(n):
		z = len(bin(a[i])[2:])
		b.append(z)
	freq = {}
	for item in b:
		if item in freq:
			freq[item] += 1
		else:
			freq[item] = 1
	sum = 0
	for val in freq:
		u = freq[val]
		if u >= 2:
			sum += u * (u - 1) // 2
	print(sum)


def power(b):
	i = 0
	while 2 ** i <= b:
		i += 1
	return i
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	p = [power(i) for i in a]
	prev = p[0]
	count = 0
	ans = 0
	for j in p:
		if j == prev:
			count += 1
		else:
			prev = j
			ans += count * (count - 1) / 2
			count = 1
	ans += count * (count - 1) / 2
	print(int(ans))


import sys
from math import sqrt, gcd, ceil, log, floor
from bisect import bisect, bisect_left
from collections import defaultdict, Counter, deque
from heapq import heapify, heappush, heappop
input = sys.stdin.readline
read = lambda : list(map(int, input().strip().split()))
MOD = 998244353

def main():
	ans_ = ''
	for _ in range(int(input())):
		n = int(input())
		arr = read()
		dic = defaultdict(int)
		ans = 0
		for elem in arr:
			l = len(bin(elem))
			if l in dic:
				dic[l] += 1
				ans += dic[l]
			else:
				dic[l] = 0
		ans_ += str(ans) + '\n'
	print(ans_)
main()


import math
from collections import Counter
cases = int(input())
for t in range(cases):
	n = int(input())
	a = list(map(int, input().split()))
	d = [int(math.log2(i)) for i in a]
	d = Counter(d)
	print(sum([d[i] * (d[i] - 1) // 2 for i in d if d[i] > 1]))


import io, os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	if n == 1:
		print(0)
	else:
		num = [0] * 31
		for j in range(n):
			num[len(bin(arr[j])[2:]) - 1] += 1
		ans = 0
		for j in range(31):
			if num[j] > 1:
				a = num[j]
				b = a * (a - 1) // 2
				ans += b
		print(ans)


def and_or(l):
	m = [0] * 32
	k = 0
	for i in l:
		k += m[i.bit_length()]
		m[i.bit_length()] += 1
	return k
for _ in range(int(input())):
	input()
	l = list(map(int, input().split()))
	print(and_or(l))


import sys
import math
input = sys.stdin.readline
from collections import deque
from queue import LifoQueue

def binary_search(a, n):
	L = 0
	R = len(a) - 1
	while L <= R:
		mid = L + (R - L) // 2
		if a[mid] == n:
			return mid
		elif a[mid] < n:
			L = mid + 1
		else:
			R = mid - 1
	return -1
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	ans = 0
	p = []
	for i in range(n):
		p.append(len('{0:b}'.format(a[i])))
	di = {}
	for i in p:
		if di.get(i, False):
			di[i] += 1
		else:
			di[i] = 1
	ans = 0
	for i in di:
		ans += di[i] * (di[i] - 1) // 2
	print(ans)


import math
t = int(input())
while t:
	t -= 1
	n = int(input())
	a = [int(i) for i in input().split()]
	l = dict()
	s = 0
	for i in a:
		if i == 1:
			i += 0.5
		l[int(math.log2(i))] = l.get(int(math.log2(i)), 0) + 1
	for i in l:
		k = l[i]
		s += k * (k - 1) // 2
	print(s)


import collections
for t in range(int(input())):
	n = int(input())
	num = list(map(int, input().split()))
	k = collections.defaultdict(list)
	val = max(num)
	l = len(bin(val)[2:])
	for i in num:
		val = len(bin(i)[2:])
		k[l - val].append(i)
	out = 0
	for i in k:
		if len(k[i]) > 1:
			val = 0
			for j in range(1, len(k[i])):
				val += j
			out += val
	print(out)


import bisect
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	arr.sort()
	i = 0
	j = 0
	ans = 0
	while i < n:
		bil = bisect.bisect_left(arr, 2 ** j)
		bir = bisect.bisect_right(arr, 2 ** (j + 1) - 1)
		ll = bir - bil
		ans += ll * (ll - 1) // 2
		i = bir
		j += 1
	print(ans)


from collections import Counter
for _ in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	bt = [len(bin(i)[2:]) for i in a]
	d = Counter(bt)
	ans = 0
	for i in d:
		ans += d[i] * (d[i] - 1) // 2
	print(ans)


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


import math
import sys

class Read:

	@staticmethod
	def string():
		return input()

	@staticmethod
	def int():
		return int(input())

	@staticmethod
	def list(sep=' '):
		return input().split(sep)

	@staticmethod
	def list_int(sep=' '):
		return list(map(int, input().split(sep)))

def solve():
	n = Read.int()
	a = Read.list_int()
	tmp = {}
	for i in a:
		b = bin(i)
		l = len(b) - 2
		if l in tmp:
			tmp[l] = tmp[l] + 1
		else:
			tmp[l] = 1
	res = 0
	for v in tmp.values():
		for i in range(v):
			res = res + i
	print(res)
query_count = Read.int()
while query_count:
	query_count -= 1
	solve()


import math

def ans(n, a):
	m = {}
	count = 0
	for i in range(n):
		p = math.log(a[i], 2) // 1
		if p in m:
			count += m[p]
		else:
			m[p] = 0
		m[p] += 1
	print(count)
t = int(input())
while t > 0:
	n = int(input())
	a = list(map(int, input().split(' ')))
	ans(n, a)
	t -= 1


import sys, os, io
I = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
for tc in range(1, 1 + int(I())):
	n = int(I())
	a = list(map(int, I().split()))
	ans = 0
	while len(a) > 0:
		b = []
		c = 0
		for x in a:
			x >>= 1
			if x > 0:
				b.append(x)
			else:
				c += 1
		a = b
		ans += c * (c - 1) // 2
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	ans = 0

	def search(x):
		i = x + 1
		n = len(a)
		j = n - 1
		while i <= j:
			k = (i + j) // 2
			if a[x] & a[k] < a[x] ^ a[k]:
				j = k - 1
			else:
				i = k + 1
		return k
	x = 0
	while x < n - 1:
		y = search(x)
		if a[y] & a[x] < a[y] ^ a[x]:
			y -= 1
		ans += y - x
		x += 1
	print(ans)


from collections import defaultdict as dd
t = int(input())
while t:
	n = int(input())
	l = list(map(int, input().split()))
	d = dd(int)
	ans = 0
	for i in l:
		d[len(bin(i))] += 1
	for i in d.keys():
		temp = d[i]
		ans += temp * (temp - 1) // 2
	print(ans)
	t = t - 1


import math

def answer(n, a):
	cnt = [0] * 31
	for e in a:
		cnt[int(math.log2(e))] += 1
	pairs = 0
	for i in range(31):
		pairs += math.comb(cnt[i], 2)
	return pairs

def main():
	t = int(input())
	while t:
		n = int(input())
		a = [int(i) for i in input().split()]
		print(answer(n, a))
		t -= 1
	return
main()


from sys import stdin
from math import log2

def readline():
	return stdin.readline()

def solve(n, a):
	l = [int(log2(i)) + 1 for i in a]
	c = [0] * 33
	for v in l:
		c[v] += 1
	res = 0
	for v in c:
		res += v * (v - 1) // 2
	return res
tests = int(readline())
for t in range(0, tests):
	n = int(readline())
	a = list(map(int, readline().rstrip('\n').split(' ')))
	print(solve(n, a))


for i in ' ' * int(input()):
	n = int(input())
	L = list(map(int, input().split()))
	check = [0] * 30
	for i in L:
		ct = 0
		while i > 1:
			i //= 2
			ct += 1
		check[ct] += 1
	c = 0
	for i in check:
		if i > 1:
			c += i * (i - 1) // 2
	print(c)


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

def print(x):
	sys.stdout.write(str(x) + '\n')
t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	l = defaultdict(int)
	for j in range(n):
		b = bin(a[j])
		l[len(b)] += 1
	ans = 0
	for j in l:
		ans += l[j] * (l[j] - 1) // 2
	print(ans)


n = int(input())
for i in range(n):
	x = int(input())
	l = list(map(int, input().split()))
	c = []
	d = 0
	for i in range(x):
		c.append(len(str(bin(l[i]))))
	d = c.copy()
	d = list(set(d))
	ans = 0
	for i in d:
		if c.count(i) > 1:
			ans += c.count(i) * (c.count(i) - 1) // 2
	if len(l) == 1:
		print(0)
	else:
		print(ans)


for i in range(int(input())):
	n = int(input())
	lst = list(map(int, input().split()))
	bitlst = []
	for i in lst:
		bitlst.append(len(bin(i)) - 2)
	c = [0] * 33
	for i in bitlst:
		c[i] = c[i] + 1
	sums = 0
	for i in c:
		sums = i * (i - 1) / 2 + sums
	print(int(sums))


from math import log2
for u in range(int(input())):
	n = int(input())
	x = [int(w) for w in input().split()]
	if n == 1:
		print(0)
	else:
		d = {}
		for i in x:
			y = int(log2(i))
			d[y] = d.get(y, 0) + 1
		ans = 0
		for i in d:
			if d[i] > 1:
				ans += d[i] * (d[i] - 1) // 2
		print(ans)


from math import log2, floor
for t in range(int(input())):
	count = 0
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	counter = 0
	power = 0
	for i in range(1, n):
		if a[i] >= 2 ** (floor(log2(a[power])) + 1):
			count = count + counter * (counter + 1) // 2
			counter = 0
			power = i
		else:
			counter += 1
	count = count + counter * (counter + 1) // 2
	print(count)


from collections import defaultdict
t = int(input())
for _ in range(t):
	d = defaultdict(int)
	n = int(input())
	a = list(map(int, input().split()))
	for num in a:
		cnt = 0
		while num > 0:
			cnt += 1
			num = num // 2
		d[cnt] += 1
	ans = 0
	for i in d:
		ans += d[i] * (d[i] - 1) // 2
	print(ans)


import math
t = int(input())
for i in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	l = {}
	c = 0
	for x in a:
		y = int(math.log2(x))
		if y in l:
			l[y] += 1
		else:
			l[y] = 1
	k = 0
	for x in l:
		a = l[x]
		k += int(a * (a - 1) / 2)
	print(k)


_ = int(input())
for t in range(_):
	n = int(input())
	a = list(map(int, input().split()))
	b = []
	for i in range(n):
		x = bin(a[i]).replace('0b', '')
		b.append(list(x))
	m = max(a)
	x = bin(m).replace('0b', '')
	p = len(x)
	d = {}
	for i in range(p):
		d[i] = 0
	for i in range(len(b)):
		d[b[i].index('1') + p - len(b[i])] += 1
	c = 0
	for i in range(p):
		c += d[i] * (d[i] - 1) // 2
	print(c)


import os, sys
from io import BytesIO, IOBase
from math import log2

def inp():
	return int(input())

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

def instr():
	s = input()
	return list(s[:len(s)])

def inmap():
	return map(int, input().split())
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 i in range(inp()):
	n = inp()
	a = inlist()
	a.sort()
	bit = [0] * 32
	for i in range(n):
		z = int(log2(a[i]))
		bit[z] += 1
	ans = 0
	for i in range(32):
		if bit[i] > 0:
			ans += bit[i] * (bit[i] - 1) // 2
	print(ans)


import math
t = int(input())
while t > 0:
	n = int(input())
	arr = [int(i) for i in input().split()]
	arr.sort(reverse=True)
	count = 0
	dict1 = {}
	for i in arr:
		if int(math.log2(i)) in dict1:
			dict1[int(math.log2(i))] += 1
		else:
			dict1[int(math.log2(i))] = 1
	for (a, b) in dict1.items():
		if b >= 2:
			count += b * (b - 1) // 2
	print(count)
	t -= 1


_ = int(input())
while _:
	_ -= 1
	n = input()
	num = list(map(int, input().split()))
	temp = [0 for i in range(100)]
	ans = 0
	for i in num:
		temp[len(str(bin(i))) - 2] += 1
	for i in temp:
		ans += i * (i - 1) // 2
	print(ans)


from collections import Counter
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	b = list(map(lambda x: len(bin(x)) - 2, arr))
	b.sort()
	c = Counter(b)
	ans = 0
	for a in c.values():
		ans += a * (a - 1) // 2
	print(ans)


def bin(n, lo, hi):
	if lo == hi:
		if n >= 2 ** lo:
			return lo
		return lo - 1
	num = (lo + hi) // 2
	val = 2 ** num
	if n == val:
		return num
	elif n < val:
		return bin(n, lo, num - 1)
	else:
		return bin(n, num + 1, hi)

def fn(k):
	return k * (k - 1) // 2
t = int(input())
for test_cases in range(t):
	n = int(input())
	l = [int(i) for i in input().split(' ')]
	ls = [0] * 31
	for i in l:
		ls[bin(i, 0, 30)] += 1
	print(sum(list(map(fn, ls))))


import math
t = input()
for _ in range(int(t)):
	n = input()
	line = input()
	ls = list(map(int, line.split()))
	max_val = max(ls)
	lower_bound = 1
	total_count = 0
	while lower_bound <= max_val:
		upper_bound = (lower_bound << 1) - 1
		count = 0
		for val in ls:
			if val >= lower_bound and val <= upper_bound:
				count += 1
		total_count += count * (count - 1) // 2
		lower_bound = lower_bound << 1
	print(total_count)


t = int(input())
while t:
	t -= 1
	n = int(input())
	trr = [int(x) for x in input().split()]
	arr = []
	for (i, e) in enumerate(trr):
		arr.append(bin(e)[2:])
	count = [0] * 100
	for e in arr:
		count[len(e)] += 1
	ans = 0
	for e in count:
		ans += e * (e - 1) // 2
	print(ans)


import math
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = []
	for i in a:
		b.append(int(math.log(i, 2)))
	c = [0] * 40
	for i in b:
		c[i] += 1
	ans = 0
	for i in c:
		ans += i * (i - 1) // 2
	print(ans)


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


from __future__ import division, print_function
import os, 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 ii():
	return int(input())

def si():
	return input()

def mi():
	return map(int, input().strip().split(' '))

def msi():
	return map(str, input().strip().split(' '))

def li():
	return list(mi())

def dmain():
	sys.setrecursionlimit(1000000)
	threading.stack_size(1024000)
	thread = threading.Thread(target=main)
	thread.start()
import sys
input = sys.stdin.readline
scanner = lambda : int(input())
string = lambda : input().rstrip()
get_list = lambda : list(read())
read = lambda : map(int, input().split())
get_float = lambda : map(float, input().split())

def ceil(x):
	if x != int(x):
		x = int(x) + 1
	return x

def factorial(x, m):
	val = 1
	while x > 0:
		val = val * x % m
		x -= 1
	return val

def fact(x):
	val = 1
	while x > 0:
		val *= x
		x -= 1
	return val

def swaparr(arr, a, b):
	temp = arr[a]
	arr[a] = arr[b]
	arr[b] = temp

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

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

def is_integer(n):
	return math.ceil(n) == math.floor(n)

def nCr(n, k):
	if k > n:
		return 0
	if k > n - k:
		k = n - k
	res = 1
	for i in range(k):
		res = res * (n - i)
		res = res / (i + 1)
	return int(res)

def primefs(n):
	primes = {}
	while n % 2 == 0 and n > 0:
		primes[2] = primes.get(2, 0) + 1
		n = n // 2
	for i in range(3, int(n ** 0.5) + 2, 2):
		while n % i == 0 and n > 0:
			primes[i] = primes.get(i, 0) + 1
			n = n // i
	if n > 2:
		primes[n] = primes.get(n, 0) + 1
	return primes

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 swap(a, b):
	temp = a
	a = b
	b = temp
	return (a, b)

def find(x, link):
	p = x
	while p != link[p]:
		p = link[p]
	while x != p:
		nex = link[x]
		link[x] = p
		x = nex
	return p

def union(x, y, link, size):
	x = find(x, link)
	y = find(y, link)
	if size[x] < size[y]:
		(x, y) = swap(x, y)
	if x != y:
		size[x] += size[y]
		link[y] = x

def sieve(n):
	prime = [True for i in range(n + 1)]
	(prime[0], prime[1]) = (False, False)
	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 phi(n):
	result = n
	p = 2
	while p * p <= n:
		if n % p == 0:
			while n % p == 0:
				n = n // p
			result = result * (1.0 - 1.0 / float(p))
		p = p + 1
	if n > 1:
		result = result * (1.0 - 1.0 / float(n))
	return int(result)

def is_prime(n):
	if n == 0:
		return False
	if n == 1:
		return True
	for i in range(2, int(n ** (1 / 2)) + 1):
		if not n % i:
			return False
	return True

def next_prime(n, primes):
	while primes[n] != True:
		n += 1
	return n
MAXN = int(100000.0 + 5)

def spf_sieve():
	spf[1] = 1
	for i in range(2, MAXN):
		spf[i] = i
	for i in range(4, MAXN, 2):
		spf[i] = 2
	for i in range(3, ceil(MAXN ** 0.5), 2):
		if spf[i] == i:
			for j in range(i * i, MAXN, i):
				if spf[j] == j:
					spf[j] = i
spf = [0 for i in range(MAXN)]

def factoriazation(x):
	res = []
	for i in range(2, int(x ** 0.5) + 1):
		while x % i == 0:
			res.append(i)
			x //= i
	if x != 1:
		res.append(x)
	return res

def factors(n):
	res = []
	for i in range(1, int(n ** 0.5) + 1):
		if n % i == 0:
			res.append(i)
			res.append(n // i)
	return list(set(res))

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

def float_array():
	return list(map(float, input().strip().split()))

def str_array():
	return input().strip().split()

def binary_search(low, high, w, h, n):
	while low < high:
		mid = low + (high - low) // 2
		if check(mid, w, h, n):
			low = mid + 1
		else:
			high = mid
	return low

def check(beauty, s, n, count):
	pass
MOD = int(1000000000.0) + 7
CMOD = 998244353
INF = float('inf')
NINF = -float('inf')
alphs = 'abcdefghijklmnopqrstuvwxyz'
from itertools import permutations
import math
import bisect as bis
import random
import sys
import collections as collect

def solve():
	n = scanner()
	a = get_list()
	a.sort()
	ans = 0
	for i in range(100):
		if 2 ** i > a[-1]:
			break
		m = bis.bisect_left(a, 2 ** i)
		j = bis.bisect_right(a, 2 ** (i + 1) - 1)
		ans += nCr(j - m, 2)
	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')

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')
for i in range(scanner()):
	solve()


from collections import Counter

def positionOfFirstBit(num):
	if num == 0:
		return 0
	count = 0
	while num > 0:
		num = num >> 1
		count += 1
	return count
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	if n == 1:
		print(0)
		continue
	firstBitPosition = []
	for i in range(n):
		firstBitPosition.append(positionOfFirstBit(arr[i]))
	counter = Counter(firstBitPosition)
	ans = 0
	for (key, val) in counter.items():
		ans += val * (val - 1) // 2
	print(ans)


from collections import Counter as C
for i in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	bt = C([len(bin(i)[2:]) - bin(i)[2:].index('1') for i in arr]).values()
	ans = sum([i * (i - 1) // 2 for i in bt])
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	d = dict()
	ans = 0
	for i in range(n):
		l = len(str(bin(a[i])))
		if l in d.keys():
			ans += d[l]
			d[l] += 1
		else:
			d[l] = 1
	print(ans)


from collections import Counter
for _ in range(int(input())):
	input()
	a = [int(x) for x in input().split()]
	(bi, ans) = (Counter(), 0)
	for e in a:
		v = len(bin(e)[2:])
		ans += bi[v]
		bi[v] += 1
	print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	for i in range(n):
		a[i] = len(bin(a[i])) - 2
	d = {}
	for i in a:
		if i in d:
			d[i] += 1
		else:
			d[i] = 1
	res = 0
	for i in d:
		if d[i] > 1:
			res += d[i] * (d[i] - 1) // 2
	print(res)


from bisect import bisect_left
from collections import Counter
beki2 = [pow(2, i) for i in range(40)]
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	for i in range(n):
		a[i] = 2 ** (a[i].bit_length() - 1)
	c = Counter(a)
	ans = 0
	for i in c.most_common():
		ans += i[1] * (i[1] - 1) // 2
	print(ans)


import math
import sys
input = sys.stdin.readline
for nt in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	group = [0] * 31
	for i in a:
		x = int(math.log2(i))
		group[x] += 1
	ans = 0
	for i in group:
		ans += i * (i - 1) // 2
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = [*map(int, input().split())]
	b = [0] * 35
	ans = 0
	for num in a:
		bin_num = bin(num)
		l = len(bin_num)
		b[l - 2] += 1
	for num in b:
		ans += num * (num - 1) // 2
	print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	array = list(map(int, input().split()))
	output = 0
	compartments = {}
	for j in array:
		j_n = j
		nums = 0
		while j_n != 1:
			j_n = j_n // 2
			nums += 1
		if nums not in compartments:
			compartments[nums] = [j]
		else:
			compartments[nums].append(j)
	for (k, v) in compartments.items():
		if len(v) > 1:
			a = len(v)
			o = 0
			i = 1
			while a > 1:
				o += a - 1
				a -= 1
			output += o
	print(output)


from typing import List, Tuple

def count_valid(values: List[int]):
	bits = [0] * 32
	for number in values:
		bit_current = 0
		while 1 << bit_current + 1 <= number:
			bit_current += 1
		bits[bit_current] += 1
	return sum((item * (item - 1) // 2 for item in bits))

def read_input() -> Tuple[int, List[int]]:
	cases = int(input())
	while cases:
		cases = cases - 1
		_ = int(input())
		values = list(map(int, input().strip().split(' ')))
		print(count_valid(values))

def main():
	read_input()
main()


import io
import os
from collections import Counter, defaultdict, deque

def solve(N, A):
	highBit = defaultdict(list)
	for pos in reversed(range(32)):
		mask = 1 << pos
		mask2 = (1 << pos + 1) - 1
		for x in A:
			if x & mask and x <= mask2:
				highBit[pos].append(x)
	ans = 0
	for (k, v) in highBit.items():
		m = len(v)
		ans += m * (m - 1) // 2
	return ans
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
TC = int(input())
for tc in range(1, TC + 1):
	(N,) = [int(x) for x in input().split()]
	A = [int(x) for x in input().split()]
	ans = solve(N, A)
	print(ans)


import math
import collections

def count(arr):
	d = collections.defaultdict(int)
	for i in range(len(arr)):
		x = math.floor(math.log(arr[i], 2))
		if x in d:
			d[x] += 1
		else:
			d[x] = 1
	count = 0
	for key in d:
		if d[key] == 1:
			continue
		count += d[key] * (d[key] - 1) // 2
	return count
T = int(input())
for t in range(T):
	n = input()
	arr = [int(i) for i in input().split(' ')]
	print(count(arr))


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	count = [0 for i in range(50)]
	for i in range(n):
		s = bin(a[i])[2:]
		count[len(s)] += 1
	ans = 0
	for i in count:
		ans += i * (i - 1) // 2
	print(ans)


input = __import__('sys').stdin.readline
for _ in range(int(input())):
	n = int(input())
	s = list(map(int, input().split()))
	ans = 0
	bt = [0] * 40
	for i in s:
		mb = 1
		j = 0
		while mb * 2 <= i:
			mb *= 2
			j += 1
		ans += bt[j]
		bt[j] += 1
	print(ans)


import math as m
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	mp = [0] * 100
	ans = 0
	for i in a:
		mp[int(m.log(i, 2))] += 1
	for i in mp:
		ans += i * (i - 1) // 2
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	fre = [0] * 30
	for j in range(31):
		for i in range(n):
			if a[i] >= 2 ** j and a[i] <= 2 ** (j + 1) - 1:
				fre[j] += 1
			if a[i] >= 2 ** (j + 1):
				break
	ans = 0
	for i in range(30):
		if fre[i] > 1:
			x = fre[i]
			ans += x * (x - 1) // 2
	print(ans)


from sys import stdin, stdout
import math
from collections import Counter, deque, defaultdict
L = lambda : list(map(int, stdin.readline().strip().split()))
M = lambda : map(int, stdin.readline().strip().split())
I = lambda : int(stdin.readline().strip())
S = lambda : stdin.readline().strip()
C = lambda : stdin.readline().strip().split()

def pr(a):
	return ' '.join(list(map(str, a)))

def solve():
	n = I()
	a = L()
	b = [0] * 32
	ans = 0
	for i in range(n):
		for j in range(32, -1, -1):
			if pow(2, j) & a[i]:
				break
		b[j] += 1
		ans += b[j] - 1
	print(ans)
for _ in range(I()):
	solve()


import sys
max_int = 1000000001
min_int = -max_int
t = int(input())
for _t in range(t):
	n = int(sys.stdin.readline())
	a = list(map(int, sys.stdin.readline().split()))
	a.sort(reverse=True)
	bit = 31
	d = {}
	c = 0
	for one in a:
		if not 1 << bit & one:
			d[bit] = c
			c = 0
			while not 1 << bit & one:
				bit -= 1
		c += 1
	d[bit] = c
	out = 0
	for k in d:
		if d[k] > 1:
			out += d[k] * (d[k] - 1) // 2
	print(out)


q = int(input())
for _ in range(q):
	n = int(input())
	counter = [0 for i in range(32)]
	numbers = list(map(int, input().split()))
	for i in range(n):
		pos = numbers[i].bit_length() - 1
		counter[pos] += 1
	sum = 0
	for i in range(32):
		sum += counter[i] * (counter[i] - 1) // 2
	print(sum)


import sys
import math
from collections import defaultdict as dd
mod = 1000000007

def f(n):
	c = 0
	while n:
		c += 1
		n >>= 1
	return c
T = 1
T = int(sys.stdin.readline())
for _ in range(T):
	n = int(input())
	l = list(map(int, input().split()))
	d = dd(int)
	ans = 0
	for i in l:
		k = f(i)
		if d[k]:
			ans += d[k]
		d[k] += 1
	print(ans)


import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, copy, functools
inf = 10 ** 20
eps = 1.0 / 10 ** 10
mod = 10 ** 9 + 7
mod2 = 998244353
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]

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

def LLI():
	return [list(map(int, l.split())) for l in sys.stdin.readlines()]

def LI_():
	return [int(x) - 1 for x in sys.stdin.readline().split()]

def LF():
	return [float(x) for x in sys.stdin.readline().split()]

def LS():
	return sys.stdin.readline().split()

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

def F():
	return float(sys.stdin.readline())

def S():
	return input()

def pf(s):
	return print(s, flush=True)

def pe(s):
	return print(str(s), file=sys.stderr)

def JA(a, sep):
	return sep.join(map(str, a))

def JAA(a, s, t):
	return s.join((t.join(map(str, b)) for b in a))

def IF(c, t, f):
	return t if c else f

def YES(c):
	return IF(c, 'YES', 'NO')

def Yes(c):
	return IF(c, 'Yes', 'No')

def main():
	t = I()
	rr = []
	for _ in range(t):
		n = I()
		a = LI()
		c = [0] * 64
		for t in a:
			k = 0
			while t > 0:
				k += 1
				t //= 2
			c[k] += 1
		r = 0
		for t in c:
			r += t * (t - 1) // 2
		rr.append(r)
	return JA(rr, '\n')
print(main())


from math import log

def solve(n, arr):
	bits = [0 for i in range(31)]
	for a in arr:
		bits[int(log(a, 2))] += 1
	return sum((x * (x - 1) // 2 for x in bits))
t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	print(solve(n, arr))


import math

def fun(arr, n):
	l = [int(math.log2(i) + 1) for i in arr]
	s = set(l)
	res = 0
	for i in s:
		if l.count(i) > 1:
			num = l.count(i)
			res += num * (num - 1) / 2
	print(int(res))
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	fun(arr, n)


from collections import Counter
t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	m = []
	s = 0
	for i in range(len(l)):
		for j in range(35, -1, -1):
			if l[i] & 1 << j:
				break
		m.append(j)
	c = Counter(m)
	for (i, j) in c.items():
		s += j * (j - 1) // 2
	print(s)


from sys import stdin, stdout
input = stdin.readline
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = [0] * 32
	ans = 0
	for i in range(n):
		x = bin(a[i])[2:]
		ans += b[len(x) - 1]
		b[len(x) - 1] += 1
	print(ans)


import sys
import random
from fractions import Fraction
from math import *

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

def iinput():
	return int(input())

def finput():
	return float(input())

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

def linput():
	return list(input())

def rinput():
	return map(int, tinput())

def fiinput():
	return map(float, tinput())

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

def trinput():
	return tuple(rinput())

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

def NOYES(fl):
	if fl:
		print('NO')
	else:
		print('YES')

def YESNO(fl):
	if fl:
		print('YES')
	else:
		print('NO')

def main():
	n = iinput()
	q = rlinput()
	w = [0 for i in range(35)]
	for i in range(n):
		for o in range(30, -1, -1):
			if 1 << o & q[i] != 0:
				w[o] += 1
				break
	print(sum([w[o] * (w[o] - 1) // 2 for o in range(30, -1, -1)]))
for i in range(iinput()):
	main()


from itertools import permutations
from collections import defaultdict
from math import *
import os
import sys
from io import BytesIO, IOBase
from heapq import nlargest
from bisect import *
import copy
import itertools
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')
t = int(input())
while t:
	t -= 1
	n = int(input())
	arr = list(map(int, input().split()))
	ll = []
	for i in range(n):
		ll.append(len(bin(arr[i])))
	ll.sort()
	val = list(set(ll))
	n = len(val)
	ans = 0
	for i in range(n):
		c1 = bisect_right(ll, val[i]) - bisect_left(ll, val[i]) - 1
		ans += c1 * (c1 + 1) // 2
	print(ans)


from sys import stdin, stdout
import math
import heapq
import collections
input = stdin.readline

def inputnum():
	return int(input())

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

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

def inputstring():
	return [x for x in input()]

def inputstrings():
	return [x for x in input().split()]

def inputstringnum():
	return [ord(x) - ord('a') for x in input()]

def inputmatrixchar(rows):
	arr2d = [[j for j in input().strip()] for i in range(rows)]
	return arr2d

def inputmatrixint(rows):
	arr2d = []
	for _ in range(rows):
		arr2d.append([int(i) for i in input().split()])
	return arr2d
t = int(input())
for q in range(t):
	n = inputnum()
	a = inputlist()
	ans = 0
	cur = 2
	cnt = 0
	a.sort()
	for i in range(n):
		if a[i] >= cur:
			ans += cnt * (cnt - 1) // 2
			while a[i] >= cur:
				cur *= 2
			cnt = 1
		else:
			cnt += 1
	ans += cnt * (cnt - 1) // 2
	print(ans)


import itertools
import math
import sys
from collections import defaultdict

def stdinWrapper():
	data = '1\n0\n3 3 3 3 3 \n\n'
	for line in data.split('\n'):
		yield line
if '--debug' not in sys.argv:

	def stdinWrapper():
		while True:
			yield input()
inputs = stdinWrapper()

def inputWrapper():
	return next(inputs)

def getType(_type):
	return _type(inputWrapper())

def getArray(_type):
	return [_type(x) for x in inputWrapper().split()]

def solve(a):

	def max_pow_of_2(x):
		return pow(2, math.floor(math.log2(x)))
	a = list(reversed(sorted(a)))
	tgt = max_pow_of_2(a[0])
	bucket = 1
	result = 0
	for i in range(1, len(a)):
		if a[i] >= tgt:
			result += bucket
			bucket += 1
		else:
			tgt = max_pow_of_2(a[i])
			bucket = 1
	return result
t = getType(int)
for _ in range(t):
	n = getType(int)
	a = getArray(int)
	res = solve(a)
	print(res)


from sys import stdin
import math
for _ in range(int(stdin.readline())):
	n = int(stdin.readline())
	a = list(map(int, stdin.readline().split()))
	m = 0
	d = [0] * 50
	for i in a:
		d[int(math.log(i, 2))] += 1
	res = 0
	for i in d:
		res += i * (i - 1) // 2
	print(res)


from collections import Counter
t = int(input())
for _ in range(t):
	sum = 0
	n = int(input())
	a = list(map(int, input().split()))
	l = []
	for i in a:
		l.append(len(bin(i)) - 2)
	for j in Counter(l).values():
		sum += j * (j - 1) / 2
	print(int(sum))


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	d = {}
	for i in a:
		l = len(bin(i))
		if l in d:
			d[l] += 1
		else:
			d[l] = 1
	sol = 0
	for i in d.values():
		sol += i * (i - 1) // 2
	print(sol)


T = int(input())
for case in range(T):
	n = int(input())
	a = list(map(int, input().split()))
	b = [0 for i in range(n)]
	s = [0 for i in range(33)]
	for i in range(n):
		for j in range(30, -1, -1):
			if a[i] >> j & 1 != 0:
				b[i] = j
				break
		s[b[i]] += 1
	ans = 0
	for i in range(n):
		ans += s[b[i]] - 1
	print(int(ans / 2))


from collections import defaultdict
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	indexarr = []
	for i in arr:
		k = bin(i).replace('0b', '')
		index = k.index('1')
		s = len(k) - index
		indexarr.append(s)
	d = defaultdict(int)
	for i in indexarr:
		d[i] += 1
	sum1 = 0
	for i in d:
		if d[i] >= 2:
			s = d[i]
			sum1 += s * (s - 1) // 2
	print(sum1)


import sys
input = sys.stdin.readline
(inp, ip) = (lambda : int(input()), lambda : [int(w) for w in input().split()])
for _ in range(inp()):
	n = inp()
	x = ip()
	ct = [0] * 32
	ans = 0
	for i in range(n - 1, -1, -1):
		bn = bin(x[i])[2:].zfill(32)
		for j in range(32):
			if bn[j] == '1':
				ans += ct[j]
				ct[j] += 1
				break
	print(ans)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	count = 0
	arr.sort()
	if n == 1:
		print(0)
	else:
		i = 0
		while i < n - 1:
			tmp = 0
			a = bin(arr[i]).replace('0b', '')
			i += 1
			while i < n:
				b = bin(arr[i]).replace('0b', '')
				if len(a) == len(b):
					tmp += 1
					i += 1
				else:
					i -= 1
					break
			count += tmp * (tmp + 1) // 2
			i += 1
		print(count)


t = int(input())

def fun(n):
	n = int(n)
	l = 0
	while n > 0:
		n //= 2
		l += 1
	return l
for i in range(t):
	n = int(input())
	l = list(map(fun, input().split()))
	l.sort()
	c = 0
	for i in set(l):
		t = l.count(i)
		c += t * (t - 1) / 2
	print(int(c))


import math
t = int(input())
for tt in range(t):
	n = int(input())
	A = [int(i) for i in input().split()]
	arr = [0] * 32
	for i in range(n):
		x = math.floor(math.log2(A[i]))
		arr[x] += 1
	ans = 0
	for i in range(32):
		ans += arr[i] * (arr[i] - 1) // 2
	print(ans)


t = int(input())
while t > 0:
	n = int(input())
	a = list(map(int, input().split()))
	count = dict()
	for i in a:
		dig = 0
		while i != 0:
			i = i // 2
			dig += 1
		count[dig] = count.get(dig, 0) + 1
	ans = 0
	for i in count:
		ans += count[i] * (count[i] - 1) // 2
	print(ans)
	t -= 1


game = int(input())
for f in range(game):
	n = int(input())
	a = list(map(int, input().split()))
	c = 0
	count = 0
	li = []
	for value in a:
		c = bin(value)
		li.append(len(c))
	for i in range(1, len(bin(max(a))) + 1):
		le = li.count(i)
		if le > 1:
			count += le * (le - 1) // 2
	print(count)


from math import *
t = int(input())
while t > 0:
	n = int(input())
	a = [int(x) for x in input().split()]
	val = []
	for i in range(32):
		val.append(0)
	for i in a:
		x = int(log2(i))
		val[x] += 1
	ans = 0
	for i in val:
		ans = ans + i * (i - 1) // 2
	print(ans)
	t -= 1


import math
t = int(input())
arr = []
nums = []
for i in range(t):
	nums.append(int(input()))
	arr.append(list(map(int, input().split(' '))))
for i in range(len(arr)):
	one_bit_pos = [0] * 32
	for j in range(len(arr[i])):
		val = arr[i][j]
		ind = math.floor(math.log(val, 2))
		one_bit_pos[ind] += 1
	sum_val = 0
	for j in range(len(one_bit_pos)):
		sum_val += int(one_bit_pos[j] * (one_bit_pos[j] - 1) / 2)
	print(sum_val)


t = int(input())
while t:
	t -= 1
	n = int(input())
	a = [int(i) for i in input().split()]
	ans = 0
	b = [0 for i in range(n)]
	for i in range(n):
		k = a[i]
		j = 0
		while k:
			j += 1
			k >>= 1
		b[i] = j
	c = [0 for i in range(32)]
	c[b[0]] += 1
	for i in range(1, n):
		ans += c[b[i]]
		c[b[i]] += 1
	print(ans)


from collections import defaultdict
t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	la = [len(bin(i)) for i in l]
	hash = defaultdict(int)
	for i in la:
		hash[i] += 1
	ans = 0
	for i in hash:
		ans += hash[i] * (hash[i] - 1) // 2
	print(ans)


from sys import stdout, stdin
from collections import defaultdict, deque
import math
t = int(stdin.readline())
for _ in range(t):
	n = int(stdin.readline())
	l = list(map(int, stdin.readline().split()))
	d = defaultdict(int)
	for i in l:
		s = len(bin(i))
		d[s - 2] += 1
	ans = 0
	for i in d.keys():
		ans += d[i] * (d[i] - 1) // 2
	print(ans)


TC = int(input())
for tc in range(TC):
	N = int(input())
	A = list(map(int, input().split()))
	B = [0 for _ in range(32)]
	for (i, a) in enumerate(A):
		B[a.bit_length()] += 1
	result = 0
	for b in B:
		if b > 1:
			result += b * (b - 1) // 2
	print(result)


from collections import Counter

def solveB(xs):
	d = Counter(xs)
	cnt = 0
	for x in d.values():
		cnt += x * (x - 1) // 2
	return cnt
t = int(input())
for _ in range(t):
	n = int(input())
	xs = [int(x) for x in input().split()]
	xs = [x.bit_length() for x in xs]
	print(solveB(xs))


def isPairGood(a, b):
	return a & b >= a ^ b

def main():
	try:
		for _ in range(int(input())):
			n = int(input())
			l = [int(i) for i in input().split()][:n]
			arr = [0] * 40
			for i in l:
				arr[i.bit_length()] += 1
			output = 0
			for i in arr:
				output = output + i * (i - 1) // 2
			print(output)
	except:
		pass
main()


from sys import stdin

def log(n):
	ans = 0
	while n:
		ans += 1
		n //= 2
	return ans
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, stdin.readline().split()))
	cnt = [0] * 40
	for i in a:
		cnt[log(i)] += 1
	ans = 0
	for i in range(40):
		cnt[i] -= 1
		ans += max(0, cnt[i] * (cnt[i] + 1) // 2)
	print(ans)


for time in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	l.sort()
	plist = [pow(2, x) for x in range(35)]

	def dc(a, b, power):
		if a == b or a + 1 == b:
			return 0
		if power == -1:
			return 0
		div = b
		for i in range(a, b):
			if l[i] % plist[power + 1] >= plist[power]:
				div = i
				break
		return (b - div) * (b - div - 1) // 2 + dc(a, div, power - 1)
	ans = dc(0, n, 33)
	print(ans)


i = input()
inp = []
for a in range(0, int(i)):
	input()
	inp += [input()]
for s in inp:
	arr = [int(n) for n in s.split()]
	d = {}
	for x in arr:
		l = len(bin(x)) - 2
		if l in d:
			d[l] += 1
		else:
			d[l] = 1
	c = 0
	for n in d:
		x = d[n]
		c += x * (x - 1) // 2
	print(c)


from collections import Counter

def ans(A):
	A = [len('{0:b}'.format(a)) for a in A]
	cA = Counter(A)
	ret = 0
	for k in cA:
		v = cA[k]
		ret += v * (v - 1) // 2
	return ret
T = int(input())
for t in range(T):
	input()
	A = list(map(int, input().split()))
	print(ans(A))


from math import *

def ask(a):
	cur = a[0]
	k = 1
	ans = 0
	for i in range(1, len(a)):
		if a[i] == a[i - 1]:
			k += 1
		else:
			ans += k * (k - 1) // 2
			cur = a[i]
			k = 1
	ans += k * (k - 1) // 2
	return ans
for i in range(int(input())):
	n = input()
	a = list(map(int, input().split()))
	for i in range(len(a)):
		a[i] = ceil(log(a[i], 2) + 1e-06)
	a = sorted(a)
	print(ask(sorted(a)))


from __future__ import division, print_function
from sys import stdin, stdout
import bisect
import math
import heapq
i_m = 9223372036854775807

def inin():
	return int(input())

def stin():
	return input()

def spin():
	return map(int, stin().split())

def lin():
	return list(map(int, stin().split()))

def matrix(n):
	return [list(map(int, input().split())) for i in range(n)]

def count2Dmatrix(i, list):
	return sum((c.count(i) for c in list))

def modinv(n, p):
	return pow(n, p - 2, p)

def nCr(n, r):
	i = 1
	while i < r:
		n *= n - i
		i += 1
	return n // math.factorial(r)

def GCD(x, y):
	x = abs(x)
	y = abs(y)
	if min(x, y) == 0:
		return max(x, y)
	while y:
		(x, y) = (y, x % y)
	return x

def Divisors(n):
	l = []
	for i in range(1, int(math.sqrt(n) + 1)):
		if n % i == 0:
			if n // i == i:
				l.append(i)
			else:
				l.append(i)
				l.append(n // i)
	return l
prime = []

def SieveOfEratosthenes(n):
	global prime
	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
	f = []
	for p in range(2, n):
		if prime[p]:
			f.append(p)
	return f
q = []

def dfs(n, d, v, c):
	global q
	v[n] = 1
	x = d[n]
	q.append(n)
	j = c
	for i in x:
		if i not in v:
			f = dfs(i, d, v, c + 1)
			j = max(j, f)
	return j
for _ in range(inin()):
	n = inin()
	a = lin()
	b = [0] * 32
	pairs = 0
	for i in range(n):
		b[len(bin(a[i])) - 2] += 1
	b = list(filter(lambda a: a != 0, b))
	for i in b:
		if i > 1:
			pairs += nCr(i, 2)
	print(pairs)


import math
l = int(input())
r = 0
for i in range(l):
	r = 0
	n = int(input())
	a = list(map(int, input().split()))
	b = [-1] * n
	for i in range(n):
		b[i] = math.floor(math.log2(a[i])) + 1
	b.sort()
	k = 0
	s = 1
	for i in range(n - 1):
		if b[i] == b[i + 1]:
			s = s + 1
		else:
			k = k + s * (s - 1) // 2
			s = 1
	k = k + s * (s - 1) // 2
	print(k)


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	arr.sort()
	c = 0
	i = 0
	x = 2
	total = 0
	while x < 2 ** 35:
		while i < len(arr) and arr[i] < x:
			c += 1
			i += 1
		total += c * (c - 1) // 2
		c = 0
		x *= 2
	print(total)


import math
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	x = [int(math.log2(l[i])) for i in range(n)]
	x.sort()
	count = 0
	c = 1
	for i in range(1, n):
		if x[i] == x[i - 1]:
			c += 1
		else:
			count += c * (c - 1) // 2
			c = 1
	count += c * (c - 1) // 2
	print(count)


from math import gcd
from bisect import bisect_left, bisect_right
from itertools import combinations
from itertools import permutations
from bisect import bisect_left
from math import ceil
from math import radians
from heapq import heapify, heappush, heappop
import bisect
from math import pi
from collections import deque
from math import factorial
from math import log, ceil
from collections import defaultdict
from math import *
from sys import stdin, stdout
import itertools
import os
import sys
import threading
from collections import deque, Counter, OrderedDict, defaultdict
from heapq import *
from fractions import Fraction
mod = int(pow(10, 9) + 7)

def ii():
	return int(input())

def si():
	return str(input())

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

def li1():
	return list(mi())

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

def fsi():
	return str(stdin.readline())

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

def fli():
	return list(fmi())
abd = {'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25}

def getKey(item):
	return item[0]

def sort2(l):
	return sorted(l, key=getKey)

def d2(n, m, num):
	return [[num for x in range(m)] for y in range(n)]

def isPowerOfTwo(x):
	return x and (not x & x - 1)

def decimalToBinary(n):
	return bin(n).replace('0b', '')

def ntl(n):
	return [int(i) for i in str(n)]

def powerMod(x, y, p):
	res = 1
	x %= p
	while y > 0:
		if y & 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res
graph = defaultdict(list)
visited = [0] * 1000000
col = [-1] * 1000000

def bfs(d, v):
	q = []
	q.append(v)
	visited[v] = 1
	while len(q) != 0:
		x = q[0]
		q.pop(0)
		for i in d[x]:
			if visited[i] != 1:
				visited[i] = 1
				q.append(i)
		print(x)

def make_graph(e):
	d = {}
	for i in range(e):
		(x, y) = mi()
		if x not in d:
			d[x] = [y]
		else:
			d[x].append(y)
		if y not in d:
			d[y] = [x]
		else:
			d[y].append(x)
	return d

def gr2(n):
	d = defaultdict(list)
	for i in range(n):
		(x, y) = mi()
		d[x].append(y)
	return d

def connected_components(graph):
	seen = set()

	def dfs(v):
		vs = set([v])
		component = []
		while vs:
			v = vs.pop()
			seen.add(v)
			vs |= set(graph[v]) - seen
			component.append(v)
		return component
	ans = []
	for v in graph:
		if v not in seen:
			d = dfs(v)
			ans.append(d)
	return ans

def primeFactors(n):
	s = set()
	while n % 2 == 0:
		s.add(2)
		n = n // 2
	for i in range(3, int(sqrt(n)) + 1, 2):
		while n % i == 0 and i % 2 == 1:
			s.add(i)
			n = n // i
	if n > 2 and n % 2 == 1:
		s.add(n)
	return s

def find_all(a_str, sub):
	start = 0
	while True:
		start = a_str.find(sub, start)
		if start == -1:
			return
		yield start
		start += len(sub)

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

def dijkstra(edges, f, t):
	g = defaultdict(list)
	for (l, r, c) in edges:
		g[l].append((c, r))
	(q, seen, mins) = ([(0, f, ())], set(), {f: 0})
	while q:
		(cost, v1, path) = heappop(q)
		if v1 not in seen:
			seen.add(v1)
			path = (v1, path)
			if v1 == t:
				return (cost, path)
			for (c, v2) in g.get(v1, ()):
				if v2 in seen:
					continue
				prev = mins.get(v2, None)
				next = cost + c
				if prev is None or next < prev:
					mins[v2] = next
					heappush(q, (next, v2, path))
	return float('inf')

def binsearch(a, l, r, x):
	while l <= r:
		mid = l + (r - 1) // 2
		if a[mid]:
			return mid
		elif a[mid] > x:
			l = mid - 1
		else:
			r = mid + 1
	return -1

def readTree(n):
	adj = [set() for _ in range(n)]
	for _ in range(n - 1):
		(u, v) = map(int, input().split())
		adj[u - 1].add(v - 1)
		adj[v - 1].add(u - 1)
	return adj

def treeOrderByDepth(n, adj, root=0):
	parent = [-2] + [-1] * (n - 1)
	ordered = []
	q = deque()
	q.append(root)
	depth = [0] * n
	while q:
		c = q.popleft()
		ordered.append(c)
		for a in adj[c]:
			if parent[a] == -1:
				parent[a] = c
				depth[a] = depth[c] + 1
				q.append(a)
	return (ordered, parent, depth)

def isSubSequence(str1, str2):
	m = len(str1)
	n = len(str2)
	j = 0
	i = 0
	while j < m and i < n:
		if str1[j] == str2[i]:
			j = j + 1
		i = i + 1
	return j == m

def nextPowerOf2(n):
	count = 0
	if n and (not n & n - 1):
		return n
	while n != 0:
		n >>= 1
		count += 1
	return 1 << count

def cou(n):
	c = 0
	while n > 1:
		c += 1
		n //= 2
	return c

def sortsec(l):
	return sorted(l, key=lambda x: x[1])

def BinarySearch(a, x):
	i = bisect_left(a, x)
	if i:
		return i - 1
	else:
		return -1

def subarray(A):
	r = set()
	p = {0}
	for x in A:
		p = {x | y for y in p} | {x}
		r |= p
	return len(r)

def setBitNumber(n):
	n |= n >> 1
	n |= n >> 2
	n |= n >> 4
	n |= n >> 8
	n |= n >> 16
	n = n + 1
	return n >> 1

def countSetBits(n):
	count = 0
	while n:
		count += n & 1
		n >>= 1
	return count
for _ in range(ii()):
	n = ii()
	l = li1()
	d = defaultdict(int)
	for i in l:
		d[setBitNumber(i)] += 1
	count = 0
	for i in d.keys():
		count += d[i] * (d[i] - 1) // 2
	print(count)


for _ in range(int(input())):
	n = int(input())
	l = [int(x) for x in input().split()]
	d = {}
	for i in l:
		t = bin(i).replace('0b', '')
		x = t.index('1')
		x = len(t) - x
		if x in d:
			d[x] += 1
		else:
			d[x] = 1
	c = 0
	for i in d:
		c += d[i] * (d[i] - 1) // 2
	print(c)


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')
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().rstrip().split(' ')))
	d = {}
	for i in range(n):
		a = bin(l[i])
		z = len(a) - 2
		if not d.get(z, 0):
			d[z] = 0
		d[z] += 1
	res = 0
	for i in d.values():
		res += (i - 1) * i // 2
	print(res)


n = int(input())
for _ in range(n):
	k = int(input())
	arr = list(map(int, input().split()))
	storage = [0] * 30
	for v in arr:
		for t in range(29, -1, -1):
			if v >> t == 1:
				storage[t] += 1
				break
	total = 0
	for v in storage:
		if v > 1:
			total += v * (v - 1) // 2
	print(total)


for i in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	i = 0
	cnt = [0 for i in range(40)]
	ans = 0
	for i in range(n):
		b = -1
		for j in range(40):
			if l[i] & 1 << j != 0:
				b = j
		ans += cnt[b]
		cnt[b] += 1
	print(ans)


import sys
read = lambda : sys.stdin.readline().strip()
readi = lambda : map(int, read().split())
from collections import *
from math import *
for _ in range(int(read())):
	n = int(read())
	nums = list(readi())
	count = defaultdict(int)
	for num in nums:
		i = 0
		while num > 0:
			num = num >> 1
			i += 1
		count[i] += 1
	ans = 0
	for vals in count.values():
		ans += vals * (vals - 1) // 2
	print(ans)


input = __import__('sys').stdin.readline
for _ in range(int(input())):
	n = int(input())
	a = [*map(int, input().split())]
	count = [0] * 40
	ans = 0
	for i in a:
		(tmp, m) = (1, 0)
		while tmp * 2 <= i:
			tmp *= 2
			m += 1
		ans += count[m]
		count[m] += 1
	print(ans)


from collections import defaultdict
for _ in range(int(input())):
	n = int(input())
	A = list(map(int, input().split()))
	d = defaultdict(int)
	for i in A:
		l = len(bin(i))
		d[l] += 1
	k = 0
	for i in d.values():
		k += i * (i - 1) // 2
	print(k)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	counts = [0] * 32
	for x in a:
		counts[len(bin(x)) - 2] += 1
	total = 0
	for count in counts:
		total += count * (count - 1) // 2
	print(total)


from collections import defaultdict
import sys
import math

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 f(n):
	cnt = 0
	while n:
		n = n // 2
		cnt += 1
	return cnt
for _ in range(int(input())):
	n = int(input())
	a = get_array()
	d = defaultdict(int)
	for i in range(n):
		d[f(a[i])] += 1
	ans = 0
	for i in d.values():
		ans += i * (i - 1) // 2
	print(ans)


from collections import defaultdict, deque
import heapq as hq
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	ans = 0
	for bit in range(32, -1, -1):
		count = 0
		for i in range(n):
			num = arr[i]
			if num != -1 and num & 1 << bit:
				count += 1
				arr[i] = -1
		ans += count * (count - 1) // 2
	print(ans)


from collections import defaultdict as dd

def case():
	n = int(input())
	a = sorted(list(map(int, input().split())))
	res = 0
	l = 0
	for pw in range(1, 31):
		r = l
		while r < n and a[r] < 1 << pw:
			r += 1
		cnt = r - l
		res += cnt * (cnt - 1) // 2
		l = r
	print(res)

def main():
	t = int(input())
	for _ in range(t):
		case()
main()


import math

def get_int():
	return int(input())

def get_ints():
	return [int(i) for i in input().split()]
for t in range(0, get_int()):
	n = get_int()
	array = get_ints()
	array.sort()
	count = 0
	mask = 0
	same_power = 0
	for i in range(0, n):
		if array[i] > mask:
			count += (same_power * same_power - same_power) // 2
			mask += 1
			while mask <= array[i]:
				mask *= 2
			mask -= 1
			same_power = 1
		else:
			same_power += 1
	count += (same_power * same_power - same_power) // 2
	print(count)


from math import log2
t = int(input())
for _ in range(t):
	n = int(input())
	aklst = [0 for i in range(33)]
	a = list(map(int, input().split()))
	for ele in a:
		aklst[int(log2(ele))] += 1
	print(sum([ele * (ele - 1) // 2 for ele in aklst]))


from collections import defaultdict
for _ in range(int(input())):
	n = int(input())
	data = [int(x) for x in input().split()]
	ans = 0
	temp = []
	bag = dict()
	for q in data:
		t = bin(q)
		t = t[2:]
		if t[0] == '0':
			temp.append(0)
		else:
			temp.append(len(t))
	for q in temp:
		if q != 0:
			if q in bag:
				ans += bag[q]
				bag[q] += 1
			else:
				bag[q] = 1
	print(ans)


import sys

def input():
	return sys.stdin.readline().rstrip()
t = int(input())
for i in range(t):
	n = int(input())
	count = [0] * 32
	zero_count = 0
	arr = list(map(int, input().split()))
	for val in arr:
		if val == 0:
			zero_count += 1
		else:
			count[len(bin(val)) - 3] += 1
	ans = 0
	if zero_count > 1:
		ans += zero_count * (zero_count - 1) // 2
	for c in count:
		if c > 1:
			ans += c * (c - 1) // 2
	print(ans)


from sys import stdin, stdout, maxsize
tup = lambda : map(int, stdin.readline().split())
I = lambda : int(stdin.readline())
lint = lambda : [int(x) for x in stdin.readline().split()]
S = lambda : stdin.readline().replace('\n', '').strip()
stpr = lambda x: stdout.write(f'{x}' + '\n')
star = lambda x: print(' '.join(map(str, x)))
from math import ceil, floor
from _collections import defaultdict

def seybit(n):
	n |= n >> 1
	n |= n >> 2
	n |= n >> 4
	n |= n >> 8
	n |= n >> 16
	n += 1
	return n >> 1
for _ in range(I()):
	n = I()
	a = lint()
	cnt = defaultdict(int)
	for i in a:
		x = seybit(i)
		cnt[x] += 1
	ans = 0
	for i in cnt:
		ans += cnt[i] * (cnt[i] - 1) // 2
	print(ans)


import sys

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

def input_split():
	return [int(i) for i in input().split()]
testCases = int(input())
answers = []

def most_sig(x):
	most = 0
	while x != 0:
		x = x // 2
		most += 1
	return most - 1
for _ in range(testCases):
	n = int(input())
	arr = input_split()
	arr = [most_sig(a) for a in arr]
	counts = [0 for i in range(32)]
	counts[arr[0]] += 1
	ans = 0
	for i in range(1, n):
		ans += counts[arr[i]]
		counts[arr[i]] += 1
	answers.append(ans)
print(*answers, sep='\n')


import sys
from collections import Counter
input = sys.stdin.readline

def solve():
	n = int(input())
	L = list(map(int, input().split()))
	M = []
	for i in range(n):
		M.append(len(bin(L[i])))
	c = Counter(M)
	ans = 0
	for i in c:
		ans += c[i] * (c[i] - 1) // 2
	print(ans)
for _ in range(int(input())):
	solve()


t = int(input(''))
for _ in range(t):
	sz = [0] * 31
	n = int(input(''))
	arr = list(map(int, input().split()))
	arr.sort()
	sol = 0
	cnt = 0
	for i in arr:
		x = bin(i)
		x = x[2:]
		sz[len(x)] += 1
	for i in sz:
		if i > 1:
			sol += i * (i - 1) // 2
	print(sol)


from collections import defaultdict
import math

def countBits(number):
	return int(math.log(number) / math.log(2) + 1)
t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	dic = defaultdict(lambda : 0)
	for i in range(n):
		dic[countBits(l[i])] += 1
	ans = 0
	for i in dic.values():
		if i == 1:
			continue
		ans = ans + i * (i - 1) // 2
	print(ans)


from collections import Counter

def solve(arr):
	ans = 0
	c = Counter((i.bit_length() for i in arr))
	for (k, v) in c.items():
		if v > 1:
			ans += v * (v - 1) // 2
	return ans
T = int(input())
for _ in range(T):
	N = int(input())
	arr = list(map(int, input().split()))
	print(solve(arr))


from collections import Counter
t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	arr1 = []
	for i in range(len(arr)):
		bnr = bin(arr[i]).replace('0b', '')
		if bnr[0] == '1':
			arr1.append(len(bnr) - 1)
	p = Counter(arr1)
	count = 0
	for i in p.values():
		if i >= 2:
			count += (i - 1) * (i - 1 + 1) // 2
	print(count)


import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__
from collections import defaultdict

def gift():
	for _ in range(t):
		n = int(input())
		arry = list(map(int, input().split()))
		dic = defaultdict(lambda : 0)
		for ele in arry:
			dic[len(bin(ele))] += 1
		ans = 0
		for (k, v) in dic.items():
			ans += (v - 1) * v // 2
		yield ans
t = int(input())
ans = gift()
print(*ans, sep='\n')


import math

def setBitNumber(n):
	k = int(math.log(n, 2))
	return 2 ** k
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	for i in range(n):
		a[i] = setBitNumber(a[i])
	a.sort()
	b = 0
	s = 0
	for i in range(1, n):
		if a[i] == a[i - 1]:
			b = b + 1
		else:
			s = s + b * (b + 1) // 2
			b = 0
	if b > 0:
		s = s + b * (b + 1) // 2
	print(s)


def process():
	n = int(input())
	l = list(map(int, input().split()))
	counts = [0 for i in range(32)]
	for i in l:
		if i == 0:
			count[0] += 1
		else:
			counts[len(bin(i)) - 1] += 1
	count = 0
	for i in counts:
		if i != 0:
			count += i * (i - 1) // 2
	print(count)

def main():
	for i in range(int(input())):
		process()
main()


t = int(input())

def maxer(n):
	return len(n)
for _ in range(t):
	n = int(input())
	l1 = [bin(int(x))[2:] for x in input().split()]
	crit = len(max(l1, key=maxer))
	l1 = sorted([list(x.rjust(crit, '0')).index('1') for x in l1])
	ans = 0
	for x in set(l1):
		k = l1.count(x)
		ans += k * (k - 1) // 2
	print(ans)


import collections
import math

def solution(n, a):
	b = [int(math.log(x, 2)) for x in a]
	c = collections.Counter(b)
	count = 0
	for i in range(n):
		c[b[i]] -= 1
		count += c[b[i]]
	return count
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split(' ')))
	print(solution(n, a))


from sys import stdin

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

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

def linput():
	return list(map(int, stdin.readline().split()))
from collections import defaultdict, Counter
t = iinput()
while t:
	t -= 1
	n = iinput()
	a = linput()
	a.sort()
	cnt = Counter(a)
	pairs = 0
	bin_arr = [bin(e)[2:] for e in a]
	mx_len = len(bin(a[-1])[2:])
	bin_arr = ['0' * (mx_len - len(e)) + e for e in bin_arr]
	d = defaultdict(list)
	for e in bin_arr:
		i = 0
		while e[i] != '1':
			i += 1
		d[i].append(e)
	for each in d:
		m = len(d[each])
		if m > 1:
			pairs += m * (m - 1) // 2
	print(pairs)


import sys
import math
t = int(input())
for tc in range(t):
	n = int(input())
	ans = 0
	dictionary = dict()
	lst = list(map(int, input().split()))
	for item in lst:
		idx = int(math.log2(item))
		dictionary[idx] = dictionary.get(idx, 0) + 1
	for i in dictionary.keys():
		ans += dictionary[i] * (dictionary[i] - 1) / 2
	print(int(ans))


from __future__ import division, print_function
import os, 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 ii():
	return int(input())

def fi():
	return float(input())

def si():
	return input()

def msi():
	return map(str, input().split())

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

def li():
	return list(mi())

def read():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')

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

def lcm(x, y):
	return x * y // gcd(x, y)
mod = 1000000007

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

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

def modu(a, b, m):
	a = a % m
	inv = modInverse(b, m)
	if inv == -1:
		return -999999999
	else:
		return inv * a % m
from math import log, sqrt, factorial, cos, tan, sin, radians, floor, log2
import bisect
from decimal import *
getcontext().prec = 8
abc = 'abcdefghijklmnopqrstuvwxyz'
pi = 3.141592653589793

def main():
	for _ in range(ii()):
		n = ii()
		a = li()
		cnt = [0] * 30
		for i in range(n):
			x = int(floor(log2(a[i])))
			cnt[x] += 1
		ans = 0
		for i in range(30):
			ans += cnt[i] * (cnt[i] - 1) // 2
		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')

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 math

def check(a):
	d = {}
	for i in range(len(a)):
		k = int(math.log(a[i], 2))
		if k not in d:
			d[k] = 1
		else:
			d[k] += 1
	c = 0
	for (k, v) in d.items():
		if d[k] > 1:
			c += v * (v - 1) // 2
	return c
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	print(check(a))


def ands_bigger_than_xors(l):
	t = {}
	for i in l:
		a = len(bin(i))
		if a not in t:
			t[a] = 0
		t[a] += 1
	ans = 0
	for i in t:
		ans += t[i] * (t[i] - 1) // 2
	return ans
t = int(input())
for k in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	print(ands_bigger_than_xors(l))


def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		a = list(map(int, input().split()))
		d = {}
		for e in a:
			b = bin(e)
			bl = len(b)
			if bl not in d:
				d[bl] = 1
			else:
				d[bl] += 1
		total = 0
		for k in d:
			total += d[k] * (d[k] - 1) // 2
		print(total)
main()


def fact(x):
	if x == 0:
		return 1
	else:
		return x * fact(x - 1)

def lower_bound(li, num):
	answer = -1
	start = 0
	end = len(li) - 1
	while start <= end:
		middle = (end + start) // 2
		if li[middle] >= num:
			answer = middle
			end = middle - 1
		else:
			start = middle + 1
	return answer

def upper_bound(li, num):
	answer = -1
	start = 0
	end = len(li) - 1
	while start <= end:
		middle = (end + start) // 2
		if li[middle] <= num:
			answer = middle
			start = middle + 1
		else:
			end = middle - 1
	return answer

def abs(x):
	return x if x >= 0 else -x

def binary_search(li, val, lb, ub):
	ans = 0
	while lb <= ub:
		mid = (lb + ub) // 2
		if li[mid] > val:
			ub = mid - 1
		elif val > li[mid]:
			lb = mid + 1
		else:
			ans = 1
			break
	return ans

def kadane(x):
	sum_so_far = 0
	current_sum = 0
	for i in x:
		current_sum += i
		if current_sum < 0:
			current_sum = 0
		else:
			sum_so_far = mpos(sum_so_far, current_sum)
	return sum_so_far

def pref(li):
	pref_sum = [0]
	for i in li:
		pref_sum.append(pref_sum[-1] + i)
	return pref_sum

def graph(n, m):
	adj = dict()
	for i in range(1, n + 1):
		adj.setdefault(i, 0)
	for i in range(m):
		(a, b) = map(int, input().split())
		adj[a] += 1
		adj[b] += 1
	return adj
import sys
import threading
from math import *
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	di = dict()
	for i in a:
		ll = len(bin(i)) - 2
		if ll in di:
			di[ll] += 1
		else:
			di[ll] = 1
	ans = 0
	for i in di.values():
		ans += i * (i - 1) // 2
	print(ans)


from bisect import *
from collections import *
from math import gcd, ceil, sqrt, floor, inf
from heapq import *
from typing import List
from itertools import *
from operator import add, mul, sub, xor, truediv, floordiv
from functools import *
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')

def RL():
	return map(int, sys.stdin.readline().rstrip().split())

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

def N():
	return int(input())

@lru_cache(None)
def fact(x, mod=0):
	if x < 2:
		return 1
	if not mod:
		return fact(x - 1) * x
	return fact(x - 1, mod) * x % mod

@lru_cache(None)
def per(i, j, mod=0):
	if i < j:
		return 0
	if not mod:
		return fact(i) // fact(i - j)
	ans = 1
	for k in range(i - j + 1, i + 1):
		ans = ans * k % mod
	return ans

@lru_cache(None)
def com(i, j, mod=0):
	if i < j:
		return 0
	if not mod:
		return per(i, j) // fact(j)
	return per(i, j) * inverse(fact(j), mod) % mod

def catalan(n):
	return com(2 * n, n) // (n + 1)

def linc(f, t, l, r):
	while l < r:
		mid = (l + r) // 2
		if t > f(mid):
			l = mid + 1
		else:
			r = mid
	return l

def rinc(f, t, l, r):
	while l < r:
		mid = (l + r + 1) // 2
		if t < f(mid):
			r = mid - 1
		else:
			l = mid
	return l

def ldec(f, t, l, r):
	while l < r:
		mid = (l + r) // 2
		if t < f(mid):
			l = mid + 1
		else:
			r = mid
	return l

def rdec(f, t, l, r):
	while l < r:
		mid = (l + r + 1) // 2
		if t > f(mid):
			r = mid - 1
		else:
			l = mid
	return l

def isprime(n):
	for i in range(2, int(n ** 0.5) + 1):
		if n % i == 0:
			return False
	return True

def binfun(x):
	c = 0
	for w in arr:
		c += ceil(w / x)
	return c

def lowbit(n):
	return n & -n

def inverse(a, m):
	a %= m
	if a <= 1:
		return a
	return (1 - inverse(m, a) * m) // a % m

class BIT:

	def __init__(self, arr):
		self.arr = arr
		self.n = len(arr) - 1

	def update(self, x, v):
		while x <= self.n:
			self.arr[x] += v
			x += x & -x

	def query(self, x):
		ans = 0
		while x:
			ans += self.arr[x]
			x &= x - 1
		return ans

class smt:

	def __init__(self, l, r, arr):
		self.l = l
		self.r = r
		self.value = (1 << 31) - 1 if l < r else arr[l]
		mid = (l + r) // 2
		if l < r:
			self.left = smt(l, mid, arr)
			self.right = smt(mid + 1, r, arr)
			self.value &= self.left.value & self.right.value

	def setvalue(self, x, val):
		if self.l == self.r:
			self.value = val
			return
		mid = (self.l + self.r) // 2
		if x <= mid:
			self.left.setvalue(x, val)
		else:
			self.right.setvalue(x, val)
		self.value = self.left.value & self.right.value

	def ask(self, l, r):
		if l <= self.l and r >= self.r:
			return self.value
		val = (1 << 31) - 1
		mid = (self.l + self.r) // 2
		if l <= mid:
			val &= self.left.ask(l, r)
		if r > mid:
			val &= self.right.ask(l, r)
		return val

class DSU:

	def __init__(self, n):
		self.c = [-1] * n

	def same(self, x, y):
		return self.find(x) == self.find(y)

	def find(self, x):
		if self.c[x] < 0:
			return x
		self.c[x] = self.find(self.c[x])
		return self.c[x]

	def union(self, u, v):
		(u, v) = (self.find(u), self.find(v))
		if u == v:
			return False
		if self.c[u] < self.c[v]:
			(u, v) = (v, u)
		self.c[u] += self.c[v]
		self.c[v] = u
		return True

	def size(self, x):
		return -self.c[self.find(x)]

class UFS:

	def __init__(self, n):
		self.parent = [i for i in range(n)]
		self.ranks = [0] * n

	def find(self, x):
		if x != self.parent[x]:
			self.parent[x] = self.find(self.parent[x])
		return self.parent[x]

	def union(self, u, v):
		(pu, pv) = (self.find(u), self.find(v))
		if pu == pv:
			return False
		if self.ranks[pu] >= self.ranks[pv]:
			self.parent[pv] = pu
			if self.ranks[pv] == self.ranks[pu]:
				self.ranks[pu] += 1
		else:
			self.parent[pu] = pv

def Prime(n):
	c = 0
	prime = []
	flag = [0] * (n + 1)
	for i in range(2, n + 1):
		if not flag[i]:
			prime.append(i)
			c += 1
		for j in range(c):
			if i * prime[j] > n:
				break
			flag[i * prime[j]] = prime[j]
			if i % prime[j] == 0:
				break
	return prime

def dij(s, graph):
	d = {}
	d[s] = 0
	heap = [(0, s)]
	seen = set()
	while heap:
		(dis, u) = heappop(heap)
		if u in seen:
			continue
		for v in graph[u]:
			if v not in d or d[v] > d[u] + graph[u][v]:
				d[v] = d[u] + graph[u][v]
				heappush(heap, (d[v], v))
	return d
t = N()
for i in range(t):
	n = N()
	a = RLL()
	res = [0] * 32
	for x in a:
		cur = 0
		while x:
			x //= 2
			cur += 1
		res[cur] += 1
	ans = sum((res[i] * (res[i] - 1) // 2 for i in range(32)))
	print(ans)


def comb(a, b):
	c = 1
	for i in range(a - b + 1, a + 1):
		c = c * i
	return int(c / 2)
from collections import Counter
for o in range(int(input())):
	n = int(input())
	s = list(map(int, input().split()))
	for i in range(n):
		s[i] = len(bin(s[i]))
	r = Counter(s)
	c = 0
	for i in r.values():
		if i > 1:
			c = c + comb(i, 2)
	print(c)


import math
import collections

def main():
	n = int(input())
	a = list(map(int, input().split()))
	b = [math.floor(math.log2(e)) for e in a]
	c = collections.Counter(b)
	ans = 0
	for key in c:
		if c[key] >= 2:
			ans += c[key] * (c[key] - 1) // 2
	print(ans)
t = int(input())
for i in range(t):
	main()


import math
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	counters = [0] * 32
	res = 0
	for i in range(n):
		count = int(math.log2(arr[i]))
		res += counters[count]
		counters[count] += 1
	print(res)


I = lambda : map(int, input().split())
for _ in [0] * int(input()):
	n = int(input())
	a = list(I())
	d = {}
	op = 0
	for i in range(n):
		yi = len(bin(a[i])) - 2
		if yi not in d:
			d[yi] = 0
		else:
			d[yi] = d[yi] + 1
			op = op + d[yi]
	print(op)


import sys
import math as m

def main():
	pass

def binary(n):
	return bin(n).replace('0b', '')

def decimal(s):
	return int(s, 2)

def pow2(n):
	p = 0
	while n > 1:
		n //= 2
		p += 1
	return p

def isPrime(n):
	if n == 1:
		return False
	else:
		root = int(n ** 0.5)
		root += 1
		for i in range(2, root):
			if n % i == 0:
				return False
		return True

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

def iinp():
	return int(input())

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

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

def p(a):
	print(a)
for _ in range(iinp()):
	n = iinp()
	a = linp()
	curr = 1
	cntm = 0
	cnt = 0
	a.sort()
	for i in a:
		s = binary(i)
		if len(s) == curr:
			cnt += 1
		elif cnt == 1:
			curr = len(s)
			cnt = 1
		else:
			cntm = cntm + (cnt - 1) * cnt // 2
			curr = len(s)
			cnt = 1
	if cnt == 1:
		pass
	else:
		cntm = cntm + (cnt - 1) * cnt // 2
	print(cntm)


from sys import stdin, stdout

def setBitNumber(n):
	msb = 0
	n = n // 2
	while n > 0:
		n = n // 2
		msb += 1
	bits[msb] += 1
for _ in range(int(stdin.readline())):
	n = int(stdin.readline())
	a = list(map(int, stdin.readline().split()))
	bits = [0] * 34
	for v in a:
		setBitNumber(v)
	ans = 0
	for v in bits:
		ans += v * (v - 1) // 2
	print(ans)


import math

def nc2(n):
	return int(n * (n - 1) / 2)
t = int(input())
for h in range(t):
	k = int(input())
	n = list(map(int, input().split()))
	n.sort()
	j = 0
	app = 0
	ans = 0
	for i in range(k):
		if j == int(math.log(n[i], 2)):
			app += 1
		elif j < int(math.log(n[i], 2)):
			ans += nc2(app)
			app = 1
			j = int(math.log(n[i], 2))
	ans += nc2(app)
	print(ans)


def main():
	for _ in range(int(input())):
		n = int(input())
		a = [int(x) for x in input().split()]
		cnt = [0] * 30
		for i in range(n):
			(cur, tmp) = (1, 1)
			while cur <= a[i]:
				cur *= 2
				tmp += 1
			cnt[tmp - 2] += 1
		print(sum([x * (x - 1) // 2 for x in cnt]))
main()


t = int(input())
while t:
	t -= 1
	n = int(input())
	l = list(map(int, input().split()))
	pairs = 0
	d = {}
	for (i, v) in enumerate(l):
		x = 32
		while x > 0:
			if v & 1 << x:
				l[i] = 1 << x
				break
			x -= 1
		x = d.get(l[i], 0)
		if x:
			pairs += x
		d[l[i]] = x + 1
	print(pairs)


import sys
input = sys.stdin.readline
import copy
t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	ans = [0 for _ in range(40)]
	for j in a:
		check = 1
		while True:
			if 2 ** check > j:
				ans[check] += 1
				break
			check += 1
	res = 0
	for k in ans:
		res += k * (k - 1) / 2
	print(int(res))


import sys, os, io

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

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

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

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

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

def wia(a):
	sys.stdout.write(' '.join([str(x) for x in a]) + '\n')
import math, datetime, functools, itertools, operator, bisect, fractions
from collections import deque, defaultdict, OrderedDict, Counter
from fractions import Fraction
from decimal import Decimal
N = 1000000
factorialNumInverse = [None] * (N + 1)
naturalNumInverse = [None] * (N + 1)
fact = [None] * (N + 1)

def InverseofNumber(p):
	naturalNumInverse[0] = naturalNumInverse[1] = 1
	for i in range(2, N + 1, 1):
		naturalNumInverse[i] = naturalNumInverse[p % i] * (p - int(p / i)) % p

def InverseofFactorial(p):
	factorialNumInverse[0] = factorialNumInverse[1] = 1
	for i in range(2, N + 1, 1):
		factorialNumInverse[i] = naturalNumInverse[i] * factorialNumInverse[i - 1] % p

def factorial(p):
	fact[0] = 1
	for i in range(1, N + 1):
		fact[i] = fact[i - 1] * i % p

def Binomial(N, R, p):
	ans = fact[N] * factorialNumInverse[R] % p * factorialNumInverse[N - R] % p
	return ans

def main():
	starttime = datetime.datetime.now()
	if os.path.exists('input.txt'):
		sys.stdin = open('input.txt', 'r')
		sys.stdout = open('output.txt', 'w')
	r = []
	for j in range(32):
		r.append([2 ** j, 2 ** (j + 1)])
	for _ in range(ri()):
		n = ri()
		a = ria()
		z = r.copy()
		d = {}
		for i in a:
			for j in range(32):
				if i >= z[j][0] and i < z[j][1]:
					if j in d:
						d[j] += 1
					else:
						d[j] = 1
					break
		ans = 0
		for i in d:
			if d[i] >= 2:
				ans += d[i] * (d[i] - 1) // 2
		print(ans)
	endtime = datetime.datetime.now()
	time = (endtime - starttime).total_seconds() * 1000
	if os.path.exists('input.txt'):
		print('Time:', time, 'ms')

class FastReader(io.IOBase):
	newlines = 0

	def __init__(self, fd, chunk_size=1024 * 8):
		self._fd = fd
		self._chunk_size = chunk_size
		self.buffer = io.BytesIO()

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size))
			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, size=-1):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size if size == -1 else size))
			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()

class FastWriter(io.IOBase):

	def __init__(self, fd):
		self._fd = fd
		self.buffer = io.BytesIO()
		self.write = self.buffer.write

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

class FastStdin(io.IOBase):

	def __init__(self, fd=0):
		self.buffer = FastReader(fd)
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

class FastStdout(io.IOBase):

	def __init__(self, fd=1):
		self.buffer = FastWriter(fd)
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.flush = self.buffer.flush
sys.stdin = FastStdin()
sys.stdout = FastStdout()
main()


from math import log2
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	count = [0] * 32
	for i in range(n):
		idx = int(log2(arr[i]))
		count[idx] += 1
	res = 0
	for i in range(32):
		res += count[i] * (count[i] - 1) // 2
	print(res)


t = int(input())
import math
for _ in range(t):
	n = int(input())
	nums = list(map(int, input().split()))
	ans = 0
	lastBit = [0] * n
	dic = {}
	for i in range(n):
		power = math.log(nums[i], 2)
		power = math.floor(power) + 1
		lastBit[i] = power
	for i in reversed(range(n)):
		bit = lastBit[i]
		if bit in dic:
			ans += dic[bit]
			dic[bit] += 1
		else:
			dic[bit] = 1
	print(ans)


import math
for t in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	counter = dict()
	for i in range(n):
		ele = int(math.log2(arr[i]))
		str_ele = str(ele)
		if str_ele in counter:
			counter[str_ele] += 1
		else:
			counter[str_ele] = 1
	count = 0
	for i in counter:
		j = counter[i]
		if j > 1:
			count += int(j * (j - 1) / 2)
	print(count)


from collections import Counter
import math
for _ in ' ' * int(input()):
	n = int(input())
	a = list(map(int, input().split()))
	for i in range(n):
		a[i] = 2 ** int(math.log(a[i], 2))
	lst = Counter(a)
	cnt = 0
	for i in lst.values():
		cnt += i * (i - 1) // 2
	print(cnt)


from sys import stdin, stdout, setrecursionlimit

class Tail_Recursion_Optimization:

	def __init__(self, RECURSION_LIMIT, STACK_SIZE):
		setrecursionlimit(RECURSION_LIMIT)
		threading.stack_size(STACK_SIZE)
		return None

class SOLVE:

	def solve(self):
		R = stdin.readline
		W = stdout.write
		ans = []
		for i in range(int(R())):
			n = int(R())
			a = [int(x) for x in R().split()]
			b = {}
			for j in range(n):
				l = len(bin(a[j]))
				if l not in b:
					b[l] = 1
				else:
					b[l] += 1
			pairs = 0
			for j in range(n):
				l = len(bin(a[j]))
				b[l] -= 1
				if b[l]:
					pairs += b[l]
			ans.append(str(pairs))
		W('\n'.join(ans))
		return 0

def main():
	s = SOLVE()
	s.solve()
main()


def sen(n):
	if n == 0:
		return 0
	msb = 0
	n = int(n / 2)
	while n > 0:
		n = int(n / 2)
		msb += 1
	return 1 << msb
for _ in range(int(input())):
	k = int(input())
	a = list(map(int, input().split()))
	s = dict()
	t = []
	ans = 0
	hb = []
	for elem in a:
		hb.append(sen(elem))
	for elem in set(hb):
		t.append(hb.count(elem))
	for elem in t:
		ans += elem * (elem - 1) // 2
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	d = [0] * 35
	ans = 0
	for x in a:
		k = x.bit_length() - 1
		ans += d[k]
		d[k] += 1
	print(ans)


import math
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split(' ')))
	l = [0] * 30
	for i in a:
		l[math.floor(math.log(i, 2))] += 1
	ans = 0
	for i in l:
		if i >= 2:
			ans += i * (i - 1) // 2
	print(ans)


def main(length, arr):
	pass
	groups_sizes = {}
	result = 0
	for number in arr:
		binary_form = '{0:b}'.format(number)
		value = groups_sizes.get(len(binary_form), 0)
		groups_sizes[len(binary_form)] = value + 1
	for key in groups_sizes:
		value = groups_sizes[key]
		sub_result = value * (value - 1) // 2
		result += sub_result
	return result
tt = int(input())
for _ in range(tt):
	length = int(input())
	arr = list(map(int, input().split(' ')))
	print(main(length, arr))


for _ in range(int(input())):
	n = int(input())
	arr = [int(x) for x in input().split()]
	d = {}
	for i in range(n):
		a = arr[i]
		b = bin(a)[2:]
		c = len(b)
		if c in d.keys():
			d[c] += 1
		else:
			d[c] = 1
	ans = 0
	for i in d.keys():
		ans += d[i] * (d[i] - 1) // 2
	print(ans)


from collections import defaultdict
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	dict = defaultdict(int)
	for i in a:
		s = str(bin(i))
		s = s[2:]
		dict[len(s)] += 1
	su = 0
	for i in dict.keys():
		if dict[i] >= 2:
			su += dict[i] * (dict[i] - 1) // 2
	print(su)


def msb(n):
	return len(bin(n)) - 2
for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	d = {}
	for j in a:
		m = msb(j)
		if m in d:
			d[m] += 1
		else:
			d[m] = 1
	ans = 0
	for jj in d:
		ans += d[jj] * (d[jj] - 1) // 2
	print(ans)


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


import math

def getFirstSetBitPos(n):
	return math.log2(n & -n) + 1
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	d = {}
	cnt = 0
	for i in range(n):
		l = len(bin(arr[i])[2:])
		d[l] = d.get(l, 0) + 1
	for i in d:
		cnt += d[i] * (d[i] - 1) // 2
	print(cnt)


import math

def bitss(x):
	return int(math.log2(x)) + 1
t = int(input())
for o in range(t):
	n = int(input())
	l = str.split(input(), ' ')
	for i in range(n):
		l[i] = int(l[i])
	d = {}
	for i in range(n):
		if d.get(bitss(l[i])) == None:
			d[bitss(l[i])] = 1
		else:
			d[bitss(l[i])] = d[bitss(l[i])] + 1
	arr = d.keys()
	ans = 0
	for i in arr:
		temp = d[i]
		temp = temp * (temp - 1)
		temp = temp // 2
		ans = ans + temp
	print(ans)


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


import math
t = int(input())
for t1 in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	l = [0 for i in range(64)]
	for i in range(n):
		l[int(math.log(a[i], 2))] += 1
	c = 0
	for i in range(64):
		c += l[i] * (l[i] - 1) // 2
	print(c)


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

def str(x=b''):
	return x if type(x) is bytes else _str(x).encode()

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()

def mpint():
	return map(int, sys.stdin.readline().split(' '))

def itg():
	return int(sys.stdin.readline())
from bisect import bisect_left, bisect_right

def low(x):
	return 1 << x.bit_length() - 1

def high(x):
	return (1 << x.bit_length()) - 1

def solve():
	n = itg()
	arr = sorted(mpint())
	ans = 0
	for a in arr:
		lo = low(a)
		hi = high(a)
		ans += bisect_right(arr, hi) - bisect_left(arr, lo) - 1
	return ans >> 1
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
for _ in range(itg()):
	print(solve())


for _ in range(int(input())):
	n = int(input())
	d = [0 for i in range(40)]
	for x in input().split(' '):
		k = int(x)
		if k == 0:
			d[0] += 1
			continue
		b = bin(k)[2:]
		l = len(b)
		b = b.find('1')
		d[l - b] += 1
	ans = 0
	for i in d:
		if i > 1:
			n = i
			ans += n * (n - 1) // 2
	print(ans)


T = int(input())
ans_ls = [0] * T
for t in range(T):
	N = int(input())
	a_ls = list(map(int, input().split()))
	bit_ls = [[0] * 30 for _ in range(N)]
	head_count = [0] * 30
	ans = 0
	total_head = float('inf')
	for i in range(N):
		ls = []
		a = a_ls[i]
		while a > 0:
			(a, r) = divmod(a, 2)
			ls.append(r)
		for _ in range(30 - len(ls)):
			ls.append(0)
		ls = ls[::-1]
		head = -1
		for j in range(30):
			if ls[j] == 1:
				head = j
				break
		add = head_count[head]
		ans += add
		head_count[head] += 1
	ans_ls[t] = ans
for ans in ans_ls:
	print(ans)


results = []
ts = int(input())
for t in range(ts):
	input()
	count = 0
	A = [int(x) for x in input().split()]
	B = [0] * 32
	for n in A:
		B[len(bin(n)) - 3] += 1
	for b in B:
		count += b * (b - 1) // 2
	results.append(str(count))
print(' '.join(results))


def max2(num):
	count = 1
	while num > 1:
		count += 1
		num = num // 2
	return count
t = int(input())
while t:
	d1 = {}
	n = int(input())
	a = list(map(int, input().split()))
	for i in range(0, n):
		p = max2(a[i])
		if p in d1.keys():
			d1[p] += 1
		else:
			d1[p] = 1
	final = 0
	for i in list(d1.keys()):
		x = d1[i] * (d1[i] - 1)
		final = final + x // 2
	print(final)
	t = t - 1


from math import log
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	a = [int(log(i, 2)) for i in a]
	d = {}
	for i in a:
		if i in d:
			d[i] += 1
		else:
			d[i] = 1
	cnt = 0
	for i in d:
		cnt += d[i] * (d[i] - 1) // 2
	print(cnt)


for _ in range(int(input())):
	n = int(input())
	lis = list(map(int, input().split()))
	num = []
	for i in lis:
		v1 = bin(i)[2:]
		num.append(len(v1))
	co = 0
	cou = [0 for i in range(50)]
	cou[num[0]] += 1
	for i in range(1, n, 1):
		co += cou[num[i]]
		cou[num[i]] += 1
	print(co)


import math
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = [0] * 32
	for i in range(n):
		b[int(math.log(a[i], 2))] += 1
	s = 0
	for i in b:
		s += i * (i - 1) // 2
	print(s)


from sys import stdin
input = stdin.readline
from math import log2

def solve():
	n = int(input())
	a = list(map(int, input().split()))
	cnt = [0] * 32
	res = 0
	for i in a:
		cnt[int(log2(i))] += 1
	for i in cnt:
		res += (i - 1) * i // 2
	print(res)
t = int(input())
for i in range(t):
	solve()


import os
import sys
from io import BytesIO, IOBase

def main1():
	for _ in range(int(input())):
		n = int(input())
		a = list(map(int, input().split()))
		cnt = 0
		for i in range(1, n):
			if a[i] < a[i - 1]:
				cnt += 1
		if cnt == n - 1:
			print('NO')
		else:
			print('YES')

def main2():
	for _ in range(int(input())):
		n = int(input())
		a = list(map(int, input().split()))
		ans = 0
		cnt = [0] * 100
		for i in range(n):
			c = len(bin(a[i])[2:])
			ans += cnt[c]
			cnt[c] += 1
		print(ans)

def main3():
	for _ in range(int(input())):
		(n, q) = map(int, input().split())
		a = list(map(int, input().split()))
		flag = 1
		ans = 0
		for i in range(1, n - 1):
			if flag:
				if a[i - 1] < a[i] > a[i + 1]:
					ans += a[i]
					flag = 0
			if not flag:
				if a[i - 1] > a[i] < a[i + 1]:
					ans -= a[i]
					flag = 1
		if flag:
			if a[n - 1] > a[n - 2]:
				ans += a[n - 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')
main2()


import sys
t = int(input())
comb = [[0] * 3 for _ in range(100001)]
comb[1][0] = 1
comb[1][1] = 1
for i in range(2, 100001):
	comb[i][0] = 1
	for j in range(1, 3):
		comb[i][j] = comb[i - 1][j - 1] + comb[i - 1][j]
for _ in range(t):
	n = int(sys.stdin.readline())
	arr = list(map(int, sys.stdin.readline().split()))
	bitArr = [0] * 50
	ans = 0
	for x in arr:
		bitArr[x.bit_length()] += 1
	for x in bitArr:
		if x >= 2:
			ans += comb[x][2]
	print(ans)


import sys
import math
from collections import Counter, defaultdict
LI = lambda : list(map(int, input().split()))
MAP = lambda : map(int, input().split())
IN = lambda : int(input())
S = lambda : input()

def case():
	n = IN()
	a = LI()
	ans = [0] * 32
	x = 0
	for i in range(n):
		m = 0
		for j in range(32, -1, -1):
			if pow(2, j) & a[i]:
				m = j
				break
		x += ans[m]
		ans[m] += 1
	print(x)
for _ in range(IN()):
	case()


for _ in range(int(input())):
	n = int(input())
	arr = [int(x) for x in input().split()]
	d = {}
	ans = []
	for i in range(n):
		ans.append(bin(arr[i])[2:])
	for i in range(n):
		if len(ans[i]) in d:
			d[len(ans[i])] += 1
		else:
			d[len(ans[i])] = 1
	val = 0
	for (i, j) in d.items():
		val += j * (j - 1) // 2
	print(val)


import math
t = int(input())
for _ in range(t):
	ans = 0
	logarithms = [0] * 30
	n = int(input())
	arr = list(map(int, input().split()))
	for el in arr:
		logarithms[math.floor(math.log(el, 2))] += 1
	for i in range(len(logarithms)):
		ans += logarithms[i] * (logarithms[i] - 1) / 2
	print(int(ans))


from collections import Counter
t = int(input())
for __ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	d = Counter()
	for i in range(len(arr)):
		d[len(bin(arr[i]))] += 1
	c = 0
	for i in d:
		c += d[i] * (d[i] - 1) // 2
	print(c)


from collections import defaultdict
for t in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	frq = defaultdict(lambda : 0)
	for i in arr:
		frq[len(bin(i)[2:])] += 1
	ans = 0
	for i in frq:
		ans += frq[i] * (frq[i] - 1) // 2
	print(ans)


from collections import defaultdict
import math

def solve(arr):
	count = 0
	s = defaultdict(int)
	for i in range(len(arr)):
		s[int(math.log2(arr[i]))] += 1
	for i in s:
		count += s[i] * (s[i] - 1) // 2
	return count
t = int(input())
for _ in range(t):
	n = int(input())
	s = list(map(int, input().split()))
	print(solve(s))


import sys
import collections
mod = 1000000007

def set_io():
	fin = open('input.txt', 'r')
	fout = open('output.txt', 'w')
	sys.stdin = fin
	sys.stdout = fout

def solution(arr, n):
	bits = collections.defaultdict(int)
	for i in range(n):
		num = arr[i]
		count = 0
		while num > 0:
			num = num >> 1
			count += 1
		bits[count] += 1
	npairs = 0
	for i in range(1, 32):
		npairs += bits[i] * (bits[i] - 1) // 2
	print(npairs)

def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		arr = list(map(int, input().strip().split()))
		solution(arr, n)
main()


from sys import stdin, stdout
from math import *
testing = False

def tp(*args):
	pass
if testing:
	tp = print

def largest(n):
	best = 1
	while best <= n:
		best *= 2
	return int(best / 2)

def main():
	t = int(stdin.readline())
	for _ in range(t):
		n = int(stdin.readline())
		a = [int(i) for i in stdin.readline().split()]
		b = {}
		for i in range(n):
			val = largest(a[i])
			b[val] = b.get(val, 0) + 1
		count = 0
		tp(b)
		for i in b.keys():
			val = b[i]
			op = int(val * (val - 1) / 2)
			count += op
		print(count)
main()


t = int(input())
for _ in range(t):
	n = int(input())
	L = sorted(list(map(int, input().split())))
	C = []
	z = 1
	for i in range(31):
		C.append(z)
		z = z * 2
	c = a = 0
	d = -1
	for j in range(31):
		if L[0] < C[j]:
			b = j
			break
	for i in L:
		if i < C[b]:
			d = d + 1
			if d > 0:
				a = a + d
		else:
			for j in range(b + 1, 31):
				if i < C[j]:
					b = j
					break
			c = c + a
			a = d = 0
	c = c + a
	print(c)


from os import path
import sys
from heapq import heappush, heappop
from functools import cmp_to_key as ctk
from collections import deque, defaultdict as dd
from bisect import bisect, bisect_left, bisect_right, insort, insort_left, insort_right
from itertools import permutations
from datetime import datetime
from math import ceil, sqrt, log, gcd

def ii():
	return int(input())

def si():
	return input().rstrip()

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

def li():
	return list(mi())
abc = 'abcdefghijklmnopqrstuvwxyz'
mod = 998244353
inf = float('inf')
vow = ['a', 'e', 'i', 'o', 'u']
(dx, dy) = ([-1, 1, 0, 0], [0, 0, 1, -1])

def bo(i):
	return ord(i) - ord('a')

def solve():
	for _ in range(ii()):
		n = ii()
		a = li()
		f = dd(int)
		for i in a:
			for j in range(30, -1, -1):
				if i & 1 << j:
					f[j] += 1
					break
		ans = 0
		for i in f.values():
			ans += i * (i - 1) // 2
		print(ans)
if path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
else:
	input = sys.stdin.readline
solve()


T = int(input())
for t in range(T):
	n = int(input())
	A = list(map(int, input().split()))
	bits = dict()
	for i in range(n):
		k = A[i].bit_length()
		if k not in bits:
			bits[k] = 0
		bits[k] += 1
	ans = 0
	for x in bits.values():
		ans += x * (x - 1) / 2
	print(int(ans))


import math
for _ in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	dicti = {}
	for z in range(n):
		r = int(math.log(a[z], 2))
		if r in dicti:
			dicti[r] += 1
		else:
			dicti[r] = 1
	ans = 0
	for (y, z) in dicti.items():
		ans += z * (z - 1) // 2
	print(ans)


import math
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	(d, ans) = ({}, 0)
	for i in range(n):
		r = int(math.log(a[i], 2)) + 1
		if r not in d:
			d[r] = 1
		else:
			d[r] += 1
	for i in d:
		ans += d[i] * (d[i] - 1) // 2
	print(ans)


from collections import *
for T in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(lambda x: len(bin(x)[2:]), a))
	b.sort()
	(sum, cnt) = (0, 1)
	for (i, x) in enumerate(b):
		if i > 0:
			if b[i] != b[i - 1]:
				sum += cnt * (cnt - 1) >> 1
				cnt = 1
			else:
				cnt += 1
	sum += cnt * (cnt - 1) >> 1
	print(sum)


for _ in range(int(input())):
	input()
	a = list(map(int, input().split()))
	c = {}
	for x in a:
		h = x.bit_length()
		c[h] = c.get(h, 0) + 1
	print(sum((v * (v - 1) // 2 for v in c.values())))


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	num = []
	for i in range(n):
		x = bin(l[i])[2:]
		num.append(x)
	count = 0
	num1 = [0] * 100000
	for i in range(n):
		num1[len(bin(l[i]))] += 1
	for i in num1:
		count += i * (i - 1) // 2
	print(count)


from math import log
t = int(input())
for _ in range(t):
	n = int(input())
	dic = {}
	for temp in map(int, input().split()):
		key = int(log(temp) / log(2))
		a = dic.get(key, 0)
		dic[key] = a + 1
	res = 0
	for elm in dic:
		temp = dic[elm]
		res += temp * (temp - 1) // 2
	print(res)


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


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()
	bit_cnt = defaultdict(int)
	for val in ls:
		bit_cnt[int(math.log(val, 2))] += 1
	ans = 0
	for (key, val) in bit_cnt.items():
		ans += val * (val - 1) // 2
	print(ans)

def main():
	solve()
TestCases = True
if TestCases:
	for i in range(get_int()):
		main()
else:
	main()


T = int(input())
for _ in range(T):
	n = int(input())
	a = list(map(int, input().split()))
	tot = 0
	visited = set()
	for i in range(29, -1, -1):
		tmp = 0
		for j in range(len(a)):
			if j in visited:
				continue
			if a[j] & 1 << i:
				tmp += 1
				visited.add(j)
		tot += tmp * (tmp - 1) // 2
	print(tot)


import sys
input = sys.stdin.readline

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().strip()

def listStr():
	return list(input().strip())
import collections as col
import math, itertools

def solve():
	N = getInt()
	A = getInts()
	len_count = col.defaultdict(int)
	for a in A:
		x = len(bin(a))
		len_count[x] += 1
	ans = 0
	for key in len_count:
		z = len_count[key]
		ans += z * (z - 1) // 2
	return ans
for _ in range(getInt()):
	print(solve())


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	a = sorted(a)
	cnt = 0
	cnt1 = 0
	for i in range(1, n):
		if a[i] & a[i - 1] >= a[i] ^ a[i - 1]:
			cnt += 1
			cnt1 += cnt
		else:
			cnt = 0
	print(cnt1)


import os, sys
from collections import defaultdict
from io import BytesIO, IOBase

def main():
	for i in range(int(input())):
		n = int(input())
		a = [int(x) for x in input().split()]
		x = defaultdict(lambda : 0)
		for j in a:
			x[len(bin(j)) - 2] += 1
		an = 0
		for j in x:
			if x[j] > 1:
				an += (x[j] - 1) * x[j] // 2
		print(an)
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()


for t in range(int(input())):
	n = int(input())
	a = [len(bin(int(x))[2:]) for x in input().split()]
	a.sort()
	a.append(100)
	ans = 0
	tmp = 1
	for i in range(1, len(a)):
		if a[i - 1] == a[i]:
			tmp = tmp + 1
		else:
			ans = ans + tmp * (tmp - 1) / 2
			tmp = 1
	print(int(ans))


import sys
input = sys.stdin.readline
from bisect import bisect_right
bin_s = [1]
while bin_s[-1] <= 10 ** 9:
	bin_s.append(bin_s[-1] * 2)

def main():
	n = int(input())
	alst = list(map(int, input().split()))
	cnt = [0 for _ in range(35)]
	for a in alst:
		pos = bisect_right(bin_s, a)
		cnt[pos] += 1
	ans = 0
	for num in cnt:
		ans += num * (num - 1) // 2
	print(ans)
for _ in range(int(input())):
	main()


from collections import defaultdict
t = int(input())
while t != 0:
	d = defaultdict(int)
	n = int(input())
	list1 = list(map(int, input().split()))
	list2 = []
	for i in range(len(list1)):
		list2.append(len(bin(list1[i])[2:]))
	for i in range(len(list2)):
		d[list2[i]] += 1
	c = 0
	for i in d.keys():
		if d[i] > 1:
			c += d[i] * (d[i] - 1) // 2
	print(c)
	t -= 1


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	d = {}
	for i in range(n):
		t = 0
		while arr[i] > 0:
			arr[i] >>= 1
			t += 1
		if t not in d:
			d[t] = 0
		d[t] += 1
	ans = 0
	for i in d.values():
		ans += i * (i - 1) // 2
	print(ans)


t = int(input())
for test in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	li = [0 for j in range(50)]
	for i in range(n):
		num = bin(a[i])
		num = num[2:]
		nn = len(num)
		li[nn] = li[nn] + 1
	pairs = 0
	for i in range(len(li)):
		bb = li[i]
		res = bb * (bb - 1) // 2
		pairs += res
	print(pairs)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	ans = 0
	a.sort()
	s = 0
	for i in range(n - 1):
		if a[i] & a[i + 1] >= a[i] ^ a[i + 1]:
			s += 1
			ans += s
		else:
			s = 0
	print(ans)


import sys
import bisect as bi
import math
from collections import defaultdict as dd
import heapq
import itertools
import io
import os
input = sys.stdin.readline
import random
mo = 10 ** 9 + 7

def cin():
	return map(int, sin().split())

def ain():
	return list(map(int, sin().split()))

def sin():
	return input()

def inin():
	return int(input())

def msb(n):
	n |= n >> 1
	n |= n >> 2
	n |= n >> 4
	n |= n >> 8
	n |= n >> 16
	n |= n >> 32
	n |= n >> 64
	return n - (n >> 1)

def pref(a, n, f):
	pre = [0] * n
	if f == 0:
		pre[0] = a[0]
		for i in range(1, n):
			pre[i] = a[i] + pre[i - 1]
	else:
		pre[-1] = a[-1]
		for i in range(n - 2, -1, -1):
			pre[i] = pre[i + 1] + a[i]
	return pre
for _ in range(inin()):
	n = inin()
	l = ain()
	d = dd(int)
	for i in l:
		d[msb(i)] += 1
	ans = 0
	for i in d.values():
		ans += i * (i - 1) // 2
	print(ans)


from sys import stdin, stdout
import heapq as hq
from math import log, floor
from collections import defaultdict
mem = {}
t = int(input())
for tc in range(t):
	n = int(stdin.readline())
	ar = list(map(int, stdin.readline().split()))
	res = 0
	c = defaultdict(int)
	for item in ar:
		c[floor(log(item, 2))] += 1
	for key in c:
		if c[key] > 1:
			res += c[key] * (c[key] - 1) // 2
	stdout.write(str(res) + '\n')


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


T = int(input())
for _ in range(T):
	n = int(input())
	a = list(map(int, input().split()))
	cnt = [0] * 35
	for aa in a:
		cnt[aa.bit_length()] += 1
	ans = 0
	for c in cnt:
		ans += c * (c - 1) // 2
	print(ans)


rr = lambda : input().strip()
rri = lambda : int(rr())
rrm = lambda : [int(x) for x in rr().split()]
from sys import stdin, stdout, setrecursionlimit
from math import ceil

def sol():
	n = rri()
	v = rrm()
	fr = [0 for i in range(32)]
	for i in v:
		x = 1 << 31
		j = 31
		while x > 0:
			if x & i:
				fr[j] += 1
				break
			x //= 2
			j -= 1
	res = 0
	for i in fr:
		res += i * (i - 1) // 2
	print(res)
	return

def main():
	T = rri()
	for t in range(1, T + 1):
		ans = sol()
main()


from math import log2, floor
t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	d = {}
	for i in l:
		num = floor(log2(i)) + 1
		d[num] = d[num] + 1 if d.get(num) else 1
	s = 0
	for i in d.keys():
		s += d[i] * (d[i] - 1) // 2
	print(s)


from collections import Counter
t = int(input())
for _ in range(t):
	n = int(input())
	li = list(map(int, input().split()))
	ans = []
	for i in li:
		ans.append(len(bin(i)[2:]))
	c = Counter(ans)
	a = 0
	for i in c:
		a += c[i] * (c[i] - 1) // 2
	print(a)


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	g = [0] * 100
	b = 1
	k = 0
	c = 0
	for j in range(n):
		if a[j] >= b:
			while a[j] >= b:
				b = b * 2
			g[c] = j - k
			c = c + 1
			k = j
		if j == n - 1:
			g[c] = n - k
			c = c + 1
	sum0 = 0
	for j in range(c):
		sum0 = sum0 + g[j] * (g[j] - 1) // 2
	print(sum0)


for _ in range(int(input())):
	n = int(input())
	ar = list(map(int, input().split()))
	ar.sort()
	ans = 0
	bbb = [0, 0, 0]
	t = []
	jj = False
	xxx = 0
	for i in range(n):
		t.append(len(bin(ar[i])[2:]))
	i = 0
	last = 0
	count = 0
	while i < n:
		if last == t[i]:
			count += 1
			xxx += 1
		else:
			ans += count * (count + 1) // 2
			last = t[i]
			count = 0
			jj = True
		i += 1
	ans += count * (count + 1) // 2
	print(ans)


def algo(n, arr):
	res = 0
	for i in range(32, -1, -1):
		c = 0
		for j in arr:
			if j >= 1 << i and j < 1 << i + 1:
				c += 1
		res += c * (c - 1) // 2
	return res
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	ans = algo(n, arr)
	print(ans)


import math
steps = int(input())
for i in range(steps):
	arr1 = []
	t = 0
	c = -1
	n = int(input())
	y = [int(k) for k in input().split()]
	for j in range(n):
		arr1.append(len(bin(y[j])) - 2)
	arr1.sort()
	arr1.reverse()
	for h in range(len(arr1)):
		if arr1[h] != arr1[h - 1] or h < 1:
			c = arr1.count(arr1[h])
			t += int(c * (c - 1) / 2)
	print(t)


def setBitNumber(n):
	if n == 0:
		return 0
	msb = 0
	n = int(n / 2)
	while n > 0:
		n = int(n / 2)
		msb += 1
	return 1 << msb
t = int(input())
while t:
	t -= 1
	n = int(input())
	p = list(map(int, input().split()))
	a = []
	for i in p:
		a.append(setBitNumber(i))
	c = {}
	for i in a:
		if c.get(i) == None:
			c[i] = 0
		else:
			c[i] = c.get(i) + 1
	ans = 0
	for i in c.values():
		if i != 0:
			ans += i * (i + 1) // 2
	print(ans)


import math

def ans(n, a):
	m = [0 for i in range(32)]
	count = 0
	for i in range(n):
		p = int(math.log2(a[i]))
		count += m[p]
		m[p] += 1
	print(count)
t = int(input())
while t > 0:
	n = int(input())
	a = list(map(int, input().split(' ')))
	ans(n, a)
	t -= 1


def testcase():
	n = int(input())
	arr = list(map(int, input().split()))
	msbs = []
	for i in range(n):
		value = 1
		temp = arr[i]
		while temp > 0:
			temp >>= 1
			value <<= 1
		msbs.append(value)
	msbs.sort()
	last = -1
	ans = 0
	for i in range(len(msbs) - 1):
		if msbs[i] != msbs[i + 1]:
			freq = i - last
			ans += freq * (freq - 1) // 2
			last = i
	freq = len(msbs) - 1 - last
	ans += freq * (freq - 1) // 2
	print(ans)
	return
t = int(input())
for _ in range(t):
	testcase()


from sys import stdin, stdout
from collections import Counter
st = lambda : list(stdin.readline().strip())
li = lambda : list(map(int, stdin.readline().split()))
mp = lambda : map(int, stdin.readline().split())
inp = lambda : int(stdin.readline())
pr = lambda n: stdout.write(str(n) + '\n')
mod = 1000000007
INF = float('inf')

def SET(n):
	i = 32
	while not n & 1 << i:
		i -= 1
	return i

def solve():
	n = inp()
	l = li()
	x = []
	for i in l:
		x.append(SET(i))
	d = Counter(x)
	ans = 0
	for j in d:
		i = d[j]
		ans += i * (i - 1) // 2
	pr(ans)
for _ in range(inp()):
	solve()


t = int(input())

def sat(n):
	t = n * (n - 1) / 2
	return int(t)
for _ in range(t):
	n = int(input())
	a = [int(p) for p in input().split()]
	c = 0
	b = [0 for i in range(50)]
	for i in range(n):
		b[a[i].bit_length()] += 1
	for i in range(50):
		c += sat(b[i])
	print(c)


from math import log2
for _ in range(int(input())):
	n = int(input())
	p = [*map(int, input().split())]
	m = [[] for i in range(30)]
	for i in p:
		m[int(log2(i))].append(i)
	ans = 0
	for i in m:
		ans += len(i) * (len(i) - 1) // 2
	print(ans)


import math
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	c = [0 for i in range(32)]
	ans = 0
	for i in range(n):
		j = int(math.log2(l[i]))
		ans += c[j]
		c[j] += 1
	print(ans)


from math import *
from collections import *
from itertools import *
from functools import *
from bisect import *
from heapq import *
from operator import *
from sys import *
setrecursionlimit(100000000)
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	t = [0] * 35
	for i in range(n):
		for j in reversed(range(31)):
			if l[i] & 1 << j:
				t[j] += 1
				break
	ans = 0
	for i in range(35):
		ans += t[i] * (t[i] - 1) // 2
	print(ans)


t = int(input())

def MSB(n):
	if n == 0:
		return 0
	msb = 0
	n = n // 2
	while n > 0:
		n = n // 2
		msb += 1
	return 1 << msb

def solve():
	d = {}
	for i in range(n):
		x = MSB(l[i])
		d[x] = d.get(x, 0) + 1
	ans = 0
	for i in d:
		ans += (d[i] - 1) * d[i] // 2
	return ans
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	print(solve())


from collections import Counter
import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__
t = int(input())

def bits(x):
	return len(bin(x)[2:])
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	arr = [bits(x) for x in arr]
	occ = Counter(arr)
	ans = 0
	for k in occ:
		ans += occ[k] * (occ[k] - 1) // 2
	print(ans)


def nextPowerOf2(b, n):
	n -= 1
	n |= n >> 1
	n |= n >> 2
	n |= n >> 4
	n |= n >> 8
	n |= n >> 16
	n += 1
	if n > b:
		n = n // 2
	return n
a = int(input())
for i in range(a):
	b = int(input())
	c = [int(x) for x in input().split()]
	count = 0
	for j in range(b):
		c[j] = nextPowerOf2(c[j], c[j])
	c.sort()
	d = c
	count = d[0]
	count1 = 0
	count2 = 0
	for j in range(b):
		if count != d[j]:
			count2 = count2 + count1 * (count1 - 1) // 2
			count1 = 0
			count = d[j]
		if count == d[j]:
			count1 = count1 + 1
	count2 = count2 + count1 * (count1 - 1) // 2
	print(count2)


from collections import defaultdict
t = int(input())
while t:
	t -= 1
	n = int(input())
	a = list(map(int, input().split()))
	freq = defaultdict(int)
	ans = 0
	for x in a:
		temp = x
		count = 0
		while temp:
			parity = temp % 2
			temp //= 2
			count += 1
			if temp == 0:
				freq[count] += 1
	for (_, v) in freq.items():
		ans = ans + max(0, v * (v - 1))
	print(ans // 2)


for _ in range(int(input())):
	n = int(input())
	(*a,) = map(int, input().split())
	cnt = [0] * 100
	ans = 0
	for i in a:
		cnt[len(bin(i))] += 1
	for i in cnt:
		ans += i * (i - 1) // 2
	print(ans)


from collections import Counter
t = int(input())
for i in range(t):
	n = int(input())
	w = [len(bin(int(k))[2:]) for k in input().split()]
	q = Counter(w)
	res = 0
	for j in q.keys():
		res += q[j] * (q[j] - 1) // 2
	print(res)


from math import log2, floor
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	b = []
	for x in a:
		b.append(floor(log2(x)))
	b.sort()
	c = {}
	for x in b:
		if x in c:
			c[x] += 1
		else:
			c[x] = 1
	res = 0
	for x in c:
		res += c[x] * (c[x] - 1) // 2
	print(res)


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	arr = arr + [0, 0, 0, 0, 0]
	i = 0
	h = {}
	for j in range(0, 41):
		h[j] = 0
	while i <= n:
		c = 0
		x = arr[i]
		while x:
			x >>= 1
			c += 1
		i += 1
		h[c] += 1
	ans = 0
	for i in range(1, 41):
		ans += int(h[i] * (h[i] - 1) / 2)
	print(ans)


from math import *
for tt in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	mx = max(l)
	d = {i: [] for i in range(1, int(log2(mx)) + 2)}
	for i in l:
		pos = int(log2(i)) + 1
		d[pos].append(i)
	ans = 0
	for i in d:
		ans += len(d[i]) * (len(d[i]) - 1) // 2
	print(ans)


import sys
import math

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

def iinput():
	return int(input())

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

def get_list():
	return list(map(int, sys.stdin.readline().strip().split()))
mod = int(1000000000.0) + 7

def highestPowerof2(n):
	p = int(math.log(n, 2))
	return int(pow(2, p))
for _ in range(iinput()):
	n = iinput()
	a = get_list()
	d = {}
	for i in range(n):
		temp = highestPowerof2(a[i])
		d[temp] = d.get(temp, 0) + 1
	ans = 0
	for key in d:
		ans += d[key] * (d[key] - 1) // 2
	print(ans)


from collections import defaultdict
from queue import deque
from math import log2

def arrinp():
	return [*map(int, input().split(' '))]

def mulinp():
	return map(int, input().split(' '))

def intinp():
	return int(input())

def fun(n):
	return (n * n + n) // 2

def solution():
	n = intinp()
	a = arrinp()
	d = defaultdict(int)
	for i in a:
		d[int(log2(i))] += 1
	ans = 0
	for i in d:
		ans += d[i] * (d[i] - 1) // 2
	print(ans)
	return
testcases = 1
testcases = int(input())
for _ in range(testcases):
	solution()


import sys
input = sys.stdin.readline
from bisect import bisect_left
from bisect import bisect_right
from math import ceil, factorial

def ceil(x):
	if x != int(x):
		x = int(x) + 1
	return x

def swaparr(arr, a, b):
	temp = arr[a]
	arr[a] = arr[b]
	arr[b] = temp

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

def nCr(n, k, modulus=1):
	if k > n - k:
		k = n - k
	res = 1
	for i in range(k):
		res = res * (n - i)
		res = res / (i + 1)
		res %= modulus
	return int(res)

def primefs(n):
	primes = {}
	while n % 2 == 0 and n > 0:
		primes[2] = primes.get(2, 0) + 1
		n = n // 2
	for i in range(3, int(n ** 0.5) + 2, 2):
		while n % i == 0 and n > 0:
			primes[i] = primes.get(i, 0) + 1
			n = n // i
	if n > 2:
		primes[n] = primes.get(n, 0) + 1
	return primes

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 swap(a, b):
	temp = a
	a = b
	b = temp
	return (a, b)

def find(x, link):
	p = x
	while p != link[p]:
		p = link[p]
	while x != p:
		nex = link[x]
		link[x] = p
		x = nex
	return p

def union(x, y, link, size):
	x = find(x, link)
	y = find(y, link)
	if size[x] < size[y]:
		(x, y) = swap(x, y)
	if x != y:
		size[x] += size[y]
		link[y] = x

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
MAXN = int(1000000.0 + 5)

def spf_sieve():
	spf[1] = 1
	for i in range(2, MAXN):
		spf[i] = i
	for i in range(4, MAXN, 2):
		spf[i] = 2
	for i in range(3, ceil(MAXN ** 0.5), 2):
		if spf[i] == i:
			for j in range(i * i, MAXN, i):
				if spf[j] == j:
					spf[j] = i

def factoriazation(x):
	ret = {}
	while x != 1:
		ret[spf[x]] = ret.get(spf[x], 0) + 1
		x = x // spf[x]
	return ret

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

def float_array():
	return list(map(float, input().strip().split()))

def str_array():
	return input().strip().split()
MOD = int(1000000000.0) + 7
CMOD = 998244353
INF = float('inf')
NINF = -float('inf')
for _ in range(int(input())):
	n = int(input())
	a = sorted(int_array())
	dick = {}
	for i in a:
		cnt = 0
		while i:
			cnt += 1
			i //= 2
		dick[cnt] = dick.get(cnt, 0) + 1
	ans = 0
	for i in dick:
		x = dick[i]
		ans += x * (x - 1) // 2
	print(ans)


from bisect import bisect_left as bl
from bisect import bisect_right as br
from heapq import heappush, heappop, heapify
import math
from collections import *
from functools import reduce, cmp_to_key
import sys
input = sys.stdin.readline
from itertools import accumulate
from functools import lru_cache
M = mod = 998244353

def factors(n):
	return sorted(set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0))))

def inv_mod(n):
	return pow(n, mod - 2, mod)

def li():
	return [int(i) for i in input().rstrip('\n').split()]

def st():
	return input().rstrip('\n')

def val():
	return int(input().rstrip('\n'))

def li2():
	return [i for i in input().rstrip('\n')]

def li3():
	return [int(i) for i in input().rstrip('\n')]
for _ in range(val()):
	n = val()
	l = li()
	ans = 0
	bits = [0] * 32
	for i in range(n - 1, -1, -1):
		for j in range(31, -1, -1):
			if l[i] >> j & 1:
				ans += bits[j]
				bits[j] += 1
				break
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	mp = [0] * 31
	for i in a:
		lst = -1
		for j in range(31):
			if i >> j & 1:
				lst = j
		if lst != -1:
			mp[lst] += 1
	res = 0
	for i in range(31):
		res += mp[i] * (mp[i] - 1) // 2
	print(res)


from math import log2
t = int(input())
for tc in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	bin_length_list = []
	for i in range(n):
		binary = bin(a[i])
		bin_length_list.append(len(binary))
	count = 0
	z = max(a)
	for j in range(1, len(bin(z)) + 1):
		c = bin_length_list.count(j)
		count += c * (c - 1) // 2
	print(count)


from functools import lru_cache
from sys import stdin, stdout
import sys
from math import *
input = stdin.readline
for __ in range(int(input())):
	n = int(input())
	a = [0] * 33
	ar = list(map(int, input().split()))
	for i in range(n):
		l = len(bin(ar[i])[2:])
		a[l] += 1
	ans = 0
	for i in range(32):
		if a[i] > 0:
			ans += a[i] * (a[i] - 1) / 2
	print(int(ans))


def bits(n):
	binary = bin(n)[2:]
	return len(binary)
for t in range(int(input())):
	n = int(input())
	arr = [int(x) for x in input().split()]
	my_dict = {}
	for ele in arr:
		temp = bits(ele)
		my_dict[temp] = my_dict.get(temp, 0) + 1
	count = 0
	for ele in my_dict:
		n = my_dict[ele]
		count += n * (n - 1) // 2
	print(count)


testcases = int(input())
for case in range(testcases):
	n = int(input())
	l = [int(zax) for zax in input().split()]
	MSB = [0] * 36
	largest_bit = 0
	for i in range(n):
		MSB[len(bin(l[i])[2:]) - 1] += 1
		largest_bit = max(largest_bit, len(bin(l[i])[2:]) - 1)
	cum_sum = 0
	answer = 0
	for j in range(0, largest_bit + 1):
		answer += MSB[j] * cum_sum
		cum_sum += MSB[j]
	print(n * (n - 1) // 2 - answer)


t = int(input())
for _ in range(t):
	n = int(input())
	arr = [int(p) for p in input().split()]
	cntr = {}
	for i in range(len(arr)):
		try:
			cntr[len(bin(arr[i]))] += 1
		except KeyError:
			cntr[len(bin(arr[i]))] = 1
	total = 0
	for i in cntr:
		n = cntr[i]
		total += n * (n - 1) // 2
	print(total)


def ans(n, ar):
	d = {}
	for i in range(n):
		k = bin(ar[i])[2:]
		if k[0] == '1':
			d[len(k)] = d.get(len(k), 0) + 1
	cnt = 0
	for (i, j) in d.items():
		if j > 1:
			cnt += j * (j - 1) // 2
	return cnt
for i in range(int(input())):
	n = int(input())
	ar = [int(i) for i in input().split()]
	print(ans(n, ar))


import bisect
import functools
import math
T = int(input())
for Tid in range(T):
	n = int(input())
	a = list(map(int, input().split()))
	tab = [0 for i in range(35)]
	ans = 0
	for i in range(n):
		j = 0
		while 2 << j <= a[i]:
			j = j + 1
		ans = ans + tab[j]
		tab[j] = tab[j] + 1
	print(ans)


import sys
input = sys.stdin.readline
I = lambda : list(map(int, input().split()))
(t,) = I()
for _ in range(t):
	(n,) = I()
	l = I()
	an = [0] * 33
	for i in range(n):
		k = 1 << 32
		for j in range(32, -1, -1):
			if l[i] & k:
				an[j] += 1
				break
			k //= 2
	ans = 0
	for i in an:
		ans += i * (i - 1) // 2
	print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	c = {}
	for i in l:
		k = len(bin(i)[2:])
		if k in c:
			c[k] += 1
		else:
			c[k] = 1
	ans = 0
	for i in c:
		ans += c[i] * (c[i] - 1) // 2
	print(ans)


import sys
import math
mod = 998244353
dynamic_factorial = False

def main(test_case=False):
	n = int(input()) if test_case else 1
	for _ in range(n):
		test()
		print()

def fast_pow(a: int, b: int):
	res = 1
	while b > 0:
		if b & 1:
			res *= a
		a *= a
		b >>= 1
	return res

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

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

def bits(n: int):
	return list(bin(n)).count('1')

def flush():
	sys.stdout.flush()

def parr(arr):
	print(*arr, sep=' ')

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

def ext_gcd(a: int, b: int):
	if b == 0:
		return [a, [1, 0]]
	res = ext_gcd(b, a % b)
	g = res[0]
	if g != 1:
		return False
	x1 = res[1][0]
	y1 = res[1][1]
	x = y1
	y = x1 - y1 * (a // b)
	return [g, [x, y]]

def get_set(a):
	res = {}
	for x in a:
		if x in res:
			res[x] += 1
		else:
			res[x] = 1
	return res
fact = {0: 1}

def factorial(n):
	if dynamic_factorial:
		if n not in fact:
			fact[n] = factorial(n - 1) * n
		return fact[n]
	else:
		res = 1
		while n > 0:
			res = res * n % mod
			n -= 1
		return res

def sum_of(a):
	res = 0
	for x in a:
		res = (res + x) % mod
	return res

def prod_of(a):
	res = 1
	for x in a:
		res = res * x % mod
	return res

def test():
	n = get()[0]
	a = get()
	b = [-1] * n
	for i in range(n):
		b[i] = len(bin(a[i])) - 3
	b.sort()
	i = 0
	ans = 0
	while i < n:
		while i + 1 < n and b[i] == b[i + 1]:
			i += 1
		ans += c2(b.count(b[i]))
		i += 1
	print(ans)
main(test_case=True)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	p = []
	c = 0
	q = 2
	for j in range(n):
		if a[j] < q:
			c = c + 1
		else:
			p.append(c)
			while a[j] >= q:
				q = q * 2
			c = 1
	s = 0
	p.append(c)
	for v in p:
		s = s + v * (v - 1) // 2
	print(s)


t = int(input())
from collections import Counter
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	z = []
	for item in arr:
		x = item
		c = 0
		while x > 0:
			c += 1
			x = x >> 1
		z.append(c)
	c = Counter(z)
	ans = 0
	for item in c.values():
		ans += item * (item - 1) // 2
	print(ans)


import sys
import math
t = int(sys.stdin.readline())
while t:
	n = int(sys.stdin.readline())
	numbers = [int(num) for num in sys.stdin.readline().split()]
	bins = {}
	pairs = 0
	for num in numbers:
		bit_length = num.bit_length()
		if bit_length in bins:
			pairs += bins[bit_length]
			bins[bit_length] += 1
		else:
			bins[bit_length] = 1
	print(pairs)
	t -= 1


def setBitNumber(n):
	if n == 0:
		return 0
	msb = 0
	n = int(n / 2)
	while n > 0:
		n = int(n / 2)
		msb += 1
	return msb
for _ in range(int(input())):
	n = int(input())
	ls = list(map(int, input().split()))
	ar = [0] * 40
	snt = 0
	for i in range(n):
		ar[setBitNumber(ls[i])] += 1
	for i in ar:
		snt += (i - 1) * i // 2
	print(snt)


t = int(input())
for j in range(t):
	n = int(input())
	nlist = list(map(int, input().split()))[:n]
	d = {}
	for i in range(n):
		k = len(bin(nlist[i]))
		if k in d:
			d[k] += 1
		else:
			d[k] = 1
	ans = 0
	for i in d:
		ans += d[i] * (d[i] - 1) // 2
	print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	arr2 = [0] * 32
	for j in arr:
		s = len(bin(j).replace('0b', ''))
		arr2[s] += 1
	sum = 0
	for k in arr2:
		sum += k * (k - 1) // 2
	print(sum)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	y = []
	for i in a:
		y.append(len(bin(i)) - 2)
	d = {}
	for i in range(n):
		if y[i] in d:
			d[y[i]] = d[y[i]] + 1
		else:
			d[y[i]] = 1
	s = list(set(y))
	ans = 0
	for i in s:
		num = d[i]
		ans = ans + num * (num - 1) // 2
	print(ans)


import math
test = int(input())
for q in range(test):
	n = int(input())
	x = list(map(int, input().split()))
	m = {}
	for i in range(n):
		a = math.floor(math.log2(x[i]))
		if a in m:
			m[a] += 1
		else:
			m[a] = 1
	ans = 0
	for i in m.values():
		ans += i * (i - 1)
	print(ans // 2)


t = int(input())
for u in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	bit = [[] for i in range(32)]
	ans = 0
	for i in range(n):
		s = str(bin(a[i])[2:])
		temp = 0
		for j in s:
			temp += 1
			if j == '1':
				bit[len(s) - temp].append(a[i])
				break
	for i in range(32):
		n = len(bit[i])
		if n > 1:
			ans += n * (n - 1) // 2
	print(ans)


import math
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	a_ = [int(math.log2(z)) for z in a]
	a_.sort()
	k = 1
	ans = 0
	for i in range(1, n):
		if a_[i] != a_[i - 1]:
			ans += k * (k - 1) // 2
			k = 1
		else:
			k += 1
	ans += k * (k - 1) // 2
	print(ans)


from collections import defaultdict as d
t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	lengthCnts = d(lambda : 0)
	ans = 0
	for i in range(n - 1, -1, -1):
		b = len(bin(a[i]))
		ans += lengthCnts[b]
		lengthCnts[b] += 1
	print(ans)


from sys import stdin
from math import ceil, gcd
for _ in range(int(stdin.readline())):
	n = int(stdin.readline())
	arr = list(map(int, stdin.readline().split()))
	d = {}
	for i in range(n):
		s = bin(arr[i])[2:]
		s = '0' * (30 - len(s)) + s
		for k in range(30):
			if s[k] == '1':
				d[k] = d.get(k, 0) + 1
				break
	count = 0
	for ele in d:
		count += d[ele] * (d[ele] - 1) // 2
	print(count)


import math
for _ in range(int(input())):
	N = int(input())
	array = [int(x) for x in input().split()]
	binarray = [int(math.log(x) / math.log(2)) + 1 for x in array]
	dict = {}
	for i in binarray:
		if i in dict:
			dict[i] += 1
		else:
			dict[i] = 1
	tobechecked = []
	for i in dict:
		tobechecked.append(dict[i])
	tobeprinted = 0
	for i in tobechecked:
		tobeprinted += i * (i - 1) // 2
	print(tobeprinted)


for t in range(int(input())):
	n = int(input())
	x = list(map(int, input().split()))
	d = dict()
	for i in range(39):
		d[i] = 0
	for i in x:
		num = len(str(bin(i)))
		d[num] += 1
	ans = 0
	for s in d:
		ans += d[s] * (d[s] - 1) // 2
	print(ans)


import os
import heapq
import sys, threading
import math
import operator
from collections import defaultdict, deque
from io import BytesIO, IOBase
sys.setrecursionlimit(10 ** 5)

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

def inpt():
	return [int(k) for k in input().split()]

def main():
	for _ in range(int(input())):
		n = int(input())
		d = defaultdict(int)
		ar = inpt()
		for i in range(n):
			p = 0
			v = 0
			for j in range(0, 33):
				v = 1 << j
				if v & ar[i]:
					p = v
			d[p] += 1
		ans = 0
		for i in d.keys():
			ans += d[i] * (d[i] - 1) // 2
		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
t = int(input())
for _ in range(t):
	n = int(input())
	nList = list(map(int, input().split()))
	ans = 0
	for i in range(40):
		cnt = 0
		for j in range(n):
			s = 1 << i
			s2 = 1 << i + 1
			if nList[j] & s == s and s <= nList[j] < s2:
				cnt += 1
		if cnt > 1:
			ans += cnt * (cnt - 1) // 2
	print(ans)


def setBitNumber(n):
	if n == 0:
		return 0
	msb = 0
	n = int(n / 2)
	while n > 0:
		n = int(n / 2)
		msb += 1
	return 1 << msb
testcases = int(input())
for testcase in range(testcases):
	n = int(input())
	temparr = input()
	temparr = temparr.split()
	arr = []
	for i in temparr:
		arr.append(int(i))
	dicts = {}
	ans = 0
	for i in arr:
		msb = setBitNumber(i)
		if msb in dicts:
			ans += dicts[msb]
		else:
			dicts[msb] = 0
		dicts[msb] += 1
	print(ans)


from math import log2
t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	d = dict()
	for i in range(n):
		k = 1 + int(log2(a[i]))
		if k not in d.keys():
			d[k] = 1
		else:
			d[k] += 1
	ans = 0
	for v in d.values():
		ans += v * (v - 1) // 2
	print(ans)


t = int(input())
from collections import defaultdict
for _ in range(t):
	n = int(input())
	(*a,) = map(int, input().split())
	d = defaultdict(int)
	for ai in a:
		d[ai.bit_length()] += 1
	ans = 0
	for key in d:
		ans += d[key] * (d[key] - 1) // 2
	print(ans)


from sys import stdin, stdout
from math import log2
from collections import Counter

def find(arr, N):
	S = Counter()
	arr.sort()
	for k in arr:
		S[int(log2(k))] += 1
	r = 0
	for i in S:
		c = S[i]
		r += c * (c - 1) // 2
	return r

def main():
	for _ in range(int(stdin.readline())):
		N = int(stdin.readline())
		arr = list(map(int, stdin.readline().split()))
		print(find(arr, N))
main()


from bisect import bisect_left
import os
import sys
from io import BytesIO, IOBase
from collections import Counter, defaultdict
from collections import deque
from functools import cmp_to_key
import math
import heapq
import re

def sin():
	return input()

def ain():
	return list(map(int, sin().split()))

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

def iin():
	return int(sin())
MAX = float('inf')
MIN = float('-inf')
MOD = 1000000007

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
	s = set()
	for p in range(2, n + 1):
		if prime[p]:
			s.add(p)
	return s

def readTree(n, m):
	adj = [deque([]) for _ in range(n + 1)]
	for _ in range(m):
		(u, v) = ain()
		adj[u].append(v)
		adj[v].append(u)
	return adj

def main():
	for _ in range(iin()):
		n = iin()
		a = ain()
		d = deque([])
		for i in range(n):
			d.append(len(bin(a[i])[2:]))
		dic = {}
		for i in d:
			if i in dic:
				dic[i] += 1
			else:
				dic[i] = 1
		ans = 0
		for i in dic.values():
			if i > 1:
				ans += i * (i - 1) // 2
		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')
if os.getcwd() == 'D:\\code':
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
	input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


import math as m
t = int(input())
for i in range(t):
	cnt = 0
	n = int(input())
	s = input()
	s = s.split()
	o = []
	for j in s:
		o.append(m.log2(int(j)) // 1)
	k = set(o)
	for j in k:
		x = o.count(j)
		if x > 1:
			cnt += x * (x - 1) // 2
	print(cnt)


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


for _ in range(int(input())):
	n = int(input())
	L = list(map(int, input().split()))
	import math
	ans = {}
	for i in L:
		x = 0
		while i > 0:
			x = i & -i
			i -= x
		if int(math.log2(x)) not in ans.keys():
			ans[int(math.log2(x))] = 1
		else:
			ans[int(math.log2(x))] += 1
	res = 0
	for i in ans.values():
		res += i * (i - 1) // 2
	print(res)


import sys
input = sys.stdin.readline
T = int(input())
for _ in range(T):
	N = int(input())
	A = list(map(int, input().split()))
	G = {}
	for i in A:
		val = len(bin(i)) - 2
		try:
			G[val] = G[val] + 1
		except:
			G[val] = 1
	ans = 0
	for i in G:
		val = G[i]
		ans = ans + val * (val - 1) // 2
	print(ans)


import math
import random
import heapq, bisect
import sys
from collections import deque, defaultdict
from fractions import Fraction
import sys
import threading
from collections import defaultdict
mod = 10 ** 9 + 7
mod1 = 998244353
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')

class TreeNode:

	def __init__(self, k, v):
		self.key = k
		self.value = v
		self.left = None
		self.right = None
		self.parent = None
		self.height = 1
		self.num_left = 1
		self.num_total = 1

class AvlTree:

	def __init__(self):
		self._tree = None

	def add(self, k, v):
		if not self._tree:
			self._tree = TreeNode(k, v)
			return
		node = self._add(k, v)
		if node:
			self._rebalance(node)

	def _add(self, k, v):
		node = self._tree
		while node:
			if k < node.key:
				if node.left:
					node = node.left
				else:
					node.left = TreeNode(k, v)
					node.left.parent = node
					return node.left
			elif node.key < k:
				if node.right:
					node = node.right
				else:
					node.right = TreeNode(k, v)
					node.right.parent = node
					return node.right
			else:
				node.value = v
				return

	@staticmethod
	def get_height(x):
		return x.height if x else 0

	@staticmethod
	def get_num_total(x):
		return x.num_total if x else 0

	def _rebalance(self, node):
		n = node
		while n:
			lh = self.get_height(n.left)
			rh = self.get_height(n.right)
			n.height = max(lh, rh) + 1
			balance_factor = lh - rh
			n.num_total = 1 + self.get_num_total(n.left) + self.get_num_total(n.right)
			n.num_left = 1 + self.get_num_total(n.left)
			if balance_factor > 1:
				if self.get_height(n.left.left) < self.get_height(n.left.right):
					self._rotate_left(n.left)
				self._rotate_right(n)
			elif balance_factor < -1:
				if self.get_height(n.right.right) < self.get_height(n.right.left):
					self._rotate_right(n.right)
				self._rotate_left(n)
			else:
				n = n.parent

	def _remove_one(self, node):
		replacement = node.left or node.right
		if node.parent:
			if AvlTree._is_left(node):
				node.parent.left = replacement
			else:
				node.parent.right = replacement
			replacement.parent = node.parent
			node.parent = None
		else:
			self._tree = replacement
			replacement.parent = None
		node.left = None
		node.right = None
		node.parent = None
		self._rebalance(replacement)

	def _remove_leaf(self, node):
		if node.parent:
			if AvlTree._is_left(node):
				node.parent.left = None
			else:
				node.parent.right = None
			self._rebalance(node.parent)
		else:
			self._tree = None
		node.parent = None
		node.left = None
		node.right = None

	def remove(self, k):
		node = self._get_node(k)
		if not node:
			return
		if AvlTree._is_leaf(node):
			self._remove_leaf(node)
			return
		if node.left and node.right:
			nxt = AvlTree._get_next(node)
			node.key = nxt.key
			node.value = nxt.value
			if self._is_leaf(nxt):
				self._remove_leaf(nxt)
			else:
				self._remove_one(nxt)
			self._rebalance(node)
		else:
			self._remove_one(node)

	def get(self, k):
		node = self._get_node(k)
		return node.value if node else -1

	def _get_node(self, k):
		if not self._tree:
			return None
		node = self._tree
		while node:
			if k < node.key:
				node = node.left
			elif node.key < k:
				node = node.right
			else:
				return node
		return None

	def get_at(self, pos):
		x = pos + 1
		node = self._tree
		while node:
			if x < node.num_left:
				node = node.left
			elif node.num_left < x:
				x -= node.num_left
				node = node.right
			else:
				return (node.key, node.value)
		raise IndexError('Out of ranges')

	@staticmethod
	def _is_left(node):
		return node.parent.left and node.parent.left == node

	@staticmethod
	def _is_leaf(node):
		return node.left is None and node.right is None

	def _rotate_right(self, node):
		if not node.parent:
			self._tree = node.left
			node.left.parent = None
		elif AvlTree._is_left(node):
			node.parent.left = node.left
			node.left.parent = node.parent
		else:
			node.parent.right = node.left
			node.left.parent = node.parent
		bk = node.left.right
		node.left.right = node
		node.parent = node.left
		node.left = bk
		if bk:
			bk.parent = node
		node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1
		node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right)
		node.num_left = 1 + self.get_num_total(node.left)

	def _rotate_left(self, node):
		if not node.parent:
			self._tree = node.right
			node.right.parent = None
		elif AvlTree._is_left(node):
			node.parent.left = node.right
			node.right.parent = node.parent
		else:
			node.parent.right = node.right
			node.right.parent = node.parent
		bk = node.right.left
		node.right.left = node
		node.parent = node.right
		node.right = bk
		if bk:
			bk.parent = node
		node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1
		node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right)
		node.num_left = 1 + self.get_num_total(node.left)

	@staticmethod
	def _get_next(node):
		if not node.right:
			return node.parent
		n = node.right
		while n.left:
			n = n.left
		return n

class SegmentTree1:

	def __init__(self, data, default=2 ** 30, func=lambda a, b: min(a, b)):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)

class SegmentTree:

	def __init__(self, data, default=0, func=lambda a, b: a + b):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)

class Factorial:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorials = [1, 1]
		self.invModulos = [0, 1]
		self.invFactorial_ = [1, 1]

	def calc(self, n):
		if n <= -1:
			print('Invalid argument to calculate n!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.factorials):
			return self.factorials[n]
		nextArr = [0] * (n + 1 - len(self.factorials))
		initialI = len(self.factorials)
		prev = self.factorials[-1]
		m = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * i % m
		self.factorials += nextArr
		return self.factorials[n]

	def inv(self, n):
		if n <= -1:
			print('Invalid argument to calculate n^(-1)')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		p = self.MOD
		pi = n % p
		if pi < len(self.invModulos):
			return self.invModulos[pi]
		nextArr = [0] * (n + 1 - len(self.invModulos))
		initialI = len(self.invModulos)
		for i in range(initialI, min(p, n + 1)):
			next = -self.invModulos[p % i] * (p // i) % p
			self.invModulos.append(next)
		return self.invModulos[pi]

	def invFactorial(self, n):
		if n <= -1:
			print('Invalid argument to calculate (n^(-1))!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.invFactorial_):
			return self.invFactorial_[n]
		self.inv(n)
		nextArr = [0] * (n + 1 - len(self.invFactorial_))
		initialI = len(self.invFactorial_)
		prev = self.invFactorial_[-1]
		p = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * self.invModulos[i % p] % p
		self.invFactorial_ += nextArr
		return self.invFactorial_[n]

class Combination:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorial = Factorial(MOD)

	def ncr(self, n, k):
		if k < 0 or n < k:
			return 0
		k = min(k, n - k)
		f = self.factorial
		return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD

def powm(a, n, m):
	if a == 1 or n == 0:
		return 1
	if n % 2 == 0:
		s = powm(a, n // 2, m)
		return s * s % m
	else:
		return a * powm(a, n - 1, m) % m

def sort_list(list1, list2):
	zipped_pairs = zip(list2, list1)
	z = [x for (_, x) in sorted(zipped_pairs)]
	return z

def product(l):
	por = 1
	for i in range(len(l)):
		por *= l[i]
	return por

def binarySearchCount(arr, n, key):
	left = 0
	right = n - 1
	count = 0
	while left <= right:
		mid = int((right + left) / 2)
		if arr[mid] < key:
			count = mid + 1
			left = mid + 1
		else:
			right = mid - 1
	return count

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

def binary(x, length):
	y = bin(x)[2:]
	return y if len(y) >= length else '0' * (length - len(y)) + y

def countGreater(arr, n, k):
	l = 0
	r = n - 1
	leftGreater = n
	while l <= r:
		m = int(l + (r - l) / 2)
		if arr[m] > k:
			leftGreater = m
			r = m - 1
		else:
			l = m + 1
	return n - leftGreater

class TrieNode:

	def __init__(self):
		self.children = [None] * 26
		self.isEndOfWord = False

class Trie:

	def __init__(self):
		self.root = self.getNode()

	def getNode(self):
		return TrieNode()

	def _charToIndex(self, ch):
		return ord(ch) - ord('a')

	def insert(self, key):
		pCrawl = self.root
		length = len(key)
		for level in range(length):
			index = self._charToIndex(key[level])
			if not pCrawl.children[index]:
				pCrawl.children[index] = self.getNode()
			pCrawl = pCrawl.children[index]
		pCrawl.isEndOfWord = True

	def search(self, key):
		pCrawl = self.root
		length = len(key)
		for level in range(length):
			index = self._charToIndex(key[level])
			if not pCrawl.children[index]:
				return False
			pCrawl = pCrawl.children[index]
		return pCrawl != None and pCrawl.isEndOfWord

class Node:

	def __init__(self, data):
		self.data = data
		self.count = 0
		self.left = None
		self.right = None

class BinaryTrie:

	def __init__(self):
		self.root = Node(0)

	def insert(self, pre_xor):
		self.temp = self.root
		for i in range(31, -1, -1):
			val = pre_xor & 1 << i
			if val:
				if not self.temp.right:
					self.temp.right = Node(0)
				self.temp = self.temp.right
				self.temp.count += 1
			if not val:
				if not self.temp.left:
					self.temp.left = Node(0)
				self.temp = self.temp.left
				self.temp.count += 1
		self.temp.data = pre_xor

	def query(self, xor):
		self.temp = self.root
		for i in range(31, -1, -1):
			val = xor & 1 << i
			if not val:
				if self.temp.left and self.temp.left.count > 0:
					self.temp = self.temp.left
				elif self.temp.right:
					self.temp = self.temp.right
			elif self.temp.right and self.temp.right.count > 0:
				self.temp = self.temp.right
			elif self.temp.left:
				self.temp = self.temp.left
			self.temp.count -= 1
		return xor ^ self.temp.data
for ik in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	ans = 0
	d = defaultdict(int)
	for i in range(n):
		for j in range(30, -1, -1):
			if l[i] & 1 << j > 0:
				d[j] += 1
				break
	for i in d:
		ans += d[i] * (d[i] - 1) // 2
	print(ans)


def check_msb(num):
	num |= num >> 1
	num |= num >> 2
	num |= num >> 4
	num |= num >> 8
	num |= num >> 16
	return num + 1 >> 1
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	d = {}
	for a in arr:
		x = check_msb(a)
		d[x] = 1 + d.get(x, 0)
	ans = 0
	for val in d.values():
		ans += val * (val - 1) // 2
	print(ans)


from math import log2
t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	ans = 0
	b = [0] * 32
	for i in range(n):
		tmp = int(log2(l[i]))
		ans += b[tmp]
		b[tmp] += 1
	print(ans)


t = int(input())
results = []
for i in range(t):
	n = int(input())
	nums = [int(val) for val in input().split()]
	total = 0
	list_count = [0] * 32
	for num in nums:
		str_b = bin(num)[2:]
		list_count[len(str_b) - 1] += 1
	for count in list_count:
		total += count * (count - 1) // 2
	results.append(total)
for result in results:
	print(result)


from collections import Counter
for _ in range(int(input())):
	n = int(input())
	x = [0 for i in range(30)]
	l = list(map(int, input().split()))
	for i in l:
		for j in range(29, -1, -1):
			if i >> j & 1:
				x[j] += 1
				break
	ans = 0
	for i in x:
		if i > 1:
			ans += i * (i - 1) // 2
	print(ans)


from math import log as ll
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	if n == 1:
		print(0)
	else:
		d = {}
		for i in range(n):
			val = int(ll(a[i], 2))
			if val in d:
				d[val] += 1
			else:
				d[val] = 1
		ans = 0
		for (i, k) in d.items():
			if k >= 2:
				ans += k * (k - 1) // 2
		print(ans)


from fractions import Fraction
from collections import defaultdict
import math
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))

def input():
	return sys.stdin.readline().rstrip('\r\n')
t = int(input())
for _ in range(t):
	n = int(input())
	l = list([int(x) for x in input().split()])
	d = [0] * 31
	sum = 0
	for i in l:
		val = int(math.log(i, 2))
		sum += d[val]
		d[val] += 1
	print(sum)


from collections import Counter

def rightbit(a):
	while a & a - 1 != 0:
		a &= a - 1
	return a

def main():
	for _ in range(int(input())):
		n = int(input())
		a = list(map(int, input().split()))
		for i in range(n):
			a[i] = rightbit(a[i])
		d = dict(Counter(a))
		c = 0
		for i in d:
			j = d[i]
			c += j * (j - 1) // 2
		print(c)
main()


def solve():
	n = int(input())
	a = list(map(int, input().split()))
	d = [0] * 100
	c = 0
	for i in range(n):
		k = len(bin(a[i])) - 2
		c += d[k]
		d[k] += 1
	print(c)
t = int(input())
i = 0
while i < t:
	solve()
	i += 1


N = int(input())
for _ in range(N):
	n = int(input())
	nums = list(map(int, input().split()))
	cnt = {}
	ans = 0
	for x in nums:
		i = 0
		while x:
			i += 1
			x >>= 1
		y = 1 << i
		z = cnt.get(y, 0)
		ans += z
		cnt[y] = z + 1
	print(ans)


from collections import defaultdict
cases = int(input())

def get_1_pos(num):
	r = -1
	while num > 0:
		r += 1
		num >>= 1
	return r
for _ in range(cases):
	n = int(input())
	ds = list(map(int, input().split()))
	dic = defaultdict(int)
	for v in ds:
		dic[get_1_pos(v)] += 1
	total = 0
	for k in dic.keys():
		total += dic[k] * (dic[k] - 1) // 2
	print(total)


import math
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	cnt = [0] * 100
	ans = 0
	for ele in a:
		cnt[int(math.log(ele, 2))] += 1
	for i in cnt:
		ans += i * (i - 1) // 2
	print(ans)


for _ in range(int(input())):
	n = int(input())
	ar = list(map(int, input().split()))
	d = {}
	for i in range(n):
		temp = ar[i] // 2
		cnt = 0
		while temp:
			cnt += 1
			temp //= 2
		try:
			d[cnt] += 1
		except:
			d[cnt] = 1
	s = 0
	for i in d:
		s += d[i] * (d[i] - 1) // 2
	print(s)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	set_bit = [0] * n
	for i in range(n):
		ct = 0
		while a[i] >> ct != 0:
			ct += 1
		set_bit[i] = ct
	freq = [0] * 31
	for i in set_bit:
		freq[i] += 1
	ans = 0
	for i in freq:
		ans += i * (i - 1) // 2
	print(ans)


for _ in range(int(input())):
	n = int(input())
	lis = list(map(int, input().split()))
	has = [0] * 100
	ans = 0
	for i in lis:
		s = bin(i)
		a = len(s)
		has[a] += 1
	for i in has:
		ans += i * (i - 1) // 2
	print(ans)


import sys
sys.setrecursionlimit(10000)
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	sizes = {}
	for ai in a:
		x = len(bin(ai)) - 2
		if x not in sizes:
			sizes[x] = 1
		else:
			sizes[x] += 1
	ans = 0
	for (keys, vals) in sizes.items():
		if vals > 1:
			ans += vals * (vals - 1) // 2
	print(ans)


from enum import *
from copy import *
from sys import *
from collections import *
from itertools import *
from functools import *
from math import *

def l2i(fr=None):
	return map_type_to_fr(int, fr)[0]

def i2i(fr=None):
	return l2i(fr)

def dbg(*args):
	print(*args, file=stderr)

def dbv(var_name):
	print(f'{var_name} = {eval(var_name)}', file=stderr)

def map_type_to_fr(t, fr):
	global GETLINE_OFFSET
	if fr is None:
		if RUNNING_ON_MY_PC:
			fr = TEST_INP[GETLINE_OFFSET].split()
			GETLINE_OFFSET += 1
		else:
			fr = input().split()
	return list(map(t, fr))

def mis(fr=None):
	return map_type_to_fr(int, fr)

def mbs(fr=None):
	return map_type_to_fr(bool, fr)

def gs(fr=None):
	return map_type_to_fr(str, fr)[0]
import os
import sysconfig
RUNNING_ON_MY_PC = os.name == 'nt' and platform == 'win32' and (sysconfig.get_platform() == 'win-amd64')
GETLINE_OFFSET = 0
TEST_INP = '5\n5\n1 4 3 7 10\n3\n1 1 1\n4\n6 2 5 3\n2\n2 4\n1\n1\n'.split('\n')

def solve():
	n = i2i()
	a = mis()
	curr = 2 ** 30
	ans = 0
	used = set()
	for i in range(30 + 1):
		curr_used = set()
		p = 0
		for x in a:
			if x in used:
				continue
			if x & curr:
				p += 1
				curr_used.add(x)
		ans += p * (p - 1) // 2
		used.update(curr_used)
		curr >>= 1
	return ans
q = i2i()
for i in range(q):
	print(solve())


def SetBit(num):
	count = 0
	while num:
		count += 1
		num >>= 1
	return count
T = int(input())
for _ in range(T):
	N = int(input())
	A = list(map(int, input().split()))
	arr = [0] * N
	for j in range(N):
		arr[j] = SetBit(A[j])
	ans = 0
	d = {}
	for j in range(N):
		if arr[j] in d:
			d[arr[j]] += 1
		else:
			d[arr[j]] = 1
	for j in d:
		x = d[j]
		ans += x * (x - 1) // 2
	print(ans)


import math
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	c = 0
	dict1 = dict()
	for i in range(0, n):
		y = int(math.log(a[i], 2))
		if y in dict1:
			dict1[y] += 1
		else:
			dict1[y] = 1
	sum = 0
	for items in dict1:
		if dict1[items] >= 2:
			z = dict1[items] * (dict1[items] - 1) // 2
			sum += z
	print(sum)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	l = []
	s = set()
	for i in range(n):
		p = bin(a[i])
		p = p[2:]
		l.append(len(p))
		s.add(len(p))
	s = list(s)
	s.sort()
	ans = 0
	for i in range(len(s)):
		k = l.count(s[i])
		b = k * (k - 1)
		b = b // 2
		ans = ans + b
	print(ans)


import math
from decimal import *
import random

def getbin(n):
	large = -1
	for i in range(32):
		if n >> i:
			large = int(i)
	return large
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	d = {}
	ans = 0
	for x in arr:
		val = getbin(x)
		if val not in d:
			d[val] = 0
		d[val] += 1
	for x in d:
		ans += d[x] * (d[x] - 1) // 2
	print(ans)


MOD = 1000000007
from collections import defaultdict as dd, Counter, deque
import math

def si():
	return input()

def ii():
	return int(input())

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

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

def out(v):
	print(v)

def spout():
	print(v, end=' ')

def d2b(n):
	return bin(n).replace('0b', '')

def twod(n, m, num):
	return [[num for x in range(m)] for y in range(n)]

def vow():
	return ['a', 'e', 'i', 'o', 'u']

def let():
	return [chr(i) for i in range(97, 123)]

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

def ispow2(x):
	return x and (not x & x - 1)

def prime_factors(n):
	i = 2
	factors = []
	while i * i <= n:
		if n % i:
			i += 1
		else:
			n //= i
			factors.append(i)
	if n > 1:
		factors.append(n)
	return list(factors)
t = ii()
while t:
	t -= 1
	n = ii()
	a = li()
	d = {}
	s = 0
	for i in range(30):
		d[i] = 0
	for i in a:
		d[int(math.log2(i))] += 1
	for i in d.keys():
		if d[i] > 0:
			s += d[i] * (d[i] - 1) // 2
	print(s)


import sys
sys.setrecursionlimit(10 ** 5)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep='\n')

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

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

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

def LLI(rows_number):
	return [LI() for _ in range(rows_number)]

def SI():
	return sys.stdin.readline()[:-1]
for _ in range(II()):
	cnt = [0] * 35
	n = II()
	aa = LI()
	for a in aa:
		cnt[a.bit_length()] += 1
	ans = 0
	for c in cnt:
		ans += c * (c - 1) // 2
	print(ans)


for x in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	v = [0] * 30
	for i in range(n):
		k = 0
		j = 0
		while j < 30:
			if arr[i] >> j & 1:
				k = j
			j += 1
		v[k] += 1
	ans = 0
	for i in range(30):
		ans += v[i] * (v[i] - 1) // 2
	print(ans)


def mi():
	return map(int, input().split())
for _ in range(int(input())):
	n = int(input())
	a = list(mi())
	a.sort()
	i = 0
	ans = 0
	for p in range(1, 101):
		t = []
		while i < n:
			if a[i] < 2 ** p:
				t.append(a[i])
			else:
				break
			i += 1
		l = len(t)
		ans += l * (l - 1) // 2
		if i == n:
			break
	print(ans)


from collections import *
for u in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	d = []
	for i in range(n):
		s = bin(l[i])[2:][::-1]
		x = 0
		for i in range(len(s)):
			if s[i] == '1':
				x = i + 1
		d.append(x)
	dd = defaultdict(int)
	for i in d:
		dd[i] += 1
	c = 0
	for i in dd:
		r = dd[i] * (dd[i] - 1) // 2
		c += r
	print(c)


import math
from collections import Counter
for _ in range(int(input())):
	n = int(input())
	lis = list(map(int, input().split()))
	lis = sorted([int(math.log(i, 2)) for i in lis])
	x = Counter(lis)
	y = 0
	for num in x:
		z = x[num]
		if z > 1:
			y += z * (z - 1) // 2
	print(y)


from sys import *
from math import *

def lenght(n):
	x = 0
	while 2 ** x <= n:
		x += 1
	return x
t = int(stdin.readline())
for _ in range(t):
	n = int(stdin.readline())
	a = list(map(int, stdin.readline().split()))
	b = [0] * 35
	ans = 0
	for i in range(n):
		b[lenght(a[i])] += 1
	for i in range(35):
		ans += b[i] * (b[i] - 1) // 2
	print(ans)


def process(n, li):
	hashi = {}
	res = 0
	for i in li:
		t = -1
		for j in range(0, 30):
			if 1 << j & i:
				t = max(t, j)
		if t in hashi:
			res += hashi[t]
			hashi[t] += 1
		else:
			hashi[t] = 1
	return res

def brute(n, li):
	res = 0
	for i in range(0, n):
		for j in range(i + 1, n):
			if li[i] & li[j] >= li[i] ^ li[j]:
				res += 1
	return res
tests = int(input())
for i in range(tests):
	n = int(input())
	li = list(map(int, input().split()))
	print(process(n, li))


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 *
t = ii1()
for i in range(t):
	n = ii1()
	arr = iia()
	ans = 0
	mask = [0] * 40
	for i in range(n):
		if mask[len(bin(arr[i])) - 2] > 0:
			ans += mask[len(bin(arr[i])) - 2]
		mask[len(bin(arr[i])) - 2] += 1
	print(ans)


import math
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	msb = [0] * 38
	for i in range(len(arr)):
		k = int(math.log(arr[i], 2))
		msb[k + 1] += 1
	pairs = 0
	for i in msb:
		pairs += int(i * (i - 1) // 2)
	print(pairs)


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

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

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

def solution(n, nums):
	freq = [0] * 31
	res = 0
	for i in range(n):
		freq[nums[i].bit_length()] += 1
	for i in range(31):
		res += freq[i] * (freq[i] - 1) // 2
	return res
for _ in range(getint()):
	n = getint()
	nums = getlistints()
	print(solution(n, nums))


from sys import stdin, stdout
from collections import Counter
from itertools import permutations
import bisect
import math
I = lambda : map(int, stdin.readline().split())
I1 = lambda : stdin.readline()

def Highest_setbit(x):
	if x == 0:
		return 0
	c = 0
	x //= 2
	while x:
		x //= 2
		c += 1
	return c + 1
for _ in range(int(I1())):
	n = int(I1())
	a = list(I())
	b = [Highest_setbit(a[i]) for i in range(n)]
	c = list(Counter(b).values())
	ans = 0
	for x in c:
		ans += x * (x - 1) // 2
	print(ans)


for _ in range(int(input())):
	n = int(input())
	s = list(map(int, input().split()))
	ans = 0
	l = [0] * 32
	for i in range(n):
		p = len(bin(s[i])[2:])
		ans += l[p]
		l[p] += 1
	print(ans)


from collections import defaultdict, Counter, deque
t = int(input())
arr = []
for _ in range(t):
	k = input()
	arr.append(list(map(int, input().split())))
for lis in arr:
	newarr = [len(bin(i)) for i in lis]
	c = Counter(newarr)
	total = 0
	for i in c:
		total += c[i] * (c[i] - 1) // 2
	print(total)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	t = []
	for i in arr:
		x = bin(i)[2:]
		x = '0' * (32 - len(x)) + x
		t.append(x)
	dic = {}
	for i in range(32):
		dic[i] = 0
	c = 0
	for i in t:
		for j in range(32):
			if i[j] == '1':
				c += dic[j]
				dic[j] += 1
				break
	print(c)


for w in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	m = max(a)
	ans = 0
	t = 0
	a = sorted(a)
	if n == 1:
		print(0)
	else:
		for i in range(33):
			t = 0
			c = pow(2, i)
			d = pow(2, i + 1)
			for j in range(n):
				if a[j] >= c and a[j] < d:
					t += 1
			ans += t * (t - 1) // 2
		print(ans)


from collections import defaultdict as dd
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	d = dd(int)
	ans = 0
	for i in a:
		d[len(bin(i))] += 1
	for i in d.keys():
		temp = d[i]
		ans += temp * (temp - 1) // 2
	print(ans)


import math
from collections import defaultdict

def setBitNumber(n):
	k = int(math.log(n, 2))
	return 2 ** k
t = int(input())
while t:
	t -= 1
	n = int(input())
	arr = [int(_) for _ in input().split(' ')]
	arr_ = [setBitNumber(_) for _ in arr]
	ans = defaultdict(int)
	for i in arr_:
		ans[i] += 1
	op = 0
	for (k, v) in ans.items():
		if v >= 2:
			op = op + v * (v - 1) // 2
	print(op)


from sys import stdin
N = int(stdin.readline())
for case in range(N):
	ans = 0
	length = int(stdin.readline())
	array = list(map(int, stdin.readline().split()))
	dicti = {}
	for i in array:
		if dicti.get(len(bin(i)), -1) == -1:
			dicti[len(bin(i))] = 1
		else:
			dicti[len(bin(i))] += 1
	for i in dicti:
		a = dicti[i]
		ans += a * (a - 1) // 2
	print(ans)


import math
from datetime import date

def main():
	t = int(input())
	while t > 0:
		t -= 1
		n = int(input())
		a = [int(x) for x in input().split()]
		cnt = [0 for i in range(40)]
		ans = 0
		for i in range(n):
			b = -1
			for j in range(40):
				if a[i] & 1 << j != 0:
					b = j
			ans += cnt[b]
			cnt[b] += 1
		print(ans)
main()


t = int(input())

def highest(x):
	s = list(bin(x)[2:])[::-1]
	h = 0
	for i in range(len(s)):
		if s[i] == '1':
			h = i
	return h
while t:
	n = int(input())
	a = list(map(int, input().split()))
	d = {}
	for i in range(n):
		h = highest(a[i])
		if h not in d:
			d[h] = 0
		d[h] += 1
	cnt = 0
	for x in a:
		cnt += d[highest(x)] - 1 if d[highest(x)] > 0 else 0
		d[highest(x)] = max(0, d[highest(x)] - 1)
	print(cnt)
	t -= 1


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	d = {}
	res = 0
	for i in range(n):
		ind = len(bin(arr[i]))
		if ind in d:
			res += d[ind]
			d[ind] += 1
		else:
			d[ind] = 1
	print(res)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	l.sort()
	cnt = 0
	i = 0
	ii = 31
	j = n
	jj = n
	ans = 0
	while ii > 0:
		ii -= 1
		f = -1
		cnt = 0
		i = 0
		while i < jj:
			bit = l[i] >> ii
			if bit == 1:
				if f == -1:
					f = 1
					j = i
				cnt += 1
			i += 1
		jj = j
		if cnt % 2:
			cnt = (cnt - 1) // 2 * cnt
		else:
			cnt = cnt // 2 * (cnt - 1)
		ans += cnt
	print(ans)


T = int(input())
frange = range
fint = int
h1 = {j: 1 << j for j in range(32)}
for i in frange(T):
	n = fint(input())
	arr = list(map(fint, input().split()))
	ans = 0
	for j in frange(29, -1, -1):
		h = 0
		for i in frange(n):
			if arr[i] >= h1[j] and arr[i] < h1[j + 1]:
				h += 1
		ans += h * (h - 1) // 2
	print(ans)


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 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 sieve():
	li = [True] * (2 * 10 ** 5 + 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 = []
	for i in range(2 * 10 ** 5 + 5):
		if li[i] == True:
			prime.append(i)
	return prime

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

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

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
mod = 10 ** 9 + 7

def solve():
	n = II()
	a = L()
	mx = max(a)
	mx = bin(mx)[2:]
	mxx = len(mx) - 1
	li = [0] * (mxx + 1)
	for ele in a:
		f = bin(ele)[2:].rjust(mxx + 1, '0')
		for i in range(len(f)):
			if f[i] == '1':
				li[mxx - i] += 1
				break
	ans = 0
	for ele in li:
		if ele >= 2:
			ans += ele * (ele - 1) // 2
	P(ans)
	return
t = II()
for i in range(t):
	solve()


import math
t = int(input())
for _ in range(t):
	dp = [0] * 100
	n = int(input())
	a = [int(i) for i in input().split()]
	a.sort()
	i = 0
	j = 1
	r = 0
	while i < n:
		if a[i] < j:
			dp[r] += 1
			i += 1
		else:
			j *= 2
			r += 1
	t = 0
	for i in range(100):
		t += dp[i] * (dp[i] - 1) // 2
	print(t)


from collections import Counter
from collections import OrderedDict
from collections import defaultdict

def SOCountR(mylist):
	return OrderedDict(sorted(Counter(mylist).items(), reverse=True))

def SOCount(mylist):
	return OrderedDict(sorted(Counter(mylist).items()))

def freq_count(mylist):
	return Counter(mylist)
import math

def setBitNumber(n):
	k = int(math.log(n, 2))
	return 2 ** k
t = int(input())
for _ in range(t):
	n = int(input())
	arr = [int(x) for x in input().split()]
	arr1 = [setBitNumber(x) for x in arr]
	fre = dict(freq_count(arr1))
	count = 0
	for k in fre:
		v = fre[k]
		if v == 1:
			pass
		count += v * (v - 1) // 2
	print(count)


from sys import stdin, stdout
input = stdin.readline
print = lambda x: stdout.write(str(x) + '\n')
from collections import defaultdict
for _ in range(int(input())):
	n = int(input())
	cnt = defaultdict(int)
	for i in map(int, input().split()):
		cnt[len(bin(i)) - 2] += 1
	ans = 0
	for i in cnt:
		if cnt[i] > 1:
			ans += cnt[i] * (cnt[i] - 1) // 2
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	m = max(a)
	t1 = 0
	d = {}
	for i in range(n):
		l = len(bin(a[i])[2:])
		if l not in d:
			d[l] = 1
		else:
			d[l] += 1
	t2 = 0
	for i in d:
		if d[i] > 1:
			t2 += d[i] * (d[i] - 1) // 2
	print(t2)


for _ in range(int(input())):
	n = int(input())
	arr = [int(x) for x in input().split()]
	d = {}
	ans = 0
	for i in arr:
		for j in range(31, -1, -1):
			if i & 1 << j:
				bit = j
				break
		if bit in d:
			ans += d[bit]
			d[bit] += 1
		else:
			d[bit] = 1
	print(ans)


from collections import Counter

def msb(x):
	x = bin(x)
	z = 0
	j = 0
	for i in range(len(x) - 1, 1, -1):
		z += 1
		if x[i] == '1':
			j = z
	return j
for _ in range(int(input())):
	n = int(input())
	x = list(map(int, input().split()))
	l = list(map(msb, x))
	c = Counter(l)
	ans = 0
	for i in c:
		if c[i] > 1:
			ans += c[i] * (c[i] - 1) // 2
	print(ans)


import math

def answer(n, A):
	d = {}
	for i in range(n):
		pos = 0
		c = 1
		for j in range(32, -1, -1):
			if A[i] & c << j:
				pos = j
				break
		if pos in d:
			d[pos] += 1
		else:
			d[pos] = 1
	count = 0
	for i in d:
		num = d[i]
		count += num * (num - 1) // 2
	return count
t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	print(answer(n, arr))


import math
from collections import defaultdict
from sys import stdin
input = stdin.readline
T = int(input())
for _ in range(T):
	n = int(input())
	arr = list(map(int, input().split()))
	bit = [0] * 64
	for i in arr:
		k = int(math.log(i, 2))
		bit[k] += 1
	c = 0
	for i in bit:
		if i > 1:
			c += i * (i - 1) // 2
	print(c)


for _ in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	powers_of_two = [0] * 30
	for num in a:
		power = 0
		while True:
			if pow(2, power) <= num < pow(2, power + 1):
				powers_of_two[power] += 1
				break
			power += 1
	all = 0
	for quantity in powers_of_two:
		if quantity >= 2:
			all += (1 + quantity - 1) * (quantity - 1) // 2
	print(all)


def algo(n, arr):
	arr.sort()
	res = 0
	e = 30
	while e >= 0 and n > 0:
		c = 0
		while n > 0 and arr[n - 1] >= 2 ** e:
			c += 1
			n -= 1
		res += c * (c - 1) // 2
		e -= 1
	return res
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	ans = algo(n, arr)
	print(ans)


T = int(input())
while T > 0:
	n = int(input())
	arr = list(map(int, input().split()))
	arr.sort()
	res = 0
	prev = arr[0]
	count = 1
	for i in range(1, n):
		if arr[i] & prev >= arr[i] ^ prev:
			res += count
			count += 1
		else:
			prev = arr[i]
			count = 1
	print(res)
	T -= 1


import sys
input = sys.stdin.readline
t = int(input())
for you in range(t):
	n = int(input())
	l = input().split()
	li = [int(i) for i in l]
	hashi = dict()
	count = 0
	for i in li:
		for j in range(31, -1, -1):
			if i & 1 << j:
				ans = j
				break
		if ans in hashi:
			count += hashi[ans]
			hashi[ans] += 1
		else:
			hashi[ans] = 1
	print(count)


from collections import Counter
import math
for ad in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	x = []
	l.sort()
	s = 1
	temp = []
	for i in l:
		if i < 2 ** s:
			temp.append(i)
		else:
			if temp != []:
				x.append(temp)
			temp = [i]
			s = int(math.log(i, 2)) + 1
	if temp != []:
		x.append(temp)
	ans = 0
	for i in x:
		y = len(i)
		ans += y * (y - 1) // 2
	print(ans)


from collections import defaultdict

def main():
	T = int(input())
	while T > 0:
		T -= 1
		N = int(input())
		v = list(map(int, input().split()))
		dp = defaultdict(set)
		for i in range(N):
			for bit in range(31, -1, -1):
				if v[i] & 1 << bit:
					dp[bit].add(i)
					break
		ans = 0
		for bit in range(31, -1, -1):
			size = len(dp[bit])
			ans += size * (size - 1) >> 1
		print(ans)
main()


import math
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	for i in range(n):
		arr[i] = int(math.log(arr[i], 2))
	arr.sort()
	curr = arr[0]
	c = 0
	i = 0
	r = 0
	while i < n:
		curr = arr[i]
		c = 1
		j = i + 1
		while j < n and curr == arr[j]:
			c += 1
			j += 1
		r += c * (c - 1) // 2
		i = j
	print(r)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	b = dict()
	ans = 0
	for i in a:
		if b.get(len(bin(i)), 0) == 0:
			b[len(bin(i))] = 1
		else:
			b[len(bin(i))] += 1
	for key in b:
		ans += b[key] * (b[key] - 1) // 2
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(lambda x: len(bin(int(x))), input().split()))
	k = [0 for i in range(40)]
	for b in a:
		k[b] += 1
	total = 0
	for b in k:
		total += b * (b - 1) // 2
	print(total)


from collections import defaultdict
import atexit
import io
import sys
t = int(input())
while t > 0:
	n = int(input())
	arr = [int(i) for i in input().split()]
	dct = defaultdict(int)
	for i in arr:
		c = len(bin(i)) - 2
		dct[c] += 1
	ctr = 0
	for i in dct:
		if dct[i] > 1:
			ctr = ctr + dct[i] * (dct[i] - 1) // 2
	print(ctr)
	t -= 1


import sys
from itertools import accumulate

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

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

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

def list4d(a, b, c, d, e):
	return [[[[e for l in range(d)] for k 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
EPS = 10 ** (-10)
for _ in range(INT()):
	N = INT()
	A = LIST()
	B = list2d(32, N, 0)
	for i in range(N):
		B[A[i].bit_length() - 1][i] = 1
	for k in range(32):
		B[k] = list(accumulate(B[k][::-1]))[::-1]
	ans = 0
	for i in range(N):
		ans += B[A[i].bit_length() - 1][i] - 1
	print(ans)


from sys import stdin, stdout
import math, sys, heapq
from itertools import permutations, combinations
from collections import defaultdict, deque, OrderedDict
from os import path
import random
import bisect as bi

def yes():
	print('YES')

def no():
	print('NO')
if path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')

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

	def In():
		return map(int, input().split())
else:

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

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

def dict(a):
	d = {}
	for x in a:
		if d.get(x, -1) != -1:
			d[x] += 1
		else:
			d[x] = 1
	return d

def find_gt(a, x):
	i = bi.bisect_left(a, x)
	if i != len(a):
		return i
	else:
		return -1

def main():
	try:
		n = I()
		l = list(In())
		d = {}
		for x in l:
			t = len(bin(x))
			if d.get(t, -1) != -1:
				d[t] += 1
			else:
				d[t] = 1
		ans = 0
		for x in d:
			ans += d[x] * (d[x] - 1) // 2
		print(ans)
	except:
		pass
M = 998244353
P = 1000000007
for _ in range(I()):
	main()


for i in range(int(input())):
	a = int(input())
	l = [int(x) for x in input().split()]
	m = []
	for j in range(a):
		c = bin(l[j])
		c = len(c)
		m.append(c)
	b1 = list(set(m))
	d = 0
	for i in b1:
		x = m.count(i)
		if x > 1:
			k = x * (x - 1) // 2
			d += k
	print(d)


T = int(input())
while T:
	n = int(input())
	A = list(map(int, input().split()))
	D = {}
	for i in A:
		x = bin(i)[2:]
		if len(x) in D:
			D[len(x)] += 1
		else:
			D[len(x)] = 1
	c = 0
	for i in D:
		c += D[i] * (D[i] - 1) // 2
	print(c)
	T = T - 1


import math
T = int(input())
for _ in range(T):
	n = int(input())
	a = list(map(int, input().split()))
	b = {}
	for x in a:
		l = math.floor(math.log2(x)) + 1
		if l not in b:
			b[l] = 0
		b[l] += 1
	ans = 0
	for (k, v) in b.items():
		ans += v * (v - 1) // 2
	print(ans)


from collections import Counter

def DB(num):
	k = []
	while num > 0:
		k.append(num % 2)
		num = num // 2
	return k
l = []
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	s = []
	for i in range(n):
		s.append(len(DB(a[i])))
	d = dict(Counter(s))
	m = set(s)
	sum = 0
	for i in m:
		if d[i] > 1:
			sum = sum + (d[i] - 1) * d[i] // 2
	l.append(sum)
for i in l:
	print(i)


from collections import Counter
import string
import math
import sys

def array_int():
	return [int(i) for i in sys.stdin.readline().split()]

def vary(number_of_variables):
	if number_of_variables == 1:
		return int(sys.stdin.readline())
	if number_of_variables >= 2:
		return map(int, sys.stdin.readline().split())

def makedict(var):
	return dict(Counter(var))
mod = 100000007
import math

def countBits(number):
	return int(math.log(number) / math.log(2) + 1)
for _ in range(vary(1)):
	n = vary(1)
	num = array_int()
	tt = []
	for i in range(n):
		tt.append(countBits(num[i]))
	tti = makedict(tt)
	count = 0
	for i in tti:
		if tti[i] > 1:
			count += tti[i] * (tti[i] - 1) // 2
	print(count)


import math
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	loga = [math.floor(math.log(i, 2)) for i in a]
	dic = {}
	for i in loga:
		try:
			dic[i] += 1
		except:
			dic[i] = 1
	ans = 0
	for i in dic:
		ans += (dic[i] - 1) * dic[i] // 2
	print(ans)


import sys

def get_bucket(x):
	count = 0
	while x > 1:
		x //= 2
		count += 1
	return count
t = int(input())
for i in range(t):
	n = int(input())
	A = list(map(int, input().strip().split(' ')))
	ans = 0
	buckets = {j: 0 for j in range(30)}
	for a in A:
		bucket = get_bucket(a)
		ans += buckets[bucket]
		buckets[bucket] += 1
	print(ans)


for _ in range(int(input())):
	n = int(input())
	c = 0
	a = [int(x) for x in input().split()]
	if n < 2:
		print(0)
	else:
		b = []
		for i in range(n):
			x = len(bin(a[i])) - 2
			b.append(x)
		b.sort()
		b1 = list(set(b))
		l = len(b1)
		b1 = list(set(b))
		for i in b1:
			x = b.count(i)
			if x > 1:
				k = x * (x - 1) // 2
				c += k
		print(c)


import os
import sys
from math import *
from collections import *
from bisect import *
from io import BytesIO, IOBase

def vsInput():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
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')
ALPHA = 'abcdefghijklmnopqrstuvwxyz/'
M = 10 ** 9 + 7
EPS = 1e-06

def Ceil(a, b):
	return a // b + int(a % b > 0)

def value():
	return tuple(map(int, input().split()))

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

def Int():
	return int(input())

def Str():
	return input()

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

def Bits(n):
	count = 0
	while n:
		count += 1
		n >>= 1
	return count
for _ in range(Int()):
	n = Int()
	a = array()
	have = defaultdict(int)
	ans = 0
	for i in a:
		here = Bits(i)
		ans += have[here]
		have[here] += 1
	print(ans)


from __future__ import division, print_function
from fractions import Fraction
import sys
import os
from io import BytesIO, IOBase
from itertools import *
import bisect
from heapq import *
from math import *
from copy import *
from collections import deque
from collections import Counter as counter
from itertools import combinations as comb
from itertools import permutations as permutate
from bisect import bisect_left as bl
from bisect import bisect_right as br
from bisect import bisect
BUFSIZE = 8192
from sys import stderr

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
mod = 1000000007

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 zerolist(n):
	return [0] * n

def nextline():
	out('\n')

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

def printlist(a):
	for p in range(0, len(a)):
		out(str(a[p]) + ' ')

def pow(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
from functools import reduce

def factors(n):
	return set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0)))

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

def solve():
	n = int(inp())
	ar = lis()
	m = max(ar).bit_length()
	ans = 0
	for i in range(m - 1, -1, -1):
		s = [].copy()
		n_s = [].copy()
		for el in ar:
			if el & 1 << i:
				s.append(el)
			else:
				n_s.append(el)
		k = len(s)
		ans += k * (k - 1) // 2
		ar = n_s.copy()
	print(ans)
testcase(int(inp()))


(z, zz) = (input, lambda : list(map(int, z().split())))
zzz = lambda : [int(i) for i in stdin.readline().split()]
(szz, graph, mod, szzz) = (lambda : sorted(zz()), {}, 10 ** 9 + 7, lambda : sorted(zzz()))
from string import *
from re import *
from collections import *
from queue import *
from sys import *
from collections import *
from math import *
from heapq import *
from itertools import *
from bisect import *
from collections import Counter as cc
from math import factorial as f
from bisect import bisect as bs
from bisect import bisect_left as bsl
from itertools import accumulate as ac

def lcd(xnum1, xnum2):
	return xnum1 * xnum2 // gcd(xnum1, xnum2)

def prime(x):
	p = ceil(x ** 0.5) + 1
	for i in range(2, p):
		if x % i == 0 and x != 2 or x == 0:
			return 0
	return 1

def dfs(u, visit, graph):
	visit[u] = True
	for i in graph[u]:
		if not visit[i]:
			dfs(i, visit, graph)
num = int(z())
for _ in range(num):
	n = int(z())
	arr = zzz()
	cnt = [0] * 35
	for i in arr:
		cnt[len(bin(i))] += 1
	total = 0
	for i in cnt:
		total += i * (i - 1) // 2
	print(total)


import collections
t = int(input())
for i in range(t):
	n = int(input())
	s = list(map(int, input().strip(' ').split(' ')))
	ans = 0
	count = collections.Counter()
	for i in s:
		k = 0
		while i >> k != 0:
			k += 1
		if k in count:
			ans += count[k]
		count[k] += 1
	print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	l = [int(x) for x in input().split(' ')]
	d = {}
	for i in l:
		b = str(bin(i))
		b = b[::-1]
		z = b.rfind('1')
		if z not in d:
			l2 = []
			l2.append(i)
			d[z] = l2
		else:
			d[z].append(i)
	count = 0
	for i in d:
		if len(d[i]) > 1:
			count += len(d[i]) * (len(d[i]) - 1) / 2
	print(int(count))


def sbit(n):
	n |= n >> 1
	n |= n >> 2
	n |= n >> 4
	n |= n >> 8
	n |= n >> 16
	n += 1
	return n >> 1
for i in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	l.sort()
	cnt = 1
	ans = 0
	for i in range(1, n):
		if sbit(l[i - 1]) == sbit(l[i]):
			cnt += 1
		else:
			if cnt >= 2:
				ans += cnt * (cnt - 1) // 2
			cnt = 1
	if cnt >= 2:
		ans += cnt * (cnt - 1) // 2
	print(ans)


def ind(n):
	i = -1
	while n:
		i += 1
		n //= 2
	return i
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	(anz, onez) = (0, [0] * 31)
	for el in l:
		onez[ind(el)] += 1
	for el in onez:
		if el > 1:
			anz += el * (el - 1) // 2
	print(anz)


import sys, os.path
from collections import *
from copy import *
import math
mod = 10 ** 9 + 7
if os.path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
for t in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	if n == 1:
		print(0)
	else:
		d = {}
		for i in a:
			s = len(bin(i)) - 2
			if s in d:
				d[s] += 1
			else:
				d[s] = 1
		ans = 0
		for i in d:
			ans += d[i] * (d[i] - 1) // 2
		print(ans)


import sys
import math
t = int(sys.stdin.readline().strip())
for idx_t in range(t):
	n = int(sys.stdin.readline().strip())
	a_vec = sys.stdin.readline().strip().split(' ')
	a_bits_dict = dict()
	for this_a in a_vec:
		a_bits = int(math.log2(int(this_a)))
		if a_bits in a_bits_dict:
			a_bits_dict[a_bits] += 1
		else:
			a_bits_dict[a_bits] = 1
	this_res = 0
	for (k, v) in a_bits_dict.items():
		this_res = this_res + int(v * (v - 1) / 2)
	print(this_res)


from sys import stdin
input = stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	vis = [0] * n
	ans = 0
	for k in range(31, -1, -1):
		c = 0
		for (i, v) in enumerate(l):
			if vis[i] == 0 and v & 1 << k:
				vis[i] = 1
				c += 1
		ans += c * (c - 1) // 2
	print(ans)


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


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	d = dict()
	for i in range(n):
		x = bin(a[i]).replace('0b', '')
		d.setdefault(len(x), 0)
		d[len(x)] += 1
	ans = 0
	for i in d:
		ans += d[i] * (d[i] - 1) // 2
	print(ans)


import sys, math, itertools, collections, copy
input = sys.stdin.readline
INF = 10 ** 9 + 20

def solve():
	n = int(input())
	a = [int(x) for x in input().split()]
	cnt = dict()
	for i in range(len(a)):
		s = bin(a[i])
		s = s[:-1]
		if not len(s) in cnt:
			cnt[len(s)] = 1
		else:
			cnt[len(s)] += 1
	ans = 0
	for key in cnt:
		ans += cnt[key] * (cnt[key] - 1) // 2
	print(ans)
for _ in range(int(input())):
	solve()


def comb(num):
	return num * (num - 1) // 2
import math
t = int(input())
while t > 0:
	n = int(input())
	l = [int(x) for x in input().split()]
	a = []
	for i in l:
		a.append(int(math.log2(i)) + 1)
	b = {}
	for i in a:
		if i in b:
			b[i] += 1
		else:
			b[i] = 1
	count = 0
	for i in b.values():
		if i > 1:
			count += comb(i)
	print(count)
	t -= 1


for q11 in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	d = {}
	for q in range(n):
		q1 = 1
		while q1 <= a[q]:
			q1 *= 2
		if q1 / 2 in d:
			d[q1 / 2] += 1
		else:
			d[q1 / 2] = 1
	ans = 0
	for q in d.values():
		ans += q * (q - 1) // 2
	print(ans)


for _ in range(int(input())):
	n = int(input())
	l = [int(i) for i in input().split()][:n]
	arr = [True for i in range(n)]
	arr1 = []
	for i in range(n):
		temp = bin(l[i])[2:].zfill(30)
		arr1.append(temp)
	cnt = 0
	for i in range(30):
		cnt1 = 0
		for j in range(n):
			if arr[j]:
				if arr1[j][i] == '1':
					arr[j] = False
					cnt1 += 1
		cnt += cnt1 * (cnt1 - 1) // 2
	print(cnt)


from collections import defaultdict
from math import log2, ceil
for _ in range(int(input())):
	n = int(input())
	d = defaultdict(int)
	ans = 0
	for x in input().split():
		msb = int(log2(int(x)) + 1)
		ans += d[msb]
		d[msb] += 1
	print(ans)


from collections import defaultdict
from math import *

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 solve(n):
	n |= n >> 1
	n |= n >> 2
	n |= n >> 4
	n |= n >> 8
	n |= n >> 16
	n = n + 1
	return n >> 1
for i in range(int(input())):
	n = int(input())
	l = [int(c) for c in input().split()]
	d = defaultdict(lambda : 0)
	for i in l:
		d[solve(i)] += 1
	count = 0
	values = list(d.values())
	for i in values:
		if i > 1:
			count += nCr(i, 2)
	print(int(count))


import sys
import math, bisect
sys.setrecursionlimit(10 ** 5)
from itertools import groupby, accumulate
from heapq import heapify, heappop, heappush
from collections import deque, Counter, defaultdict

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

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

def Neo():
	return list(map(int, sys.stdin.readline().split()))
for _ in range(I()):
	N = I()
	A = Neo()
	d = defaultdict(int)
	for i in A:
		d[len(bin(i)[2:])] += 1
	Ans = 0
	for i in d.values():
		Ans += i * (i - 1) // 2
	print(Ans)


import math
t = int(input())
while t > 0:
	t = t - 1
	n = int(input())
	l1 = [None] * n
	l = input().split()
	c = 0
	for i in l:
		m = int(i)
		y = math.floor(math.log2(m)) + 1
		l1[c] = (m, y)
		c = c + 1
	l1.sort(key=lambda x: x[1])
	co = 1
	tco = 0
	for i in range(n):
		if i < n - 1 and l1[i][1] == l1[i + 1][1]:
			co += 1
		else:
			tco = tco + co * (co - 1) // 2
			co = 1
	print(tco)


import sys, io, os, time
from collections import defaultdict
from collections import Counter
from collections import deque
from itertools import combinations
from itertools import permutations
import bisect, math, heapq
alphabet = 'abcdefghijklmnopqrstuvwxyz'
input = sys.stdin.readline
for ii in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	ans = 0
	count = [0] * 33
	for i in range(n):
		bit = 0
		for j in range(33):
			if a[i] & 1 << j:
				bit = j
		count[bit] += 1
	for i in range(33):
		ans += count[i] * (count[i] - 1) // 2
	print(ans)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	o = [0 for i in range(1, 33)]
	for i in l:
		x = len(bin(i)) - 2
		o[x] = o[x] + 1
	ans = 0
	for i in o:
		ans = i * (i - 1) // 2 + ans
	print(ans)


import sys
ii = lambda : sys.stdin.readline().strip()
idata = lambda : [int(x) for x in ii().split()]

def solve():
	n = int(ii())
	data = idata()
	slov = {}
	ans = 0
	for i in range(n):
		slov[len(bin(data[i]))] = slov[len(bin(data[i]))] + 1 if len(bin(data[i])) in slov else 1
	for i in slov:
		ans += slov[i] * (slov[i] - 1) // 2
	print(ans)
	return
for t in range(int(ii())):
	solve()


t = int(input())
from collections import Counter as c

def pow(a):
	n = len(a)
	b = []
	for i in a:
		d = 0
		while i != 0:
			i = i // 2
			d += 1
		b.append(d)
	return b
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	b = pow(a)
	b = c(b)
	d = 0
	for i in b:
		if b[i] > 1:
			d += b[i] * (b[i] - 1) // 2
	print(d)


from collections import defaultdict

def process(arr):
	bitCounts = defaultdict(int)
	count = 0
	for n in arr:
		bit = 1
		while n:
			lsb = n & ~(n - 1)
			n -= lsb
			bit = lsb
		count += bitCounts[bit]
		bitCounts[bit] += 1
	return count
t = int(input())
for _ in range(t):
	input()
	print(process(map(int, input().split())))


t = int(input())
for k in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	c = 0
	d = 0
	for i in range(1, n):
		l = len(str(bin(a[i]))) - len(str(bin(a[i - 1])))
		if str(bin(a[i]).replace('b', ''))[1:][0] == ('0' * l + str(bin(a[i - 1]).replace('b', ''))[1:])[0]:
			d += 1
			c += d
		else:
			d = 0
	print(c)


import sys
import bisect
get_input = sys.stdin.readline
t = int(get_input().strip())
for i in range(t):
	n = int(get_input().strip())
	arr = list(map(int, get_input().split()))
	sorted_arr = sorted(arr)
	count = 0
	for k in range(n - 1):
		num = sorted_arr[k]
		th = 1 << len(bin(num)) - 2
		idx = bisect.bisect_left(sorted_arr, th, lo=k)
		if idx >= n:
			idx -= 1
		if sorted_arr[idx] < th:
			count += idx - k
		else:
			count += idx - k - 1
	print(count)


import sys, math
import io, os
from bisect import bisect_left as bl, bisect_right as br, insort
from heapq import heapify, heappush, heappop
from collections import defaultdict as dd, deque, Counter

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

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

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

def out(var):
	sys.stdout.write(str(var) + '\n')
from decimal import Decimal
mod = 998244353
INF = float('inf')
for t in range(int(data())):
	n = int(data())
	a = mdata()
	a.sort()
	cnt = 1
	ans = 0
	for i in range(1, n):
		if len(bin(a[i])) == len(bin(a[i - 1])):
			cnt += 1
		else:
			ans += cnt * (cnt - 1) // 2
			cnt = 1
	ans += cnt * (cnt - 1) // 2
	out(ans)


import math
import collections
t = int(input())
for w in range(t):
	n = int(input())
	l = [int(i) for i in input().split()]
	if n == 1:
		print(0)
	else:
		d = {}
		for i in range(n):
			l[i] = int(math.log(l[i], 2))
		d = collections.Counter(l)
		s = 0
		for i in d:
			s += d[i] * (d[i] - 1) // 2
		print(s)


import collections
import math

def coun(x):
	return int(math.factorial(x) / (math.factorial(2) * math.factorial(x - 2)))
t = int(input())
for p in range(t):
	schet = 0
	n = int(input())
	mas = list(map(int, input().split()))
	bin_dict = dict()
	for i in range(len(mas)):
		if len(bin(mas[i])[2:]) in bin_dict:
			bin_dict[len(bin(mas[i])[2:])] += 1
		else:
			bin_dict[len(bin(mas[i])[2:])] = 1
	for i in bin_dict.items():
		schet += i[1] * (i[1] - 1) // 2
	print(schet)


def nCr(n, r):
	return fact(n) / (fact(r) * fact(n - r))

def fact(n):
	res = 1
	for i in range(2, n + 1):
		res = res * i
	return res
import collections
t = int(input())
for _ in range(t):
	n = int(input())
	ai = list(map(int, input().split()))
	for i in range(len(ai)):
		ai[i] = len(bin(ai[i]))
	d = collections.Counter(ai)
	ans = 0
	for i in d.values():
		if i > 1:
			ans += i * (i - 1) // 2
	print(int(ans))


from collections import Counter
import math
import sys
from bisect import bisect, bisect_left, bisect_right

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

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 mod():
	return 10 ** 9 + 7
for i in range(INT()):
	n = INT()
	arr = LIST()
	ans = [0] * 30
	for i in arr:
		x = int(math.log(i, 2))
		ans[x] += 1
	fans = 0
	for i in ans:
		if i == 1:
			continue
		elif i == 2:
			fans += 1
		else:
			fans += i * (i - 1) // 2
	print(fans)


from math import *
t = int(input())
while t:
	t = t - 1
	n = int(input())
	a = list(map(int, input().split()))
	d = dict()
	for i in a:
		z = floor(log(i, 2))
		d[z] = d.get(z, 0) + 1
	out = 0
	for i in d.values():
		out += i * (i - 1) // 2
	print(out)


from math import log
for _ in range(int(input())):
	x = int(input())
	(*li,) = map(int, input().split())
	for i in range(x):
		li[i] = int(log(li[i], 2)) + 1
	cnt = 0
	li1 = [0] * 31
	for i in li:
		li1[i] += 1
	for i in li1:
		cnt += i * (i - 1) // 2
	print(cnt)


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	count = [0] * 30
	for i in range(n):
		curr = 1
		temp = 1
		while curr <= arr[i]:
			curr *= 2
			temp += 1
		count[temp - 2] += 1
	ans = 0
	for i in count:
		if i != 0:
			ans += i * (i - 1) // 2
	print(ans)


from math import log2
T = int(input())
for t in range(T):
	N = int(input())
	L = list(map(int, input().split()))
	arr = [0] * 100000
	for i in L:
		arr[int(log2(i))] += 1
	count = 0
	for i in arr:
		if i > 1:
			count += i * (i - 1) // 2
	print(count)


T = int(input())
res = list()
for i in range(T):
	n = int(input())
	x = list(map(int, input().split()))
	res.append(x)
for i in range(len(res)):

	def binary(num):
		k = 0
		while num > 0:
			num = int(num / 2)
			k += 1
		return k
	dictionary = dict()
	for j in range(len(res[i])):
		a = binary(res[i][j])
		if a in dictionary:
			dictionary[a] += 1
		else:
			dictionary[a] = 1
	sum = 0
	for l in dictionary:
		if dictionary[l] > 1:
			n = dictionary[l]
			sum += int(n * (n - 1) / 2)
	print(sum)


def fn(ar, n):
	l = [0] * 32
	for i in ar:
		x = i
		c = 0
		while x:
			c += 1
			x = x // 2
		l[c] += 1
	s = 0
	for i in range(32):
		if l[i]:
			p = l[i]
			s += p * (p - 1) / 2
	return s
for _ in range(int(input())):
	n = int(input())
	ar = list(map(int, input().split()))
	print(int(fn(ar, n)))


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, list(input().split())))
	l = [k.bit_length() for k in l]
	if n == 1:
		print(0)
	else:
		p = 0
		y = 1
		l.sort()
		for i in range(1, n):
			if l[i] == l[i - 1]:
				y += 1
			else:
				p += y * (y - 1) / 2
				y = 1
		p += y * (y - 1) / 2
		print(int(p))


t = int(input())
while t > 0:
	n = int(input())
	a = [int(x) for x in input().split()]
	count = 0
	b = [0] * 33
	for i in range(n):
		z = a[i]
		c = 0
		while z > 0:
			z = z // 2
			c += 1
		b[c] += 1
	for i in range(len(b)):
		if b[i] > 1:
			k = b[i]
			count += k * (k - 1) // 2
	print(count)
	t -= 1


t = int(input())
for _ in range(t):
	c = 0
	n = int(input())
	a = [bin(int(vals)) for vals in input().split()]
	d = {}
	for x in a:
		if len(x) in d:
			d[len(x)] += 1
		else:
			d[len(x)] = 1
	for m in d:
		if d[m] != 1:
			c += d[m] * (d[m] - 1) / 2
	print(int(c))


import math
for _ in range(int(input())):
	n = int(input())
	nums = [int(math.log2(int(i))) for i in input().split()]
	d = {}
	for num in nums:
		d[num] = d.get(num, 0) + 1
	print(sum([i * (i - 1) // 2 for i in d.values()]))


import math
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	d = {}
	for x in l:
		s = math.log(x, 2)
		p = int(s)
		if p in d:
			d[p] = d[p] + 1
		else:
			d[p] = 1
	cnt = 0
	for i in d:
		x = d[i] - 1
		s = x * (x + 1) // 2
		cnt = cnt + s
	print(cnt)


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 math import gcd, ceil

def pre(s):
	n = len(s)
	pi = [0] * n
	for i in range(1, n):
		j = pi[i - 1]
		while j and s[i] != s[j]:
			j = pi[j - 1]
		if s[i] == s[j]:
			j += 1
		pi[i] = j
	return pi

def prod(a):
	ans = 1
	for each in a:
		ans = ans * each
	return ans

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

def binary(x, length=16):
	y = bin(x)[2:]
	return y if len(y) >= length else '0' * (length - len(y)) + y
for _ in range(int(input()) if True else 1):
	n = int(input())
	a = list(map(int, input().split()))
	highest = [-1] * (n + 69)
	di = {}
	for i in range(n):
		b = bin(a[i])[2:][::-1]
		for j in range(len(b)):
			if int(b[j]):
				highest[i] = j
		di[highest[i]] = 1 if highest[i] not in di else di[highest[i]] + 1
	ans = 0
	for each in di:
		ans += di[each] * (di[each] - 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')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def RL():
	return map(int, sys.stdin.readline().rstrip().split())

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

def N():
	return int(input())

def print_list(l):
	print(' '.join(map(str, l)))
from collections import defaultdict as dc
for _ in range(N()):
	n = N()
	a = RLL()
	dic = dc(int)
	for i in a:
		k = 0
		while i >= 1 << k:
			k += 1
		dic[k] += 1
	res = 0
	for i in dic.values():
		res += i * (i - 1) >> 1
	print(res)


def cal(x):
	now = 0
	while x > 0:
		x = x >> 1
		now += 1
	return now
for _ in range(0, int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	vis = [0] * 64
	ans = 0
	for i in a:
		id = cal(i)
		ans += vis[id]
		vis[id] += 1
	print(ans)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	ans = 0
	counts = {}
	for i in arr:
		m = i.bit_length()
		if m in counts:
			counts[m] += 1
		else:
			counts[m] = 1
	for x in counts.values():
		ans += x * (x - 1) // 2
	print(ans)


def chr2bin(x):
	return bin(int(x))[2:]
t = int(input())
for e in range(t):
	n = int(input())
	A = [*map(chr2bin, input().split())]
	lst = [0 for i in range(31)]
	for a in A:
		lst[len(a)] += 1
	count = 0
	for k in lst:
		count += k * (k - 1) // 2
	print(count)


from collections import defaultdict
t = int(input())
while t > 0:
	n = int(input())
	a = [int(i) for i in input().split()]
	d = defaultdict(int)
	for i in a:
		c = len(bin(i)) - 2
		d[c] += 1
	ans = 0
	for i in d:
		if d[i] > 1:
			ans += d[i] * (d[i] - 1) // 2
	print(ans)
	t -= 1


def decimalToBinary(num):
	return bin(num).replace('0b', '')
for _ in range(int(input())):
	n = int(input())
	ans = 0
	arr = list(map(int, input().split()))
	dp = [0] * 10 ** 5
	for i in range(n):
		c = 0
		m = decimalToBinary(arr[i])[::-1]
		for j in range(len(m)):
			if m[j] == '1':
				c = j
		dp[c] += 1
	a = 0
	ans = 0
	for i in dp:
		if i > 1:
			a = i * (i - 1) // 2
			ans += a
	print(ans)


import collections
n = int(input())
for i in range(n):
	count = int(input())
	numbers = list(map(int, input().split()))
	d = collections.Counter()
	for i in numbers:
		binValue = bin(i)[2:]
		d[len(binValue)] += 1
	ans = 0
	for key in d:
		numbersCount = d[key] * (d[key] - 1)
		numbersCount /= 2
		ans += numbersCount
	print(int(ans))


from sys import stdin
tt = int(stdin.readline())
for loop in range(tt):
	n = int(stdin.readline())
	a = list(map(int, stdin.readline().split()))
	lis = [0] * 40
	for i in a:
		lis[i.bit_length()] += 1
	ans = 0
	for i in lis:
		ans += i * (i - 1) // 2
	print(ans)


import math as mt

def count(n):
	n = int(n)
	if n == 0 or n == 1:
		return 1
	return int(mt.log(n, 2) + 1)
for _ in range(int(input())):
	n = int(input())
	b = 0
	c = sorted(list(map(count, input().split(' '))))
	s = 0
	a = c[0]
	for i in range(1, n):
		if c[i] == a:
			b = b + 1
		else:
			b = b + 1
			a = c[i]
			s = s + (b * b - b) // 2
			b = 0
	b = b + 1
	print(s + (b * b - b) // 2)


T = int(input())
from collections import Counter
for _ in range(T):
	N = int(input())
	A = list(map(int, input().split()))
	c = Counter()
	for a in A:
		b = len(bin(a))
		c[b] += 1
	ans = 0
	for v in c.values():
		ans += v * (v - 1) // 2
	print(ans)


from collections import defaultdict
testCases = int(input())
for _ in range(testCases):
	n = int(input())
	arr = list(map(int, input().split()))
	count = 0
	d = defaultdict(int)
	for i in arr:
		d[len(bin(i)) - 2] += 1
	for i in d.values():
		count += i * (i - 1) // 2
	print(count)


t = int(input())
i = 0
while i < t:
	n = int(input())
	a = list(map(int, input().split()))
	answer = 0
	step = 2
	while step <= 1073741824:
		index = 0
		P = -1
		while index < n:
			if step // 2 <= a[index] < step:
				P += 1
			index += 1
		answer += P * (P + 1) // 2
		step *= 2
	print(answer)
	i += 1


from math import *
t = int(input())
while t:
	t -= 1
	n = int(input())
	a = list(map(int, input().split()))
	h = {}
	s = 0
	for i in a:
		k = len(bin(i))
		if k in h:
			h[k] += 1
		else:
			h[k] = 1
	for i in h:
		s += h[i] * (h[i] - 1) // 2
	print(s)


from collections import Counter
import math

def main(lst):
	lst.sort()
	dic = Counter()
	res = 0
	for i in lst:
		res += dic[int(math.log2(i))]
		dic[int(math.log2(i))] += 1
	return res
a = int(input())
for i in range(a):
	n = input()
	lst = list(map(int, input().split()))
	print(main(lst))


from collections import defaultdict
T = int(input())

def extractlen(n):
	ans = 0
	while n > 0:
		n //= 2
		ans += 1
	return ans
for t in range(T):
	n = int(input())
	A = [int(i) for i in input().split()]
	d = defaultdict(int)
	for a in A:
		d[extractlen(a)] += 1
	ans = 0
	for i in d:
		if d[i] >= 2:
			ans += d[i] * (d[i] - 1) // 2
	print(ans)


for t in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	lis = [0] * 32
	rj = 0
	for i in a:
		x = len(bin(i)) - 2
		rj += lis[x]
		lis[x] += 1
	print(rj)


import sys
from collections import defaultdict as dc
import math
from bisect import bisect, bisect_left, bisect_right
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	x = [0] * 32
	for i in range(n):
		s = bin(l[i])[2:]
		p = len(s)
		x[p - 1] += 1
	c = 0
	for i in x:
		c += i * (i - 1) // 2
	print(c)


import sys
input = sys.stdin.readline
t = int(input())
for ii in range(t):
	n = int(input())
	a = [int(i) for i in input().split() if i != '\n']
	dicta = dict()
	ans = 0
	for i in range(n):
		leng = len(bin(a[i]))
		if leng in dicta:
			dicta[leng] += 1
		else:
			dicta[leng] = 1
	for i in dicta:
		ans += dicta[i] * (dicta[i] - 1) / 2
	print(int(ans))


from sys import stdin, stdout

def INPUT():
	return list((int(i) for i in stdin.readline().split()))

def inp():
	return stdin.readline()

def out(x):
	return stdout.write(x)
import math
INT_MAX = 10 ** 13
from bisect import bisect_left
import sys
MOD = 10 ** 9 + 7
for _ in range(int(input())):
	n = int(input())
	A = INPUT()
	A.sort()
	X = [0] * 33
	for i in range(n):
		X[int(math.log2(A[i]))] += 1
	ans = 0
	f = 0
	bool = False
	for i in range(33):
		z = X[i]
		ans += z * (z - 1) // 2
	print(ans)


for T in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	bit_count = [0] * 41
	for i in a:
		for j in range(30, -1, -1):
			if i & 1 << j:
				bit_count[j] += 1
				break
	s = 0
	for i in bit_count:
		if i != 0 and i != 1:
			s += i * (i - 1) // 2
	print(s)


from collections import defaultdict
t = int(input())
for ii in range(t):
	n = int(input())
	a = list(reversed(list(map(int, input().split(' ')))))
	m = defaultdict(int)
	res = 0
	for i in range(len(a)):
		s = str(bin(a[i])[2:])
		res += m[len(s)]
		m[len(s)] += 1
	print(res)


import sys, os, io
input = sys.stdin.readline
from bisect import *
T = int(input())
ans = [0] * T
for t in range(T):
	N = int(input())
	A = list(map(int, input().split()))
	A.sort()
	p = 2
	ind1 = 0
	while p >> 1 <= A[-1]:
		ind2 = bisect_left(A, p)
		ans[t] += (ind2 - ind1) * (ind2 - ind1 - 1) // 2
		ind1 = ind2
		p <<= 1
print(*ans, sep='\n')


import sys
import math
import collections
import bisect
import itertools
import decimal
import copy
import heapq
INF = 10 ** 20
MOD = 10 ** 9 + 7
ni = lambda : int(sys.stdin.readline().rstrip())
ns = lambda : map(int, sys.stdin.readline().rstrip().split())
na = lambda : list(map(int, sys.stdin.readline().rstrip().split()))
na1 = lambda : list(map(lambda x: int(x) - 1, sys.stdin.readline().rstrip().split()))
flush = lambda : sys.stdout.flush()

def main():
	t = ni()
	for _ in range(t):
		n = ni()
		a = na()
		bit = [[0 for _ in range(32)] for __ in range(n)]
		for (i, ai) in enumerate(a):
			for j in range(32):
				tmp = ai >> j & 1
				bit[i][j] = tmp
		zero = list(range(n))
		ans = 0
		for i in range(31, -1, -1):
			one = []
			nxt = []
			for idx in zero:
				if bit[idx][i] == 1:
					one.append(idx)
				else:
					nxt.append(idx)
			ans += max(0, len(one) * (len(one) - 1) // 2)
			zero = nxt
		print(ans)
main()


import collections
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	cnter = collections.defaultdict(list)
	for num in arr:
		bin_string = bin(num)[2:]
		cnter[len(bin_string)].append(bin_string)
	res = 0
	for (k, v) in cnter.items():
		n = len(v)
		res += n * (n - 1) // 2
	print(res)


from sys import stdin, stdout
input = stdin.readline
from collections import defaultdict as dd
import math

def geti():
	return map(int, input().strip().split())

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

def gets():
	return input()

def geta():
	return int(input())

def print_s(s):
	stdout.write(s + '\n')

def bit(n):
	k = int(math.log(n, 2))
	return 2 ** k

def solve():
	for _ in range(geta()):
		n = geta()
		a = getl()
		for i in range(n):
			a[i] = bit(a[i])
		count = dd(int)
		for i in a:
			count[i] += 1
		ans = 0
		for i in count.keys():
			ans += count[i] * (count[i] - 1) // 2
		print(ans)
solve()


from collections import Counter as cnt
import math

def bi(n):
	return len(bin(n).replace('0b', ''))
for p in range(int(input())):
	(n, l, d) = (int(input()), [], {})
	a = list(map(int, input().split()))
	for i in a:
		l.append(bi(i))
	for i in range(n):
		if l[i] in d:
			d[l[i]] += 1
		else:
			d[l[i]] = 1
	(s, f) = (list(set(l)), 0)
	for i in s:
		j = d[i]
		f += j * (j - 1) // 2
	print(f)


from math import log2

def readGenerator():
	while True:
		tokens = input().split(' ')
		for t in tokens:
			yield t
reader = readGenerator()

def readWord():
	return next(reader)

def readInt():
	return int(next(reader))

def readFloat():
	return float(next(reader))

def readLine():
	return input()

def solve(n, a):
	b = list(map(lambda x: int(log2(x)), a))
	l = [0] * 33
	for i in range(n):
		l[b[i]] += 1
	k = 0
	for i in l:
		k += i * (i - 1) // 2
	return k
t = readInt()
for _ in range(0, t):
	n = readInt()
	a = [readInt() for _ in range(0, n)]
	print(solve(n, a))


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


t = int(input())
for test in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	dd = []
	res = 0
	for x in range(n):
		dd.append(0)
	for bit in range(31, -1, -1):
		cnt = 0
		for i in range(n):
			if 1 << bit & a[i] and dd[i] == 0:
				dd[i] = 1
				cnt = cnt + 1
		res = res + (cnt - 1) * cnt / 2
	print(int(res))


import math

def solve(n, a):
	store = [0 for _ in range(34)]
	for i in a:
		store[int(math.log(i, 2))] += 1
	ans = 0
	for i in store:
		ans += (i - 1) * i / 2
	return int(ans)
t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	print(solve(n, a))


from math import log2, ceil
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	hash = {}
	for i in arr:
		seg = int(log2(i)) + 1
		hash[seg] = hash.get(seg, 0) + 1
	ans = 0
	for i in hash:
		ans += hash[i] * (hash[i] - 1) >> 1
	print(ans)