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.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
import java.util.Collections;
import java.util.HashMap;
import java.util.InputMismatchException;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Scanner;
import java.util.Stack;
import java.util.TreeSet;
public class prac1 {
	static long mod=1000000007;
	static class pair implements Comparable<pair>
	{
		Integer x, y;
		pair(int x,int y)
		{
			this.x=x;
			this.y=y;
		}
		public int compareTo(pair o) {
			if(x.compareTo(o.x)!=0)
				return x.compareTo(o.x);
			else
				return y.compareTo(o.y);
		}  
	}
	static int dx[]={1,1,-1,-1,1,-1,0,0},dy[]={-1,1,-1,1,0,0,1,-1};
	static ArrayList<pair> al[];
	static boolean visited[];
	static int parent[];
	static int count=0;
	public static void main(String[] args) throws IOException {
		new Thread(null ,new Runnable(){
            public void run(){
                try{
                    solve();
                } catch(Exception e){
                    e.printStackTrace();
                }
            }
        },"1",1<<26).start();
	}
	public static void solve()
	{
		InputReader in=new InputReader(System.in);
		PrintWriter w=new PrintWriter(System.out);
		int n=in.nextInt();
		int m=in.nextInt();
		al=new ArrayList[n+1];
		for(int i=0;i<=n;i++)
			al[i]=new ArrayList<pair>();
		for(int i=0;i<m;i++)
		{
			int x=in.nextInt();
			int y=in.nextInt();
			int c=in.nextInt();
			al[x].add(new pair(y,c));
			al[y].add(new pair(x,c));
		}
		int q=in.nextInt();
		for(int i=0;i<q;i++)
		{
			
			count=0;
			int x=in.nextInt();
			int y=in.nextInt();
			for(int j=1;j<=100;j++)
			{
				visited=new boolean[n+1];
				dfs(x,y,j);
			}
			System.out.println(count);
		}
	}
	public static void dfs(int x,int end,int c)
	{
		visited[x]=true;
		if(x==end)
		{
			count++;
			return;
		}
		for(int i=0;i<al[x].size();i++)
		{
			if(al[x].get(i).y==c&&!visited[al[x].get(i).x])
			{
				dfs(al[x].get(i).x,end,c);
			}
		}
	}
public static long gcd(long x, long y)
{
	if(x==0)
		return y;
	if(y==0)
		
		return x;
	long r=0, a, b;
	a = (x > y) ? x : y; // a is greater number
	b = (x < y) ? x : y; // b is smaller number
	r = b;
	while(a % b != 0)
	{
		r = a % b;
		a = b;
		b = r;
	}
	return r;
}

static class InputReader {
	 
	private final InputStream stream;
	private final byte[] buf = new byte[8192];
	private int curChar, snumChars;
	private SpaceCharFilter filter;

	public InputReader(InputStream stream) {
		this.stream = stream;
	}

	public int snext() {
		if (snumChars == -1)
			throw new InputMismatchException();
		if (curChar >= snumChars) {
			curChar = 0;
			try {
				snumChars = stream.read(buf);
			} catch (IOException e) {
				throw new InputMismatchException();
			}
			if (snumChars <= 0)
				return -1;
		}
		return buf[curChar++];
	}

	public int nextInt() {
		int c = snext();
		while (isSpaceChar(c)) {
			c = snext();
		}
		int sgn = 1;
		if (c == '-') {
			sgn = -1;
			c = snext();
		}
		int res = 0;
		do {
			if (c < '0' || c > '9')
				throw new InputMismatchException();
			res *= 10;
			res += c - '0';
			c = snext();
		} while (!isSpaceChar(c));
		return res * sgn;
	}

	public long nextLong() {
		int c = snext();
		while (isSpaceChar(c)) {
			c = snext();
		}
		int sgn = 1;
		if (c == '-') {
			sgn = -1;
			c = snext();
		}
		long res = 0;
		do {
			if (c < '0' || c > '9')
				throw new InputMismatchException();
			res *= 10;
			res += c - '0';
			c = snext();
		} while (!isSpaceChar(c));
		return res * sgn;
	}

	public int[] nextIntArray(int n) {

		int a[] = new int[n];
		for (int i = 0; i < n; i++) {
			a[i] = nextInt();
		}
		return a;
	}
	public int[] nextstartIntArray(int n) {

		int a[] = new int[n+1];
		for (int i = 1; i <= n; i++) {
			a[i] = nextInt();
		}
		return a;
	}
	public String readString() {
		int c = snext();
		while (isSpaceChar(c)) {
			c = snext();
		}
		StringBuilder res = new StringBuilder();
		do {
			res.appendCodePoint(c);
			c = snext();
		} while (!isSpaceChar(c));
		return res.toString();
	}

	public String nextLine() {
		int c = snext();
		while (isSpaceChar(c))
			c = snext();
		StringBuilder res = new StringBuilder();
		do {
			res.appendCodePoint(c);
			c = snext();
		} while (!isEndOfLine(c));
		return res.toString();
	}

	public boolean isSpaceChar(int c) {
		if (filter != null)
			return filter.isSpaceChar(c);
		return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
	}

	private boolean isEndOfLine(int c) {
		return c == '\n' || c == '\r' || c == -1;
	}

	public interface SpaceCharFilter {
		public boolean isSpaceChar(int ch);
	}
}
}
