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;
const int pm = 512;
vector<int> grupy[100005];
vector<int> tablice[100005];
vector<int> duze;
int odp[100005 / pm][100005 / pm];
int licz_odp(int a, int b) {
  if (grupy[a].size() > grupy[b].size()) swap(a, b);
  int wyn = 0;
  for (int i : grupy[a]) wyn += tablice[b][i];
  return wyn;
}
void szukaj_pierwiastkow_rownania_czwartego_stopnia_z_trzema_niewiadomymi() {
  for (int i = 1; i <= n; i++) {
    if (grupy[i].size() >= pm) {
      duze.emplace_back(i);
      tablice[i].resize(100005, 0);
      for (int j : grupy[i]) tablice[i][j] = 1;
    }
  }
  for (int i = 0; i < (int)duze.size(); i++)
    for (int j = i + 1; j < (int)duze.size(); j++)
      odp[i][j] = odp[j][i] = licz_odp(duze[i], duze[j]);
}
int zapytanie(int a, int b) {
  if (grupy[a].size() > grupy[b].size()) swap(a, b);
  if (grupy[a].size() >= pm) {
    int anum, bnum;
    for (int j = 0; j < (int)duze.size(); j++) {
      if (duze[j] == a)
        anum = j;
      else if (duze[j] == b)
        bnum = j;
    }
    return odp[anum][bnum];
  }
  if (grupy[b].size() >= pm) return licz_odp(a, b);
  int wyn = 0;
  int i = 0, j = 0;
  while (i < (int)grupy[a].size() && j < (int)grupy[b].size()) {
    if (grupy[a][i] == grupy[b][j]) {
      wyn++;
      i++;
      j++;
    } else if (grupy[a][i] < grupy[b][j])
      i++;
    else
      j++;
  }
  return wyn;
}
int fut[100005];
int fuf(int w) { return fut[w] == w ? w : fut[w] = fuf(fut[w]); }
void fuu(int u, int v) { fut[fuf(u)] = fuf(v); }
int l;
void dorzuc(const vector<pair<int, int> >& kraw) {
  if (kraw.empty()) return;
  vector<int> v;
  for (auto& i : kraw) {
    v.emplace_back(i.first);
    v.emplace_back(i.second);
  }
  sort(v.begin(), v.end());
  v.resize(unique(v.begin(), v.end()) - v.begin());
  for (int i : v) fut[i] = i;
  for (auto& i : kraw) fuu(i.first, i.second);
  vector<pair<int, int> > par;
  for (int i : v) par.emplace_back(fuf(i), i);
  sort(par.begin(), par.end());
  int kolor = -1;
  for (auto& i : par) {
    if (i.first != kolor) {
      l++;
      kolor = i.first;
    }
    grupy[i.second].emplace_back(l);
  }
}
int main() {
  int m;
  scanf("%d%d", &n, &m);
  vector<pair<int, pair<int, int> > > kraw;
  while (m--) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    kraw.emplace_back(c, make_pair(a, b));
  }
  sort(kraw.begin(), kraw.end());
  vector<pair<int, int> > acc;
  int kolor = -1;
  for (auto& i : kraw) {
    if (i.first == kolor)
      acc.emplace_back(i.second);
    else {
      dorzuc(acc);
      acc.resize(0);
      acc.emplace_back(i.second);
      kolor = i.first;
    }
  }
  dorzuc(acc);
  szukaj_pierwiastkow_rownania_czwartego_stopnia_z_trzema_niewiadomymi();
  int q;
  scanf("%d", &q);
  while (q--) {
    int a, b;
    scanf("%d%d", &a, &b);
    printf("%d\n", zapytanie(a, b));
  }
  return 0;
}
