#C4950. Maximum Unique Words in Phrases

    ID: 48545 Type: Default 1000ms 256MiB

Maximum Unique Words in Phrases

Maximum Unique Words in Phrases

You are given a number of test cases. In each test case, you are provided with several phrases. Your task is to determine the total number of unique words that can be formed by combining all phrases in each test case. A word is defined as any sequence of characters separated by spaces.

For instance, consider the phrases "hello world", "world is beautiful", and "beautiful day". The unique words are: hello, world, is, beautiful, and day, yielding a total count of 5 unique words.

The mathematical representation is given by: $$\text{Result} = \left|\bigcup_{i=1}^{n} \{\text{words in phrase}_i\}\right|$$

inputFormat

The input is read from stdin and is formatted as follows:

  • The first line contains an integer T, the number of test cases.
  • For each test case, the first line contains an integer N, the number of phrases.
  • This is followed by N lines, where each line is a single phrase.

For example:

5
3
hello world
world is beautiful
beautiful day
2
good morning
good night
0
4
a
b
c
d
4
a
a
a
a

outputFormat

For each test case, print a single integer on a new line representing the number of unique words contained in all the phrases combined.

For the sample input above, the output would be:

5
3
0
4
1
## sample
5
3
hello world
world is beautiful
beautiful day
2
good morning
good night
0
4
a
b
c
d
4
a
a
a
a
5

3 0 4 1

</p>