#include <bits/stdc++.h>
using namespace std;
int n, m, a, b, c, q, ans, p[101][101], col;
int f(int number) {
  if (p[col][number] == 0) return 0;
  if (p[col][number] != number) p[col][number] = f(p[col][number]);
  return p[col][number];
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> a >> b >> col;
    if (p[col][a] == 0) p[col][a] = a;
    if (p[col][b] == 0)
      p[col][b] = p[col][a];
    else
      p[col][f(b)] = p[col][a];
    for (int j = 1; j < n + 1; j++) p[col][j] = f(j);
  }
  for (col = 1; col <= m; col++)
    for (int j = 1; j <= n; j++) p[col][j] = f(j);
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> a >> b;
    ans = 0;
    for (col = 1; col <= m; col++)
      if (p[col][a] != 0 && p[col][b] != 0 && p[col][a] == p[col][b]) ans++;
    cout << ans << '\n';
  }
  return 0;
}
