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>
#pragma GCC optimize("Ofast")
using namespace std;
struct dsu {
  int n, fa[100005];
  void init(int len) {
    n = len;
    for (int i = 1; i <= n; i++) fa[i] = i;
  }
  int get(int x) { return fa[x] == x ? x : (fa[x] = get(fa[x])); }
  void up(int x, int y) {
    x = get(x);
    y = get(y);
    if (rand() & 1) {
      fa[x] = y;
    } else
      fa[y] = x;
  }
} D;
vector<pair<int, int>> co[100005];
const int len = 300;
pair<int, int> qs[100005];
int ans[100005];
long long s[16000000];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  srand(time(0));
  int n, m;
  cin >> n >> m;
  int x, y, z;
  for (int i = 1; i <= m; i++) {
    cin >> x >> y >> z;
    co[z].emplace_back(make_pair(x, y));
  }
  int q;
  cin >> q;
  for (int i = 1; i <= q; i++) {
    cin >> qs[i].first >> qs[i].second;
    if (qs[i].first > qs[i].second) swap(qs[i].first, qs[i].second);
  }
  int cnt = 0;
  for (int i = 1; i <= m; i++) {
    if (co[i].size() >= len) {
      D.init(n);
      for (auto u : co[i]) D.up(u.first, u.second);
      for (int j = 1; j <= q; j++) {
        if (D.get(qs[j].first) == D.get(qs[j].second)) ans[j]++;
      }
    } else {
      vector<int> v;
      for (auto u : co[i]) v.emplace_back(u.first), v.emplace_back(u.second);
      sort(v.begin(), v.end());
      v.erase(unique(v.begin(), v.end()), v.end());
      int l = v.size();
      D.init(l);
      for (auto u : co[i]) {
        D.up(lower_bound(v.begin(), v.end(), u.first) - v.begin() + 1,
             lower_bound(v.begin(), v.end(), u.second) - v.begin() + 1);
      }
      for (int i1 = 0; i1 < l; i1++) {
        for (int i2 = i1 + 1; i2 < l; i2++) {
          if (D.get(i1 + 1) == D.get(i2 + 1)) {
            s[++cnt] = 1ll * v[i1] * (n + 1) + v[i2];
          }
        }
      }
    }
  }
  sort(s + 1, s + cnt + 1);
  for (int i = 1; i <= q; i++) {
    long long t = 1ll * qs[i].first * (n + 1) + qs[i].second;
    ans[i] +=
        upper_bound(s + 1, s + cnt + 1, t) - lower_bound(s + 1, s + cnt + 1, t);
    cout << ans[i] << '\n';
  }
  return 0;
}
