#include <bits/stdc++.h>
using namespace std;
const long long N = 1004;
vector<pair<long long, long long>> gr[N];
long long vis[N] = {};
bool dfs(long long curr, long long &col, long long &dest) {
  vis[curr] = 1;
  if (curr == dest) return true;
  bool ans = 0;
  for (auto x : gr[curr]) {
    if (!vis[x.first] and x.second == col) {
      ans = ans or dfs(x.first, col, dest);
    }
  }
  return ans;
}
void solve(long long tc) {
  long long n, m;
  cin >> n >> m;
  long long x, y, c;
  for (long long i = 0; i < m; i++) {
    cin >> x >> y >> c;
    gr[x].push_back({y, c});
    gr[y].push_back({x, c});
  }
  long long q;
  cin >> q;
  for (long long i = 0; i < q; i++) {
    cin >> x >> y;
    long long ans = 0;
    for (long long j = 1; j <= m; j++) {
      memset(vis, 0, sizeof(vis));
      vis[x] = 1;
      ans += dfs(x, j, y);
    }
    cout << ans << "\n";
  }
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  solve(1);
  return 0;
}
