#include <bits/stdc++.h>
using namespace std;
vector<long long> adj[200];
vector<pair<long long, pair<long long, long long> > > p;
long long vis[210000];
void dfs(long long node) {
  vis[node] = 1;
  for (long long u = 0; u < adj[node].size(); u++) {
    if (!vis[adj[node][u]]) dfs(adj[node][u]);
  }
}
int main() {
  long long n, m, q, a, b, c, j;
  cin >> n >> m;
  for (long long i = 0; i < m; ++i) {
    cin >> a >> b >> c;
    p.push_back(make_pair(c, make_pair(a, b)));
  }
  sort(p.begin(), p.end());
  cin >> q;
  for (long long g = 0; g < q; ++g) {
    long long ans = 0;
    cin >> a >> b;
    long long x;
    for (long long i = 0; i < p.size();) {
      x = p[i].first;
      j = i;
      while (p[j].first == x) {
        adj[p[j].second.first].push_back(p[j].second.second);
        adj[p[j].second.second].push_back(p[j].second.first);
        j++;
      }
      dfs(a);
      if (vis[b]) ans++;
      i = j;
      for (long long k = 0; k < 200; k++) {
        vis[k] = 0;
        adj[k].clear();
      }
    }
    cout << ans << endl;
  }
  return 0;
}
