Mr. Kitayuta has just bought an undirected graph consisting of 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 ≤ 100, 1 ≤ m ≤ 100), 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 ≤ 100), 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. 
2 1
1 2 1
1
1 2
 2 3
1 2 3
1 2 2
1 2 1
1
1 2
 2 5
1 2 1
1 2 2
1 2 3
1 2 4
1 2 5
1
1 2

1
 3
 5

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;


public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new PrintWriter(System.out));
       
        String[] ln1 = br.readLine().split(" ");
        
        int n = Integer.parseInt(ln1[0]);
        int m = Integer.parseInt(ln1[1]);
        
        Graph G = new Graph(n,m);
        
        for(int i=0;i<m;i++){
            
            String[] ln2 = br.readLine().split(" ");
            
            int a = Integer.parseInt(ln2[0]);
            int b = Integer.parseInt(ln2[1]);
            int c = Integer.parseInt(ln2[2]);
            
            G.addEdge(a,b,c);
            G.addEdge(b,a,c);
        }
        
        int Q = Integer.parseInt(br.readLine());
        for(int q=0;q<Q;q++){
            String[] ln3 = br.readLine().split(" ");
            
            int u = Integer.parseInt(ln3[0]);
            int v = Integer.parseInt(ln3[1]);
            u-=1;v-=1;
            
            //dist
            //System.out.println("checking "+u+" "+v);
            G.check(u,v);
        }
        
        //G.check();
        
        
    }

    private static class Graph {

        int[] last;
        int[] to;
        int[] next;
        int[] color;
        
        int edgeId = -1;
        
        int cnt=-1;
        boolean yes = false;
        int c = -1;
        int p = -1;
        
        public Graph(int n,int m) {
            this.last = new int[n];
            this.to=new int[200*m];
            this.next=  new int[200*m];
            this.color = new int[200*m];
            for(int i=0;i<n;i++){
                this.last[i] = -1;
            }
            
        }

        private void addEdge(int a, int b, int c) {
            
            a-=1;b-=1;
            this.edgeId+=1;
            this.to[edgeId] = b;
            this.next[edgeId] = this.last[a];
            this.last[a] = edgeId;
            this.color[edgeId] = c;
        }

        private void dfs() {
            
            int[] seen = new int[this.last.length];
            
            for(int i=0;i<seen.length;i++){
                if(seen[i]==0){
                    
                    seen[i]=1;
                    for(int id = last[i]; id!=-1; id=next[id]){
                        int v = to[id];
                        //System.out.println("From "+i+" to "+v+" with color of "+color[id]);
                    }
                }
            }
        }

        private void check(int u, int v) {
            
            int[] seen = new int[this.last.length];
            
           // System.out.println("Starting with "+u+" Destination is "+v);
            
            this.cnt = 0;
            this.p=u;
            checkUtil(u,v,seen);
           // System.out.println("Made "+this.cnt);
            System.out.println(this.cnt);
           // System.exit(0);
        }

        private void checkUtil(int u,int v, int[] seen) {
            
            seen[u]=1;
            
           
            for(int id=last[u]; id!=-1; id=next[id]){
                
                int s = this.to[id];
                
                if(this.p==u)   this.c=-1;
                
                int c = this.color[id];
                
                if(this.c == -1){
                    
                    this.c = c;
                    
                    //System.out.println("Changed color to "+c);
                    
                }
                
                if(s==v && this.c==c){
                    this.cnt+=1;seen[s]=1;
                }
                
                else if(s!= v && seen[s]==0 && this.c==c){
                    
                   // System.out.println("traversing edges ");
                    
                    checkUtil(s,v,seen);
                }
                
                
            }
            
            
            
        }
    }//graph class
}
