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.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.ArrayList;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author El-Bishoy
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        OutputWriter out = new OutputWriter(outputStream);
        Div2B505 solver = new Div2B505();
        solver.solve(1, in, out);
        out.close();
    }

    static class Div2B505 {
        ArrayList<Edge>[] edgeList;
        int[][] edgeColor;
        boolean[] visited;

        public void solve(int testNumber, InputReader in, OutputWriter out) {
            int n = in.nextInt(), m = in.nextInt();
            edgeList = new ArrayList[n];
            visited = new boolean[n];
            for (int i = 0; i < n; i++) {
                edgeList[i] = new ArrayList<>();
            }
            edgeColor = new int[n][n];
            while (m-- > 0) {
                int v = in.nextInt() - 1, w = in.nextInt() - 1, color = in.nextInt();
                edgeList[v].add(new Edge(v, w, color));
                edgeList[w].add(new Edge(w, v, color));
            }

            int q = in.nextInt();

            int[][][] checkColors;
            while (q-- > 0) {
                int s = in.nextInt() - 1, t = in.nextInt() - 1;
                checkColors = new int[n][n][101];
                int cnt = 0;
                for (Edge e : edgeList[s]) {
                    visited = new boolean[n];
                    visited[s] = true;
                    if (checkColors[s][t][e.color()] == 1) continue;


                    if (dfs(e.to(), t, e.color())) {
                        cnt++;
                        checkColors[s][t][e.color()] = 1;
                    }
                }

                out.println(cnt);
            }


        }

        boolean dfs(int s, int t, int prevColor) {

            if (visited[s]) return false;

            if (s == t)
                return true;

            visited[s] = true;

            for (Edge e : edgeList[s]) {
                int v = e.from(), w = e.to(), c = e.color();
                if (prevColor == c)
                    if (dfs(w, t, prevColor))
                        return true;


            }
            return false;
        }

        public class Edge {
            int v;
            int w;
            int color;

            public Edge(int v, int w, int color) {
                this.v = v;
                this.w = w;
                this.color = color;
            }

            public Edge(int v, int w) {
                this(v, w, 1);
            }

            public int from() {
                return v;
            }

            public int to() {
                return w;
            }

            public int color() {
                return color;
            }

            public String toString() {
                return "Edge{" +
                        "v=" + v +
                        ", w=" + w +
                        ", color=" + color +
                        '}';
            }

        }

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private InputReader.SpaceCharFilter filter;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public int read() {
            if (numChars == -1) {
                throw new InputMismatchException();
            }
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0) {
                    return -1;
                }
            }
            return buf[curChar++];
        }

        public int nextInt() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public boolean isSpaceChar(int c) {
            if (filter != null) {
                return filter.isSpaceChar(c);
            }
            return isWhitespace(c);
        }

        public static boolean isWhitespace(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        public interface SpaceCharFilter {
            public boolean isSpaceChar(int ch);

        }

    }

    static class OutputWriter {
        private final PrintWriter writer;

        public OutputWriter(OutputStream outputStream) {
            writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
        }

        public OutputWriter(Writer writer) {
            this.writer = new PrintWriter(writer);
        }

        public void close() {
            writer.close();
        }

        public void println(int i) {
            writer.println(i);
        }

    }
}

