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;
inline void in(int &x) {
  x = 0;
  char c = getchar();
  while (c < '0' || c > '9') c = getchar();
  while (c >= '0' && c <= '9')
    x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
}
struct edge {
  int a, b, c;
  edge(int _a = 0, int _b = 0, int _c = 0) : a(_a), b(_b), c(_c) {}
  bool operator<(const edge &x) const { return c < x.c; }
} e[100005];
struct query {
  int a, b, id;
  long long val;
  query(int _a = 0, int _b = 0, int _id = 0, long long _val = 0)
      : a(_a), b(_b), id(_id), val(_val) {}
  bool operator<(const query &x) const { return val < x.val; }
} q[100005];
const int maxn = 2e7 + 7;
int curcol, compc, fa[100005], col[100005], comp[100005], ans[100005], extrac;
long long extra[maxn];
int Find(int x) {
  if (col[x] != curcol) {
    col[x] = curcol;
    comp[compc++] = x;
    fa[x] = x;
  }
  return fa[x] == x ? x : fa[x] = Find(fa[x]);
}
int n, m, Q;
int main() {
  in(n);
  in(m);
  for (int i = 0; i < m; ++i) {
    in(e[i].a), in(e[i].b), in(e[i].c);
    if (e[i].a > e[i].b) swap(e[i].a, e[i].b);
  }
  in(Q);
  for (int i = 0; i < Q; ++i) {
    in(q[i].a);
    in(q[i].b);
    if (q[i].a > q[i].b) swap(q[i].a, q[i].b);
    q[i].id = i;
    q[i].val = 1LL * q[i].a * 100005 + q[i].b;
  }
  sort(e, e + m);
  sort(q, q + Q);
  for (int i = 0, j = 0; i < m; i = j) {
    while (j < m && e[j].c == e[i].c) ++j;
    curcol = e[i].c;
    compc = 0;
    for (int k = i; k < j; ++k) {
      int x = Find(e[k].a), y = Find(e[k].b);
      if (x != y) fa[x] = y;
    }
    if (j - i >= 320) {
      for (int k = 0; k < Q; ++k)
        if (Find(q[k].a) == Find(q[k].b)) ++ans[q[k].id];
    } else {
      for (int k = 0; k < compc; ++k)
        for (int l = k + 1; l < compc; ++l) {
          int x = comp[k], y = comp[l];
          if (Find(x) == Find(y)) {
            if (x > y) swap(x, y);
            extra[extrac++] = 1LL * x * 100005 + y;
          }
        }
    }
  }
  sort(extra, extra + extrac);
  for (int i = 0, j = 0, k = 0; i < extrac; i = j) {
    while (j < extrac && extra[i] == extra[j]) ++j;
    while (k < Q && q[k].val < extra[i]) ++k;
    while (k < Q && q[k].val == extra[i]) {
      ans[q[k].id] += j - i;
      ++k;
    }
  }
  for (int i = 0; i < Q; ++i) printf("%d\n", ans[i]);
  return 0;
}
