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.lang.*;
import java.util.*;
import java.io.*;

public class Template {

  public static void main(String[] args) throws Exception {
    InputReader reader = new InputReader(System.in);
    PrintWriter writer = new PrintWriter(System.out, true);
    try {
      new Solution(reader, writer).run();
    } finally {
      reader.close();
      writer.close();
    }
  }
}

class DSU {
  private int[] r;
  DSU(int n) {
    this.r = new int[n];
    for (int i=0; i<n; i++) {
      r[i] = i;
    }
  }

  public int find(int idx) {
    if (idx == r[idx]) {
      return idx;
    }
    return r[idx] = find(r[idx]);
  }

  public void union(int u, int v) {
    r[find(u)] = find(v);
  }
}
class Solution {
  private InputReader reader;
  private PrintWriter writer;

  Solution(InputReader reader, PrintWriter writer) {
    this.reader = reader;
    this.writer = writer;
  }

  public void run() throws Exception {
    int n = reader.nextInt();
    int m = reader.nextInt();

    Map<Integer, DSU> colors = new HashMap<Integer, DSU>();
    for (int i=0; i<m; i++) {
      int u = reader.nextInt()-1;
      int v = reader.nextInt()-1;
      int c = reader.nextInt();
      DSU dsu = colors.get(c);
      if (null == dsu) {
        dsu = new DSU(n);
        colors.put(c, dsu);
      }
      dsu.union(u,v);
    }
    int q = reader.nextInt();
    for (int i=0; i<q; i++) {
      int u = reader.nextInt()-1;
      int v = reader.nextInt()-1;
      int result = 0;
      for (Map.Entry<Integer, DSU> pair: colors.entrySet()) {
        DSU dsu = pair.getValue();
        if (dsu.find(u) == dsu.find(v)) {
          result++;
        }
      }
      writer.println(result);
    }
  }
}

class InputReader {
  private BufferedReader reader;
  private StringTokenizer tokenizer;

  InputReader(InputStream inputStream) {
    this.reader = new BufferedReader(new InputStreamReader(inputStream));
  }

  protected String nextToken() throws Exception {
    while (null == tokenizer || !tokenizer.hasMoreTokens()) {
      this.tokenizer = new StringTokenizer(reader.readLine());
    }
    return tokenizer.nextToken();
  }

  public int nextInt() throws Exception {
    return Integer.parseInt(nextToken());
  }

  public void close() throws Exception {
    reader.close();
  }
}
