#include <bits/stdc++.h>
using namespace std;
int nvertices, nedges;
vector<vector<pair<int, int>>> adjList(110);
vector<bool> visited;
void dfs(int u, int c) {
  visited[u] = true;
  for (auto e : adjList[u]) {
    if (!visited[e.first] && (e.second == c)) {
      dfs(e.first, c);
    }
  }
}
int main() {
  cin >> nvertices >> nedges;
  for (int i = 0; i < nedges; i++) {
    int u, v, c;
    cin >> u >> v >> c;
    adjList[u].push_back({v, c});
    adjList[v].push_back({u, c});
  }
  int m;
  cin >> m;
  while (m--) {
    int u, v;
    cin >> u >> v;
    set<int> ans;
    for (auto e : adjList[u]) {
      visited.assign(110, false);
      visited[u] = true;
      dfs(e.first, e.second);
      if (visited[v] == true) ans.insert(e.second);
    }
    cout << ans.size() << endl;
  }
  return 0;
}
