Mr. Kitayuta has just bought an undirected graph consisting of *n* vertices and *m* edges. The vertices of the graph are numbered from 1 to *n*. Each edge, namely edge *i*, has a color *c*<sub class="lower-index">*i*</sub>, connecting vertex *a*<sub class="lower-index">*i*</sub> and *b*<sub class="lower-index">*i*</sub>.

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

In the *i*-th query, he gives you two integers — *u*<sub class="lower-index">*i*</sub> and *v*<sub class="lower-index">*i*</sub>.

Find the number of the colors that satisfy the following condition: the edges of that color connect vertex *u*<sub class="lower-index">*i*</sub> and vertex *v*<sub class="lower-index">*i*</sub> directly or indirectly.
The first line of the input contains space-separated two integers — *n* and *m* (2 ≤ *n* ≤ 100, 1 ≤ *m* ≤ 100), denoting the number of the vertices and the number of the edges, respectively.

The next *m* lines contain space-separated three integers — *a*<sub class="lower-index">*i*</sub>, *b*<sub class="lower-index">*i*</sub> (1 ≤ *a*<sub class="lower-index">*i*</sub> &lt; *b*<sub class="lower-index">*i*</sub> ≤ *n*) and *c*<sub class="lower-index">*i*</sub> (1 ≤ *c*<sub class="lower-index">*i*</sub> ≤ *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*, (*a*<sub class="lower-index">*i*</sub>, *b*<sub class="lower-index">*i*</sub>, *c*<sub class="lower-index">*i*</sub>) ≠ (*a*<sub class="lower-index">*j*</sub>, *b*<sub class="lower-index">*j*</sub>, *c*<sub class="lower-index">*j*</sub>).

The next line contains a integer — *q* (1 ≤ *q* ≤ 100), denoting the number of the queries.

Then follows *q* lines, containing space-separated two integers — *u*<sub class="lower-index">*i*</sub> and *v*<sub class="lower-index">*i*</sub> (1 ≤ *u*<sub class="lower-index">*i*</sub>, *v*<sub class="lower-index">*i*</sub> ≤ *n*). It is guaranteed that *u*<sub class="lower-index">*i*</sub> ≠ *v*<sub class="lower-index">*i*</sub>.
# _
#####################################################################################################################

def main():
    nVertices, nEdges = map(int, input().split())
    colorfulGraph = {vertex: [] for vertex in map(str, range(1, nVertices+1))}

    for i in range(nEdges):
        vertex1, vertex2, color = input().split()
        colorfulGraph[vertex1].append((vertex2, color))
        colorfulGraph[vertex2].append((vertex1, color))

    def nColorsThatConnect(vertex1, vertex2):
        nColors, colorsUsed = 0, set()
        for neighbor in colorfulGraph[vertex1]:
            if neighbor[1] not in colorsUsed and \
                    possibleColor(neighbor[1], {vertex1, neighbor[0]}, neighbor[0], vertex2):
                nColors += 1
                colorsUsed.add(neighbor[1])
        return nColors

    def possibleColor(color, examinedVertices, start, destination):
        if start == destination:
            return True
        for neighbor in colorfulGraph[start]:
            if neighbor[0] not in examinedVertices and neighbor[1] == color:
                examinedVertices.add(neighbor[0])
                if possibleColor(color, examinedVertices, neighbor[0], destination):
                    return True
        return False

    for j in range(int(input())):
        vertex1, vertex2 = input().split()
        print(nColorsThatConnect(vertex1, vertex2))


if __name__ == '__main__':
    # print(main())
    main()
