#include <bits/stdc++.h>
using namespace std;
void dfs(vector<pair<int, int> > adj[], vector<bool>& visited, int u, int c) {
  visited[u] = true;
  vector<pair<int, int> >::iterator it1;
  for (it1 = adj[u].begin(); it1 != adj[u].end(); it1++) {
    if (!visited[(*it1).first] && (*it1).second == c) {
      dfs(adj, visited, (*it1).first, c);
    }
  }
}
signed main() {
  int n, m;
  cin >> n >> m;
  vector<pair<int, int> > adj[101];
  while (m--) {
    int a, b, c;
    cin >> a >> b >> c;
    adj[a].push_back(make_pair(b, c));
    adj[b].push_back(make_pair(a, c));
  }
  int q;
  cin >> q;
  while (q--) {
    int x, y;
    cin >> x >> y;
    int ans = 0;
    for (int i = 1; i <= 100; i++) {
      vector<bool> visited(101, false);
      dfs(adj, visited, x, i);
      if (visited[y]) ans++;
    }
    cout << ans << endl;
  }
  return 0;
}
