Hint
What kind Data Structure should you use in-order to know how many times that a single word is written.
Tutorial
1722C - Word Game
To quickly check how many times a single word is written, you should store a dictionary and increment the count for each word every time you see it in the input. Then, you can iterate through each guy, find the number of times their word appears, and update their score based on that. For instance, if a word appears only once, it could get a score of +3, twice could get a +1, and otherwise no score is added.
the time complexity is $$$O(n)$$$ per test-case
Solution
t = int(input())
for _ in range(t):
n = int(input())
words = []
dict = {}
for _ in range(3):
word = list(map(str, input().split()))
for x in word:
dict[x] = dict.get(x, 0) + 1
words.append(word)
for i in range(3):
total = 0
for word in words[i]:
if dict[word] == 1:
total += 3
elif dict[word] == 2:
total += 1
# print each guys total
print(total, end=" ")
# print a new line
print()