Infinity War

Time limit: 0.8s Memory limit: 8MB Input: infinitywar.in Output: infinitywar.out

After the events that took place in New York, the NN worlds from the Marvel Universe are at war. The world i (1iN)i \ (1 ≤ i ≤ N) is represented, in this final war, by an army consisting of KiK_i soldiers (possibly zero). Each soldier has a single super power represented by a positive integer (between 11 and PP). The powers of all the soldiers in an army are different.

It has been observed that, in direct battle, two soldiers annihilate each other if and only if they have the same power. For example, if an army consisting of soldiers with powers {1,3,5}\{1, 3, 5\} battles an army {2,3,6}\{2, 3, 6\}, the following soldiers remain alive at the end of the battle: {1,2,5,6}\{1, 2, 5, 6\}.

The NN worlds are arranged sequentially. The first world has index 11 and the last world has index NN.

Task

Thanos is pretty sure that he can win the war and destroy the universe, but he also wants to have fun while doing it. He has prepared QQ questions for you. For each question you are given two indexes xx and yy and you have to find the number of soldiers that would survive a battle between the armies with indexes x,x+1,,yx, x + 1, \dots, y.

Input

The first line of the input file infinitywar.in contains two numbers NN and QQ.

The next NN lines contain the descriptions of the armies. Line i+1i+1 (where 1iN1 ≤ i ≤ N) contains a number KiK_i (number of soldiers) followed by KiK_i numbers (power number of each soldier).

The next QQ lines each contain two numbers xx and yy separated by a space.

Output

The output file infinitywar.out must contain QQ lines. Each line must contain a single number, the answer to the corresponding question.

Constraints and notes

  • 1N50 0001 \leq N \leq 50 \ 000
  • 1P10 0001 \leq P \leq 10 \ 000
  • 1Q100 0001 \leq Q \leq 100 \ 000
  • K1+K2++KN300 000K_1 + K_2 + \dots + K_N \leq 300 \ 000
  • 1xyN1 \leq x \leq y \leq N for every question.
  • For 30%30\% of tests: N10 000,P500N ≤ 10 \ 000, P ≤ 500 şi Q10 000Q ≤ 10 \ 000
  • For another 40%40\% of tests: P5 000P ≤ 5 \ 000 şi Q30 000Q ≤ 30 \ 000

Example

infinitywar.in

4 3
2 1 2
3 1 3 97
2 1 341
5 4 2 981 341 97
1 3
2 4
1 4

infinitywar.out

5
4
4

Log in or sign up to be able to send submissions!