#include <bits/stdc++.h>
const double pi = acos(-1);
const long long INF = 1e18 + 5;
const long long mod = 1e9 + 7, MOD = 998244353;
const int N = 2e5 + 5, M = 505;
using namespace std;
int n, m, vid = 0;
vector<pair<int, int> > adj[101];
bool vis[101];
bool dfs(int Src, int Dest, int Color) {
  if (Src == Dest) return 1;
  bool ret = 0;
  for (int i = 0; i < adj[Src].size(); i++) {
    if (!vis[adj[Src][i].first] && adj[Src][i].second == Color) {
      vis[adj[Src][i].first] = 1;
      ret += dfs(adj[Src][i].first, Dest, Color);
    }
  }
  return ret;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int u, v, c;
    cin >> u >> v >> c;
    adj[u].push_back({v, c});
    adj[v].push_back({u, c});
  }
  int q;
  cin >> q;
  while (q--) {
    int u, v, ans = 0;
    cin >> u >> v;
    for (int i = 1; i <= m; i++) {
      for (int j = 0; j < 101; j++) vis[j] = 0;
      ans += dfs(u, v, i);
    }
    cout << ans << "\n";
  }
  return 0;
}
