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 double pi = 3.14159265359;
template <typename T>
T abs(T x) {
  return x > 0 ? x : -x;
}
template <typename T>
T sqr(T x) {
  return x * x;
}
const int maxn = 100005;
const int magic = 333;
vector<int> g[maxn];
int used[maxn];
int currUsed = 1;
vector<int> visited;
void dfs(int v) {
  used[v] = currUsed;
  visited.push_back(v);
  for (int to : g[v])
    if (used[to] != currUsed) dfs(to);
}
vector<vector<int> > smallSets;
vector<int> smallSetsByVertex[maxn];
vector<vector<int> > bigSets;
vector<pair<int, int> > queries[maxn];
int ans[maxn];
int n;
int cnt[maxn];
void processSmallSets() {
  for (int i = 0; i < n; i++) {
    currUsed++;
    vector<int> was;
    for (int j = 0; j < smallSetsByVertex[i].size(); j++) {
      for (int x : smallSets[smallSetsByVertex[i][j]]) {
        if (used[x] != currUsed) {
          was.push_back(x);
          used[x] = currUsed;
        }
        cnt[x]++;
      }
    }
    for (auto q : queries[i]) {
      ans[q.second] += cnt[q.first];
    }
    for (int x : was) cnt[x] = 0;
  }
}
int color[maxn];
void processBigSets() {
  for (int i = 0; i < bigSets.size(); i++) {
    vector<int> was;
    currUsed++;
    for (int x : bigSets[i]) {
      if (used[x] != currUsed) {
        used[x] = currUsed;
        was.push_back(x);
      }
      color[x] = 1;
    }
    for (int i = 0; i < n; i++) {
      if (!color[i]) continue;
      for (auto q : queries[i]) {
        ans[q.second] += color[q.first];
      }
    }
    for (int x : was) color[x] = 0;
  }
}
vector<pair<int, int> > edges[maxn];
int main() {
  int m;
  scanf("%d %d", &n, &m);
  for (int i = 0; i < m; i++) {
    int u, v, c;
    scanf("%d %d %d", &u, &v, &c);
    u--;
    v--;
    c--;
    if (u > v) swap(u, v);
    edges[c].push_back(make_pair(u, v));
  }
  for (int i = 0; i < maxn; i++) {
    if (edges[i].empty()) continue;
    vector<int> need;
    for (auto e : edges[i]) {
      need.push_back(e.first);
      need.push_back(e.second);
      g[e.first].push_back(e.second);
      g[e.second].push_back(e.first);
    }
    currUsed++;
    for (int x : need) {
      if (used[x] == currUsed) continue;
      visited.clear();
      dfs(x);
      for (int x : visited) g[x].clear();
      if (visited.size() > magic)
        bigSets.push_back(visited);
      else {
        smallSets.push_back(visited);
        for (int x : visited) {
          smallSetsByVertex[x].push_back(smallSets.size() - 1);
        }
      }
    }
  }
  int q;
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    int u, v;
    scanf("%d %d", &u, &v);
    u--;
    v--;
    if (u > v) swap(u, v);
    queries[u].push_back(make_pair(v, i));
  }
  processBigSets();
  processSmallSets();
  for (int i = 0; i < q; i++) printf("%d\n", ans[i]);
  return 0;
}
