#include <bits/stdc++.h>
using namespace std;
const int OO = (int)1e5;
const double PI = acos(-1.0);
const double EPS = (1e-9);
void fast() {
  std::ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
}
vector<pair<int, int> > g[110];
int n, m, a, b, c, q, u, v;
bool vis[110];
int path(int x, int y, int clr) {
  vis[x] = 1;
  queue<int> q;
  q.push(x);
  while (!q.empty()) {
    int cur = q.front();
    q.pop();
    if (cur == y) return 1;
    for (auto i : g[cur])
      if (i.second == clr && !vis[i.first]) q.push(i.first), vis[i.first] = 1;
  }
  return 0;
}
int main() {
  fast();
  cin >> n >> m;
  for (int(i) = 0; (i) < (int)m; (i)++) {
    cin >> a >> b >> c;
    g[a].push_back({b, c});
    g[b].push_back({a, c});
  }
  cin >> q;
  while (q--) {
    cin >> u >> v;
    int ans = 0;
    for (int i = 1; i <= 100; i++) {
      memset(vis, 0, sizeof(vis));
      ;
      ans += path(u, v, i);
    }
    cout << ans << endl;
  }
}
