#include <bits/stdc++.h>
using namespace std;
int M[101][101][101], flag[101], n, m, x, y, z;
int dfs(int u, int col) {
  if (u == y) return true;
  for (int i = 1; i <= n; i++)
    if (!flag[i] && M[u][i][col]) {
      flag[i] = 1;
      if (dfs(i, col)) return true;
    }
  return false;
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> x >> y >> z;
    M[x][y][z] = M[y][x][z] = 1;
  }
  int q;
  cin >> q;
  while (q--) {
    cin >> x >> y;
    int ans = 0;
    for (int i = 1; i <= m; i++) {
      memset(flag, 0, sizeof(flag));
      if (dfs(x, i)) ans++;
    }
    cout << ans << endl;
  }
}
