#include <bits/stdc++.h>
using namespace std;
class DSU {
 public:
  vector<long long> p;
  DSU() {}
  DSU(long long n) {
    p.resize(n + 1);
    for (long long i = 1; i <= n; i++) p[i] = i;
  }
  void join(long long a, long long b) { p[find(b)] = find(a); }
  long long find(long long a) { return a == p[a] ? a : p[a] = find(p[a]); }
  bool check(long long a, long long b) { return find(a) == find(b); }
};
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long n, m;
  cin >> n >> m;
  DSU d(n);
  vector<DSU> D;
  for (long long i = 0; i <= m; i++) D.push_back(d);
  for (long long i = 0; i < m; i++) {
    long long u, v, c;
    cin >> u >> v >> c;
    D[c].join(u, v);
  }
  long long q;
  cin >> q;
  while (q--) {
    long long u, v;
    cin >> u >> v;
    long long ans = 0;
    for (long long i = 1; i <= m; i++)
      if (D[i].check(u, v)) ans++;
    cout << ans << endl;
  }
}
