#include <bits/stdc++.h>
using namespace std;
int dp[104][104][104] = {0};
int main() {
  short int i, j, k, l, m, t, c, q, n;
  cin >> n >> m;
  set<int> vs;
  for (i = 0; i < m; i++) {
    cin >> j >> k >> l;
    dp[j][k][l] = 1;
    dp[k][j][l] = 1;
    vs.insert(l);
  }
  for (int k = 1; k <= n; ++k) {
    for (int i = 1; i <= n; ++i)
      for (int j = 1; j <= n; ++j) {
        for (auto c : vs) {
          if ((dp[i][k][c] && dp[k][j][c])) dp[i][j][c] = 1;
        }
      }
  }
  cin >> q;
  for (i = 0; i < q; i++) {
    int ans = 0;
    cin >> j >> k;
    for (auto c : vs)
      if (dp[j][k][c] == 1) ans++;
    cout << ans << endl;
  }
  return 0;
}
