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.util.Set;
import java.util.HashMap;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author prakharjain
 */
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);
        _506D solver = new _506D();
        solver.solve(1, in, out);
        out.close();
    }

    static class _506D {
        public void solve(int testNumber, InputReader in, OutputWriter out) {
            int n = in.nextInt();
            int m = in.nextInt();

            edge[] edges = new edge[m];

            List<Integer> majorColors = new ArrayList<>();

            for (int i = 0; i < m; i++) {
                edges[i] = new edge(in.nextInt() - 1, in.nextInt() - 1, in.nextInt() - 1);
            }

            int[] cc = new int[m];

            for (int i = 0; i < m; i++) {
                cc[edges[i].c]++;
            }

            int sqrt = 316;

            Map<Integer, DsuInteger> dsuMaj = new HashMap<>();
            Map<Integer, DSU> dsuMin = new HashMap<>();

            Set[] va = new Set[m];

            for (int i = 0; i < m; i++) {
                if (cc[i] > sqrt) {
                    majorColors.add(i);
                    DsuInteger dsu = new DsuInteger(n);
                    for (int j = 0; j < n; j++) {
                        dsu.createSet(j);
                    }
                    dsuMaj.put(i, dsu);
                } else {
                    DSU<Integer> dsu = new DSU<>();
                    dsuMin.put(i, dsu);
                    va[i] = new HashSet();
                }
            }

            for (int i = 0; i < edges.length; i++) {
                edge edge = edges[i];
                if (cc[edge.c] > sqrt) {
                    DsuInteger dsu = dsuMaj.get(edge.c);
                    dsu.mergeSets(edge.a, edge.b);
                } else {
                    va[edge.c].add(edge.a);
                    va[edge.c].add(edge.b);
                }
            }

            Map[] ma = new Map[n];

            for (int i = 0; i < n; i++) {
                ma[i] = new HashMap<Integer, Integer>();
            }

            for (int i = 0; i < m; i++) {
                if (va[i] != null) {
                    DSU<Integer> dsu = dsuMin.get(i);

                    for (int num : (Set<Integer>) va[i]) {
                        dsu.createSet(num);
                    }
                }
            }

            for (int i = 0; i < edges.length; i++) {
                edge edge = edges[i];
                if (va[edge.c] != null) {
                    DSU<Integer> dsu = dsuMin.get(edge.c);
                    dsu.mergeSets(edge.a, edge.b);
                }
            }

            for (int i = 0; i < m; i++) {
                if (va[i] != null) {
                    DSU<Integer> dsu = dsuMin.get(i);

                    for (Integer ca : (Set<Integer>) va[i]) {
                        for (Integer cb : (Set<Integer>) va[i]) {
                            if (ca < cb) {
                                int fs1 = dsu.findSet(ca);
                                int fs2 = dsu.findSet(cb);

                                if (fs1 == fs2) {
                                    int min = ca;
                                    int max = cb;

                                    ma[min].merge(max, 1, (x, y) -> (int) x + (int) y);
                                }
                            }
                        }
                    }
                }
            }

            int q = in.nextInt();

            while (q-- > 0) {
                int u = in.nextInt() - 1;
                int v = in.nextInt() - 1;

                int min = Math.min(u, v);
                int max = Math.max(u, v);

                long ans = (int) ma[min].getOrDefault(max, 0);

                for (int i = 0; i < majorColors.size(); i++) {
                    int c = majorColors.get(i);

                    DsuInteger dsu = dsuMaj.get(c);

                    int fs1 = dsu.findSet(u);
                    int fs2 = dsu.findSet(v);

                    if (fs1 == fs2) {
                        ans++;
                    }
                }

                out.println(ans);
            }
        }

        class edge {
            int a;
            int b;
            int c;

            public edge(int a, int b, int c) {
                this.a = a;
                this.b = b;
                this.c = c;
            }

        }

        class DsuInteger {
            int[] parent;
            int[] rank;
            int n;

            public DsuInteger(int n) {
                this.n = n;
                this.parent = new int[n];
                this.rank = new int[n];
            }

            int createSet(int x) {
                parent[x] = x;
                rank[x] = 0;
                return x;
            }

            int findSet(int x) {
                int par = parent[x];
                if (x != par) {
                    parent[x] = findSet(par);
                    return parent[x];
                }
                return par;
            }

            int mergeSets(int x, int y) {
                int rx = findSet(x);
                int ry = findSet(y);

                if (rx == ry) {
                    return rx;
                }

                int fp = -1;

                if (rank[rx] > rank[ry]) {
                    parent[ry] = rx;
                    fp = rx;
                } else {
                    parent[rx] = ry;
                    fp = ry;
                }

                if (rank[rx] == rank[ry]) {
                    rank[ry] = rank[ry] + 1;
                }

                return fp;
            }

        }

        class DSU<T> {
            Map<T, T> parent = new HashMap<>();
            Map<T, Integer> rank = new HashMap<>();

            T createSet(T x) {
                parent.put(x, x);
                rank.put(x, 0);
                return x;
            }

            T findSet(T x) {
                T par = parent.get(x);
                if (!x.equals(par)) {
                    parent.put(x, findSet(par));
                    return parent.get(x);
                }
                return par;
            }

            T mergeSets(T x, T y) {
                T rx = findSet(x);
                T ry = findSet(y);

                if (rx.equals(ry)) {
                    return rx;
                }

                T fp = null;

                if (rank.get(rx) > rank.get(ry)) {
                    parent.put(ry, rx);
                    fp = rx;
                } else {
                    parent.put(rx, ry);
                    fp = ry;
                }

                if (rank.get(rx).equals(rank.get(ry))) {
                    rank.put(ry, rank.get(ry) + 1);
                }

                return fp;
            }

        }

    }

    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(long i) {
            writer.println(i);
        }

    }

    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 static boolean isWhitespace(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        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 interface SpaceCharFilter {
            public boolean isSpaceChar(int ch);

        }

    }
}

