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.awt.Point;
import java.io.*;
import static java.lang.Math.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import static java.math.BigInteger.*;
import java.util.*;
import static java.util.Arrays.*;
import static java.lang.Character.*;
import static java.lang.Math.*;
import static java.math.BigInteger.*;
import static java.util.Arrays.*;
import java.awt.Point;

//<editor-fold defaultstate="collapsed" desc="Main">
public class Main implements Runnable {

    // https://netbeans.org/kb/73/java/editor-codereference_ru.html#display
    static boolean oj;

    @Override
    public void run() {
        try {
            Locale.setDefault(Locale.US);
        } catch (Exception e) {
        }
        oj = true;
        try {
            oj = System.getProperty("MYLOCAL") == null;
        } catch (Exception e) {
        }
//
        if (oj) {
            sc = new FastScanner(new InputStreamReader(System.in));
            out = new PrintWriter(new OutputStreamWriter(System.out));
        } else {
            try {
                sc = new FastScanner(new FileReader("input.txt"));
//                sc = new FastScanner(new FileReader("C:\\Users\\Igor\\Desktop\\e\\tests\\02"));
                out = new PrintWriter(new FileWriter("output.txt"));
            } catch (IOException e) {
                MLE();
            }
        }
        Solver s = new Solver();
        s.sc = sc;
        s.out = out;

        s.solve();
        if (!oj) {
            err.println("Time: " + (System.currentTimeMillis() - timeBegin) / 1e3);
            err.printf("Mem: %d\n", (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) >> 20);
        }
        out.flush();
    }

    public static void exit() {
        err.println("Time: " + (System.currentTimeMillis() - timeBegin) / 1e3);
        err.printf("Mem: %d\n", (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) >> 20);
        out.flush();
        out.close();
        System.exit(0);
    }

    public static void MLE() {
        int[][] arr = new int[1024 * 1024][];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = new int[1024 * 1024];
        }
    }

    public static void main(String[] args) {
        new Main().run();
        //new Thread(null, new Main(), "myThread", 100 * 1024 * 1024).start();
    }

    static long timeBegin = System.currentTimeMillis();
    static FastScanner sc;
    static PrintWriter out;
    static PrintStream err = System.err;
}
    //</editor-fold>

//<editor-fold defaultstate="collapsed" desc="FastScanner">
class FastScanner {

    BufferedReader br;
    StringTokenizer st;

    FastScanner(InputStreamReader reader) {
        br = new BufferedReader(reader);
        st = new StringTokenizer("");
    }

    String next() {
        while (!st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (Exception ex) {
                return null;
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    String nextLine() {
        try {
            return br.readLine();
        } catch (IOException ex) {
            return null;
        }
    }
}
//</editor-fold>

class Solver {

    void aser(boolean OK) {
        if (!OK) {
            Main.MLE();
//            for(;;);
        }
    }

    FastScanner sc;
    PrintWriter out;
    static PrintStream err = System.err;

    void solve() {
        int n, m;
        ArrayList<Rib>[] ribs;
        HashSet<Integer>[] idLeader;

        n = sc.nextInt();
        m = sc.nextInt();
        ribs = new ArrayList[m + 1];
        for (int c = 1; c <= m; c++) {
            ribs[c] = new ArrayList<>(1);
        }
        for (int iter = 0; iter < m; iter++) {
            Rib r = new Rib(sc.nextInt(), sc.nextInt());
            int c = sc.nextInt();
            ribs[c].add(r);
        }

        idLeader = new HashSet[n + 1];
        for (int v = 1; v <= n; v++) {
            idLeader[v] = new HashSet<>();
        }
        for (int c = 1, id = 0; c <= m; c++) {
            DSU dsu = new DSU(ribs[c]);
            HashMap<Integer, Integer> idLd = new HashMap<>();
            for (Map.Entry<Integer, Integer> e : dsu.map.entrySet()) {
                int v = e.getKey();
                int idV = e.getValue();
                int leader = dsu.getParent(idV);
                if (!idLd.containsKey(leader)) {
                    idLd.put(leader, id++);
                }
                int idLeade = idLd.get(leader);
                idLeader[v].add(idLeade);
            }
        }

        int Q = sc.nextInt();
        HashMap<String, Integer> answers = new HashMap<>();
        for (int iter = 0; iter < Q; iter++) {
            int a = sc.nextInt();
            int b = sc.nextInt();
            if (idLeader[a].size() > idLeader[b].size()
                    || idLeader[a].size() == idLeader[b].size() && a > b) {
                int t = a;
                a = b;
                b = t;
            }
            String hashQ = a + "," + b;
            Integer get = answers.get(hashQ);
            if (get != null) {
                out.println(get);
                continue;
            }

            int curAns = 0;
            for (Integer x : idLeader[a]) {
                if (idLeader[b].contains(x)) {
                    ++curAns;
                }
            }
            out.println(curAns);

            answers.put(hashQ, curAns);
        }
    }
}

final class DSU {

    HashMap<Integer, Integer> map;
    int[] p;
    int[] sz;

    public DSU(ArrayList<Rib> arr) {
        map = new HashMap<>();
        for (Rib r : arr) {
            if (!map.containsKey(r.a)) {
                map.put(r.a, map.size());
            }
            if (!map.containsKey(r.b)) {
                map.put(r.b, map.size());
            }
        }
        p = new int[map.size()];
        sz = new int[map.size()];
        for (int i = 0; i < p.length; i++) {
            p[i] = i;
        }
        for (int i = 0; i < sz.length; i++) {
            sz[i] = 1;
        }
        for (Rib r : arr) {
            int a = map.get(r.a);
            int b = map.get(r.b);
            unit(a, b);
        }
    }

    int getParent(int v) {
        if (p[v] == v) {
            return v;
        }
        return p[v] = getParent(p[v]);
    }

    void unit(int a, int b) {
        a = getParent(a);
        b = getParent(b);
        if (sz[a] < sz[b]) {
            int t = a;
            a = b;
            b = t;
        }
        p[b] = a;
        sz[a] += sz[b];
    }

}

class Rib {

    int a, b;

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

}
