Mr. Kitayuta has just bought an undirected graph with n vertices and m edges. The vertices of the graph are numbered from 1 to n. Each edge, namely edge i, has a color ci, connecting vertex ai and bi.

Mr. Kitayuta wants you to process the following q queries.

In the i-th query, he gives you two integers - ui and vi.

Find the number of the colors that satisfy the following condition: the edges of that color connect vertex ui and vertex vi directly or indirectly.

Input

The first line of the input contains space-separated two integers - n and m(2 ≤ n ≤ 105, 1 ≤ m ≤ 105), denoting the number of the vertices and the number of the edges, respectively.

The next m lines contain space-separated three integers - ai, bi(1 ≤ ai < bi ≤ n) and ci(1 ≤ ci ≤ m). Note that there can be multiple edges between two vertices. However, there are no multiple edges of the same color between two vertices, that is, if i ≠ j, (ai, bi, ci) ≠ (aj, bj, cj).

The next line contains a integer- q(1 ≤ q ≤ 105), denoting the number of the queries.

Then follows q lines, containing space-separated two integers - ui and vi(1 ≤ ui, vi ≤ n). It is guaranteed that ui ≠ vi.

Output

For each query, print the answer in a separate line.

Examples

Input

4 5
1 2 1
1 2 2
2 3 1
2 3 3
2 4 3
3
1 2
3 4
1 4


Output

2
1
0


Input

5 7
1 5 1
2 5 1
3 5 1
4 5 1
1 2 2
2 3 2
3 4 2
5
1 5
5 1
2 5
1 5
1 4


Output

1
1
1
1
2

Note

Let's consider the first sample. 

<image> The figure above shows the first sample. 

  * Vertex 1 and vertex 2 are connected by color 1 and 2. 
  * Vertex 3 and vertex 4 are connected by color 3. 
  * Vertex 1 and vertex 4 are not connected by any single color. 
// practice with rainboy
import java.io.*;
import java.util.*;

public class CF506D extends PrintWriter {
	CF506D() { super(System.out); }
	Scanner sc = new Scanner(System.in);
	public static void main(String[] $) {
		CF506D o = new CF506D(); o.main(); o.flush();
	}

	static final int N = 100000, M = 350;
	static class E {
		int i, j, c;
		E(int i, int j, int c) {
			this.i = i; this.j = j; this.c = c;
		}
	}

	int[] dsu;
	int find(int i) {
		return dsu[i] < 0 ? i : (dsu[i] = find(dsu[i]));
	}
	void join(int i, int j) {
		i = find(i);
		j = find(j);
		if (i == j)
			return;
		if (dsu[i] > dsu[j])
			dsu[i] = j;
		else {
			if (dsu[i] == dsu[j])
				dsu[i]--;
			dsu[j] = i;
		}
	}

	static Random rand = new Random();
	static class V {
		V l, r;
		long key;
		int val, x;
		V(long key, int val) {
			this.key = key; this.val = val;
			x = rand.nextInt();
		}
	}
	V v_, l_, r_;
	void split(V v, long key) {
		if (v == null) {
			v_ = l_ = r_ = null;
		} else if (v.key < key) {
			split(v.r, key);
			v.r = l_; l_ = v;
		} else if (v.key > key) {
			split(v.l, key);
			v.l = r_; r_ = v;
		} else {
			v_ = v; l_ = v.l; r_ = v.r;
			v.l = v.r = null;
		}
	}
	V merge(V u, V v) {
		if (u == null)
			return v;
		if (v == null)
			return u;
		if (u.x <= v.x) {
			u.r = merge(u.r, v);
			return u;
		} else {
			v.l = merge(u, v.l);
			return v;
		}
	}
	long compose(int i, int j) {
		if (i > j) {
			int tmp = i; i = j; j = tmp;
		}
		return (long) i * N + j;
	}
	void add(int i, int j) {
		long key = compose(i, j);
		split(v_, key);
		if (v_ == null)
			v_ = new V(key, 0);
		v_ = merge(l_, merge(v_, r_));
	}
	void update(int i, int j) {
		long key = compose(i, j);
		split(v_, key);
		if (v_ != null)
			v_.val++;
		v_ = merge(l_, merge(v_, r_));
	}
	int query(int i, int j) {
		long key = compose(i, j);
		split(v_, key);
		int val = v_.val;
		v_ = merge(l_, merge(v_, r_));
		return val;
	}

	void main() {
		int n = sc.nextInt();
		int m = sc.nextInt();
		E[] ee = new E[m];
		for (int h = 0; h < m; h++) {
			int i = sc.nextInt() - 1;
			int j = sc.nextInt() - 1;
			int c = sc.nextInt();
			ee[h] = new E(i, j, c);
		}
		Arrays.sort(ee, (e, f) -> e.c - f.c);
		int q = sc.nextInt();
		int[] ii = new int[q];
		int[] jj = new int[q];
		int[] ans = new int[q];
		for (int h = 0; h < q; h++) {
			int i = sc.nextInt() - 1;
			int j = sc.nextInt() - 1;
			ii[h] = i; jj[h] = j;
			add(i, j);
		}
		dsu = new int[n]; Arrays.fill(dsu, -1);
		boolean[] used = new boolean[n];
		int[] qq = new int[n];
		for (int h = 0, h_; h < m; h = h_) {
			h_ = h + 1;
			while (h_ < m && ee[h_].c == ee[h].c)
				h_++;
			int cnt = 0;
			for (int h1 = h; h1 < h_; h1++) {
				int i = ee[h1].i, j = ee[h1].j;
				join(i, j);
				if (!used[i]) {
					used[i] = true;
					qq[cnt++] = i;
				}
				if (!used[j]) {
					used[j] = true;
					qq[cnt++] = j;
				}
			}
			if (h_ - h < M)
				for (int h1 = 0; h1 < cnt; h1++)
					for (int h2 = h1 + 1; h2 < cnt; h2++) {
						int i = qq[h1], j = qq[h2];
						if (find(i) == find(j))
							update(i, j);
					}
			else
				for (int h1 = 0; h1 < q; h1++) {
					int i = ii[h1], j = jj[h1];
					if (find(i) == find(j))
						ans[h1]++;
				}
			for (int h1 = 0; h1 < cnt; h1++) {
				int i = qq[h1];
				used[i] = false;
				dsu[i] = -1;
			}
		}
		for (int h = 0; h < q; h++)
			println(ans[h] + query(ii[h], jj[h]));
	}
}
