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 MX = 200005, D = 400, INF = 1000010000;
const long long int LINF = 1000000000000000000ll;
const double eps = 1e-10;
const int di[] = {-1, 0, 1, 0}, dj[] = {0, -1, 0, 1};
vector<pair<int, int> > e[MX];
pair<int, int> q[MX];
vector<pair<int, int> > p;
int ans[MX];
struct uf {
  vector<int> d;
  vector<int> hist;
  uf() {}
  uf(int mx) : d(mx, -1) {}
  int root(int x) {
    if (d[x] < 0) return x;
    return root(d[x]);
  }
  void unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y) return;
    if (d[x] > d[y]) swap(x, y);
    if (d[x] == d[y]) {
      hist.push_back(x);
      d[x]--;
    }
    hist.push_back(y);
    d[y] = x;
  }
  void rev() {
    for (int i = (int)hist.size() - 1; i >= 0; --i) {
      d[hist[i]] = -1;
    }
    hist.clear();
  }
};
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; ++i) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    c--;
    a--;
    b--;
    e[c].push_back(pair<int, int>(a, b));
  }
  int qs;
  scanf("%d", &qs);
  for (int i = 0; i < qs; ++i) {
    int a, b;
    scanf("%d%d", &a, &b);
    a--;
    b--;
    if (a > b) swap(a, b);
    q[i] = pair<int, int>(a, b);
  }
  uf t(n + 5);
  for (int c = 0; c < m; ++c) {
    if ((int)(e[c]).size() == 0) continue;
    for (pair<int, int> a : e[c]) {
      t.unite(a.first, a.second);
    }
    if ((int)(e[c]).size() < D) {
      vector<int> v;
      for (pair<int, int> a : e[c]) {
        v.push_back(a.first);
        v.push_back(a.second);
      }
      sort(v.begin(), v.end());
      v.erase(unique(v.begin(), v.end()), v.end());
      for (int i = 0; i < v.size(); ++i)
        for (int j = 0; j < i; ++j) {
          if (t.root(v[i]) == t.root(v[j]))
            p.push_back(pair<int, int>(v[j], v[i]));
        }
    } else {
      for (int qi = 0; qi < qs; ++qi) {
        ans[qi] += (t.root(q[qi].first) == t.root(q[qi].second));
      }
    }
    t.rev();
  }
  sort(p.begin(), p.end());
  for (int qi = 0; qi < qs; ++qi) {
    int res = ans[qi];
    auto it = equal_range(p.begin(), p.end(), q[qi]);
    res += it.second - it.first;
    printf("%d\n", res);
  }
  return 0;
}
