#include <bits/stdc++.h>
using namespace std;
vector<bool> visited;
vector<bool> visitedColor;
vector<vector<pair<int, int>>> V;
int ans;
void dfs(int curr, int v, int color) {
  if (curr == v) {
    if (!visitedColor.at(color)) {
      visitedColor.at(color) = true;
      ++ans;
    }
    return;
  }
  visited.at(curr) = true;
  for (auto [node, c] : V.at(curr)) {
    if ((c == color) && !visited.at(node)) {
      dfs(node, v, color);
    }
  }
  visited.at(curr) = false;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m;
  cin >> n >> m;
  V = vector<vector<pair<int, int>>>(n + 1);
  visited = vector<bool>(n + 1);
  int n1, n2, c;
  for (size_t i = 0; i < m; i++) {
    cin >> n1 >> n2 >> c;
    V.at(n1).push_back(make_pair(n2, c));
    V.at(n2).push_back(make_pair(n1, c));
  }
  int q, u, v;
  cin >> q;
  for (size_t i = 0; i < q; i++) {
    visitedColor = vector<bool>(m + 1);
    cin >> u >> v;
    ans = 0;
    visited.at(u) = true;
    for (auto [node, color] : V.at(u)) {
      dfs(node, v, color);
    }
    visited.at(u) = false;
    cout << ans << endl;
  }
  return 0;
}
