#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int maxn = 1e5 + 5;
int n, m, q;
vector<int> po;
vector<pair<int, int>> edg[maxn];
map<pair<int, int>, int> res;
int fa[maxn], ans[maxn], qu[maxn], qv[maxn], cor[maxn];
int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); }
void merge(int x, int y) { fa[x] = y; }
void input() {
  int i, s, e, cor;
  scanf("%d%d", &n, &m);
  for (i = 1; i <= m; i++) {
    scanf("%d%d%d", &s, &e, &cor);
    edg[cor].push_back(make_pair(s, e));
  }
  scanf("%d", &q);
  for (i = 1; i <= q; i++) {
    scanf("%d%d", &qu[i], &qv[i]);
  }
}
void solve() {
  int i, j, k;
  for (i = 1; i <= m; i++) {
    int sz = edg[i].size();
    if (sz == 0) continue;
    po.clear();
    for (j = 0; j < sz; j++) {
      po.push_back(edg[i][j].first);
      po.push_back(edg[i][j].second);
    }
    sort(po.begin(), po.end());
    po.erase(unique(po.begin(), po.end()), po.end());
    int po_sz = po.size();
    for (j = 0; j < po_sz; j++) {
      int poin = po[j];
      cor[poin] = i;
      fa[poin] = poin;
    }
    for (j = 0; j < sz; j++) {
      int u = edg[i][j].first;
      int v = edg[i][j].second;
      if (find(u) != find(v)) {
        merge(find(u), find(v));
      }
    }
    if ((long long)po_sz * (long long)po_sz > n) {
      for (j = 1; j <= q; j++) {
        if (cor[qu[j]] == i && cor[qv[j]] == i) {
          if (find(qu[j]) == find(qv[j])) {
            ans[j]++;
          }
        }
      }
    } else {
      for (j = 0; j < po_sz; j++) {
        for (k = j + 1; k < po_sz; k++) {
          if (find(po[j]) == find(po[k])) {
            res[make_pair(po[k], po[j])]++;
            res[make_pair(po[j], po[k])]++;
          }
        }
      }
    }
  }
  for (i = 1; i <= q; i++) {
    printf("%d\n", ans[i] + res[make_pair(qu[i], qv[i])]);
  }
}
int main() {
  input();
  solve();
  return 0;
}
