#include <bits/stdc++.h>
using namespace std;
const int M = 110;
int a[M][M][M];
int use[M];
int n;
bool dfs(int u, int v, int c) {
  use[u] = 1;
  if (u == v) return true;
  int sum = 0;
  for (int i = 1; i <= n; i++) {
    if (a[u][i][c] == 1 && use[i] == 0) {
      if (dfs(i, v, c)) return true;
    }
  }
  return false;
}
int main() {
  int m;
  int x, y, z;
  int Max = -1;
  while (cin >> n >> m) {
    memset(a, 0, sizeof(a));
    while (m--) {
      cin >> x >> y >> z;
      a[x][y][z] = a[y][x][z] = 1;
      if (z >= Max) Max = z;
    }
    cin >> m;
    while (m--) {
      cin >> x >> y;
      int ans = 0;
      for (int i = 1; i <= Max; i++) {
        memset(use, 0, sizeof(use));
        if (dfs(x, y, i)) ans++;
      }
      cout << ans << endl;
    }
  }
  return 0;
}
