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.*;
import java.util.*;
import java.math.*;
public class cb {
	
    InputStream is;
    PrintWriter out; 
 
	static long mod=(long)(1e7+9);
	static long mx=Integer.MIN_VALUE;
	
	public static int masiha(LinkedList<pair> l[],int color,int n,int a,int b)
	{
		Queue<pair> q=new LinkedList<pair>();
		q.add(new pair(a,color));
		boolean vis[]=new boolean[n+1];
		vis[a]=true;
		int c=0;
		while(q.size()!=0)
		{
			int curr=q.poll().k;
			for(int i=0;i<l[curr].size();i++)
			{
				if(l[curr].get(i).v==color&&vis[l[curr].get(i).k]==false)
				{
					q.add(new pair(l[curr].get(i).k,color));
					vis[l[curr].get(i).k]=true;
					if(l[curr].get(i).k==b)
						c++;
				}
			}
		}
		return c;
	}
	
    void solve()
    {
		int n=ni();
		int m=ni();
		LinkedList<pair> l[]=new LinkedList[n+1];
		for(int i=0;i<=n;i++)
			l[i]=new LinkedList<pair>();
		
		for(int i=0;i<m;i++)
		{
			int s=ni(),d=ni(),co=ni();
			l[s].add(new pair(d,co));
			l[d].add(new pair(s,co));
		}
		int q=ni();
		
		for(int i=0;i<q;i++)
		{
			long ans=0;
			int a=ni(),b=ni();
			for(int j=1;j<=m;j++)
			{
				ans+=masiha(l,j,n,a,b);
			}
			out.println(ans);
		}
		
		
	}
	
	
	
	
	
	
 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	
	
 
 
 	static class pair
	{
		int k,v;
		public pair(int k,int v)
		{
			this.k=k;
			this.v=v;
		}
	}
	
	public static long [][] multiply(long a[][],long b[][])
	{
		long mul[][]=new long[2][2];
		for(int i=0;i<2;i++)
		{
			for(int j=0;j<2;j++)
			{
				mul[i][j]=0;
				for(int k=0;k<2;k++)
					mul[i][j]=(mul[i][j]+(a[i][k]*b[k][j]))%mod;
			}
		}
		return mul;
	}
	
	/* Matrix Exponention*/
	public static long [][] power(long [][] mat,int n)
	{
		long res[][]=new long[2][2];
		res[0][0]=1;res[1][1]=1;
		while(n>0)
		{
			if(n%2==1)
				res=multiply(res,mat);
			mat=multiply(mat,mat);
			n/=2;
		}
		return res;
	}
	  
	  
	  
	
    //---------- I/O Template ----------
    
    public static void main(String[] args) { new cb().run(); }
    void run() { 
        is = System.in; 
        out = new PrintWriter(System.out);
        solve();
        out.flush();
    }
    
    byte input[] = new byte[1024];
    int len = 0, ptr = 0;
    
    int readByte() { 
        if(ptr >= len) { ptr = 0; 
            try { len = is.read(input); } 
            catch(IOException e) { throw new InputMismatchException(); } 
            if(len <= 0) { return -1; } 
        } return input[ptr++];
    }
    boolean isSpaceChar(int c) { return !( c >= 33 && c <= 126 ); }
    int skip() { 
        int b = readByte(); 
        while(b != -1 && isSpaceChar(b)) { b = readByte(); } 
        return b;
    }
    
    char nc() { return (char)skip(); }
    String ns() { 
        int b = skip(); 
        StringBuilder sb = new StringBuilder(); 
        while(!isSpaceChar(b)) { sb.appendCodePoint(b); b = readByte(); } 
        return sb.toString();
    }
    String nLine() { 
        int b = skip(); 
        StringBuilder sb = new StringBuilder(); 
        while( !(isSpaceChar(b) && b != ' ') ) { sb.appendCodePoint(b); b = readByte(); } 
        return sb.toString();
    }
    int ni() { 
        int n = 0, b = readByte(); 
        boolean minus = false; 
        while(b != -1 && !( (b >= '0' && b <= '9') || b == '-')) { b = readByte(); } 
        if(b == '-') { minus = true; b = readByte(); } 
        if(b == -1) { return -1; }  //no input 
        while(b >= '0' && b <= '9') { n = n * 10 + (b - '0'); b = readByte(); } 
        return minus ? -n : n;
    }
    long nl() { 
        long n = 0L;    int b = readByte(); 
        boolean minus = false; 
        while(b != -1 && !( (b >= '0' && b <= '9') || b == '-')) { b = readByte(); } 
        if(b == '-') { minus = true; b = readByte(); } 
        while(b >= '0' && b <= '9') { n = n * 10 + (b - '0'); b = readByte(); } 
        return minus ? -n : n;
    }
 
    double nd() { return Double.parseDouble(ns()); }
    float nf() { return Float.parseFloat(ns()); }
    int[] na(int n) { 
        int a[] = new int[n]; 
        for(int i = 0; i < n; i++) { a[i] = ni(); } 
        return a;
    }
    char[] ns(int n) { 
        char c[] = new char[n]; 
        int i, b = skip(); 
        for(i = 0; i < n; i++) { 
            if(isSpaceChar(b)) { break; } 
            c[i] = (char)b; b = readByte(); 
        } return i == n ? c : Arrays.copyOf(c,i);
    }
	
}