You are given arrays A and B of length N each. Determine the number of *good* pairs.

A pair (i,j) (1≤ i,j ≤ N) is said to be *good* if all of the following conditions are satisfied:
i < j;
A_{i} = B_{j};
A_{j} = B_{i};

------ Input Format ------ 

- First line will contain T, the number of test cases. Then the test cases follow.

Description of each test case:
- First line contains an integer N - denoting the length of the arrays.
- Second line contains N space-separated integers A_{1}, A_{2}, \dots A_{N} - denoting the array A.
- Third line contains N space-separated integers B_{1}, B_{2}, \dots B_{N} - denoting the array B.

------ Output Format ------ 

For each test case, output the number of *good* pairs.

------ Constraints ------ 

$1 ≤ T ≤ 1000$
$2 ≤ N ≤ 10^{5}$
$1 ≤ A_{i}, B_{i} ≤ 10^{9}$
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.

----- Sample Input 1 ------ 
4
3
1 2 3
3 2 1
4
1 2 3 4
5 6 6 7
3
10 10 10
10 10 10
4
4 8 1 1
8 4 1 1

----- Sample Output 1 ------ 
1
0
3
2

----- explanation 1 ------ 
Test Case $1$: The only *good* pair $(i,j)$ is $(1,3)$. Here,
- $1 < 3$
- $A_{1} = 1$ and $B_{3} = 1$. Thus, $A_{1} = B_{3}$.
- $A_{3} = 3$ and $B_{1} = 3$. Thus, $A_{3} = B_{1}$.

Test Case $2$: There are no *good* pairs.

Test Case $3$: There are $3$ good pairs $(i,j)$. These are $(1,2), (1,3),$ and $(2,3)$.

Test Case $4$: There are $2$ good pairs $(i,j)$. These are $(1,2)$ and $(3,4)$.

Difficulty: MEDIUM
Tags: Data structures
Skill types: Data structures

Starter code:

Solutions:
for _ in range(int(input())):
	n = int(input())
	A = list(map(int, input().split()))
	B = list(map(int, input().split()))
	count = 0
	d = {}
	for i in range(n):
		if (B[i], A[i]) in d:
			count += d[B[i], A[i]]
		if (A[i], B[i]) in d:
			d[A[i], B[i]] += 1
		else:
			d[A[i], B[i]] = 1
	print(count)


for i in range(int(input())):
	terms = int(input())
	l1 = list(map(int, input().split()))
	l2 = list(map(int, input().split()))
	dicc = {}
	rt = 0
	for i in range(terms):
		if (l2[i], l1[i]) in dicc:
			rt = rt + dicc[l2[i], l1[i]]
		if (l1[i], l2[i]) in dicc:
			dicc[l1[i], l2[i]] += 1
		else:
			dicc[l1[i], l2[i]] = 1
	print(rt)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	count = 0
	d = {}
	for i in range(n):
		if (b[i], a[i]) in d:
			count += d[b[i], a[i]]
		if (a[i], b[i]) in d:
			d[a[i], b[i]] += 1
		else:
			d[a[i], b[i]] = 1
	print(count)


s = int(input())
for j in range(1, s + 1):
	n = int(input())
	a = list(map(int, input().split()))
	v = list(map(int, input().split()))
	d = {}
	c = 0
	for i in range(n):
		if (v[i], a[i]) in d:
			c += d[v[i], a[i]]
		if (a[i], v[i]) in d:
			d[a[i], v[i]] += 1
		else:
			d[a[i], v[i]] = 1
	print(c)


for _ in range(int(input())):
	n = int(input())
	lst1 = list(map(int, input().split()))
	lst2 = list(map(int, input().split()))
	dictionary = dict()
	count = 0
	for i in range(n):
		if (lst2[i], lst1[i]) in dictionary:
			count += dictionary[lst2[i], lst1[i]]
		if (lst1[i], lst2[i]) in dictionary:
			dictionary[lst1[i], lst2[i]] += 1
		else:
			dictionary[lst1[i], lst2[i]] = 1
	print(count)


for _ in range(int(input())):
	n = int(input())
	a = input().split()
	b = input().split()
	li = list(zip(a, b))
	cnt = 0
	di = {}
	same = 0
	for i in li:
		if i not in di:
			di[i] = 1
		else:
			di[i] += 1
	for (k, v) in di.items():
		if k[::-1] in di:
			if k == k[::-1]:
				same += -((v - 1) * di[k[::-1]] // -2)
			else:
				cnt += v * di[k[::-1]]
	print(cnt // 2 + same)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	d = {}
	c = 0
	for j in range(n):
		if (b[j], a[j]) in d:
			c += d[b[j], a[j]]
		if (a[j], b[j]) in d:
			d[a[j], b[j]] += 1
		else:
			d[a[j], b[j]] = 1
	print(c)


tests = int(input())
for i in range(tests):
	length = int(input())
	A = list(map(int, input().split()))
	B = list(map(int, input().split()))
	ans = 0
	pairs_dict = {}
	for j in range(length):
		if (B[j], A[j]) in pairs_dict:
			ans += pairs_dict[B[j], A[j]]
		if (A[j], B[j]) in pairs_dict:
			pairs_dict[A[j], B[j]] += 1
		else:
			pairs_dict[A[j], B[j]] = 1
	print(ans)


for _ in range(int(input())):
	n = int(input())
	A = [int(x) for x in input().split(' ')]
	B = [int(x) for x in input().split(' ')]
	d1 = {}
	good_pairs = 0
	for i in range(n):
		if (B[i], A[i]) in d1:
			good_pairs += d1[B[i], A[i]]
		if (A[i], B[i]) in d1:
			d1[A[i], B[i]] += 1
		else:
			d1[A[i], B[i]] = 1
	print(good_pairs)


t = int(input())
for i in range(t):
	n = int(input())
	x = list(map(int, input().split()))
	y = list(map(int, input().split()))
	count = 0
	d = {}
	for i in range(n):
		if (y[i], x[i]) in d:
			count = count + d[y[i], x[i]]
		if (x[i], y[i]) in d:
			d[x[i], y[i]] += 1
		else:
			d[x[i], y[i]] = 1
	print(count)


t = int(input())
for k in range(1, t + 1):
	N = int(input())
	a = list(map(int, input().split(' ')))
	b = list(map(int, input().split(' ')))
	d = {}
	c = 0
	for i in range(N):
		if (a[i], b[i]) in d:
			c += d[a[i], b[i]]
		if (b[i], a[i]) in d:
			d[b[i], a[i]] += 1
		else:
			d[b[i], a[i]] = 1
	print(c)


t = int(input())
while t > 0:
	count = 0
	n = int(input())
	a = [int(i) for i in input().split()]
	b = [int(i) for i in input().split()]
	d = dict()
	for i in range(n):
		if (b[i], a[i]) in d:
			count += d[b[i], a[i]]
		if (a[i], b[i]) in d:
			d[a[i], b[i]] += 1
		else:
			d[a[i], b[i]] = 1
	print(count)
	t -= 1


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	m = list(map(int, input().split()))
	d = {}
	c = 0
	for i in range(n):
		if (m[i], l[i]) in d:
			c += d[m[i], l[i]]
		if (l[i], m[i]) in d:
			d[l[i], m[i]] += 1
		else:
			d[l[i], m[i]] = 1
	print(c)


tests = int(input())
for i in range(tests):
	n = int(input())
	A = input().split()
	B = input().split()
	hashmap = {}
	count = 0
	for i in range(n):
		if (B[i], A[i]) in hashmap:
			count += hashmap[B[i], A[i]]
		if (A[i], B[i]) in hashmap:
			hashmap[A[i], B[i]] += 1
		else:
			hashmap[A[i], B[i]] = 1
	print(count)


def pair(n, a, b):
	d = {}
	ans = 0
	for i in range(n):
		if (b[i], a[i]) in d:
			ans += d[b[i], a[i]]
		if (a[i], b[i]) in d:
			d[a[i], b[i]] += 1
		else:
			d[a[i], b[i]] = 1
	return ans
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	print(pair(n, a, b))


def pair(n, a, b):
	dicc = {}
	rt = 0
	for i in range(n):
		if (b[i], a[i]) in dicc:
			rt = rt + dicc[b[i], a[i]]
		if (a[i], b[i]) in dicc:
			dicc[a[i], b[i]] += 1
		else:
			dicc[a[i], b[i]] = 1
	return rt
T = int(input())
for a in range(T):
	N = int(input())
	A = list(map(int, input().split()[:N]))
	B = list(map(int, input().split()[:N]))
	nm = pair(N, A, B)
	print(nm)


def goodpairs():
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	d = {(a[i], b[i]): 0 for i in range(n)}
	result = 0
	for i in range(n):
		if (b[i], a[i]) in d:
			result += d[b[i], a[i]]
		d[a[i], b[i]] += 1
	print(result)
for _ in range(int(input())):
	goodpairs()


T = int(input())
for i in range(T):
	N = int(input())
	A = [int(j) for j in input().split()]
	B = [int(j) for j in input().split()]
	f = 0
	d = dict()
	for i in range(N):
		d[A[i], B[i]] = 0
	for i in range(N):
		if (B[i], A[i]) in d:
			f = f + d[B[i], A[i]]
		d[A[i], B[i]] += 1
	print(f)


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	b = [int(x) for x in input().split()]
	gp = dict()
	for i in range(n):
		if (a[i], b[i]) in gp:
			gp[a[i], b[i]] += 1
		else:
			gp[a[i], b[i]] = 1
	res = 0
	for i in range(n):
		gp[a[i], b[i]] -= 1
		if (b[i], a[i]) in gp:
			res += gp[b[i], a[i]]
	print(res)


t = int(input())
for _ in range(t):
	n = int(input())
	l1 = [int(x) for x in input().split()]
	l2 = [int(x) for x in input().split()]
	d = dict()
	for j in range(n):
		if (l1[j], l2[j]) in d:
			d[l1[j], l2[j]] += 1
		else:
			d[l1[j], l2[j]] = 1
	ans = 0
	for j in range(n):
		d[l1[j], l2[j]] -= 1
		if (l2[j], l1[j]) in d:
			ans += d[l2[j], l1[j]]
	print(ans)


def pair(n, a, b):
	c = 0
	d = {}
	ans = 0
	for i in range(n):
		if (b[i], a[i]) in d:
			ans += d[b[i], a[i]]
		if (a[i], b[i]) in d:
			d[a[i], b[i]] += 1
		else:
			d[a[i], b[i]] = 1
	return ans
for k in range(int(input())):
	n = int(input())
	a = input().split()
	b = input().split()
	print(pair(n, a, b))


for z in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	b = [int(x) for x in input().split()]
	i = c = 0
	r = {}
	while i < n:
		if (b[i], a[i]) in r:
			c += r[b[i], a[i]]
		if (a[i], b[i]) not in r:
			r[a[i], b[i]] = 1
		else:
			r[a[i], b[i]] += 1
		i += 1
	print(c)


case = int(input())
for i in range(case):
	n = int(input())
	arrone = list(map(int, input().split(' ')))
	arrtwo = list(map(int, input().split(' ')))
	count = 0
	hashmap = {}
	for i in range(n):
		if (arrone[i], arrtwo[i]) not in hashmap:
			hashmap[arrone[i], arrtwo[i]] = 1
		else:
			hashmap[arrone[i], arrtwo[i]] += 1
	ans = 0
	for keys in hashmap.copy():
		(a, b) = keys
		if (a, b) in hashmap:
			if (b, a) in hashmap:
				if a != b:
					ans += hashmap.pop((a, b)) * hashmap.pop((b, a))
				else:
					N = hashmap[a, b] - 1
					ans += N * (N + 1) // 2
	print(ans)


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


for _ in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	b = [int(i) for i in input().split()]
	count = 0
	dic = {}
	for i in range(n):
		if (b[i], a[i]) in dic:
			count += dic[b[i], a[i]]
			if (a[i], b[i]) in dic:
				dic[a[i], b[i]] += 1
			else:
				dic[a[i], b[i]] = 1
		elif (a[i], b[i]) in dic:
			dic[a[i], b[i]] += 1
		else:
			dic[a[i], b[i]] = 1
	print(count)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	sample = {}
	ans = 0
	for i in range(n):
		if (b[i], a[i]) in sample:
			ans += sample[b[i], a[i]]
			if (a[i], b[i]) in sample:
				sample[a[i], b[i]] += 1
			else:
				sample[a[i], b[i]] = 1
		elif (a[i], b[i]) in sample:
			sample[a[i], b[i]] += 1
		else:
			sample[a[i], b[i]] = 1
	print(ans)


for _ in range(int(input())):
	n = int(input())
	lst1 = list(map(int, input().split()))
	lst2 = list(map(int, input().split()))
	d = {}
	for i in range(n):
		if (lst1[i], lst2[i]) not in d:
			d[lst1[i], lst2[i]] = 1
		else:
			d[lst1[i], lst2[i]] += 1
	c = 0
	for i in range(n):
		d[lst1[i], lst2[i]] -= 1
		if d[lst1[i], lst2[i]] == 0:
			del d[lst1[i], lst2[i]]
		if (lst2[i], lst1[i]) in d:
			c += d[lst2[i], lst1[i]]
		else:
			continue
	print(c)


import sys
for i in range(int(sys.stdin.readline())):
	n = int(sys.stdin.readline())
	a = list(map(int, sys.stdin.readline().split()))
	b = list(map(int, sys.stdin.readline().split()))
	d = {}
	l = []
	for i in range(n):
		t = (a[i], b[i])
		d[t] = 0
	ans = 0
	for i in range(n):
		t = (a[i], b[i])
		ans += d[t]
		t = t[::-1]
		if t in d:
			d[t] += 1
	sys.stdout.write(str(ans) + '\n')


for _ in range(int(input())):
	n = int(input())
	P = list(map(int, input().split()))
	Q = list(map(int, input().split()))
	D = {}
	for i in range(n):
		if (P[i], Q[i]) not in D:
			D[P[i], Q[i]] = 1
		else:
			D[P[i], Q[i]] += 1
	r = 0
	for i in range(n):
		D[P[i], Q[i]] -= 1
		if (Q[i], P[i]) in D:
			r = r + D[Q[i], P[i]]
	print(r)


for i in range(int(input())):
	le = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	dic = {}
	for i in range(le):
		if (a[i], b[i]) in dic:
			dic[a[i], b[i]] += 1
		else:
			dic[a[i], b[i]] = 1
	ans = 0
	for i in range(le):
		dic[a[i], b[i]] -= 1
		if (b[i], a[i]) in dic.keys():
			ans += dic[b[i], a[i]]
	print(ans)


T = int(input())
for i in range(T):
	N = int(input())
	A = list(map(int, input().split()))
	B = list(map(int, input().split()))
	res = {}
	for i in range(N):
		if (A[i], B[i]) in res:
			res[A[i], B[i]] += 1
		else:
			res[A[i], B[i]] = 1
	ans = 0
	for i in range(N):
		res[A[i], B[i]] -= 1
		if (B[i], A[i]) in res.keys():
			ans += res[B[i], A[i]]
	print(ans)


for i in range(int(input())):
	dic = {}
	n = int(input())
	A = list(map(int, input().split()))
	B = list(map(int, input().split()))
	for i in range(n):
		if (A[i], B[i]) not in dic:
			dic[A[i], B[i]] = 1
		else:
			dic[A[i], B[i]] += 1
	res = 0
	for i in range(n):
		dic[A[i], B[i]] -= 1
		if (B[i], A[i]) in dic:
			res += dic[B[i], A[i]]
	print(res)


t = int(input())
for x in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	d = {}
	for i in range(n):
		if (a[i], b[i]) not in d:
			d[a[i], b[i]] = 1
		else:
			d[a[i], b[i]] += 1
	r = 0
	for i in range(n):
		d[a[i], b[i]] -= 1
		if (b[i], a[i]) in d.keys():
			r += d[b[i], a[i]]
	print(r)


from collections import Counter
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	mp = Counter()
	for i in range(n):
		if (a[i], b[i]) not in mp:
			mp[a[i], b[i]] = 1
		else:
			mp[a[i], b[i]] += 1
	ans = 0
	keys = list(mp.keys())
	for i in keys:
		(l, r) = i
		if l == r:
			ans += mp[i] * (mp[i] - 1) // 2
		else:
			ans += mp[l, r] * mp[r, l]
		mp[l, r] = 0
		mp[r, l] = 0
	print(ans)


from collections import Counter
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	hp = Counter()
	c = 0
	for i in range(n):
		hp[a[i], b[i]] += 1
	keys = list(hp.keys())
	for q in keys:
		(i, j) = q
		l = hp[i, j]
		r = hp[j, i]
		if i == j:
			c += l * (l - 1) // 2
		else:
			c += hp[i, j] * hp[j, i]
		hp[i, j] == 0
		hp[j, i] = 0
	print(c)


from collections import Counter
t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	dic = Counter()
	for j in range(n):
		dic[a[j], b[j]] += 1
	ans = 0
	for j in range(n):
		dic[a[j], b[j]] -= 1
		ans += dic[b[j], a[j]]
	print(ans)


from collections import Counter
for _ in range(int(input())):
	n = int(input())
	ar = list(map(int, input().split()))[:n]
	br = list(map(int, input().split()))[:n]
	mp = Counter()
	for i in range(n):
		mp[ar[i], br[i]] += 1
	ans = 0
	keys = list(mp.keys())
	for x in keys:
		(i, j) = x
		(l, r) = (mp[i, j], mp[j, i])
		if i == j:
			ans += l * (l - 1) // 2
		else:
			ans += mp[i, j] * mp[j, i]
		mp[i, j] = 0
		mp[j, i] = 0
	print(ans)


for i in range(int(input())):
	n = int(input())
	l1 = list(map(int, input().split()))
	l2 = list(map(int, input().split()))
	l3 = []
	d = {}
	for j in range(n):
		l3.append((l1[j], l2[j]))
	d = {}
	for j in l3:
		d[j] = 0
	c = 0
	for j in l3:
		c += d[j]
		t = tuple(reversed(j))
		if t in d:
			d[tuple(reversed(j))] += 1
	print(c)


for _ in range(int(input())):
	n = int(input())
	A = list(map(int, input().split()))
	B = list(map(int, input().split()))
	d = {}
	for i in range(n):
		p = (A[i], B[i])
		if p not in d:
			d[p] = 1
		else:
			d[p] += 1
	ans = 0
	for i in range(n):
		d[A[i], B[i]] -= 1
		if (B[i], A[i]) in d:
			ans += d[B[i], A[i]]
	print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	A = list(map(int, input().split()))
	B = list(map(int, input().split()))
	m = {}
	for j in range(n):
		k = (A[j], B[j])
		if k not in m:
			m[k] = 1
		else:
			m[k] += 1
	a = 0
	for l in range(n):
		m[A[l], B[l]] -= 1
		if (B[l], A[l]) in m:
			a += m[B[l], A[l]]
	print(a)


tc = int(input())
for i in range(tc):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	d = {}
	for i in range(n):
		if (a[i], b[i]) not in d:
			d[a[i], b[i]] = 1
		else:
			d[a[i], b[i]] += 1
	ans = 0
	for i in range(n):
		d[a[i], b[i]] -= 1
		if (b[i], a[i]) in d:
			ans = ans + d[b[i], a[i]]
	print(ans)


n = int(input())
for _ in range(n):
	m = int(input())
	list1 = [int(i) for i in input().split()][:m]
	list2 = [int(i) for i in input().split()][:m]
	dic = {}
	ans = 0
	for i in range(m):
		if (list1[i], list2[i]) in dic:
			ans += dic[list1[i], list2[i]]
		if (list2[i], list1[i]) not in dic:
			dic[list2[i], list1[i]] = 1
		else:
			dic[list2[i], list1[i]] += 1
	print(ans)


for T in range(int(input())):
	N = int(input())
	A = tuple(input().split())
	B = tuple(input().split())
	Dict1 = {}
	counter = 0
	for i in range(N):
		counter += Dict1.get((B[i], A[i]), 0)
		Dict1[A[i], B[i]] = Dict1.get((A[i], B[i]), 0) + 1
	print(counter)


t = int(input())
for k in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	myd = dict()
	ans = 0
	for i in range(n):
		myd[a[i], b[i]] = 0
		myd[b[i], a[i]] = 0
	for i in range(n):
		ans += myd[a[i], b[i]]
		myd[b[i], a[i]] += 1
	print(ans)


from collections import Counter
test = int(input())
while test:
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	hashmap = Counter()
	for i in range(n):
		hashmap[a[i], b[i]] += 1
	ans = 0
	for i in list(hashmap.keys()):
		(l, r) = i
		var = hashmap[l, r]
		var1 = hashmap[r, l]
		if l == r:
			ans += var * (var - 1) // 2
		else:
			ans += var * var1
		hashmap[l, r] = 0
		hashmap[r, l] = 0
	print(ans)
	test -= 1


for _ in range(int(input())):
	N = int(input())
	A = list(map(int, input().split()))
	B = list(map(int, input().split()))
	my_dict = {}
	counter_a = counter_b = 0
	for i in range(N):
		x = (A[i], B[i])
		if x in my_dict.keys():
			my_dict[x] += 1
		else:
			my_dict[x] = 1
	for (key, val) in my_dict.items():
		(l, r) = (key[0], key[1])
		if (r, l) in my_dict.keys():
			if l != r:
				counter_a += my_dict[l, r] * my_dict[r, l]
			else:
				counter_b += my_dict[l, r] * (my_dict[l, r] - 1) // 2
	print(counter_a // 2 + counter_b)


t = int(input())
for _ in range(t):
	n = int(input())
	arr1 = list(map(int, input().split(' ')))
	arr2 = list(map(int, input().split(' ')))
	dict_count = {}
	for idx in range(n):
		key = hash((arr1[idx], arr2[idx]))
		if key in dict_count:
			dict_count[key] += 1
		else:
			dict_count[key] = 1
	res = 0
	for idx in range(n):
		temp = hash((arr2[idx], arr1[idx]))
		curr = hash((arr1[idx], arr2[idx]))
		if temp in dict_count and dict_count[temp] > 0:
			res += dict_count[temp]
			if arr1[idx] == arr2[idx]:
				res = res - 1
		dict_count[curr] -= 1
	print(res)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	dict1 = {}
	count = 0
	for i in range(n):
		count += dict1.get((b[i], a[i]), 0)
		dict1[a[i], b[i]] = dict1.get((a[i], b[i]), 0) + 1
	print(count)


from collections import Counter
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	hashmap = Counter()
	for i in range(n):
		hashmap[a[i], b[i]] += 1
	ans = 0
	keys = list(hashmap.keys())
	for k in keys:
		(i, j) = k
		p = hashmap[i, j]
		q = hashmap[j, i]
		if i == j:
			ans += p * (p - 1) / 2
		else:
			ans += hashmap[i, j] * hashmap[j, i]
		hashmap[i, j] = 0
		hashmap[j, i] = 0
	print(int(ans))


from collections import Counter
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	hashmap = Counter()
	for i in range(n):
		hashmap[a[i], b[i]] += 1
	ans = 0
	keys = list(hashmap.keys())
	for x in keys:
		(i, j) = x
		l = hashmap[i, j]
		r = hashmap[j, i]
		if i == j:
			ans += l * (l - 1) // 2
		else:
			ans += hashmap[i, j] * hashmap[j, i]
		hashmap[i, j] = 0
		hashmap[j, i] = 0
	print(ans)


t = int(input())
while t:
	n = int(input())
	a = input().split()
	b = input().split()
	dump = {}
	count = 0
	for i in range(n):
		count += dump.get((b[i], a[i]), 0)
		dump[a[i], b[i]] = dump.get((a[i], b[i]), 0) + 1
	print(count)
	t -= 1


def countPairs(a, b, n):
	r = 0
	d1 = {}
	ans = 0
	for i in range(n):
		d1[a[i], b[i]] = 0
		d1[b[i], a[i]] = 0
	for i in range(n):
		r += d1[a[i], b[i]]
		d1[b[i], a[i]] += 1
	print(r)
t = int(input())
for i in range(t):
	n = int(input())
	arr1 = list(map(int, input().split(' ')))
	arr2 = list(map(int, input().split(' ')))
	countPairs(arr1, arr2, n)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	s = set()
	for i in range(n):
		s.add((a[i], b[i]))
	d = {}
	for x in s:
		d[x] = 0
	ans = 0
	for i in range(n):
		ans += d[a[i], b[i]]
		try:
			d[b[i], a[i]] += 1
		except:
			continue
	print(ans)


for t in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	r = 0
	d = {}
	ans = 0
	for i in range(n):
		d[a[i], b[i]] = 0
		d[b[i], a[i]] = 0
	for i in range(n):
		r += d[a[i], b[i]]
		d[b[i], a[i]] += 1
	print(r)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	c = 0
	d = {}
	ans = 0
	for i in range(n):
		d[a[i], b[i]] = 0
		d[b[i], a[i]] = 0
	for i in range(n):
		c += d[a[i], b[i]]
		d[b[i], a[i]] += 1
	print(c)


from collections import Counter
for _ in range(int(input())):
	n = int(input())
	arr1 = list(map(int, input().split()))
	arr2 = list(map(int, input().split()))
	hashmap = Counter()
	for i in range(n):
		hashmap[arr1[i], arr2[i]] += 1
	ans = 0
	keys = list(hashmap.keys())
	for i in keys:
		(x, y) = i
		l = hashmap[x, y]
		r = hashmap[y, x]
		if x == y:
			ans += l * (l - 1) // 2
		else:
			ans += hashmap[x, y] * hashmap[y, x]
		hashmap[x, y] = 0
		hashmap[y, x] = 0
	print(ans)


from pprint import pprint
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	d = {}
	ans = 0
	for i in range(n):
		d[a[i], b[i]] = 0
		d[b[i], a[i]] = 0
	for i in range(n):
		ans += d[a[i], b[i]]
		d[b[i], a[i]] += 1
	print(ans)


for i in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	b = [int(x) for x in input().split()]
	dic = {}
	for (i, j) in zip(a, b):
		if (i, j) in dic:
			dic[i, j] += 1
		else:
			dic[i, j] = 1
	count = 0
	for i in range(0, n - 1):
		if dic[a[i], b[i]] > 1:
			dic[a[i], b[i]] -= 1
		else:
			dic[a[i], b[i]] = 0
		if (b[i], a[i]) in dic:
			count += dic[b[i], a[i]]
	print(count)