#include <bits/stdc++.h>
using namespace std;
vector<vector<pair<int, int>>> v;
int visited[100];
void dfs(int i, int final, int color) {
  if (i == final) {
    visited[final] = 1;
    return;
  }
  visited[i] = 1;
  for (auto j : v[i]) {
    if (visited[j.first] == 0 && j.second == color) {
      dfs(j.first, final, color);
    }
  }
}
int solve(int x, int y, int c) {
  memset(visited, 0, sizeof(visited));
  dfs(x, y, c);
  if (visited[y] == 1) {
    return 1;
  } else {
    return 0;
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  v.resize(n);
  for (int i = 0; i < m; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    v[b - 1].push_back({a - 1, c - 1});
    v[a - 1].push_back({b - 1, c - 1});
  }
  int q;
  cin >> q;
  while (q--) {
    int x, y;
    cin >> x >> y;
    int ans = 0;
    for (int i = 0; i < m; i++) {
      ans = ans + solve(x - 1, y - 1, i);
    }
    cout << ans << '\n';
  }
  return 0;
}
