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 long long MOD = 1e9 + 7;
const long long INF = 1e14;
const long long TE3 = 1005;
const long long TE5 = 300005;
const string YN[2] = {"NO", "YES"};
using namespace std;
long long n, m;
long long parent[TE5], size[TE5];
long long M = 1e6;
long long ec = 1;
unordered_map<long long, long long> ids;
vector<long long> colors[TE5];
unordered_map<long long, long long> cache;
long long h(long long x, long long y) { return x * M + y; }
void make_set(long long m) {
  for (long long v = (0); v < (m); ++v) {
    parent[v] = v;
    size[v] = 1;
  }
}
long long find_set(long long v) {
  if (v == parent[v]) return v;
  return parent[v] = find_set(parent[v]);
}
void union_sets(long long a, long long b) {
  a = find_set(a);
  b = find_set(b);
  if (a != b) {
    if (size[a] < size[b]) swap(a, b);
    parent[b] = a;
    size[a] += size[b];
  }
}
long long solve(long long x, long long y) {
  if (x > y) {
    swap(x, y);
  }
  long long hxy = h(x, y);
  if (cache.find(hxy) != cache.end()) {
    return cache[hxy];
  }
  long long ans = 0;
  long long cx = colors[x].size(), cy = colors[y].size();
  if (cx > cy) {
    swap(x, y);
  }
  for (long long color : colors[x]) {
    if (!binary_search(colors[y].begin(), colors[y].end(), color)) {
      continue;
    }
    long long h1 = h(x, color);
    long long h2 = h(y, color);
    if (ids.find(h1) == ids.end() || ids.find(h2) == ids.end()) {
      continue;
    }
    long long i1 = ids[h1], i2 = ids[h2];
    if (find_set(i1) == find_set(i2)) {
      ++ans;
    }
  }
  return cache[hxy] = ans;
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n >> m;
  make_set(2 * m + 2);
  vector<pair<long long, long long> > u;
  for (long long i = (0); i < (m); ++i) {
    long long x, y, c;
    cin >> x >> y >> c;
    --x;
    --y;
    long long h1 = h(x, c), h2 = h(y, c);
    if (ids.find(h1) == ids.end()) {
      ids[h1] = ec++;
    }
    if (ids.find(h2) == ids.end()) {
      ids[h2] = ec++;
    }
    union_sets(ids[h1], ids[h2]);
    colors[x].push_back(c);
    colors[y].push_back(c);
  }
  for (long long x = (0); x < (n); ++x) {
    sort(colors[x].begin(), colors[x].end());
    colors[x].erase(unique(colors[x].begin(), colors[x].end()),
                    colors[x].end());
  }
  long long q;
  cin >> q;
  while (q--) {
    long long x, y;
    cin >> x >> y;
    --x;
    --y;
    cout << solve(x, y) << endl;
  }
  return 0;
}
