#include <bits/stdc++.h>
using namespace std;
vector<long long int> adj[101];
set<pair<long long int, long long int>> color[101];
long long int visited[101];
long long int x;
void bfs(long long int a, long long int b, long long int c) {
  queue<long long int> q;
  q.push(a);
  while (!q.empty()) {
    long long int p = q.front();
    q.pop();
    if (p == b) {
      x = 1;
      visited[p] = 1;
    }
    if (visited[p]) continue;
    visited[p] = 1;
    for (long long int i = 0; i < adj[p].size(); i++) {
      if (color[c].find({p, adj[p][i]}) != color[c].end()) {
        q.push(adj[p][i]);
      }
    }
  }
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long int i, j, k, l, m, n, p, t, a, b, c, q;
  cin >> n >> m;
  set<pair<long long int, long long int>> s;
  for (i = 0; i < m; i++) {
    cin >> a >> b >> c;
    if (s.find({a, b}) == s.end()) {
      s.insert({a, b});
      adj[a].push_back(b);
      adj[b].push_back(a);
    }
    color[c].insert({a, b});
    color[c].insert({b, a});
  }
  cin >> q;
  for (i = 0; i < q; i++) {
    cin >> a >> b;
    p = 0;
    for (j = 1; j <= m; j++) {
      x = 0;
      bfs(a, b, j);
      p += x;
      memset(visited, 0, sizeof(visited));
    }
    cout << p << "\n";
  }
}
