#include <bits/stdc++.h>
using namespace std;
int n, m, q, a, b, c, s, t;
bool v[105];
vector<int> adj[105][105];
int dp[105][105][105];
int dfs(int par, int cur, int t, int cg) {
  v[cur] = true;
  if (cur == t) return 1;
  for (int x : adj[cur][cg]) {
    if (!v[x])
      if (dfs(cur, x, t, cg)) return 1;
  }
  return 0;
}
int main() {
  memset(dp, 0, sizeof dp);
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    scanf("%d%d%d", &a, &b, &c);
    adj[a][c].push_back(b);
    adj[b][c].push_back(a);
  }
  scanf("%d", &q);
  for (int i = 1; i <= q; i++) {
    scanf("%d%d", &s, &t);
    int ans = 0;
    for (int j = 1; j <= m; j++) {
      memset(v, 0, sizeof v);
      ans += dfs(0, s, t, j);
    }
    printf("%d\n", ans);
  }
  return 0;
}
