#include <bits/stdc++.h>
using namespace std;
long long int n, m;
vector<pair<long long int, long long int> > vec[105];
map<pair<long long int, long long int>, long long int> mp;
long long int solve(long long int u, long long int c, long long int vv) {
  mp[{u, c}] = 1;
  if (u == vv) return 1;
  long long int val = 0;
  for (long long int i = 0; i < vec[u].size(); i++) {
    pair<long long int, long long int> v = vec[u][i];
    if (mp[v] == 0) {
      if (c == -1 || v.second == c) {
        val += solve(v.first, v.second, vv);
      }
    }
  }
  return val;
}
int main() {
  cin >> n >> m;
  for (long long int i = 0; i < m; i++) {
    long long int x, y, c;
    cin >> x >> y >> c;
    vec[x].push_back({y, c});
    vec[y].push_back({x, c});
  }
  long long int q;
  cin >> q;
  while (q--) {
    long long int u, v;
    cin >> u >> v;
    cout << solve(u, -1, v) << endl;
    mp.clear();
  }
}
