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

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

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

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

Input

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

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

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

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

Output

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

Examples

Input

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


Output

2
1
0


Input

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


Output

1
1
1
1
2

Note

Let's consider the first sample. 

<image> The figure above shows the first sample. 

  * Vertex 1 and vertex 2 are connected by color 1 and 2. 
  * Vertex 3 and vertex 4 are connected by color 3. 
  * Vertex 1 and vertex 4 are not connected by any single color. 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Set;
import java.util.StringTokenizer;

public class Main {

	public static void main(String[] args) throws IOException {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		InputReader in = new InputReader(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		TaskB solver = new TaskB();
		solver.solve(1, in, out);
		out.close();
	}

}

class TaskD {

	public void solve(int testNumber, InputReader in, PrintWriter out) {
	}

}

class TaskA {

	public void solve(int testNumber, InputReader in, PrintWriter out) {
		String str = in.next();

		for (int i = 0; i <= str.length(); ++i) {
			for (char ch = 'a'; ch <= 'z'; ++ch) {

				if (isPolindrome(append(str, i, ch))) {
					out.println(append(str, i, ch));
					return;
				}
			}
		}
		out.println("NA");

	}

	private boolean isPolindrome(String append) {
		int size = append.length();
		int l = 0;
		int r = 0;
		if (size % 2 == 1) {
			l = size / 2;
			r = size / 2;
		} else {
			l = size / 2 - 1;
			r = size / 2;
		}

		while (l >= 0) {
			if (append.charAt(l) != append.charAt(r)) {
				return false;
			}
			l--;
			r++;
		}
		if (l >= 0) {
			return false;
		}

		return true;
	}

	private String append(String str, int index, char ch) {
		if (index == str.length()) {
			return str + ch;
		}
		char[] nstr = new char[str.length() + 1];
		nstr[index] = ch;
		for (int i = 0; i < index; ++i) {
			nstr[i] = str.charAt(i);
		}

		for (int i = index + 1; i < nstr.length; ++i) {
			nstr[i] = str.charAt(i - 1);
		}

		return new String(nstr);
	}

}

class TaskB {

	public void solve(int testNumber, InputReader in, PrintWriter out)
			throws IOException {

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

		List<Integer>[][] adj = new List[n][m];
		for (int i = 0; i < n; ++i) {		
			for (int j = 0; j < m; ++j) {
				adj[i][j] = new LinkedList<Integer>();
			}
		}
		for (int i = 0; i < m; ++i) {
			int u = in.nextInt()-1;
			int v = in.nextInt()-1;
			int c = in.nextInt()-1;
			adj[u][c].add(v);
			adj[v][c].add(u);

		}

		int q = in.nextInt();
		for (int i = 0; i < q; ++i) {
			int u = in.nextInt()-1;
			int v = in.nextInt()-1;
			out.println(colors(adj, u, v));
		}
	}

	private int colors(List<Integer>[][] adj, int u, int v) {
		int result = 0;

		for (int c = 0; c < adj[0].length; ++c) {
			Queue<Integer> q = new LinkedList<Integer>();
			Set<Integer> visited = new HashSet<Integer>();
			q.add(u);
			boolean breakIt = false;
			while (!q.isEmpty()) {
				Integer current = q.poll();
				if(visited.contains(current)){
					continue;
				}
				for (Integer neigh : adj[current][c]) {
					if (neigh == v) {
						result++;
						breakIt = true;
						break;
					} else {
						q.add(neigh);
					}
				}
				if(breakIt){
					break;
				}
				visited.add(current);
			}
		}
		return result;
	}

}

class Edge {

	int color;
	int index;

	public Edge(int color, int index) {
		super();
		this.color = color;
		this.index = index;
	}

	@Override
	public String toString() {
		return "Edge [color=" + color + ", index=" + index + "]";
	}

}

class TaskC {

	public void solve(int testNumber, InputReader in, PrintWriter out) {
	}

}

class InputReader {
	public BufferedReader reader;
	public StringTokenizer tokenizer;

	public InputReader(InputStream stream) {
		reader = new BufferedReader(new InputStreamReader(stream), 32768);
		tokenizer = null;
	}

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

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

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

}