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. 
#include <bits/stdc++.h>
using namespace std;
class graph {
  int v, e;
  list<pair<int, int> > *adj;
  list<pair<int, int> > *clrs;

 public:
  graph(int v_, int e_) : v(v_), e(e_) {
    adj = new list<pair<int, int> >[v];
    clrs = new list<pair<int, int> >[e];
  }
  void add_edge(int x, int y, int w) {
    adj[x].push_back(make_pair(y, w));
    adj[y].push_back(make_pair(x, w));
  }
  void dfs() {
    int k = 0;
    int *visited = new int[v];
    for (int c = 0; c < e; c++) {
      for (int i = 0; i < v; i++) visited[i] = 0;
      for (int i = 0; i < v; i++) {
        if (visited[i] == 0) {
          dfs_visit(i, visited, k, c);
          k++;
        }
      }
    }
  }
  void dfs_visit(int s, int *visited, int k, int c) {
    visited[s] = 1;
    list<pair<int, int> >::iterator it;
    for (it = adj[s].begin(); it != adj[s].end(); it++) {
      if (visited[(*it).first] == 0 and (*it).second == c) {
        clrs[(*it).second].push_back(make_pair(s, k));
        clrs[(*it).second].push_back(make_pair((*it).first, k));
        dfs_visit((*it).first, visited, k, c);
      }
    }
  }
  int findnum(int x, int y) {
    int cnt, fx, fy, kx, ky;
    cnt = 0;
    list<pair<int, int> >::iterator it;
    for (int i = 0; i < e; i++) {
      fx = fy = 0;
      for (it = clrs[i].begin(); it != clrs[i].end(); it++) {
        if ((*it).first == x) {
          fx = 1;
          kx = (*it).second;
        } else if ((*it).first == y) {
          fy = 1;
          ky = (*it).second;
        }
        if (fx and fy) break;
      }
      if ((fx and fy) and kx == ky) cnt++;
    }
    return cnt;
  }
  void print() {
    list<pair<int, int> >::iterator it;
    for (int i = 0; i < v; i++) {
      printf("%d : ", i);
      for (it = clrs[i].begin(); it != clrs[i].end(); it++) {
        printf("[%d %d] ", (*it).first, (*it).second);
      }
      printf("\n");
    }
  }
};
int main() {
  int n, e, x, y, w, q, *arrx, *arry;
  scanf("%d%d", &n, &e);
  graph g(n, e);
  for (int i = 0; i < e; i++) {
    scanf("%d%d%d", &x, &y, &w);
    x--;
    y--;
    w--;
    g.add_edge(x, y, w);
  }
  g.dfs();
  scanf("%d", &q);
  arrx = new int[q];
  arry = new int[q];
  for (int i = 0; i < q; i++) {
    scanf("%d%d", &arrx[i], &arry[i]);
    arrx[i]--;
    arry[i]--;
  }
  for (int i = 0; i < q; i++) {
    printf("%d\n", g.findnum(arrx[i], arry[i]));
  }
  return 0;
}
