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;
const int inf = (1 << 30) - 1;
const long long linf = (1ll << 62) - 1;
const int N = 1e5 + 100;
const int K = 700;
int n, m, q, r;
int a[N], b[N], c[N], u[N], v[N];
pair<int, int> reqList[N];
int ans[N];
vector<int> edges[N];
vector<int> vertices;
vector<int> g[N];
int comp[N];
vector<int> comps[N];
int state = 0, here[N];
int counter = 0;
void dfs(int v, int cc) {
  here[v] = state;
  if (comp[v]) {
    return;
  }
  comp[v] = cc;
  for (int to : g[v]) {
    dfs(to, cc);
  }
}
void update(int a, int b) {
  if (a > b) {
    swap(a, b);
  }
  int index = lower_bound(reqList, reqList + r, make_pair(a, b)) - reqList;
  if (index == q || reqList[index] != make_pair(a, b)) {
    return;
  }
  ans[index]++;
}
void solve(int color) {
  vertices.clear();
  for (int i : edges[color]) {
    vertices.push_back(a[i]);
    vertices.push_back(b[i]);
  }
  ++state;
  sort((vertices).begin(), (vertices).end());
  vertices.resize(unique((vertices).begin(), (vertices).end()) -
                  vertices.begin());
  for (int i : vertices) {
    g[i].clear();
    comp[i] = 0;
  }
  for (int i : edges[color]) {
    g[a[i]].push_back(b[i]);
    g[b[i]].push_back(a[i]);
  }
  int cc = 0;
  for (int i : vertices) {
    if (!comp[i]) {
      dfs(i, ++cc);
    }
  }
  for (int i = 1; i <= cc; i++) {
    comps[i].clear();
  }
  for (int i : vertices) {
    comps[comp[i]].push_back(i);
  }
  if (((int)(edges[color]).size()) >= K) {
    for (int i = 0; i < r; i++) {
      int u = reqList[i].first, v = reqList[i].second;
      if (here[u] == state && here[v] == state && comp[u] == comp[v]) {
        ans[i]++;
      }
    }
  } else {
    for (int i = 1; i <= cc; i++) {
      for (int j = ((int)(comps[i]).size()) - 1; j >= 0; j--) {
        for (int k = j - 1; k >= 0; k--) {
          update(comps[i][k], comps[i][j]);
          counter++;
        }
      }
    }
  }
}
int main() {
  assert(scanf("%d%d", &n, &m) == 2);
  for (int i = 0; i < m; i++) {
    assert(scanf("%d%d%d", &a[i], &b[i], &c[i]) == 3);
    a[i]--, b[i]--, c[i]--;
  }
  assert(scanf("%d", &q) == 1);
  for (int i = 0; i < q; i++) {
    assert(scanf("%d%d", &u[i], &v[i]) == 2);
    u[i]--, v[i]--;
    if (u[i] > v[i]) {
      swap(u[i], v[i]);
    }
    reqList[i] = make_pair(u[i], v[i]);
  }
  sort(reqList, reqList + q);
  r = unique(reqList, reqList + q) - reqList;
  for (int i = 0; i < m; i++) {
    edges[c[i]].push_back(i);
  }
  for (int i = 0; i < m; i++) {
    42;
    solve(i);
  }
  for (int i = 0; i < q; i++) {
    printf(
        "%d%c",
        ans[lower_bound(reqList, reqList + r, make_pair(u[i], v[i])) - reqList],
        " \n"[i + 1 == q]);
  }
  42;
  return 0;
}
