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.*;
import java.io.*;

import java.math.BigInteger;
import java.util.*;

public class TaskC {


    static class Edge implements Comparable<Edge> {
        int v,cost;


        public Edge( int x, int y) {
            this.v = x;
            this.cost = y;
        }

        public int compareTo(Edge o) {
            if (cost  == o.cost)
                return v - o.v;
            return v - o.v;
        }

        @Override
        public boolean equals(Object o) {
            Edge edge = (Edge) o;
            return v == edge.v &&
                   cost == edge.cost;
        }


        public String toString() {
            return  v+" "+ cost;
        }
    }
    static int n,h;
    static long sum = 0;
    static ArrayList<Edge>[]adjList;
    static int reaches;
    static boolean[]vis;
    static HashSet<Integer> set;


    static void dfs(int u ,int v, int c)
    {
        vis[u] = true;
        if(u == v)
        {
            set.add(c);
            vis[u] = false;
            return;
        }
        for (Edge e : adjList[u])
        {
            if(c == -1)
                dfs(e.v , v , e.cost);
            else if(!vis[e.v] && e.cost == c)
                dfs(e.v , v , c);
        }
        if(c != -1)
            vis[u] = false;
    }



    public static void main(String[] args) throws Exception {

        Scanner sc = new Scanner(System.in);
        PrintWriter pw = new PrintWriter(System.out);


        int n = sc.nextInt();
        int m = sc.nextInt();

        adjList = new ArrayList[n];
        for (int i = 0 ; i < n ; i++)
            adjList[i] = new ArrayList<>();

        for (int i = 0 ; i < m ; i++)
        {
            int u = sc.nextInt() - 1;
            int v = sc.nextInt() - 1;
            int c = sc.nextInt();

            adjList[u].add(new Edge(v,c));
            adjList[v].add(new Edge(u,c));

        }


        int q = sc.nextInt();

        while (q-- > 0)
        {

            int u = sc.nextInt() - 1;
            int v = sc.nextInt() - 1;

            vis = new boolean[n];

            set = new HashSet<>();
            dfs(u , v , -1);
         //   System.out.println(set);
            pw.println(set.size());
        }
        pw.close();
    }
    static class Point implements Comparable<Point>
    {
        int x ,y , idx;

        public Point(int x, int y,int idx) {
            this.x = x;
            this.y = y;
            this.idx = idx;
        }

        @Override
        public int compareTo(Point o) {
            if (x == o.x) {
                if (y == o.y)
                    return idx - o.idx;
                return y - o.y;
            }
            return x - o.x;
        }

        @Override
        public String toString() {
            return "Point{" +
                    "x=" + x +
                    ", y=" + y +
                    ", idx=" + idx +
                    '}';
        }
    }

    static class Circle implements Comparable<Circle>
    {
        int x , radius;
        public Circle(int x , int r)
        {
            this.x = x;
            this.radius = r;
        }

        public boolean isInside(Point p)
        {
            return Math.sqrt((p.x - x)*(p.x - x) + (p.y)*(p.y)) <= radius;
        }

        @Override
        public String toString() {
            return "Circle{" +
                    "x=" + x +
                    ", radius=" + radius +
                    '}';
        }

        @Override
        public int compareTo(Circle o) {
            return x - o.x;
        }
    }

    private static long gcd(long a, long b) {
        if( b == 0)
            return a;
        return gcd(b  , a%b);
    }

    static long lcm(int a, int b)
    {
        return (a*b)/gcd(a, b);
    }


    private  static int dis(int xa , int ya , int xb , int yb)
    {
        return (xa-xb)*(xa - xb) + (ya- yb)*(ya-yb);
    }


    static class Pair implements Comparable<Pair> {
        int x,y;


        public Pair(int x, int y) {
            this.x = x;
            this.y = y;
        }

        public int compareTo(Pair o) {
            if (y  == o.y)
                return x - o.x;
            return y - o.y;

        }

        public double dis(Pair a){

            return (a.x - x)*(a.x - x) + (a.y-y)*(a.y-y);
        }

        public String toString() {
            return  x+" "+ y;
        }
        public boolean overlap(Pair a)
        {
            if((this.x >= a.x && this.x <= a.y) || (a.x >= this.x && a.x <= this.y)) return true;
            return false;

        }
    }

    static class Scanner {
        StringTokenizer st;
        BufferedReader br;

        public Scanner(FileReader r) {
            br = new BufferedReader(r);
        }

        public Scanner(InputStream s) {
            br = new BufferedReader(new InputStreamReader(s));
        }

        public String next() {
            while (st == null || !st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return st.nextToken();
        }

        public boolean check() {
            if (!st.hasMoreTokens())
                return false;
            return true;
        }

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

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

        public String nextLine() {
            try {
                return br.readLine();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }

        public double nextDouble() {
            String x = next();
            StringBuilder sb = new StringBuilder("0");
            double res = 0, f = 1;
            boolean dec = false, neg = false;
            int start = 0;
            if (x.charAt(0) == '-') {
                neg = true;
                start++;
            }
            for (int i = start; i < x.length(); i++)
                if (x.charAt(i) == '.') {
                    res = Long.parseLong(sb.toString());
                    sb = new StringBuilder("0");
                    dec = true;
                } else {
                    sb.append(x.charAt(i));
                    if (dec)
                        f *= 10;
                }
            res += Long.parseLong(sb.toString()) / f;
            return res * (neg ? -1 : 1);
        }

        public boolean ready() {
            try {
                return br.ready();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}