#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<pair<int, int> > V[100005];
int par[100005], ppar[305][100005], p;
int f(int x) {
  if (par[x] == x)
    return x;
  else
    return par[x] = f(par[x]);
}
map<int, bool> M[100005];
map<int, bool>::iterator it, it2;
map<pair<int, int>, int> Q;
void g(int a) {
  if (f(a) == a) {
    for (it = M[a].begin(); it != M[a].end(); it++)
      for (it2 = M[a].begin(); it2 != M[a].end(); it2++) {
        int ta = (*it).first;
        int tb = (*it2).first;
        if (ta < tb) {
          if (Q.count(make_pair(ta, tb)))
            Q[make_pair(ta, tb)]++;
          else
            Q[make_pair(ta, tb)] = 1;
        }
      }
    M[a].clear();
  }
}
int main() {
  scanf("%d %d", &n, &m);
  for (int i = (1); i <= (m); i++) {
    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);
    V[c].push_back(make_pair(a, b));
  }
  for (int i = (1); i <= (n); i++) par[i] = i;
  for (int i = (1); i <= (m); i++) {
    int sz = V[i].size();
    for (int j = (0); j <= (sz - 1); j++) {
      int pa = f(V[i][j].first);
      int pb = f(V[i][j].second);
      if (pa != pb) par[pa] = pb;
    }
    if (sz >= 340) {
      p++;
      for (int i = (1); i <= (n); i++) ppar[p][i] = f(i);
    } else {
      for (int j = (0); j <= (sz - 1); j++) {
        int a = V[i][j].first, b = V[i][j].second;
        M[f(a)][a] = 1;
        M[f(b)][b] = 1;
      }
      for (int j = (0); j <= (sz - 1); j++) {
        int a = V[i][j].first, b = V[i][j].second;
        g(a);
        g(b);
      }
    }
    for (int j = (0); j <= (sz - 1); j++) {
      int a = V[i][j].first, b = V[i][j].second;
      par[a] = a;
      par[b] = b;
    }
  }
  int q, l, r;
  scanf("%d", &q);
  while (q--) {
    scanf("%d %d", &l, &r);
    if (l > r) swap(l, r);
    int res = 0;
    if (Q.count(make_pair(l, r))) res = Q[make_pair(l, r)];
    for (int i = (1); i <= (p); i++)
      if (ppar[i][l] == ppar[i][r]) res++;
    printf("%d\n", res);
  }
  return 0;
}
