#include <bits/stdc++.h>
using namespace std;
vector<pair<long long, long long>> adj[101];
long long vis[101];
long long sum;
void dfs(long long n, long long des, long long c) {
  vis[n] = 1;
  if (n == des) {
    sum++;
    return;
  }
  for (auto x : adj[n]) {
    long long ch = x.first;
    long long col = x.second;
    if (vis[ch] == 0 && col == c) {
      dfs(ch, des, c);
    }
  }
}
void icchhipadey() {
  long long n, m;
  cin >> n >> m;
  long long a, b, c;
  long long q;
  for (int i = 1; i <= n; i++) vis[i] = 0;
  for (int i = 0; i < m; i++) {
    cin >> a >> b >> c;
    adj[a].push_back({b, c});
    adj[b].push_back({a, c});
  }
  cin >> q;
  long long ans = 0;
  for (int i = 0; i < q; i++) {
    cin >> a >> b;
    ans = 0;
    for (long long col = 1; col <= 100; col++) {
      sum = 0;
      for (int j = 1; j <= n; j++) {
        vis[j] = 0;
      }
      dfs(a, b, col);
      ans += sum;
    }
    cout << ans << endl;
  }
}
int main() { icchhipadey(); }
