Truck Encounters

No attempts yetTime limit3sMemory limit64 MB

Problem

We watch NN trucks move along a road. The road is a number line, and every integer point on it holds one city. A city is named by the coordinate of the point it sits on.

All trucks move at the same speed, and no truck stands still at any moment. A truck covers the distance between two adjacent cities in 1 minute.

The route of every truck is given. All trucks start their routes at the same moment.

A route is an array of kk cities A1,A2,,AkA_1, A_2, \dots, A_k. The truck starts at city A1A_1 and drives to city A2A_2, then turns and drives to city A3A_3, and so on to the end of the route. Because the truck turns every time, one of the following holds.

A1<A2>A3<A4>orA1>A2<A3>A4<A_1 < A_2 > A_3 < A_4 > \dots \qquad \text{or} \qquad A_1 > A_2 < A_3 > A_4 < \dots

Turning takes no time.

For example, take the route 2, 5, 1, 7. The truck is at city 2 at the start and reaches city 5 three minutes after departure. It turns there and drives to city 1, where it arrives 7 minutes after departure. It turns again and drives to city 7, reaching it at minute 13.

A truck that has driven its whole route is taken away by aliens in their space rocket, so it disappears from the road.

For some pairs of trucks we want to know how many times the two trucks met on the road, that is, how many times they were at the same position. The position where they meet does not have to be an integer. They may meet at position 2.5.

Given the number of trucks NN, the route of each truck, and MM pairs of trucks, write a program that computes the number of encounters of each pair.

Every queried pair (ai,bi)(a_i, b_i) satisfies both of these conditions.

  • The two trucks are not at the same position at the moment when one of them (or both) disappears from the road.
  • The two trucks are not at the same position at the starting moment, nor at a moment when one of them (or both) turns.

These conditions hold only for the queried pairs, not for every pair of trucks.

Input

The first line contains the number of trucks NN and the number of queried pairs MM (1N1051 \le N \le 10^5, 1M1051 \le M \le 10^5).

The ii-th of the next NN lines describes the route of the ii-th truck. The first integer on the line is the number of cities on the route, KiK_i (2Ki3×1052 \le K_i \le 3 \times 10^5), followed by KiK_i city numbers AjA_j (1Aj1091 \le A_j \le 10^9) in the order the truck visits them.

The sum of the route lengths over all trucks does not exceed 3×1053 \times 10^5.

Each of the next MM lines contains two integers aia_i and bib_i, the numbers of the two trucks whose encounter count is asked for.

Output

Output MM lines. The ii-th line contains the number of encounters of the ii-th pair of trucks from the input.