import java.io.*;
import java.util.*;

public class Main{
	static class dsu{
		int[] p, rank;
		int N;
		dsu(int n) {
			N=n;
			p = new int[N];
			rank = new int[N];
			for (int i = 0; i < N; i++) {  p[i] = i; }
		}
		public int findSet(int i) { return p[i] == i ? i : (p[i] = findSet(p[i])); }

		public boolean isSameSet(int i, int j) { return findSet(i) == findSet(j); }

		public boolean unionSet(int i, int j)
		{
			if (isSameSet(i, j))
				return false;
			int x = findSet(i), y = findSet(j);
			if(rank[x] > rank[y]) { p[y] = x;}
			else
			{	p[x] = y;
				if(rank[x] == rank[y]) rank[y]++;
			}
			return true;
		}
	}
	static void main() throws Exception{
		int n=sc.nextInt(),m=sc.nextInt();
		int[][]edges=new int[m][3];
		HashMap<Integer, Integer>[]maps=new HashMap[m];
		for(int i=0;i<m;i++) {
			edges[i]=new int[] {sc.nextInt()-1,sc.nextInt()-1,sc.nextInt()-1};
			maps[i]=new HashMap<Integer, Integer>();
		}
		for(int i=0;i<m;i++) {
			int x=edges[i][0],y=edges[i][1],c=edges[i][2];
			if(!maps[c].containsKey(x)) {
				maps[c].put(x, maps[c].size());
			}
			if(!maps[c].containsKey(y)) {
				maps[c].put(y, maps[c].size());
			}
		}
		dsu[]ds=new dsu[m];
		for(int i=0;i<m;i++) {
			ds[i]=new dsu(maps[i].size());

		}
		HashSet<Integer>[]colHs=new HashSet[n];
		ArrayList<Integer>[]colAl=new ArrayList[n];
		for(int i=0;i<n;i++) {
			colHs[i]=new HashSet<Integer>();
			colAl[i]=new ArrayList<Integer>();
		}
		for(int i=0;i<m;i++) {
			int x=edges[i][0],y=edges[i][1],c=edges[i][2];
			ds[c].unionSet(maps[c].get(x), maps[c].get(y));
			if(!colHs[x].contains(c)) {
				colHs[x].add(c);
				colAl[x].add(c);
			}
			if(!colHs[y].contains(c)) {
				colHs[y].add(c);
				colAl[y].add(c);
			}
		}
		HashMap<Long, Integer>memo=new HashMap<Long, Integer>();
		int q=sc.nextInt();
		for(int i=0;i<q;i++) {
			int u=sc.nextInt()-1,v=sc.nextInt()-1;
			long curHash=Math.min(u, v)*1l*n+Math.max(u, v);
			if(memo.containsKey(curHash)) {
				pw.println(memo.get(curHash));
				continue;
			}
			int ans=0;
			if(colAl[u].size()>colAl[v].size()) {
				int tmp=u;
				u=v;
				v=tmp;
			}
			for(int j=0;j<colAl[u].size();j++) {
				int c=colAl[u].get(j);
				if(maps[c].containsKey(u) && maps[c].containsKey(v) && ds[c].isSameSet(maps[c].get(u), maps[c].get(v))) {
					ans++;
				}
			}
			pw.println(ans);
			memo.put(curHash, ans);
		}
	}
	public static void main(String[] args) throws Exception {
		sc = new MScanner(System.in);
		pw = new PrintWriter(System.out);
		int tc = 1;
//		tc=sc.nextInt();
		while (tc-- > 0)
			main();
		pw.flush();
	}

	static PrintWriter pw;
	static MScanner sc;

	static class MScanner {
		StringTokenizer st;
		BufferedReader br;

		public MScanner(InputStream system) {
			br = new BufferedReader(new InputStreamReader(system));
		}

		public MScanner(String file) throws Exception {
			br = new BufferedReader(new FileReader(file));
		}

		public String next() throws IOException {
			while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}

		public int[] intArr(int n) throws IOException {
			int[] in = new int[n];
			for (int i = 0; i < n; i++)
				in[i] = nextInt();
			return in;
		}

		public long[] longArr(int n) throws IOException {
			long[] in = new long[n];
			for (int i = 0; i < n; i++)
				in[i] = nextLong();
			return in;
		}

		public int[] intSortedArr(int n) throws IOException {
			int[] in = new int[n];
			for (int i = 0; i < n; i++)
				in[i] = nextInt();
			shuffle(in);
			Arrays.sort(in);
			return in;
		}

		public long[] longSortedArr(int n) throws IOException {
			long[] in = new long[n];
			for (int i = 0; i < n; i++)
				in[i] = nextLong();
			shuffle(in);
			Arrays.sort(in);
			return in;
		}

		public Integer[] IntegerArr(int n) throws IOException {
			Integer[] in = new Integer[n];
			for (int i = 0; i < n; i++)
				in[i] = nextInt();
			return in;
		}

		public Long[] LongArr(int n) throws IOException {
			Long[] in = new Long[n];
			for (int i = 0; i < n; i++)
				in[i] = nextLong();
			return in;
		}

		public String nextLine() throws IOException {
			return br.readLine();
		}

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

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

		public char nextChar() throws IOException {
			return next().charAt(0);
		}

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

		public boolean ready() throws IOException {
			return br.ready();
		}

		public void waitForInput() throws InterruptedException {
			Thread.sleep(3000);
		}
	}

	static void shuffle(int[] in) {
		for (int i = 0; i < in.length; i++) {
			int idx = (int) (Math.random() * (i + 1));
			int tmp = in[i];
			in[i] = in[idx];
			in[idx] = tmp;
		}
	}

	static void shuffle(long[] in) {
		for (int i = 0; i < in.length; i++) {
			int idx = (int) (Math.random() * (i + 1));
			long tmp = in[i];
			in[i] = in[idx];
			in[idx] = tmp;
		}
	}
}


import java.io.*;
import java.util.*;

/**
 * @author Pavel Mavrin
 */
public class D {

    private static final int BUBEN = 1000;

    public D() throws FileNotFoundException {
        br = new BufferedReader(new InputStreamReader(System.in));
        out = new PrintWriter(System.out);
//        br = new BufferedReader(new FileReader("input.txt"));
//        out = new PrintWriter("output.txt");
    }

    private void solve() throws IOException {
        int n = nextInt();
        int m = nextInt();
        List<Edge>[] byCol = new List[m];
        for (int i = 0; i < m; i++) {
            int x = nextInt() - 1;
            int y = nextInt() - 1;
            int c = nextInt() - 1;
            if (byCol[c] == null) {
                byCol[c] = new ArrayList<Edge>();
            }
            byCol[c].add(new Edge(x, y));
        }
        Arrays.sort(byCol, new Comparator<List<Edge>>() {
            public int compare(List<Edge> o1, List<Edge> o2) {
                int a = o1 == null ? 0 : o1.size();
                int b = o2 == null ? 0 : o2.size();
                return Integer.compare(a, b);
            }
        });
        init(n, BUBEN);
        int[] r = new int[2 * BUBEN];
        int rn;
        Map<Pair, Integer> small = new HashMap<Pair, Integer>();
        List<int[]> big = new ArrayList<int[]>();
        for (List<Edge> edges : byCol) {
            if (edges == null) continue;
            if (edges.size() < BUBEN) {
                rn = 0;
                for (Edge edge : edges) {
                    r[rn++] = edge.x;
                    r[rn++] = edge.y;
                }
                reinit(r, rn);
                for (Edge edge : edges) {
                    addEdge(edge.x, edge.y);
                }
                for (int i = 0; i < rn; i++) {
                    int x = r[i];
                    if (!z[x]) {
                        pn = 0;
                        dfs(x);
                        for (int ii = 0; ii < pn; ii++) {
                            for (int jj = 0; jj < pn; jj++) {
                                Pair pair = new Pair(p[ii], p[jj]);
                                Integer q = small.get(pair);
                                if (q == null) {
                                    small.put(pair, 1);
                                } else {
                                    small.put(pair, q + 1);
                                }
                            }
                        }
                    }
                }
            } else {
                int[] p = new int[n];
                for (int i = 0; i < n; i++) {
                    p[i] = i;
                }
                for (Edge edge : edges) {
                    join(p, edge.x, edge.y);
                }
                big.add(p);
            }
        }
        int q = nextInt();
        for (int i = 0; i < q; i++) {
            int u = nextInt() - 1;
            int v = nextInt() - 1;
            Pair pair = new Pair(u, v);
            int res = 0;
            {
                Integer ii = small.get(pair);
                if (ii != null) res = ii;
            }
            for (int[] p : big) {
                if (get(p, u) == get(p, v)) {
                    res++;
                }
            }
            out.println(res);
        }
//        System.out.println(big.size() + " " + small.size());
    }

    Random random = new Random();

    private void join(int[] p, int x, int y) {
        x = get(p, x);
        y = get(p, y);
        if (random.nextBoolean()) p[x] = y;
        else p[y] = x;
    }

    private int get(int[] p, int x) {
        if (p[x] == x) return x;
        p[x] = get(p, p[x]);
        return p[x];
    }

    class Pair {
        int x, y;

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

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;

            Pair pair = (Pair) o;

            if (x != pair.x) return false;
            if (y != pair.y) return false;

            return true;
        }

        @Override
        public int hashCode() {
            int result = x;
            result = 31 * result + y;
            return result;
        }
    }

    class Edge {
        int x, y;

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

    void init(int n, int m) {
        m *= 2;
        this.n = n;
        this.m = m;
        last = 0;
        head = new int[n];
        nx = new int[m];
        dst = new int[m];
        Arrays.fill(head, -1);
        z = new boolean[n];
        p = new int[n];
    }

    void reinit(int[] r, int rn) {
        for (int i = 0; i < rn; i++) {
            head[r[i]] = -1;
            z[r[i]] = false;
        }
        last = 0;
    }

    void addEdge(int x, int y) {
        nx[last] = head[x];
        dst[last] = y;
        head[x] = last;
        last++;
        nx[last] = head[y];
        dst[last] = x;
        head[y] = last;
        last++;
    }

    private void dfs(int x) {
        if (z[x]) return;
        p[pn++] = x;
        z[x] = true;
        int j = head[x];
        while (j >= 0) {
            int y = dst[j];
            dfs(y);
            j = nx[j];
        }
    }

    int n, m;
    int[] head;
    int[] nx;
    int[] dst;
    boolean[] z;
    int last;
    int[] p;
    int pn;

    BufferedReader br;
    PrintWriter out;

    StringTokenizer st;

    String next() throws IOException {
        while (st == null || !st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }

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

    public static void main(String[] args) throws IOException {
        new D().run();
    }

    private void run() throws IOException {
        solve();
        out.close();
    }

}


import java.io.*;
import java.util.*;

/**
 * @author Pavel Mavrin
 */
public class D {

    private static final int BUBEN = 200;

    public D() throws FileNotFoundException {
        br = new BufferedReader(new InputStreamReader(System.in));
        out = new PrintWriter(System.out);
//        br = new BufferedReader(new FileReader("input.txt"));
//        out = new PrintWriter("output.txt");
    }

    private void solve() throws IOException {
        int n = nextInt();
        int m = nextInt();
        List<Edge>[] byCol = new List[m];
        for (int i = 0; i < m; i++) {
            int x = nextInt() - 1;
            int y = nextInt() - 1;
            int c = nextInt() - 1;
            if (byCol[c] == null) {
                byCol[c] = new ArrayList<Edge>();
            }
            byCol[c].add(new Edge(x, y));
        }
        Arrays.sort(byCol, new Comparator<List<Edge>>() {
            public int compare(List<Edge> o1, List<Edge> o2) {
                int a = o1 == null ? 0 : o1.size();
                int b = o2 == null ? 0 : o2.size();
                return Integer.compare(a, b);
            }
        });
        init(n, BUBEN);
        int[] r = new int[2 * BUBEN];
        int rn;
        Map<Pair, Integer> small = new HashMap<Pair, Integer>();
        List<int[]> big = new ArrayList<int[]>();
        for (List<Edge> edges : byCol) {
            if (edges == null) continue;
            if (edges.size() < BUBEN) {
                rn = 0;
                for (Edge edge : edges) {
                    r[rn++] = edge.x;
                    r[rn++] = edge.y;
                }
                reinit(r, rn);
                for (Edge edge : edges) {
                    addEdge(edge.x, edge.y);
                }
                for (int i = 0; i < rn; i++) {
                    int x = r[i];
                    if (!z[x]) {
                        pn = 0;
                        dfs(x);
                        for (int ii = 0; ii < pn; ii++) {
                            for (int jj = 0; jj < pn; jj++) {
                                Pair pair = new Pair(p[ii], p[jj]);
                                Integer q = small.get(pair);
                                if (q == null) {
                                    small.put(pair, 1);
                                } else {
                                    small.put(pair, q + 1);
                                }
                            }
                        }
                    }
                }
            } else {
                int[] p = new int[n];
                for (int i = 0; i < n; i++) {
                    p[i] = i;
                }
                for (Edge edge : edges) {
                    join(p, edge.x, edge.y);
                }
                big.add(p);
            }
        }
        int q = nextInt();
        for (int i = 0; i < q; i++) {
            int u = nextInt() - 1;
            int v = nextInt() - 1;
            Pair pair = new Pair(u, v);
            int res = 0;
            {
                Integer ii = small.get(pair);
                if (ii != null) res = ii;
            }
            for (int[] p : big) {
                if (get(p, u) == get(p, v)) {
                    res++;
                }
            }
            out.println(res);
        }
//        System.out.println(big.size() + " " + small.size());
    }

    Random random = new Random();

    private void join(int[] p, int x, int y) {
        x = get(p, x);
        y = get(p, y);
        if (random.nextBoolean()) p[x] = y;
        else p[y] = x;
    }

    private int get(int[] p, int x) {
        if (p[x] == x) return x;
        p[x] = get(p, p[x]);
        return p[x];
    }

    class Pair {
        int x, y;

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

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;

            Pair pair = (Pair) o;

            if (x != pair.x) return false;
            if (y != pair.y) return false;

            return true;
        }

        @Override
        public int hashCode() {
            int result = x;
            result = 31 * result + y;
            return result;
        }
    }

    class Edge {
        int x, y;

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

    void init(int n, int m) {
        m *= 2;
        this.n = n;
        this.m = m;
        last = 0;
        head = new int[n];
        nx = new int[m];
        dst = new int[m];
        Arrays.fill(head, -1);
        z = new boolean[n];
        p = new int[n];
    }

    void reinit(int[] r, int rn) {
        for (int i = 0; i < rn; i++) {
            head[r[i]] = -1;
            z[r[i]] = false;
        }
        last = 0;
    }

    void addEdge(int x, int y) {
        nx[last] = head[x];
        dst[last] = y;
        head[x] = last;
        last++;
        nx[last] = head[y];
        dst[last] = x;
        head[y] = last;
        last++;
    }

    private void dfs(int x) {
        if (z[x]) return;
        p[pn++] = x;
        z[x] = true;
        int j = head[x];
        while (j >= 0) {
            int y = dst[j];
            dfs(y);
            j = nx[j];
        }
    }

    int n, m;
    int[] head;
    int[] nx;
    int[] dst;
    boolean[] z;
    int last;
    int[] p;
    int pn;

    BufferedReader br;
    PrintWriter out;

    StringTokenizer st;

    String next() throws IOException {
        while (st == null || !st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }

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

    public static void main(String[] args) throws IOException {
        new D().run();
    }

    private void run() throws IOException {
        solve();
        out.close();
    }

}


import java.io.*;
import java.lang.management.MemoryUsage;
import java.util.*;

/**
 * @author Pavel Mavrin
 */
public class D {

    private static final int BUBEN = 1500;
    private static final int SIZE = 15000000;

    public D() throws FileNotFoundException {
        br = new BufferedReader(new InputStreamReader(System.in));
        out = new PrintWriter(System.out);
//        br = new BufferedReader(new FileReader("input.txt"));
//        out = new PrintWriter("output.txt");
    }

    private void solve() throws IOException {
        int n = nextInt();
        int m = nextInt();
        List<Edge>[] byCol = new List[m];
        for (int i = 0; i < m; i++) {
            int x = nextInt() - 1;
            int y = nextInt() - 1;
            int c = nextInt() - 1;
            if (byCol[c] == null) {
                byCol[c] = new ArrayList<Edge>();
            }
            byCol[c].add(new Edge(x, y));
        }
        Arrays.sort(byCol, new Comparator<List<Edge>>() {
            public int compare(List<Edge> o1, List<Edge> o2) {
                int a = o1 == null ? 0 : o1.size();
                int b = o2 == null ? 0 : o2.size();
                return Integer.compare(a, b);
            }
        });
        init(n, BUBEN);
        int[] r = new int[2 * BUBEN];
        int rn;
        Map<Pair, Integer> small = new HashMap<Pair, Integer>();
        List<int[]> big = new ArrayList<int[]>();
        int cc = 0;
        for (List<Edge> edges : byCol) {
            if (edges == null) continue;
            if (edges.size() < BUBEN) {
                rn = 0;
                for (Edge edge : edges) {
                    r[rn++] = edge.x;
                    r[rn++] = edge.y;
                }
                reinit(r, rn);
                for (Edge edge : edges) {
                    addEdge(edge.x, edge.y);
                }
                for (int i = 0; i < rn; i++) {
                    int x = r[i];
                    if (!z[x]) {
                        pn = 0;
                        dfs(x);
                        for (int ii = 0; ii < pn; ii++) {
                            for (int jj = ii + 1; jj < pn; jj++) {
//                                Pair pair = new Pair(p[ii], p[jj]);
                                cc++;
                                add(p[ii], p[jj]);
//                                Integer q = small.get(pair);
//                                if (q == null) {
//                                    small.put(pair, 1);
//                                } else {
//                                    small.put(pair, q + 1);
//                                }
                            }
                        }
                    }
                }
            } else {
                int[] p = new int[n];
                for (int i = 0; i < n; i++) {
                    p[i] = i;
                }
                for (Edge edge : edges) {
                    join(p, edge.x, edge.y);
                }
                big.add(p);
            }
        }
        int q = nextInt();
        for (int i = 0; i < q; i++) {
            int u = nextInt() - 1;
            int v = nextInt() - 1;
            int res = get(u, v);
            for (int[] p : big) {
                if (get(p, u) == get(p, v)) {
                    res++;
                }
            }
            out.println(res);
        }
//        System.out.println(cc);
//        System.out.println(big.size() + " " + small.size());
//        System.out.println(MemoryUsage.);
    }

    Random random = new Random();

    private void join(int[] p, int x, int y) {
        x = get(p, x);
        y = get(p, y);
        if (random.nextBoolean()) p[x] = y;
        else p[y] = x;
    }

    private int get(int[] p, int x) {
        if (p[x] == x) return x;
        p[x] = get(p, p[x]);
        return p[x];
    }

    int[] ww = new int[SIZE];
    int[] zz = new int[SIZE];

    void add(int u, int v) {
        if (u > v) {
            int t = u; u = v; v = t;
        }
        int h = hash(u, v);
        int h2 = hash2(u, v);
        while (ww[h] > 0) {
            if (ww[h] == h2) {
                zz[h]++;
                return;
            }
            h++;
            if (h == SIZE) h = 0;
        }
        ww[h] = h2;
        zz[h] = 1;
    }

    int get(int u, int v) {
        if (u > v) {
            int t = u; u = v; v = t;
        }
        int h = hash(u, v);
        int h2 = hash2(u, v);
        while (ww[h] > 0) {
            if (ww[h] == h2) {
                return zz[h];
            }
            h++;
            if (h == SIZE) h = 0;
        }
        return 0;
    }

    private int hash(int u, int v) {
        return ((u * 239 + v) * 239) % SIZE;
    }

    private int hash2(int u, int v) {
        return ((u * 239239 + v) * 239239);
    }

    class Pair {
        int x, y;

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

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;

            Pair pair = (Pair) o;

            if (x != pair.x) return false;
            if (y != pair.y) return false;

            return true;
        }

        @Override
        public int hashCode() {
            int result = x;
            result = 31 * result + y;
            return result;
        }
    }

    class Edge {
        int x, y;

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

    void init(int n, int m) {
        m *= 2;
        this.n = n;
        this.m = m;
        last = 0;
        head = new int[n];
        nx = new int[m];
        dst = new int[m];
        Arrays.fill(head, -1);
        z = new boolean[n];
        p = new int[n];
    }

    void reinit(int[] r, int rn) {
        for (int i = 0; i < rn; i++) {
            head[r[i]] = -1;
            z[r[i]] = false;
        }
        last = 0;
    }

    void addEdge(int x, int y) {
        nx[last] = head[x];
        dst[last] = y;
        head[x] = last;
        last++;
        nx[last] = head[y];
        dst[last] = x;
        head[y] = last;
        last++;
    }

    private void dfs(int x) {
        if (z[x]) return;
        p[pn++] = x;
        z[x] = true;
        int j = head[x];
        while (j >= 0) {
            int y = dst[j];
            dfs(y);
            j = nx[j];
        }
    }

    int n, m;
    int[] head;
    int[] nx;
    int[] dst;
    boolean[] z;
    int last;
    int[] p;
    int pn;

    BufferedReader br;
    PrintWriter out;

    StringTokenizer st;

    String next() throws IOException {
        while (st == null || !st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }

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

    public static void main(String[] args) throws IOException {
        new D().run();
    }

    private void run() throws IOException {
        solve();
        out.close();
    }

}


import java.io.*;
import java.util.*;

/**
 * @author Pavel Mavrin
 */
public class D {

    private static final int BUBEN = 2000;

    public D() throws FileNotFoundException {
//        br = new BufferedReader(new InputStreamReader(System.in));
//        out = new PrintWriter(System.out);
        br = new BufferedReader(new FileReader("input.txt"));
        out = new PrintWriter("output.txt");
    }

    private void solve() throws IOException {
        int n = nextInt();
        int m = nextInt();
        List<Edge>[] byCol = new List[m];
        for (int i = 0; i < m; i++) {
            int x = nextInt() - 1;
            int y = nextInt() - 1;
            int c = nextInt() - 1;
            if (byCol[c] == null) {
                byCol[c] = new ArrayList<Edge>();
            }
            byCol[c].add(new Edge(x, y));
        }
        Arrays.sort(byCol, new Comparator<List<Edge>>() {
            public int compare(List<Edge> o1, List<Edge> o2) {
                int a = o1 == null ? 0 : o1.size();
                int b = o2 == null ? 0 : o2.size();
                return Integer.compare(a, b);
            }
        });
        init(n, BUBEN);
        int[] r = new int[2 * BUBEN];
        int rn;
        Map<Pair, Integer> small = new HashMap<Pair, Integer>();
        List<int[]> big = new ArrayList<int[]>();
        for (List<Edge> edges : byCol) {
            if (edges == null) continue;
            if (edges.size() < BUBEN) {
                rn = 0;
                for (Edge edge : edges) {
                    r[rn++] = edge.x;
                    r[rn++] = edge.y;
                }
                reinit(r, rn);
                for (Edge edge : edges) {
                    addEdge(edge.x, edge.y);
                }
                for (int i = 0; i < rn; i++) {
                    int x = r[i];
                    if (!z[x]) {
                        pn = 0;
                        dfs(x);
                        for (int ii = 0; ii < pn; ii++) {
                            for (int jj = 0; jj < pn; jj++) {
                                Pair pair = new Pair(p[ii], p[jj]);
                                Integer q = small.get(pair);
                                if (q == null) {
                                    small.put(pair, 1);
                                } else {
                                    small.put(pair, q + 1);
                                }
                            }
                        }
                    }
                }
            } else {
                int[] p = new int[n];
                for (int i = 0; i < n; i++) {
                    p[i] = i;
                }
                for (Edge edge : edges) {
                    join(p, edge.x, edge.y);
                }
                big.add(p);
            }
        }
        int q = nextInt();
        for (int i = 0; i < q; i++) {
            int u = nextInt() - 1;
            int v = nextInt() - 1;
            Pair pair = new Pair(u, v);
            int res = 0;
            {
                Integer ii = small.get(pair);
                if (ii != null) res = ii;
            }
            for (int[] p : big) {
                if (get(p, u) == get(p, v)) {
                    res++;
                }
            }
            out.println(res);
        }
//        System.out.println(big.size() + " " + small.size());
    }

    Random random = new Random();

    private void join(int[] p, int x, int y) {
        x = get(p, x);
        y = get(p, y);
        if (random.nextBoolean()) p[x] = y;
        else p[y] = x;
    }

    private int get(int[] p, int x) {
        if (p[x] == x) return x;
        p[x] = get(p, p[x]);
        return p[x];
    }

    class Pair {
        int x, y;

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

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;

            Pair pair = (Pair) o;

            if (x != pair.x) return false;
            if (y != pair.y) return false;

            return true;
        }

        @Override
        public int hashCode() {
            int result = x;
            result = 31 * result + y;
            return result;
        }
    }

    class Edge {
        int x, y;

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

    void init(int n, int m) {
        m *= 2;
        this.n = n;
        this.m = m;
        last = 0;
        head = new int[n];
        nx = new int[m];
        dst = new int[m];
        Arrays.fill(head, -1);
        z = new boolean[n];
        p = new int[n];
    }

    void reinit(int[] r, int rn) {
        for (int i = 0; i < rn; i++) {
            head[r[i]] = -1;
            z[r[i]] = false;
        }
        last = 0;
    }

    void addEdge(int x, int y) {
        nx[last] = head[x];
        dst[last] = y;
        head[x] = last;
        last++;
        nx[last] = head[y];
        dst[last] = x;
        head[y] = last;
        last++;
    }

    private void dfs(int x) {
        if (z[x]) return;
        p[pn++] = x;
        z[x] = true;
        int j = head[x];
        while (j >= 0) {
            int y = dst[j];
            dfs(y);
            j = nx[j];
        }
    }

    int n, m;
    int[] head;
    int[] nx;
    int[] dst;
    boolean[] z;
    int last;
    int[] p;
    int pn;

    BufferedReader br;
    PrintWriter out;

    StringTokenizer st;

    String next() throws IOException {
        while (st == null || !st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }

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

    public static void main(String[] args) throws IOException {
        new D().run();
    }

    private void run() throws IOException {
        solve();
        out.close();
    }

}


import java.io.*;
import java.lang.management.MemoryUsage;
import java.util.*;

/**
 * @author Pavel Mavrin
 */
public class D {

    private static final int BUBEN = 2000;
    private static final int SIZE = 15000000;

    public D() throws FileNotFoundException {
        br = new BufferedReader(new InputStreamReader(System.in));
        out = new PrintWriter(System.out);
//        br = new BufferedReader(new FileReader("input.txt"));
//        out = new PrintWriter("output.txt");
    }

    private void solve() throws IOException {
        int n = nextInt();
        int m = nextInt();
        List<Edge>[] byCol = new List[m];
        for (int i = 0; i < m; i++) {
            int x = nextInt() - 1;
            int y = nextInt() - 1;
            int c = nextInt() - 1;
            if (byCol[c] == null) {
                byCol[c] = new ArrayList<Edge>();
            }
            byCol[c].add(new Edge(x, y));
        }
        Arrays.sort(byCol, new Comparator<List<Edge>>() {
            public int compare(List<Edge> o1, List<Edge> o2) {
                int a = o1 == null ? 0 : o1.size();
                int b = o2 == null ? 0 : o2.size();
                return Integer.compare(a, b);
            }
        });
        init(n, BUBEN);
        int[] r = new int[2 * BUBEN];
        int rn;
        Map<Pair, Integer> small = new HashMap<Pair, Integer>();
        List<int[]> big = new ArrayList<int[]>();
        int cc = 0;
        for (List<Edge> edges : byCol) {
            if (edges == null) continue;
            if (edges.size() < BUBEN) {
                rn = 0;
                for (Edge edge : edges) {
                    r[rn++] = edge.x;
                    r[rn++] = edge.y;
                }
                reinit(r, rn);
                for (Edge edge : edges) {
                    addEdge(edge.x, edge.y);
                }
                for (int i = 0; i < rn; i++) {
                    int x = r[i];
                    if (!z[x]) {
                        pn = 0;
                        dfs(x);
                        for (int ii = 0; ii < pn; ii++) {
                            for (int jj = ii + 1; jj < pn; jj++) {
//                                Pair pair = new Pair(p[ii], p[jj]);
                                cc++;
                                add(p[ii], p[jj]);
//                                Integer q = small.get(pair);
//                                if (q == null) {
//                                    small.put(pair, 1);
//                                } else {
//                                    small.put(pair, q + 1);
//                                }
                            }
                        }
                    }
                }
            } else {
                int[] p = new int[n];
                for (int i = 0; i < n; i++) {
                    p[i] = i;
                }
                for (Edge edge : edges) {
                    join(p, edge.x, edge.y);
                }
                big.add(p);
            }
        }
        int q = nextInt();
        for (int i = 0; i < q; i++) {
            int u = nextInt() - 1;
            int v = nextInt() - 1;
            int res = get(u, v);
            for (int[] p : big) {
                if (get(p, u) == get(p, v)) {
                    res++;
                }
            }
            out.println(res);
        }
//        System.out.println(cc);
//        System.out.println(big.size() + " " + small.size());
//        System.out.println(MemoryUsage.);
    }

    Random random = new Random();

    private void join(int[] p, int x, int y) {
        x = get(p, x);
        y = get(p, y);
        if (random.nextBoolean()) p[x] = y;
        else p[y] = x;
    }

    private int get(int[] p, int x) {
        if (p[x] == x) return x;
        p[x] = get(p, p[x]);
        return p[x];
    }

    int[] uu = new int[SIZE];
    int[] vv = new int[SIZE];
    int[] zz = new int[SIZE];

    void add(int u, int v) {
        if (u > v) {
            int t = u; u = v; v = t;
        }
        int h = hash(u, v);
        while (uu[h] > 0 || vv[h] > 0) {
            if (uu[h] == u && vv[h] == v) {
                zz[h]++;
                return;
            }
            h++;
            if (h == SIZE) h = 0;
        }
        uu[h] = u;
        vv[h] = v;
        zz[h] = 1;
    }

    int get(int u, int v) {
        if (u > v) {
            int t = u; u = v; v = t;
        }
        int h = hash(u, v);
        while (uu[h] > 0 || vv[h] > 0) {
            if (uu[h] == u && vv[h] == v) {
                return zz[h];
            }
            h++;
            if (h == SIZE) h = 0;
        }
        return 0;
    }

    private int hash(int u, int v) {
        return ((u * 31 + v) * 31) % SIZE;
    }

    class Pair {
        int x, y;

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

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;

            Pair pair = (Pair) o;

            if (x != pair.x) return false;
            if (y != pair.y) return false;

            return true;
        }

        @Override
        public int hashCode() {
            int result = x;
            result = 31 * result + y;
            return result;
        }
    }

    class Edge {
        int x, y;

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

    void init(int n, int m) {
        m *= 2;
        this.n = n;
        this.m = m;
        last = 0;
        head = new int[n];
        nx = new int[m];
        dst = new int[m];
        Arrays.fill(head, -1);
        z = new boolean[n];
        p = new int[n];
    }

    void reinit(int[] r, int rn) {
        for (int i = 0; i < rn; i++) {
            head[r[i]] = -1;
            z[r[i]] = false;
        }
        last = 0;
    }

    void addEdge(int x, int y) {
        nx[last] = head[x];
        dst[last] = y;
        head[x] = last;
        last++;
        nx[last] = head[y];
        dst[last] = x;
        head[y] = last;
        last++;
    }

    private void dfs(int x) {
        if (z[x]) return;
        p[pn++] = x;
        z[x] = true;
        int j = head[x];
        while (j >= 0) {
            int y = dst[j];
            dfs(y);
            j = nx[j];
        }
    }

    int n, m;
    int[] head;
    int[] nx;
    int[] dst;
    boolean[] z;
    int last;
    int[] p;
    int pn;

    BufferedReader br;
    PrintWriter out;

    StringTokenizer st;

    String next() throws IOException {
        while (st == null || !st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }

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

    public static void main(String[] args) throws IOException {
        new D().run();
    }

    private void run() throws IOException {
        solve();
        out.close();
    }

}


import java.io.*;
import java.lang.management.MemoryUsage;
import java.util.*;

/**
 * @author Pavel Mavrin
 */
public class D {

    private static final int BUBEN = 500;
    private static final int SIZE = 10000000;

    public D() throws FileNotFoundException {
        br = new BufferedReader(new InputStreamReader(System.in));
        out = new PrintWriter(System.out);
//        br = new BufferedReader(new FileReader("input.txt"));
//        out = new PrintWriter("output.txt");
    }

    private void solve() throws IOException {
        int n = nextInt();
        int m = nextInt();
        List<Edge>[] byCol = new List[m];
        for (int i = 0; i < m; i++) {
            int x = nextInt() - 1;
            int y = nextInt() - 1;
            int c = nextInt() - 1;
            if (byCol[c] == null) {
                byCol[c] = new ArrayList<Edge>();
            }
            byCol[c].add(new Edge(x, y));
        }
        Arrays.sort(byCol, new Comparator<List<Edge>>() {
            public int compare(List<Edge> o1, List<Edge> o2) {
                int a = o1 == null ? 0 : o1.size();
                int b = o2 == null ? 0 : o2.size();
                return Integer.compare(a, b);
            }
        });
        init(n, BUBEN);
        int[] r = new int[2 * BUBEN];
        int rn;
        Map<Pair, Integer> small = new HashMap<Pair, Integer>();
        List<int[]> big = new ArrayList<int[]>();
        int cc = 0;
        for (List<Edge> edges : byCol) {
            if (edges == null) continue;
            if (edges.size() < BUBEN) {
                rn = 0;
                for (Edge edge : edges) {
                    r[rn++] = edge.x;
                    r[rn++] = edge.y;
                }
                reinit(r, rn);
                for (Edge edge : edges) {
                    addEdge(edge.x, edge.y);
                }
                for (int i = 0; i < rn; i++) {
                    int x = r[i];
                    if (!z[x]) {
                        pn = 0;
                        dfs(x);
                        for (int ii = 0; ii < pn; ii++) {
                            for (int jj = ii + 1; jj < pn; jj++) {
//                                Pair pair = new Pair(p[ii], p[jj]);
                                cc++;
                                add(p[ii], p[jj]);
//                                Integer q = small.get(pair);
//                                if (q == null) {
//                                    small.put(pair, 1);
//                                } else {
//                                    small.put(pair, q + 1);
//                                }
                            }
                        }
                    }
                }
            } else {
                int[] p = new int[n];
                for (int i = 0; i < n; i++) {
                    p[i] = i;
                }
                for (Edge edge : edges) {
                    join(p, edge.x, edge.y);
                }
                big.add(p);
            }
        }
        int q = nextInt();
        for (int i = 0; i < q; i++) {
            int u = nextInt() - 1;
            int v = nextInt() - 1;
            int res = get(u, v);
            for (int[] p : big) {
                if (get(p, u) == get(p, v)) {
                    res++;
                }
            }
            out.println(res);
        }
//        System.out.println(cc);
//        System.out.println(big.size() + " " + small.size());
//        System.out.println(MemoryUsage.);
    }

    Random random = new Random();

    private void join(int[] p, int x, int y) {
        x = get(p, x);
        y = get(p, y);
        if (random.nextBoolean()) p[x] = y;
        else p[y] = x;
    }

    private int get(int[] p, int x) {
        if (p[x] == x) return x;
        p[x] = get(p, p[x]);
        return p[x];
    }

    int[] uu = new int[SIZE];
    int[] vv = new int[SIZE];
    int[] zz = new int[SIZE];

    void add(int u, int v) {
        if (u > v) {
            int t = u; u = v; v = t;
        }
        int h = hash(u, v);
        while (uu[h] > 0 || vv[h] > 0) {
            if (uu[h] == u && vv[h] == v) {
                zz[h]++;
                return;
            }
            h++;
            if (h == SIZE) h = 0;
        }
        uu[h] = u;
        vv[h] = v;
        zz[h] = 1;
    }

    int get(int u, int v) {
        if (u > v) {
            int t = u; u = v; v = t;
        }
        int h = hash(u, v);
        while (uu[h] > 0 || vv[h] > 0) {
            if (uu[h] == u && vv[h] == v) {
                return zz[h];
            }
            h++;
            if (h == SIZE) h = 0;
        }
        return 0;
    }

    private int hash(int u, int v) {
        return ((u * 31 + v) * 31) % SIZE;
    }

    class Pair {
        int x, y;

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

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;

            Pair pair = (Pair) o;

            if (x != pair.x) return false;
            if (y != pair.y) return false;

            return true;
        }

        @Override
        public int hashCode() {
            int result = x;
            result = 31 * result + y;
            return result;
        }
    }

    class Edge {
        int x, y;

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

    void init(int n, int m) {
        m *= 2;
        this.n = n;
        this.m = m;
        last = 0;
        head = new int[n];
        nx = new int[m];
        dst = new int[m];
        Arrays.fill(head, -1);
        z = new boolean[n];
        p = new int[n];
    }

    void reinit(int[] r, int rn) {
        for (int i = 0; i < rn; i++) {
            head[r[i]] = -1;
            z[r[i]] = false;
        }
        last = 0;
    }

    void addEdge(int x, int y) {
        nx[last] = head[x];
        dst[last] = y;
        head[x] = last;
        last++;
        nx[last] = head[y];
        dst[last] = x;
        head[y] = last;
        last++;
    }

    private void dfs(int x) {
        if (z[x]) return;
        p[pn++] = x;
        z[x] = true;
        int j = head[x];
        while (j >= 0) {
            int y = dst[j];
            dfs(y);
            j = nx[j];
        }
    }

    int n, m;
    int[] head;
    int[] nx;
    int[] dst;
    boolean[] z;
    int last;
    int[] p;
    int pn;

    BufferedReader br;
    PrintWriter out;

    StringTokenizer st;

    String next() throws IOException {
        while (st == null || !st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }

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

    public static void main(String[] args) throws IOException {
        new D().run();
    }

    private void run() throws IOException {
        solve();
        out.close();
    }

}


import java.io.*;
import java.lang.management.MemoryUsage;
import java.util.*;

/**
 * @author Pavel Mavrin
 */
public class D {

    private static final int BUBEN = 1000;
    private static final int SIZE = 10000000;

    public D() throws FileNotFoundException {
        br = new BufferedReader(new InputStreamReader(System.in));
        out = new PrintWriter(System.out);
//        br = new BufferedReader(new FileReader("input.txt"));
//        out = new PrintWriter("output.txt");
    }

    private void solve() throws IOException {
        int n = nextInt();
        int m = nextInt();
        List<Edge>[] byCol = new List[m];
        for (int i = 0; i < m; i++) {
            int x = nextInt() - 1;
            int y = nextInt() - 1;
            int c = nextInt() - 1;
            if (byCol[c] == null) {
                byCol[c] = new ArrayList<Edge>();
            }
            byCol[c].add(new Edge(x, y));
        }
        Arrays.sort(byCol, new Comparator<List<Edge>>() {
            public int compare(List<Edge> o1, List<Edge> o2) {
                int a = o1 == null ? 0 : o1.size();
                int b = o2 == null ? 0 : o2.size();
                return Integer.compare(a, b);
            }
        });
        init(n, BUBEN);
        int[] r = new int[2 * BUBEN];
        int rn;
        Map<Pair, Integer> small = new HashMap<Pair, Integer>();
        List<int[]> big = new ArrayList<int[]>();
        int cc = 0;
        for (List<Edge> edges : byCol) {
            if (edges == null) continue;
            if (edges.size() < BUBEN) {
                rn = 0;
                for (Edge edge : edges) {
                    r[rn++] = edge.x;
                    r[rn++] = edge.y;
                }
                reinit(r, rn);
                for (Edge edge : edges) {
                    addEdge(edge.x, edge.y);
                }
                for (int i = 0; i < rn; i++) {
                    int x = r[i];
                    if (!z[x]) {
                        pn = 0;
                        dfs(x);
                        for (int ii = 0; ii < pn; ii++) {
                            for (int jj = ii + 1; jj < pn; jj++) {
//                                Pair pair = new Pair(p[ii], p[jj]);
                                cc++;
                                add(p[ii], p[jj]);
//                                Integer q = small.get(pair);
//                                if (q == null) {
//                                    small.put(pair, 1);
//                                } else {
//                                    small.put(pair, q + 1);
//                                }
                            }
                        }
                    }
                }
            } else {
                int[] p = new int[n];
                for (int i = 0; i < n; i++) {
                    p[i] = i;
                }
                for (Edge edge : edges) {
                    join(p, edge.x, edge.y);
                }
                big.add(p);
            }
        }
        int q = nextInt();
        for (int i = 0; i < q; i++) {
            int u = nextInt() - 1;
            int v = nextInt() - 1;
            int res = get(u, v);
            for (int[] p : big) {
                if (get(p, u) == get(p, v)) {
                    res++;
                }
            }
            out.println(res);
        }
//        System.out.println(cc);
//        System.out.println(big.size() + " " + small.size());
//        System.out.println(MemoryUsage.);
    }

    Random random = new Random();

    private void join(int[] p, int x, int y) {
        x = get(p, x);
        y = get(p, y);
        if (random.nextBoolean()) p[x] = y;
        else p[y] = x;
    }

    private int get(int[] p, int x) {
        if (p[x] == x) return x;
        p[x] = get(p, p[x]);
        return p[x];
    }

    int[] uu = new int[SIZE];
    int[] vv = new int[SIZE];
    int[] zz = new int[SIZE];

    void add(int u, int v) {
        if (u > v) {
            int t = u; u = v; v = t;
        }
        int h = hash(u, v);
        while (uu[h] > 0 || vv[h] > 0) {
            if (uu[h] == u && vv[h] == v) {
                zz[h]++;
                return;
            }
            h++;
            if (h == SIZE) h = 0;
        }
        uu[h] = u;
        vv[h] = v;
        zz[h] = 1;
    }

    int get(int u, int v) {
        if (u > v) {
            int t = u; u = v; v = t;
        }
        int h = hash(u, v);
        while (uu[h] > 0 || vv[h] > 0) {
            if (uu[h] == u && vv[h] == v) {
                return zz[h];
            }
            h++;
            if (h == SIZE) h = 0;
        }
        return 0;
    }

    private int hash(int u, int v) {
        return ((u * 31 + v) * 31) % SIZE;
    }

    class Pair {
        int x, y;

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

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;

            Pair pair = (Pair) o;

            if (x != pair.x) return false;
            if (y != pair.y) return false;

            return true;
        }

        @Override
        public int hashCode() {
            int result = x;
            result = 31 * result + y;
            return result;
        }
    }

    class Edge {
        int x, y;

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

    void init(int n, int m) {
        m *= 2;
        this.n = n;
        this.m = m;
        last = 0;
        head = new int[n];
        nx = new int[m];
        dst = new int[m];
        Arrays.fill(head, -1);
        z = new boolean[n];
        p = new int[n];
    }

    void reinit(int[] r, int rn) {
        for (int i = 0; i < rn; i++) {
            head[r[i]] = -1;
            z[r[i]] = false;
        }
        last = 0;
    }

    void addEdge(int x, int y) {
        nx[last] = head[x];
        dst[last] = y;
        head[x] = last;
        last++;
        nx[last] = head[y];
        dst[last] = x;
        head[y] = last;
        last++;
    }

    private void dfs(int x) {
        if (z[x]) return;
        p[pn++] = x;
        z[x] = true;
        int j = head[x];
        while (j >= 0) {
            int y = dst[j];
            dfs(y);
            j = nx[j];
        }
    }

    int n, m;
    int[] head;
    int[] nx;
    int[] dst;
    boolean[] z;
    int last;
    int[] p;
    int pn;

    BufferedReader br;
    PrintWriter out;

    StringTokenizer st;

    String next() throws IOException {
        while (st == null || !st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }

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

    public static void main(String[] args) throws IOException {
        new D().run();
    }

    private void run() throws IOException {
        solve();
        out.close();
    }

}


import java.io.*;
import java.lang.management.MemoryUsage;
import java.util.*;

/**
 * @author Pavel Mavrin
 */
public class D {

    private static final int BUBEN = 2000;
    private static final int SIZE = 10000000;

    public D() throws FileNotFoundException {
        br = new BufferedReader(new InputStreamReader(System.in));
        out = new PrintWriter(System.out);
//        br = new BufferedReader(new FileReader("input.txt"));
//        out = new PrintWriter("output.txt");
    }

    private void solve() throws IOException {
        int n = nextInt();
        int m = nextInt();
        List<Edge>[] byCol = new List[m];
        for (int i = 0; i < m; i++) {
            int x = nextInt() - 1;
            int y = nextInt() - 1;
            int c = nextInt() - 1;
            if (byCol[c] == null) {
                byCol[c] = new ArrayList<Edge>();
            }
            byCol[c].add(new Edge(x, y));
        }
        Arrays.sort(byCol, new Comparator<List<Edge>>() {
            public int compare(List<Edge> o1, List<Edge> o2) {
                int a = o1 == null ? 0 : o1.size();
                int b = o2 == null ? 0 : o2.size();
                return Integer.compare(a, b);
            }
        });
        init(n, BUBEN);
        int[] r = new int[2 * BUBEN];
        int rn;
        Map<Pair, Integer> small = new HashMap<Pair, Integer>();
        List<int[]> big = new ArrayList<int[]>();
        int cc = 0;
        for (List<Edge> edges : byCol) {
            if (edges == null) continue;
            if (edges.size() < BUBEN) {
                rn = 0;
                for (Edge edge : edges) {
                    r[rn++] = edge.x;
                    r[rn++] = edge.y;
                }
                reinit(r, rn);
                for (Edge edge : edges) {
                    addEdge(edge.x, edge.y);
                }
                for (int i = 0; i < rn; i++) {
                    int x = r[i];
                    if (!z[x]) {
                        pn = 0;
                        dfs(x);
                        for (int ii = 0; ii < pn; ii++) {
                            for (int jj = ii + 1; jj < pn; jj++) {
//                                Pair pair = new Pair(p[ii], p[jj]);
                                cc++;
                                add(p[ii], p[jj]);
//                                Integer q = small.get(pair);
//                                if (q == null) {
//                                    small.put(pair, 1);
//                                } else {
//                                    small.put(pair, q + 1);
//                                }
                            }
                        }
                    }
                }
            } else {
                int[] p = new int[n];
                for (int i = 0; i < n; i++) {
                    p[i] = i;
                }
                for (Edge edge : edges) {
                    join(p, edge.x, edge.y);
                }
                big.add(p);
            }
        }
        int q = nextInt();
        for (int i = 0; i < q; i++) {
            int u = nextInt() - 1;
            int v = nextInt() - 1;
            int res = get(u, v);
            for (int[] p : big) {
                if (get(p, u) == get(p, v)) {
                    res++;
                }
            }
            out.println(res);
        }
//        System.out.println(cc);
//        System.out.println(big.size() + " " + small.size());
//        System.out.println(MemoryUsage.);
    }

    Random random = new Random();

    private void join(int[] p, int x, int y) {
        x = get(p, x);
        y = get(p, y);
        if (random.nextBoolean()) p[x] = y;
        else p[y] = x;
    }

    private int get(int[] p, int x) {
        if (p[x] == x) return x;
        p[x] = get(p, p[x]);
        return p[x];
    }

    int[] uu = new int[SIZE];
    int[] vv = new int[SIZE];
    int[] zz = new int[SIZE];

    void add(int u, int v) {
        if (u > v) {
            int t = u; u = v; v = t;
        }
        int h = hash(u, v);
        while (uu[h] > 0 || vv[h] > 0) {
            if (uu[h] == u && vv[h] == v) {
                zz[h]++;
                return;
            }
            h++;
            if (h == SIZE) h = 0;
        }
        uu[h] = u;
        vv[h] = v;
        zz[h] = 1;
    }

    int get(int u, int v) {
        if (u > v) {
            int t = u; u = v; v = t;
        }
        int h = hash(u, v);
        while (uu[h] > 0 || vv[h] > 0) {
            if (uu[h] == u && vv[h] == v) {
                return zz[h];
            }
            h++;
            if (h == SIZE) h = 0;
        }
        return 0;
    }

    private int hash(int u, int v) {
        return ((u * 31 + v) * 31) % SIZE;
    }

    class Pair {
        int x, y;

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

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;

            Pair pair = (Pair) o;

            if (x != pair.x) return false;
            if (y != pair.y) return false;

            return true;
        }

        @Override
        public int hashCode() {
            int result = x;
            result = 31 * result + y;
            return result;
        }
    }

    class Edge {
        int x, y;

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

    void init(int n, int m) {
        m *= 2;
        this.n = n;
        this.m = m;
        last = 0;
        head = new int[n];
        nx = new int[m];
        dst = new int[m];
        Arrays.fill(head, -1);
        z = new boolean[n];
        p = new int[n];
    }

    void reinit(int[] r, int rn) {
        for (int i = 0; i < rn; i++) {
            head[r[i]] = -1;
            z[r[i]] = false;
        }
        last = 0;
    }

    void addEdge(int x, int y) {
        nx[last] = head[x];
        dst[last] = y;
        head[x] = last;
        last++;
        nx[last] = head[y];
        dst[last] = x;
        head[y] = last;
        last++;
    }

    private void dfs(int x) {
        if (z[x]) return;
        p[pn++] = x;
        z[x] = true;
        int j = head[x];
        while (j >= 0) {
            int y = dst[j];
            dfs(y);
            j = nx[j];
        }
    }

    int n, m;
    int[] head;
    int[] nx;
    int[] dst;
    boolean[] z;
    int last;
    int[] p;
    int pn;

    BufferedReader br;
    PrintWriter out;

    StringTokenizer st;

    String next() throws IOException {
        while (st == null || !st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }

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

    public static void main(String[] args) throws IOException {
        new D().run();
    }

    private void run() throws IOException {
        solve();
        out.close();
    }

}


import java.io.*;
import java.util.*;

/**
 * @author Pavel Mavrin
 */
public class D {

    private static final int BUBEN = 2000;
    private static final int SIZE = 15000000;
    private int mapSize;

    public D() throws FileNotFoundException {
        br = new BufferedReader(new InputStreamReader(System.in));
        out = new PrintWriter(System.out);
//        br = new BufferedReader(new FileReader("input.txt"));
//        out = new PrintWriter("output.txt");
    }

    private void solve() throws IOException {
        int n = nextInt();
        int m = nextInt();
        List<Edge>[] byCol = new List[m];
        for (int i = 0; i < m; i++) {
            int x = nextInt() - 1;
            int y = nextInt() - 1;
            int c = nextInt() - 1;
            if (byCol[c] == null) {
                byCol[c] = new ArrayList<Edge>();
            }
            byCol[c].add(new Edge(x, y));
        }
        Arrays.sort(byCol, new Comparator<List<Edge>>() {
            public int compare(List<Edge> o1, List<Edge> o2) {
                int a = o1 == null ? 0 : o1.size();
                int b = o2 == null ? 0 : o2.size();
                return Integer.compare(a, b);
            }
        });
        init(n, BUBEN);
        int[] r = new int[2 * BUBEN];
        int rn;
        Map<Pair, Integer> small = new HashMap<Pair, Integer>();
        List<int[]> big = new ArrayList<int[]>();
        int cc = 0;
        for (List<Edge> edges : byCol) {
            if (edges == null) continue;
            if (edges.size() < BUBEN) {
                rn = 0;
                for (Edge edge : edges) {
                    r[rn++] = edge.x;
                    r[rn++] = edge.y;
                }
                reinit(r, rn);
                for (Edge edge : edges) {
                    addEdge(edge.x, edge.y);
                }
                for (int i = 0; i < rn; i++) {
                    int x = r[i];
                    if (!z[x]) {
                        pn = 0;
                        dfs(x);
                        for (int ii = 0; ii < pn; ii++) {
                            for (int jj = ii + 1; jj < pn; jj++) {
//                                Pair pair = new Pair(p[ii], p[jj]);
                                cc++;
                                add(p[ii], p[jj]);
//                                Integer q = small.get(pair);
//                                if (q == null) {
//                                    small.put(pair, 1);
//                                } else {
//                                    small.put(pair, q + 1);
//                                }
                            }
                        }
                    }
                }
            } else {
                int[] p = new int[n];
                for (int i = 0; i < n; i++) {
                    p[i] = i;
                }
                for (Edge edge : edges) {
                    join(p, edge.x, edge.y);
                }
                big.add(p);
            }
        }
        int q = nextInt();
        for (int i = 0; i < q; i++) {
            int u = nextInt() - 1;
            int v = nextInt() - 1;
            int res = get(u, v);
            for (int[] p : big) {
                if (get(p, u) == get(p, v)) {
                    res++;
                }
            }
            out.println(res);
        }
//        System.out.println(cc);
//        System.out.println(big.size() + " " + small.size());
//        System.out.println(MemoryUsage.);
    }

    Random random = new Random();

    private void join(int[] p, int x, int y) {
        x = get(p, x);
        y = get(p, y);
        if (random.nextBoolean()) p[x] = y;
        else p[y] = x;
    }

    private int get(int[] p, int x) {
        if (p[x] == x) return x;
        p[x] = get(p, p[x]);
        return p[x];
    }

    int[] ww = new int[SIZE];
    int[] zz = new int[SIZE];

    void add(int u, int v) {
        if (u > v) {
            int t = u; u = v; v = t;
        }
        int h = hash(u, v);
        int h2 = hash2(u, v);
        while (ww[h] != 0) {
            if (ww[h] == h2) {
                zz[h]++;
                return;
            }
            h++;
            if (h == SIZE) h = 0;
        }
        ww[h] = h2;
        mapSize++;
        if (mapSize > SIZE * 0.8) throw new RuntimeException();

        zz[h] = 1;
    }

    int get(int u, int v) {
        if (u > v) {
            int t = u; u = v; v = t;
        }
        int h = hash(u, v);
        int h2 = hash2(u, v);
        while (ww[h] != 0) {
            if (ww[h] == h2) {
                return zz[h];
            }
            h++;
            if (h == SIZE) h = 0;
        }
        return 0;
    }

    private int hash(int u, int v) {
        return ((u * 239 + v) * 239) % SIZE;
    }

    private int hash2(int u, int v) {
        return ((u * 239239 + v) * 239239);
    }

    class Pair {
        int x, y;

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

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;

            Pair pair = (Pair) o;

            if (x != pair.x) return false;
            if (y != pair.y) return false;

            return true;
        }

        @Override
        public int hashCode() {
            int result = x;
            result = 31 * result + y;
            return result;
        }
    }

    class Edge {
        int x, y;

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

    void init(int n, int m) {
        m *= 2;
        this.n = n;
        this.m = m;
        last = 0;
        head = new int[n];
        nx = new int[m];
        dst = new int[m];
        Arrays.fill(head, -1);
        z = new boolean[n];
        p = new int[n];
    }

    void reinit(int[] r, int rn) {
        for (int i = 0; i < rn; i++) {
            head[r[i]] = -1;
            z[r[i]] = false;
        }
        last = 0;
    }

    void addEdge(int x, int y) {
        nx[last] = head[x];
        dst[last] = y;
        head[x] = last;
        last++;
        nx[last] = head[y];
        dst[last] = x;
        head[y] = last;
        last++;
    }

    private void dfs(int x) {
        if (z[x]) return;
        p[pn++] = x;
        z[x] = true;
        int j = head[x];
        while (j >= 0) {
            int y = dst[j];
            dfs(y);
            j = nx[j];
        }
    }

    int n, m;
    int[] head;
    int[] nx;
    int[] dst;
    boolean[] z;
    int last;
    int[] p;
    int pn;

    BufferedReader br;
    PrintWriter out;

    StringTokenizer st;

    String next() throws IOException {
        while (st == null || !st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }

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

    public static void main(String[] args) throws IOException {
        new D().run();
    }

    private void run() throws IOException {
        solve();
        out.close();
    }

}


import java.io.*;
import java.lang.management.MemoryUsage;
import java.util.*;

/**
 * @author Pavel Mavrin
 */
public class D {

    private static final int BUBEN = 1500;
    private static final int SIZE = 10000000;

    public D() throws FileNotFoundException {
        br = new BufferedReader(new InputStreamReader(System.in));
        out = new PrintWriter(System.out);
//        br = new BufferedReader(new FileReader("input.txt"));
//        out = new PrintWriter("output.txt");
    }

    private void solve() throws IOException {
        int n = nextInt();
        int m = nextInt();
        List<Edge>[] byCol = new List[m];
        for (int i = 0; i < m; i++) {
            int x = nextInt() - 1;
            int y = nextInt() - 1;
            int c = nextInt() - 1;
            if (byCol[c] == null) {
                byCol[c] = new ArrayList<Edge>();
            }
            byCol[c].add(new Edge(x, y));
        }
        Arrays.sort(byCol, new Comparator<List<Edge>>() {
            public int compare(List<Edge> o1, List<Edge> o2) {
                int a = o1 == null ? 0 : o1.size();
                int b = o2 == null ? 0 : o2.size();
                return Integer.compare(a, b);
            }
        });
        init(n, BUBEN);
        int[] r = new int[2 * BUBEN];
        int rn;
        Map<Pair, Integer> small = new HashMap<Pair, Integer>();
        List<int[]> big = new ArrayList<int[]>();
        int cc = 0;
        for (List<Edge> edges : byCol) {
            if (edges == null) continue;
            if (edges.size() < BUBEN) {
                rn = 0;
                for (Edge edge : edges) {
                    r[rn++] = edge.x;
                    r[rn++] = edge.y;
                }
                reinit(r, rn);
                for (Edge edge : edges) {
                    addEdge(edge.x, edge.y);
                }
                for (int i = 0; i < rn; i++) {
                    int x = r[i];
                    if (!z[x]) {
                        pn = 0;
                        dfs(x);
                        for (int ii = 0; ii < pn; ii++) {
                            for (int jj = ii + 1; jj < pn; jj++) {
//                                Pair pair = new Pair(p[ii], p[jj]);
                                cc++;
                                add(p[ii], p[jj]);
//                                Integer q = small.get(pair);
//                                if (q == null) {
//                                    small.put(pair, 1);
//                                } else {
//                                    small.put(pair, q + 1);
//                                }
                            }
                        }
                    }
                }
            } else {
                int[] p = new int[n];
                for (int i = 0; i < n; i++) {
                    p[i] = i;
                }
                for (Edge edge : edges) {
                    join(p, edge.x, edge.y);
                }
                big.add(p);
            }
        }
        int q = nextInt();
        for (int i = 0; i < q; i++) {
            int u = nextInt() - 1;
            int v = nextInt() - 1;
            int res = get(u, v);
            for (int[] p : big) {
                if (get(p, u) == get(p, v)) {
                    res++;
                }
            }
            out.println(res);
        }
//        System.out.println(cc);
//        System.out.println(big.size() + " " + small.size());
//        System.out.println(MemoryUsage.);
    }

    Random random = new Random();

    private void join(int[] p, int x, int y) {
        x = get(p, x);
        y = get(p, y);
        if (random.nextBoolean()) p[x] = y;
        else p[y] = x;
    }

    private int get(int[] p, int x) {
        if (p[x] == x) return x;
        p[x] = get(p, p[x]);
        return p[x];
    }

    int[] uu = new int[SIZE];
    int[] vv = new int[SIZE];
    int[] zz = new int[SIZE];

    void add(int u, int v) {
        if (u > v) {
            int t = u; u = v; v = t;
        }
        int h = hash(u, v);
        while (uu[h] > 0 || vv[h] > 0) {
            if (uu[h] == u && vv[h] == v) {
                zz[h]++;
                return;
            }
            h++;
            if (h == SIZE) h = 0;
        }
        uu[h] = u;
        vv[h] = v;
        zz[h] = 1;
    }

    int get(int u, int v) {
        if (u > v) {
            int t = u; u = v; v = t;
        }
        int h = hash(u, v);
        while (uu[h] > 0 || vv[h] > 0) {
            if (uu[h] == u && vv[h] == v) {
                return zz[h];
            }
            h++;
            if (h == SIZE) h = 0;
        }
        return 0;
    }

    private int hash(int u, int v) {
        return ((u * 31 + v) * 31) % SIZE;
    }

    class Pair {
        int x, y;

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

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;

            Pair pair = (Pair) o;

            if (x != pair.x) return false;
            if (y != pair.y) return false;

            return true;
        }

        @Override
        public int hashCode() {
            int result = x;
            result = 31 * result + y;
            return result;
        }
    }

    class Edge {
        int x, y;

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

    void init(int n, int m) {
        m *= 2;
        this.n = n;
        this.m = m;
        last = 0;
        head = new int[n];
        nx = new int[m];
        dst = new int[m];
        Arrays.fill(head, -1);
        z = new boolean[n];
        p = new int[n];
    }

    void reinit(int[] r, int rn) {
        for (int i = 0; i < rn; i++) {
            head[r[i]] = -1;
            z[r[i]] = false;
        }
        last = 0;
    }

    void addEdge(int x, int y) {
        nx[last] = head[x];
        dst[last] = y;
        head[x] = last;
        last++;
        nx[last] = head[y];
        dst[last] = x;
        head[y] = last;
        last++;
    }

    private void dfs(int x) {
        if (z[x]) return;
        p[pn++] = x;
        z[x] = true;
        int j = head[x];
        while (j >= 0) {
            int y = dst[j];
            dfs(y);
            j = nx[j];
        }
    }

    int n, m;
    int[] head;
    int[] nx;
    int[] dst;
    boolean[] z;
    int last;
    int[] p;
    int pn;

    BufferedReader br;
    PrintWriter out;

    StringTokenizer st;

    String next() throws IOException {
        while (st == null || !st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }

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

    public static void main(String[] args) throws IOException {
        new D().run();
    }

    private void run() throws IOException {
        solve();
        out.close();
    }

}


import java.io.*;
import java.util.*;

/**
 * @author Pavel Mavrin
 */
public class D {

    private static final int BUBEN = 2000;

    public D() throws FileNotFoundException {
        br = new BufferedReader(new InputStreamReader(System.in));
        out = new PrintWriter(System.out);
//        br = new BufferedReader(new FileReader("input.txt"));
//        out = new PrintWriter("output.txt");
    }

    private void solve() throws IOException {
        int n = nextInt();
        int m = nextInt();
        List<Edge>[] byCol = new List[m];
        for (int i = 0; i < m; i++) {
            int x = nextInt() - 1;
            int y = nextInt() - 1;
            int c = nextInt() - 1;
            if (byCol[c] == null) {
                byCol[c] = new ArrayList<Edge>();
            }
            byCol[c].add(new Edge(x, y));
        }
        Arrays.sort(byCol, new Comparator<List<Edge>>() {
            public int compare(List<Edge> o1, List<Edge> o2) {
                int a = o1 == null ? 0 : o1.size();
                int b = o2 == null ? 0 : o2.size();
                return Integer.compare(a, b);
            }
        });
        init(n, BUBEN);
        int[] r = new int[2 * BUBEN];
        int rn;
        Map<Pair, Integer> small = new HashMap<Pair, Integer>();
        List<int[]> big = new ArrayList<int[]>();
        for (List<Edge> edges : byCol) {
            if (edges == null) continue;
            if (edges.size() < BUBEN) {
                rn = 0;
                for (Edge edge : edges) {
                    r[rn++] = edge.x;
                    r[rn++] = edge.y;
                }
                reinit(r, rn);
                for (Edge edge : edges) {
                    addEdge(edge.x, edge.y);
                }
                for (int i = 0; i < rn; i++) {
                    int x = r[i];
                    if (!z[x]) {
                        pn = 0;
                        dfs(x);
                        for (int ii = 0; ii < pn; ii++) {
                            for (int jj = 0; jj < pn; jj++) {
                                Pair pair = new Pair(p[ii], p[jj]);
                                Integer q = small.get(pair);
                                if (q == null) {
                                    small.put(pair, 1);
                                } else {
                                    small.put(pair, q + 1);
                                }
                            }
                        }
                    }
                }
            } else {
                int[] p = new int[n];
                for (int i = 0; i < n; i++) {
                    p[i] = i;
                }
                for (Edge edge : edges) {
                    join(p, edge.x, edge.y);
                }
                big.add(p);
            }
        }
        int q = nextInt();
        for (int i = 0; i < q; i++) {
            int u = nextInt() - 1;
            int v = nextInt() - 1;
            Pair pair = new Pair(u, v);
            int res = 0;
            {
                Integer ii = small.get(pair);
                if (ii != null) res = ii;
            }
            for (int[] p : big) {
                if (get(p, u) == get(p, v)) {
                    res++;
                }
            }
            out.println(res);
        }
//        System.out.println(big.size() + " " + small.size());
    }

    Random random = new Random();

    private void join(int[] p, int x, int y) {
        x = get(p, x);
        y = get(p, y);
        if (random.nextBoolean()) p[x] = y;
        else p[y] = x;
    }

    private int get(int[] p, int x) {
        if (p[x] == x) return x;
        p[x] = get(p, p[x]);
        return p[x];
    }

    class Pair {
        int x, y;

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

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;

            Pair pair = (Pair) o;

            if (x != pair.x) return false;
            if (y != pair.y) return false;

            return true;
        }

        @Override
        public int hashCode() {
            int result = x;
            result = 31 * result + y;
            return result;
        }
    }

    class Edge {
        int x, y;

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

    void init(int n, int m) {
        m *= 2;
        this.n = n;
        this.m = m;
        last = 0;
        head = new int[n];
        nx = new int[m];
        dst = new int[m];
        Arrays.fill(head, -1);
        z = new boolean[n];
        p = new int[n];
    }

    void reinit(int[] r, int rn) {
        for (int i = 0; i < rn; i++) {
            head[r[i]] = -1;
            z[r[i]] = false;
        }
        last = 0;
    }

    void addEdge(int x, int y) {
        nx[last] = head[x];
        dst[last] = y;
        head[x] = last;
        last++;
        nx[last] = head[y];
        dst[last] = x;
        head[y] = last;
        last++;
    }

    private void dfs(int x) {
        if (z[x]) return;
        p[pn++] = x;
        z[x] = true;
        int j = head[x];
        while (j >= 0) {
            int y = dst[j];
            dfs(y);
            j = nx[j];
        }
    }

    int n, m;
    int[] head;
    int[] nx;
    int[] dst;
    boolean[] z;
    int last;
    int[] p;
    int pn;

    BufferedReader br;
    PrintWriter out;

    StringTokenizer st;

    String next() throws IOException {
        while (st == null || !st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }

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

    public static void main(String[] args) throws IOException {
        new D().run();
    }

    private void run() throws IOException {
        solve();
        out.close();
    }

}


import java.io.*;
import java.util.*;

/**
 * @author Pavel Mavrin
 */
public class D {

    private static final int BUBEN = 1000;
    private static final int SIZE = 10000000;
    private int mapSize;

    public D() throws FileNotFoundException {
        br = new BufferedReader(new InputStreamReader(System.in));
        out = new PrintWriter(System.out);
//        br = new BufferedReader(new FileReader("input.txt"));
//        out = new PrintWriter("output.txt");
    }

    private void solve() throws IOException {
        int n = nextInt();
        int m = nextInt();
        List<Edge>[] byCol = new List[m];
        for (int i = 0; i < m; i++) {
            int x = nextInt() - 1;
            int y = nextInt() - 1;
            int c = nextInt() - 1;
            if (byCol[c] == null) {
                byCol[c] = new ArrayList<Edge>();
            }
            byCol[c].add(new Edge(x, y));
        }
        Arrays.sort(byCol, new Comparator<List<Edge>>() {
            public int compare(List<Edge> o1, List<Edge> o2) {
                int a = o1 == null ? 0 : o1.size();
                int b = o2 == null ? 0 : o2.size();
                return Integer.compare(a, b);
            }
        });
        init(n, BUBEN);
        int[] r = new int[2 * BUBEN];
        int rn;
        Map<Pair, Integer> small = new HashMap<Pair, Integer>();
        List<int[]> big = new ArrayList<int[]>();
        int cc = 0;
        for (List<Edge> edges : byCol) {
            if (edges == null) continue;
            if (edges.size() < BUBEN) {
                rn = 0;
                for (Edge edge : edges) {
                    r[rn++] = edge.x;
                    r[rn++] = edge.y;
                }
                reinit(r, rn);
                for (Edge edge : edges) {
                    addEdge(edge.x, edge.y);
                }
                for (int i = 0; i < rn; i++) {
                    int x = r[i];
                    if (!z[x]) {
                        pn = 0;
                        dfs(x);
                        for (int ii = 0; ii < pn; ii++) {
                            for (int jj = ii + 1; jj < pn; jj++) {
//                                Pair pair = new Pair(p[ii], p[jj]);
                                cc++;
                                add(p[ii], p[jj]);
//                                Integer q = small.get(pair);
//                                if (q == null) {
//                                    small.put(pair, 1);
//                                } else {
//                                    small.put(pair, q + 1);
//                                }
                            }
                        }
                    }
                }
            } else {
                int[] p = new int[n];
                for (int i = 0; i < n; i++) {
                    p[i] = i;
                }
                for (Edge edge : edges) {
                    join(p, edge.x, edge.y);
                }
                big.add(p);
            }
        }
        int q = nextInt();
        for (int i = 0; i < q; i++) {
            int u = nextInt() - 1;
            int v = nextInt() - 1;
            int res = get(u, v);
            for (int[] p : big) {
                if (get(p, u) == get(p, v)) {
                    res++;
                }
            }
            out.println(res);
        }
//        System.out.println(cc);
//        System.out.println(big.size() + " " + small.size());
//        System.out.println(MemoryUsage.);
    }

    Random random = new Random();

    private void join(int[] p, int x, int y) {
        x = get(p, x);
        y = get(p, y);
        if (random.nextBoolean()) p[x] = y;
        else p[y] = x;
    }

    private int get(int[] p, int x) {
        if (p[x] == x) return x;
        p[x] = get(p, p[x]);
        return p[x];
    }

    int[] ww = new int[SIZE];
    int[] zz = new int[SIZE];

    void add(int u, int v) {
        if (u > v) {
            int t = u; u = v; v = t;
        }
        int h = hash(u, v);
        int h2 = hash2(u, v);
        while (ww[h] != 0) {
            if (ww[h] == h2) {
                zz[h]++;
                return;
            }
            h++;
            if (h == SIZE) h = 0;
        }
        ww[h] = h2;
        mapSize++;
        if (mapSize > SIZE * 0.8) throw new RuntimeException();

        zz[h] = 1;
    }

    int get(int u, int v) {
        if (u > v) {
            int t = u; u = v; v = t;
        }
        int h = hash(u, v);
        int h2 = hash2(u, v);
        while (ww[h] != 0) {
            if (ww[h] == h2) {
                return zz[h];
            }
            h++;
            if (h == SIZE) h = 0;
        }
        return 0;
    }

    private int hash(int u, int v) {
        return ((u * 239 + v) * 239) % SIZE;
    }

    private int hash2(int u, int v) {
        return ((u * 239239 + v) * 239239);
    }

    class Pair {
        int x, y;

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

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;

            Pair pair = (Pair) o;

            if (x != pair.x) return false;
            if (y != pair.y) return false;

            return true;
        }

        @Override
        public int hashCode() {
            int result = x;
            result = 31 * result + y;
            return result;
        }
    }

    class Edge {
        int x, y;

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

    void init(int n, int m) {
        m *= 2;
        this.n = n;
        this.m = m;
        last = 0;
        head = new int[n];
        nx = new int[m];
        dst = new int[m];
        Arrays.fill(head, -1);
        z = new boolean[n];
        p = new int[n];
    }

    void reinit(int[] r, int rn) {
        for (int i = 0; i < rn; i++) {
            head[r[i]] = -1;
            z[r[i]] = false;
        }
        last = 0;
    }

    void addEdge(int x, int y) {
        nx[last] = head[x];
        dst[last] = y;
        head[x] = last;
        last++;
        nx[last] = head[y];
        dst[last] = x;
        head[y] = last;
        last++;
    }

    private void dfs(int x) {
        if (z[x]) return;
        p[pn++] = x;
        z[x] = true;
        int j = head[x];
        while (j >= 0) {
            int y = dst[j];
            dfs(y);
            j = nx[j];
        }
    }

    int n, m;
    int[] head;
    int[] nx;
    int[] dst;
    boolean[] z;
    int last;
    int[] p;
    int pn;

    BufferedReader br;
    PrintWriter out;

    StringTokenizer st;

    String next() throws IOException {
        while (st == null || !st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }

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

    public static void main(String[] args) throws IOException {
        new D().run();
    }

    private void run() throws IOException {
        solve();
        out.close();
    }

}


import java.io.*;
import java.lang.management.MemoryUsage;
import java.util.*;

/**
 * @author Pavel Mavrin
 */
public class D {

    private static final int BUBEN = 200;
    private static final int SIZE = 10000000;

    public D() throws FileNotFoundException {
        br = new BufferedReader(new InputStreamReader(System.in));
        out = new PrintWriter(System.out);
//        br = new BufferedReader(new FileReader("input.txt"));
//        out = new PrintWriter("output.txt");
    }

    private void solve() throws IOException {
        int n = nextInt();
        int m = nextInt();
        List<Edge>[] byCol = new List[m];
        for (int i = 0; i < m; i++) {
            int x = nextInt() - 1;
            int y = nextInt() - 1;
            int c = nextInt() - 1;
            if (byCol[c] == null) {
                byCol[c] = new ArrayList<Edge>();
            }
            byCol[c].add(new Edge(x, y));
        }
        Arrays.sort(byCol, new Comparator<List<Edge>>() {
            public int compare(List<Edge> o1, List<Edge> o2) {
                int a = o1 == null ? 0 : o1.size();
                int b = o2 == null ? 0 : o2.size();
                return Integer.compare(a, b);
            }
        });
        init(n, BUBEN);
        int[] r = new int[2 * BUBEN];
        int rn;
        Map<Pair, Integer> small = new HashMap<Pair, Integer>();
        List<int[]> big = new ArrayList<int[]>();
        int cc = 0;
        for (List<Edge> edges : byCol) {
            if (edges == null) continue;
            if (edges.size() < BUBEN) {
                rn = 0;
                for (Edge edge : edges) {
                    r[rn++] = edge.x;
                    r[rn++] = edge.y;
                }
                reinit(r, rn);
                for (Edge edge : edges) {
                    addEdge(edge.x, edge.y);
                }
                for (int i = 0; i < rn; i++) {
                    int x = r[i];
                    if (!z[x]) {
                        pn = 0;
                        dfs(x);
                        for (int ii = 0; ii < pn; ii++) {
                            for (int jj = ii + 1; jj < pn; jj++) {
//                                Pair pair = new Pair(p[ii], p[jj]);
                                cc++;
                                add(p[ii], p[jj]);
//                                Integer q = small.get(pair);
//                                if (q == null) {
//                                    small.put(pair, 1);
//                                } else {
//                                    small.put(pair, q + 1);
//                                }
                            }
                        }
                    }
                }
            } else {
                int[] p = new int[n];
                for (int i = 0; i < n; i++) {
                    p[i] = i;
                }
                for (Edge edge : edges) {
                    join(p, edge.x, edge.y);
                }
                big.add(p);
            }
        }
        int q = nextInt();
        for (int i = 0; i < q; i++) {
            int u = nextInt() - 1;
            int v = nextInt() - 1;
            int res = get(u, v);
            for (int[] p : big) {
                if (get(p, u) == get(p, v)) {
                    res++;
                }
            }
            out.println(res);
        }
//        System.out.println(cc);
//        System.out.println(big.size() + " " + small.size());
//        System.out.println(MemoryUsage.);
    }

    Random random = new Random();

    private void join(int[] p, int x, int y) {
        x = get(p, x);
        y = get(p, y);
        if (random.nextBoolean()) p[x] = y;
        else p[y] = x;
    }

    private int get(int[] p, int x) {
        if (p[x] == x) return x;
        p[x] = get(p, p[x]);
        return p[x];
    }

    int[] uu = new int[SIZE];
    int[] vv = new int[SIZE];
    int[] zz = new int[SIZE];

    void add(int u, int v) {
        if (u > v) {
            int t = u; u = v; v = t;
        }
        int h = hash(u, v);
        while (uu[h] > 0 || vv[h] > 0) {
            if (uu[h] == u && vv[h] == v) {
                zz[h]++;
                return;
            }
            h++;
            if (h == SIZE) h = 0;
        }
        uu[h] = u;
        vv[h] = v;
        zz[h] = 1;
    }

    int get(int u, int v) {
        if (u > v) {
            int t = u; u = v; v = t;
        }
        int h = hash(u, v);
        while (uu[h] > 0 || vv[h] > 0) {
            if (uu[h] == u && vv[h] == v) {
                return zz[h];
            }
            h++;
            if (h == SIZE) h = 0;
        }
        return 0;
    }

    private int hash(int u, int v) {
        return ((u * 31 + v) * 31) % SIZE;
    }

    class Pair {
        int x, y;

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

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;

            Pair pair = (Pair) o;

            if (x != pair.x) return false;
            if (y != pair.y) return false;

            return true;
        }

        @Override
        public int hashCode() {
            int result = x;
            result = 31 * result + y;
            return result;
        }
    }

    class Edge {
        int x, y;

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

    void init(int n, int m) {
        m *= 2;
        this.n = n;
        this.m = m;
        last = 0;
        head = new int[n];
        nx = new int[m];
        dst = new int[m];
        Arrays.fill(head, -1);
        z = new boolean[n];
        p = new int[n];
    }

    void reinit(int[] r, int rn) {
        for (int i = 0; i < rn; i++) {
            head[r[i]] = -1;
            z[r[i]] = false;
        }
        last = 0;
    }

    void addEdge(int x, int y) {
        nx[last] = head[x];
        dst[last] = y;
        head[x] = last;
        last++;
        nx[last] = head[y];
        dst[last] = x;
        head[y] = last;
        last++;
    }

    private void dfs(int x) {
        if (z[x]) return;
        p[pn++] = x;
        z[x] = true;
        int j = head[x];
        while (j >= 0) {
            int y = dst[j];
            dfs(y);
            j = nx[j];
        }
    }

    int n, m;
    int[] head;
    int[] nx;
    int[] dst;
    boolean[] z;
    int last;
    int[] p;
    int pn;

    BufferedReader br;
    PrintWriter out;

    StringTokenizer st;

    String next() throws IOException {
        while (st == null || !st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }

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

    public static void main(String[] args) throws IOException {
        new D().run();
    }

    private void run() throws IOException {
        solve();
        out.close();
    }

}