#include <bits/stdc++.h>
using namespace std;
bool isPrime(long long n) {
  if (n <= 1) return false;
  if (n <= 3) return true;
  if (n % 2 == 0 || n % 3 == 0) return false;
  for (long long i = 5; i * i <= n; i = i + 6)
    if (n % i == 0 || n % (i + 2) == 0) return false;
  return true;
}
long long Ceil(long long a, long long b) {
  if (a % b == 0)
    return a / b;
  else
    return a / b + 1;
}
long long factorial(long long n) {
  if (n == 0) return 1;
  return (n * factorial(n - 1)) % 1000000007;
}
long long isPalindrome(string &str) {
  long long diff = 0;
  long long j = str.length() - 1;
  for (long long i = 0; i < j; i++, j--) {
    if (str[i] != str[j]) {
      diff++;
    }
  }
  return diff;
}
vector<pair<long long, long long>> v[102];
vector<bool> vis(102);
bool dfs(long long src, long long target, long long col) {
  vis[src] = true;
  if (src == target) return true;
  for (auto x : v[src]) {
    if (vis[x.first] == false && x.second == col) {
      if (dfs(x.first, target, col)) return true;
    }
  }
  return false;
}
void solve() {
  long long n, m;
  cin >> n >> m;
  for (long long i = 0; i < m; i++) {
    long long a, b, c;
    cin >> a >> b >> c;
    v[a].push_back({b, c});
    v[b].push_back({a, c});
  }
  long long q;
  cin >> q;
  for (long long i = 0; i < q; i++) {
    long long u, v;
    cin >> u >> v;
    long long ans = 0;
    for (long long i = 1; i <= 100; i++) {
      for (long long i = 0; i < 102; i++) vis[i] = false;
      if (dfs(u, v, i)) ans++;
    }
    cout << ans << endl;
  }
}
int32_t main() {
  std::ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  solve();
}
