8690: Colourful Journey

内存限制:128 MB 时间限制:1 S 标准输入输出
题目类型:传统 评测方式:文本比较 上传者:
提交:6 通过:0

题目描述


Recently an epoch-making smart electric car developed by NIO company goes on sale. To celebrate, the chief test driver plans to drive the new car in Byteland for qqq days.

Byteland has nnn cities numbered from 111 to nnn, and nnn bi-directional roads connecting them. For each pair of cities, the driver can always arrive one from another one through these roads. There is a garage selling car paints of some colours on each road. Every time when the driver passes through a road, he will choose one colour of car paints from the garage and repaint the car if that colour is different from the car's current colour.

In each of the next qqq days, before the day's journey starts, the driver will paint the car with any one colour even if car paints of that colour are not on sale in any garages in Byteland. Then he will drive from the aaa-th city to the bbb-th city without visiting any cities more than once.

To make the car more colourful to attract everyone's attention, the driver wonders the maximum times that he repaints the car in each day.

输入格式


The first line contains two integers nnn (2≤n≤200000)(2 \le n \le 200\,000)(2n200000) and qqq (1≤q≤200000)(1 \le q \le 200\,000)(1q200000), indicating the number of cities in Byteland and the number of days of the chief test driver's journey in Byteland.
In each of the next nnn lines, three integers uuu, vvv (1≤u,v≤n,u≠v)(1 \le u, v \le n, u \neq v)(1u,vn,u=v) and kkk (≥1)(\ge 1)(1) comes first, indicating that the uuu-th city and the vvv-th city is connected by a road, where the garage sells car paints of kkk colours. Then kkk distinct integers c1,c2,...,ckc_1, c_2, ..., c_kc1,c2,...,ck (1≤ci≤500000)(1 \le c_i \le 500\,000)(1ci500000) follows, indicating the colours of car paints sold by the garage. It is guaranteed that the sum of kkk does not exceed 500000500\,000500000.
Each of the next qqq lines contains two integers aaa and bbb (1≤a,b≤n,a≠b)(1 \le a, b \le n, a \neq b)(1a,bn,a=b), indicating a journey from the aaa-th city to the bbb-th city.

输出格式


Output qqq lines, each of which contains a single integer, indicating the maximum times of repainting the car during the journey from the aaa-th city to the bbb-th city.

输入样例 复制

5 5
1 2 4 4 5 9 2
1 4 1 4
2 3 1 2
4 5 1 2
5 3 5 2 9 6 5 4
5 2
2 3
2 4
1 3
1 5

输出样例 复制

3
4
3
3
3

分类标签