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. 
#!/usr/bin/env python
                
def shortest_dist_node(dist_so_far):
    min_k = 99999
    node = 0
    for k, v in dist_so_far.iteritems():
        if min_k > v:
            node = k
            min_k = v
    return node

def dijkstra(G, v, color, colors):
    #print 'Color',color
    dist_so_far = {}
    dist_so_far[v] = 0
    final_dist = {}
    while len(final_dist) < len(G) - 1:
        w = shortest_dist_node(dist_so_far)
        if w == 0:
            #print 'final', final_dist
            return final_dist 
        #print 'Next node', w
        # lock it down! 
        final_dist[w] = dist_so_far[w]
        del dist_so_far[w]
        for x in G[w]:
            if color not in colors[(w,x)]:
                continue
            #print ' Other', x, ' Color',colors[(w,x)]
            if x not in final_dist:
                if x not in dist_so_far:
                    dist_so_far[x] = final_dist[w] + 1 #G[w][x]
                elif final_dist[w] + 1 < dist_so_far[x]: #G[w][x] < dist_so_far[x]:
                    dist_so_far[x] = final_dist[w] + 1 #G[w][x]
    #print 'final', final_dist
    return final_dist

def main():
    n, m = map(int, raw_input().split())
    colors = {}
    adjacency = [ [] for _ in xrange(n+1)]

    for _ in xrange(m):
        start, end, color = map(int, raw_input().split())
        if (start, end) in colors:
            colors[(start, end)].append(color)
            colors[(end, start)].append(color)
        else:
            colors[(start, end)] = [color]
            colors[(end, start)] = [color]
            adjacency[start].append(end)
            adjacency[end].append(start)

    #print adjacency
    q = int(raw_input())
    for _ in xrange(q):
        start, end = map(int, raw_input().split())
        cont = 0
        colors_visited = []
        for node in adjacency[start]:
            for color in colors[(start, node)]:
                if color in colors_visited:
                    continue
                dists = dijkstra(adjacency, start, color, colors)
                if end in dists:
                    cont += 1
                    colors_visited.append(color)
        print cont
        


    pass

if __name__ == "__main__":
    main()

