#include <bits/stdc++.h>
using namespace std;
const int maxn = 105;
vector<pair<int, int>> v[maxn];
int vis[maxn];
bool dfs(int start, int des, int x) {
  if (vis[start]) return 0;
  if (start == des) return 1;
  vis[start] = 1;
  for (auto e : v[start]) {
    if (e.second == x) {
      if (dfs(e.first, des, x)) return 1;
    }
  }
  return 0;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0);
  int n, m;
  int x, y, z;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> x >> y >> z;
    v[x].push_back({y, z});
    v[y].push_back({x, z});
  }
  int q;
  cin >> q;
  while (q--) {
    cin >> x >> y;
    int ans = 0;
    for (int i = 1; i <= m; i++) {
      memset(vis, 0, sizeof(vis));
      if (dfs(x, y, i)) ans++;
    }
    cout << ans << endl;
  }
  return 0;
}
