We watch N 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 k cities A1,A2,…,Ak. The truck starts at city A1 and drives to city A2, then turns and drives to city A3, 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<…
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 N, the route of each truck, and M pairs of trucks, write a program that computes the number of encounters of each pair.
Every queried pair (ai,bi) satisfies both of these conditions.
These conditions hold only for the queried pairs, not for every pair of trucks.
The first line contains the number of trucks N and the number of queried pairs M (1≤N≤105, 1≤M≤105).
The i-th of the next N lines describes the route of the i-th truck. The first integer on the line is the number of cities on the route, Ki (2≤Ki≤3×105), followed by Ki city numbers Aj (1≤Aj≤109) in the order the truck visits them.
The sum of the route lengths over all trucks does not exceed 3×105.
Each of the next M lines contains two integers ai and bi, the numbers of the two trucks whose encounter count is asked for.
Output M lines. The i-th line contains the number of encounters of the i-th pair of trucks from the input.