eolymp
bolt
Try our new interface for solving problems
Problems

Vasya and sets

Vasya and sets

Vasya likes to formalize everything. For example, Vasya sees a lot of vegetables in his grandmother's garden, and a lot of toys in his younger brother. And what will happen if you try to combine or to intersect n sets? Large and different sets - up to a million elements (inclusive)!

Since Vasya dreams to become a mathematician, he decided to start his research with the study of simpler sets, namely sets of integers.

Input

First line contains the number of different sets n (1n20). Then n sets given in the next format:

  • First line contains number t (1t106) - the number of integers in the next line.
  • Second line contains t integers xi (1xi106) - the elements of the set.

Next line contains the number of queries m (1m100). Each of the next m lines contains the queries of two types:

  • INTERSECTION a b, where 1a, bn
  • UNION a b, where 1a, bn

Output

Print for each query:

  1. For the query of the first type the number of elements in the intersection of sets a and b.
  2. For the query of the second type the number of elements in the union of sets a and b.
Time limit 2 seconds
Memory limit 128 MiB
Input example #1
2
2
1 2
2
2 3
2
INTERSECTION 1 2
UNION 1 2
Output example #1
1
3
Author Михаил Римель
Source Distance Summer Computer School - Summer 2013