#include <bits/stdc++.h>
using namespace std;
int graph[105][105][105];
int main() {
  int n, m;
  cin >> n >> m;
  int x, y, z;
  for (int i = 0; i < m; i++) {
    cin >> x >> y >> z;
    graph[x][y][z] = 1;
    graph[y][x][z] = 1;
  }
  for (int t = 1; t <= n; t++) {
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        for (int k = 1; k <= 100; k++) {
          if (graph[i][t][k] == 1 && graph[t][j][k] == 1) graph[i][j][k] = 1;
        }
      }
    }
  }
  int q;
  cin >> q;
  while (q--) {
    int a, b;
    cin >> a >> b;
    int cnt = 0;
    for (int i = 1; i <= 100; i++)
      if (graph[a][b][i] == 1) cnt++;
    cout << cnt << "\n";
  }
  return 0;
}
