#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100005;
int N, M, Q;
int u[MAXN], v[MAXN];
int fa[MAXN];
int ans[MAXN], flag[MAXN];
map<pair<int, int>, int> Map;
vector<pair<int, int> > cedge[MAXN];
int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }
void merge(int x, int y) {
  x = find(x);
  y = find(y);
  if (x != y) {
    fa[y] = x;
  }
}
bool same(int x, int y) { return find(x) == find(y); }
int MAIN() {
  scanf("%d%d", &N, &M);
  for (int i = 0; i < N; ++i) {
    fa[i] = i;
  }
  int bd = 1 + (int)sqrt(N + 1.);
  int a, b, c;
  for (int i = 0; i < M; ++i) {
    scanf("%d%d%d", &a, &b, &c);
    a--, b--, c--;
    cedge[c].push_back(make_pair(a, b));
  }
  scanf("%d", &Q);
  for (int i = 0; i < Q; ++i) {
    scanf("%d%d", &u[i], &v[i]);
    u[i]--, v[i]--;
  }
  int tag = 0;
  for (int i = 0; i < M; ++i) {
    int size = (int)cedge[i].size();
    if (size == 0) continue;
    tag++;
    vector<int> vs;
    for (int j = 0; j < size; ++j) {
      vs.push_back(cedge[i][j].first);
      vs.push_back(cedge[i][j].second);
    }
    sort(vs.begin(), vs.end());
    vs.erase(unique(vs.begin(), vs.end()), vs.end());
    for (int j = 0; j < vs.size(); ++j) {
      fa[vs[j]] = vs[j];
      flag[vs[j]] = tag;
    }
    for (int j = 0; j < size; ++j) {
      merge(cedge[i][j].first, cedge[i][j].second);
    }
    if (vs.size() >= bd) {
      for (int j = 0; j < Q; ++j) {
        if (flag[u[j]] == tag && flag[v[j]] == tag) {
          if (same(u[j], v[j])) ans[j]++;
        }
      }
    } else {
      for (int j = 0; j < vs.size(); ++j) {
        for (int k = j + 1; k < vs.size(); ++k) {
          if (same(vs[j], vs[k])) {
            Map[make_pair(vs[j], vs[k])]++;
            Map[make_pair(vs[k], vs[j])]++;
          }
        }
      }
    }
  }
  for (int i = 0; i < Q; ++i) {
    printf("%d\n", ans[i] + Map[make_pair(u[i], v[i])]);
  }
  return 0;
}
int main() {
  ios ::sync_with_stdio(false);
  cout << fixed << setprecision(16);
  return MAIN();
}
