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;
int n, m, nq;
vector<pair<int, int> > edge[111111];
vector<int> ver[111111];
vector<pair<int, int> > q;
vector<pair<int, int> > qq;
int ans[111111];
bool upd[111111];
map<int, int> f[111111];
bitset<111111> tt[2000];
int tcnt = 0;
int F(int c, int x) {
  if (!f[c].count(x) || f[c][x] == x) return f[c][x] = x;
  return f[c][x] = F(c, f[c][x]);
}
void M(int c, int a, int b) {
  a = F(c, a);
  b = F(c, b);
  if (rand() % 2) swap(a, b);
  f[c][a] = b;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    a--;
    b--;
    c--;
    ver[c].push_back(a);
    ver[c].push_back(b);
    edge[c].push_back(make_pair(a, b));
  }
  scanf("%d", &nq);
  for (int i = 0; i < nq; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    a--;
    b--;
    if (a > b) swap(a, b);
    qq.push_back(make_pair(a, b));
  }
  q = qq;
  sort(q.begin(), q.end());
  ;
  q.resize(unique(q.begin(), q.end()) - q.begin());
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < edge[i].size(); j++) {
      M(i, edge[i][j].first, edge[i][j].second);
    }
    vector<pair<int, int> > sav;
    sav.clear();
    for (int j = 0; j < ver[i].size(); j++)
      sav.push_back(make_pair(F(i, ver[i][j]), ver[i][j]));
    sort(sav.begin(), sav.end());
    ;
    sav.resize(unique(sav.begin(), sav.end()) - sav.begin());
    int l = 0;
    for (int j = 0; j < sav.size(); j++) {
      bool f = 0;
      if (j == sav.size() - 1) f = 1;
      if (sav[j].first != sav[j + 1].first) f = 1;
      if (f) {
        int r = j;
        int len = r - l + 1;
        if (len >= 250) {
          for (int k = l; k < r + 1; k++) {
            tt[tcnt][sav[k].second] = 1;
          }
          tcnt++;
        } else {
          for (int x = l; x < r; x++) {
            for (int y = x + 1; y < r + 1; y++) {
              int xx = sav[x].second;
              int yy = sav[y].second;
              int pos = lower_bound(q.begin(), q.end(), make_pair(xx, yy)) -
                        q.begin();
              if (q[pos] == make_pair(xx, yy)) {
                ans[pos]++;
              }
            }
          }
        }
        l = r + 1;
      }
    }
  }
  for (int i = 0; i < nq; i++) {
    int pos = lower_bound(q.begin(), q.end(), qq[i]) - q.begin();
    if (upd[pos]) {
      printf("%d\n", ans[pos]);
      continue;
    }
    upd[pos] = 1;
    int x = q[pos].first;
    int y = q[pos].second;
    int res = ans[pos];
    for (int j = 0; j < tcnt; j++) {
      if (tt[j][x] && tt[j][y]) res++;
    }
    printf("%d\n", ans[pos] = res);
  }
  return 0;
}
