Streets Of VSSUT

All submissions for this problem are available.
There are N departments and M roads in VSSUT, Burla. Each road has an ID associated with it. You are to process the following queries:
In the i^{th} query you are given two numbers u_{i} and v_{i} .
Find the number of distinct IDs that satisfy the following condition: the roads of that ID connect vertex u_{i} and v_{i} either directly or indirectly.
Input
The first line of the input contains two spaceseparated integers n and m (2 ≤ n ≤ 100, 1 ≤ m ≤ 100), denoting the number of the departments and the number of the roads, respectively.
The next m lines contain three spaceseparated integers a_{i}, b_{i} (1 ≤ a_{i} < b_{i} ≤ n) and c_{i} (1 ≤ c_{i} ≤ m), where a_{i} is the source node, b_{i} is the destination node and c_{i} is the ID. Note that there can be multiple roads between two departments. However, there are no multiple roads of the same id between two departments, that is, if i ≠ j, (a_{i}, b_{i}, c_{i}) ≠ (a_{j}, b_{j}, c_{j}).
The next line contains an integer q (1 ≤ q ≤ 100), denoting the number of the queries.
Then follows q lines, containing two spaceseparated integers u_{i} and v_{i} (1 ≤ u_{i}, v_{i} ≤ n). It is guaranteed that u_{i} ≠ v_{i}.
Output
For each query, print the answer in a separate line.
Examples
Input8 9
2 8 6
5 5 1
4 1 3
6 7 6
6 6 5
4 3 6
1 7 4
1 5 3
5 7 6
2
5 8
5 7
Output
0
1
Input
7 8
7 1 4
5 3 5
2 4 6
5 6 7
3 1 7
1 3 1
3 5 3
1 4 3
2
1 7
1 3
Output
1
2
Explanation
Case 1
Query 1: There is no path connecting 5 to 8 with all the edges of same ID. So, the answer is 0.
Query 2: There is a path connecting 5 to 7 with the ID 6. So, the answer is 1.
Author:  bigo_admin 
Editorial  https://discuss.codechef.com/problems/COLGQU 
Tags  bigo_admin, dfs, easy, unionfind 
Date Added:  11012017 
Time Limit:  1 sec 
Source Limit:  50000 Bytes 
Languages:  C, CPP14, JAVA, PYTH, PYTH 3.6, PYPY, CS2, PAS fpc, PAS gpc, RUBY, PHP, GO, NODEJS, HASK, SCALA, D, PERL, FORT, WSPC, ADA, CAML, ICK, BF, ASM, CLPS, PRLG, ICON, SCM qobi, PIKE, ST, NICE, LUA, BASH, NEM, LISP sbcl, LISP clisp, SCM guile, JS, ERL, TCL, PERL6, TEXT, SCM chicken, PYP3, CLOJ, FS 
Comments
 Please login at the top to post a comment.
SUCCESSFUL SUBMISSIONS
Fetching successful submissions
HELP
If you are still having problems, see a sample solution here. 