#include <bits/stdc++.h>
using namespace std;
vector<bool> cUsed(111, false);
std::vector<bool> visited(111, false);
std::vector<std::vector<std::vector<int> > > g(
    111, std::vector<std::vector<int> >(111));
void dfs(int c, int u) {
  if (visited[u]) return;
  visited[u] = 1;
  for (int i = 0; i < g[c][u].size(); ++i) {
    int v = g[c][u][i];
    dfs(c, v);
  }
}
int main() {
  int n, m, q;
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    int a, b, c;
    cin >> a >> b >> c;
    cUsed[c] = 1;
    g[c][a].push_back(b);
    g[c][b].push_back(a);
  }
  cin >> q;
  for (int i = 0; i < q; ++i) {
    int u, v, ans = 0;
    cin >> u >> v;
    for (int c = 1; c <= 100; ++c) {
      if (!cUsed[c]) continue;
      fill(visited.begin(), visited.end(), 0);
      dfs(c, u);
      if (visited[v]) {
        ans++;
      }
    }
    cout << ans << endl;
  }
  return 0;
}
