#include <bits/stdc++.h>
using namespace std;
struct Edge {
  int x, y, c;
  bool operator<(const Edge &b) const { return c < b.c; }
} e[200010];
struct Qu {
  int x, y;
} a[200010];
set<int> s, t[200010];
map<pair<int, int>, int> ans;
vector<int> b[200010];
int n, m, q, tot, v[200010], f[200010];
int find(int i) {
  if (f[i] == i) return f[i];
  f[i] = find(f[i]);
  return f[i];
}
void work(int l, int r) {
  for (int i = l; i <= r; i++) f[find(e[i].x)] = find(e[i].y);
  s.clear(), tot = 0;
  for (int i = l; i <= r; i++) s.insert(e[i].x), s.insert(e[i].y);
  set<int>::iterator it = s.begin();
  while (it != s.end()) v[++tot] = *it, it++;
  sort(v + 1, v + tot + 1);
  if (tot <= 100)
    for (int i = 1; i <= tot; i++)
      for (int j = i + 1; j <= tot; j++)
        if (find(v[i]) == find(v[j])) ans[make_pair(v[i], v[j])]++;
  if (tot > 100)
    for (int i = 1; i <= n; i++)
      for (int j = 0; j < b[i].size(); j++)
        if (find(i) == find(b[i][j])) ans[make_pair(i, b[i][j])]++;
  for (int i = 1; i <= tot; i++) f[v[i]] = v[i];
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= m; i++) scanf("%d%d%d", &e[i].x, &e[i].y, &e[i].c);
  cin >> q;
  for (int i = 1; i <= q; i++) scanf("%d%d", &a[i].x, &a[i].y);
  for (int i = 1; i <= q; i++)
    if (a[i].x > a[i].y) swap(a[i].x, a[i].y);
  for (int i = 1; i <= q; i++) t[a[i].x].insert(a[i].y);
  for (int i = 1; i <= n; i++) {
    set<int>::iterator it = t[i].begin();
    while (it != t[i].end()) b[i].push_back(*it), it++;
  }
  sort(e + 1, e + m + 1);
  for (int i = 1; i <= n; i++) f[i] = i;
  int l = 1, r = 1;
  while (l <= m) {
    while ((r < m) && (e[r + 1].c == e[r].c)) r++;
    work(l, r);
    l = r + 1, r++;
  }
  for (int i = 1; i <= q; i++) printf("%d\n", ans[make_pair(a[i].x, a[i].y)]);
}
