#include <bits/stdc++.h>
using namespace std;
const long long int N = 105;
vector<pair<long long int, long long int>> adj[N];
vector<bool> vis(N);
bool dfs(long long int u, long long int v, long long int col) {
  vis[u] = true;
  if (u == v) {
    return true;
  }
  for (auto i : adj[u]) {
    long long int vert = i.first;
    long long int color = i.second;
    if (color == col && !vis[vert]) {
      if (dfs(vert, v, col)) {
        return true;
      }
    }
  }
  return false;
}
void assignFalse() {
  for (long long int i = 0; i < N; i++) {
    vis[i] = false;
  }
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long int n, m;
  cin >> n >> m;
  for (long long int i = 1; i <= m; i++) {
    long long int u, v, c;
    cin >> u >> v >> c;
    adj[u].push_back({v, c});
    adj[v].push_back({u, c});
  }
  long long int Q;
  cin >> Q;
  while (Q--) {
    long long int cnt = 0;
    long long int u, v;
    cin >> u >> v;
    for (long long int i = 1; i <= 100; i++) {
      assignFalse();
      if (dfs(u, v, i)) {
        cnt++;
      }
    }
    cout << cnt << endl;
  }
  return 0;
}
