#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
const int sq = 320;
int n, m, q;
int ans1[100005];
int ans2[100005];
char cnt[100005];
bool seen[100005];
vector<int> vq[100005];
vector<int> adj[100005];
vector<pair<int, int>> edg[100005];
vector<vector<int>> verts;
map<uint64_t, int> qnum;
map<uint64_t, vector<int>> qeq;
void dfs(int st) {
  seen[st] = true;
  verts.back().push_back(st);
  for (auto x : adj[st]) {
    if (!seen[x]) {
      dfs(x);
    }
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    edg[c].push_back({a, b});
  }
  scanf("%d", &q);
  for (int i = 1; i <= q; i++) {
    uint64_t a, b;
    scanf("%llu%llu", &a, &b);
    qnum[(a << 32) | b] = i;
    qnum[(b << 32) | a] = i;
    qeq[(a << 32) | b].push_back(i);
    qeq[(b << 32) | a].push_back(i);
    vq[a].push_back(i);
    vq[b].push_back(i);
  }
  for (int i = 1; i <= m; i++) {
    if (edg[i].empty()) continue;
    vector<int> cverts;
    for (int j = 0; j < verts.size(); j++) {
      for (auto x : verts[j]) {
        seen[x] = false;
        adj[x].clear();
      }
    }
    verts.clear();
    for (auto x : edg[i]) {
      adj[x.first].push_back(x.second);
      adj[x.second].push_back(x.first);
      cverts.push_back(x.first);
      cverts.push_back(x.second);
    }
    for (auto x : cverts) {
      if (!seen[x]) {
        verts.push_back({});
        dfs(x);
        if (verts.back().size() >= sq) {
          memset(cnt, 0, sizeof(cnt));
          for (auto y : verts.back()) {
            for (auto z : vq[y]) {
              cnt[z]++;
              ans1[z] += cnt[z] == 2;
            }
          }
          continue;
        }
        for (int j = 0; j < verts.back().size(); j++) {
          uint64_t v = uint64_t(verts.back()[j]) << 32;
          for (int k = j + 1; k < verts.back().size(); k++) {
            ans2[qnum[(v) | verts.back()[k]]]++;
          }
        }
      }
    }
  }
  for (auto x : qeq) {
    int tar = qnum[x.first];
    for (auto y : x.second) {
      ans2[y] = ans2[tar];
    }
  }
  for (int i = 1; i <= q; i++) printf("%d\n", ans1[i] + ans2[i]);
}
