#include <bits/stdc++.h>
using namespace std;
struct edge {
  int u, v, c;
};
set<int> s;
vector<pair<int, int> > g[100001];
map<pair<int, int>, int> ss;
edge ed[200000];
int parent[200000], used[200000], d1[200000], q, d2[200000], k, n, m, x, kl, y,
    z, v, ranka[200000];
bool CMP(const edge &i, const edge &j) { return (i.c < j.c); }
int findset(int x) {
  if (used[x] != k) {
    parent[x] = 0;
    ranka[x] = 0;
    used[x] = k;
    return x;
  }
  if (parent[x] == 0) return x;
  parent[x] = findset(parent[x]);
  return parent[x];
}
void unionset(int x, int y) {
  if (used[x] != k) {
    parent[x] = 0;
    ranka[x] = 0;
    used[x] = k;
  }
  if (used[y] != k) {
    parent[y] = 0;
    ranka[y] = 0;
    used[y] = k;
  }
  x = findset(x);
  y = findset(y);
  if (x == y) return;
  if (ranka[x] < ranka[y]) swap(x, y);
  if (ranka[x] == ranka[y]) ranka[x]++;
  parent[y] = x;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    cin >> x >> y >> z;
    ed[i].u = x;
    ed[i].v = y;
    ed[i].c = z;
  }
  sort(ed + 1, ed + m + 1, CMP);
  ed[0].c = 0;
  int ls = 0;
  for (int i = 1; i <= m; i++) {
    if (ed[i].c != ed[i - 1].c) ls = i;
    if (ed[i].c != ed[i + 1].c) {
      k++;
      s.clear();
      for (int j = ls; j <= i; j++) {
        s.insert(ed[j].u);
        s.insert(ed[j].v);
        unionset(ed[j].u, ed[j].v);
      }
      set<int>::iterator it = s.begin();
      while (it != s.end()) {
        kl++;
        d1[kl] = *it;
        d2[kl] = findset(d1[kl]);
        g[d1[kl]].push_back(make_pair(ed[i].c, d2[kl]));
        it++;
      }
    }
  }
  for (int i = 1; i <= n; i++) sort(g[i].begin(), g[i].end());
  cin >> q;
  for (int i = 1; i <= q; i++) {
    cin >> x >> y;
    if (g[x].size() < g[y].size()) swap(x, y);
    if (ss.find(make_pair(x, y)) != ss.end())
      cout << ss[make_pair(x, y)] << "\n";
    else {
      int ans = 0;
      for (int j = 0; j < g[y].size(); j++) {
        std::vector<pair<int, int> >::iterator it;
        it = std::lower_bound(g[x].begin(), g[x].end(), g[y][j]);
        if (it->first == g[y][j].first && it->second == g[y][j].second) ans++;
      }
      ss[make_pair(x, y)] = ans;
      cout << ans << "\n";
    }
  }
  return 0;
}
