#include <bits/stdc++.h>
struct Edge {
  int node;
  int color;
};
std::vector<Edge> edges[1 + 100];
bool colors[1 + 100][1 + 100];
bool viz[1 + 100];
void dfs(int node, int color) {
  viz[node] = 1;
  for (int i = 0; i < edges[node].size(); ++i)
    if (viz[edges[node][i].node] == 0 && edges[node][i].color == color)
      dfs(edges[node][i].node, color);
}
int main() {
  int N, M;
  std::cin >> N >> M;
  for (int i = 1; i <= M; ++i) {
    int x, y, c;
    std::cin >> x >> y >> c;
    edges[x].push_back((Edge){y, c});
    edges[y].push_back((Edge){x, c});
    colors[x][c] = 1;
    colors[y][c] = 1;
  }
  int Q;
  std::cin >> Q;
  for (int i = 1; i <= Q; ++i) {
    int x, y;
    std::cin >> x >> y;
    int ans = 0;
    for (int c = 1; c <= M; ++c) {
      if (colors[x][c] == 1 && colors[y][c] == 1) {
        dfs(x, c);
        if (viz[y] == 1) ++ans;
        for (int k = 1; k <= N; ++k) viz[k] = 0;
      }
    }
    std::cout << ans << '\n';
  }
  return 0;
}
