#include <bits/stdc++.h>
using namespace std;
using ii = pair<int, int>;
const int N = 105;
vector<ii> g[N];
bool bfs(int u, int nxt, int c) {
  int vis[N];
  memset(vis, 0, sizeof vis);
  vis[u]++;
  queue<ii> q;
  q.push({u, -1});
  while (!q.empty()) {
    u = q.front().first;
    q.pop();
    if (vis[nxt]) return true;
    for (auto v : g[u]) {
      if (vis[v.first] || v.second != c) continue;
      vis[v.first]++;
      q.push(v);
    }
  }
  return false;
}
int main() {
  int n, m;
  cin >> n >> m;
  int a, b, c;
  set<int> col;
  for (int i = 1; i <= m; i++) {
    cin >> a >> b >> c;
    g[a].push_back({b, c});
    g[b].push_back({a, c});
    col.insert(c);
  }
  int q;
  cin >> q;
  while (q--) {
    int u, v;
    cin >> u >> v;
    int cnt = 0;
    for (auto x : col) {
      cnt += bfs(u, v, x);
    }
    cout << cnt << "\n";
  }
}
