Mr. Kitayuta has just bought an undirected graph with n vertices and m edges. The vertices of the graph are numbered from 1 to n. Each edge, namely edge i, has a color ci, connecting vertex ai and bi.

Mr. Kitayuta wants you to process the following q queries.

In the i-th query, he gives you two integers - ui and vi.

Find the number of the colors that satisfy the following condition: the edges of that color connect vertex ui and vertex vi directly or indirectly.

Input

The first line of the input contains space-separated two integers - n and m(2 ≤ n ≤ 105, 1 ≤ m ≤ 105), denoting the number of the vertices and the number of the edges, respectively.

The next m lines contain space-separated three integers - ai, bi(1 ≤ ai < bi ≤ n) and ci(1 ≤ ci ≤ m). Note that there can be multiple edges between two vertices. However, there are no multiple edges of the same color between two vertices, that is, if i ≠ j, (ai, bi, ci) ≠ (aj, bj, cj).

The next line contains a integer- q(1 ≤ q ≤ 105), denoting the number of the queries.

Then follows q lines, containing space-separated two integers - ui and vi(1 ≤ ui, vi ≤ n). It is guaranteed that ui ≠ vi.

Output

For each query, print the answer in a separate line.

Examples

Input

4 5
1 2 1
1 2 2
2 3 1
2 3 3
2 4 3
3
1 2
3 4
1 4


Output

2
1
0


Input

5 7
1 5 1
2 5 1
3 5 1
4 5 1
1 2 2
2 3 2
3 4 2
5
1 5
5 1
2 5
1 5
1 4


Output

1
1
1
1
2

Note

Let's consider the first sample. 

<image> The figure above shows the first sample. 

  * Vertex 1 and vertex 2 are connected by color 1 and 2. 
  * Vertex 3 and vertex 4 are connected by color 3. 
  * Vertex 1 and vertex 4 are not connected by any single color. 
import java.util.Arrays;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;


public class Test
{    
    private static Set[][] graph = new HashSet[110][110];
    private static Set[][] result = new HashSet[110][110];
    
    // srm 230
    public static void main(String[] args)
    {
        init();
        Scanner scan = new Scanner(System.in);
        int i , n , m , q , a , b , c;
        n = scan.nextInt();
        m = scan.nextInt();
        for (i = 0;i < m;i ++)
        {            
            a = scan.nextInt();
            b = scan.nextInt();
            c = scan.nextInt();
            graph[a][b].add((Integer) c);
            graph[b][a].add((Integer) c);
        }
        // 进行处理
        solve(n);
        // 处理请求
        q = scan.nextInt();
        for (i = 0;i < q;i ++)
        {
            int u , v;
            u = scan.nextInt();
            v = scan.nextInt();
            System.out.println(result[u][v].size());
        }
    }
    
    private static boolean[] vis = new boolean[110];
    private static Set<Integer> colorSet = new HashSet<Integer>();
    
    private static void solve(int n)
    {
        for (int i = 1;i <= n;i ++)
        {
            colorSet.clear();
            Arrays.fill(vis , false);
            vis[i] = true;
            // 从from开始，当前位置是cur，共有n个结点
            dfs(i , i , n , true);
        }
    }
    
    private static void dfs(int from , int cur , int n , boolean first)
    {
        // 如果存在结果
        if (!colorSet.isEmpty())
            result[from][cur].addAll(colorSet);
        // 如果不存在结果
        else
        {
            if (!first)
                return;
        }
        
        for (int i = 1;i <= n;i ++)
        {
            // 如果可以到达i
            if (!vis[i] && !graph[cur][i].isEmpty())
            {   
                vis[i] = true;
                // 存储
                Set<Integer> tempSet = new HashSet<Integer>(colorSet);
                // 如果是第一次操作
                if (first)
                    colorSet.addAll(graph[cur][i]);
                else
                    colorSet.retainAll(graph[cur][i]);
                // 设置新的当前位置
                dfs(from , i , n , false);
                colorSet = tempSet;
                vis[i] = false;
            }
        }
    }
    
    private static void init()
    {
        int i , j;
        for (i = 0;i < 110;i ++)
            for (j = 0;j < 110;j ++)
            {    
                graph[i][j] = new HashSet();
                result[i][j] = new HashSet();
            }
        
    }
    
}








