#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
vector<pair<ll, ll>> adj[100];
vector<bool> vis(100, 0);
bool dfs(ll u, ll v, ll col) {
  vis[u] = 1;
  if (u == v) return 1;
  for (auto edge : adj[u]) {
    if (edge.second == col && !vis[edge.first]) {
      if (dfs(edge.first, v, col)) return 1;
    }
  }
  return 0;
}
void solveCP311() {
  ll n, m;
  cin >> n >> m;
  for (ll i = 0; i < m; i++) {
    ll u, v, c;
    cin >> u >> v >> c;
    u--, v--, c--;
    adj[u].push_back({v, c});
    adj[v].push_back({u, c});
  }
  ll q;
  cin >> q;
  while (q--) {
    ll u, v;
    cin >> u >> v;
    u--, v--;
    ll ans = 0;
    for (ll col = 0; col < 100; col++) {
      for (ll i = 0; i < 100; i++) vis[i] = 0;
      if (dfs(u, v, col)) ans++;
    }
    cout << ans << endl;
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll t = 1;
  while (t--) {
    solveCP311();
  }
  return 0;
}
