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. 
/*
 * Remember a 6.89 student can know more than a 10.0 student.
 * Grades don't determine intelligence, they test obedience. 
 * I Never Give Up.
 * I will become Candidate Master today.
 * I will defeat Saurabh Anand.
 * Skills are Cheap,Passion is Priceless.
 */
import java.util.*;
import java.util.Map.Entry;
import java.io.*;
 
import static java.lang.System.out;
import static java.util.Arrays.*;
import static java.lang.Math.*;
 
public class ContestMain{
	private static Reader in=new Reader();
	private static StringBuilder ans=new StringBuilder();
	private static long MOD=(long)1e9+7;
	private static final int N=(int) (1e5+7); // 1e5+7
    private static final double EPS=1e-9;
    private static final int LIM=26;
    private static final double PI=3.1415;
    private static ArrayList<Integer> v[]=new ArrayList[N];
//    private static int color[]=new int[N];
	private static boolean mark[]=new boolean[N];
//    private static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
//    private static ArrayList<Pair> v[]=new ArrayList[N];
	private static long powmod(long x,long n){
		if(n==0||x==0)
			return 1;
		else if(n%2==0)
			return(powmod((x*x)%MOD,n/2));
		else
			return (x*(powmod((x*x)%MOD,(n-1)/2)))%MOD;
	}
	private static void shuffle(long[] arr){
		for(int i=arr.length-1;i>=2;i--){
			int x=new Random().nextInt(i-1);
			long temp=arr[x];
			arr[x]=arr[i];
			arr[i]=temp;
		}
	}
    private static long gcd(long a, long b){
        if(b==0)return a;
        return gcd(b,a%b);
    }
//    private static boolean check(int x,int y){
//        if((x>=0&&x<n)&&(y>=0&&y<m)&&mat[x][y]!='X'&&!visited[x][y])return true;
//        return false;
//    }
    static int cnt=0;
    public static void solve(int node,int last,int col,int ar[][][]){
    	mark[node]=true;
    	if(node==last){
    		cnt++;
    		return;
    	}
    	for(int a:v[node]){
    		if(!mark[a]&&ar[node][a][col]==1)solve(a,last,col,ar);
    	}
    }
	public static void main(String[] args) throws IOException{
		int n=in.nextInt();
		int m=in.nextInt();
		int ar[][][]=new int[n+1][n+1][107];
		int x,y,c;
		for(int i=1;i<=100;i++)
			v[i]=new ArrayList();
		for(int i=0;i<m;i++){
			x=in.nextInt();
			y=in.nextInt();
			c=in.nextInt();
			ar[x][y][c]=1;
			ar[y][x][c]=1;
			v[x].add(y);
			v[y].add(x);
		}
		int q=in.nextInt();
		while(q-->0){
			x=in.nextInt();
			y=in.nextInt();
			for(int i=1;i<=100;i++){
				solve(x,y,i,ar);
				for(int j=1;j<=100;j++)
					mark[j]=false;
			}
			ans.append(cnt+"\n");
			cnt=0;
		}
		out.print(ans);
	}
	static class Pair<T> implements Comparable<Pair>{
		int l;
		int r;
		Pair(){
			l=0;
			r=0;
		}
		Pair(int k,int v){
			l=k;
			r=v;
		}
		@Override
		public int compareTo(Pair o){
			if(o.r!=r)
				return (int)-(r-o.r);
			else
				return (int)-(l-o.l);
		}
		// Fenwick tree question comparator
//        @Override
//        public int compareTo(Pair o) {
//            if(o.r!=r)return (int) (r-o.r);
//            else return (int)(l-o.l);
//        }
	}
 
	static class Reader{
		BufferedReader br;
		StringTokenizer st;
 
		public Reader(){
			br=new BufferedReader(new InputStreamReader(System.in));
		}
 
		String next(){
			while(st==null||!st.hasMoreElements()){
				try{
 
					st=new StringTokenizer(br.readLine());
				}catch(IOException e){
					e.printStackTrace();
				}
			}
			return st.nextToken();
		}
 
		int nextInt(){
			return Integer.parseInt(next());
		}
 
		long nextLong(){
			return Long.parseLong(next());
		}
 
		double nextDouble(){
			return Double.parseDouble(next());
		}
 
		String nextLine(){
			String str="";
			try{
				str=br.readLine();
			}catch(IOException e){
				e.printStackTrace();
			}
			return str;
		}
	}
}