#include <bits/stdc++.h>
using namespace std;
bool mat[100][100][100];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL), cout.tie(NULL);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    int a, b, c;
    cin >> a >> b >> c;
    --a, --b, --c;
    mat[a][b][c] = 1;
    mat[b][a][c] = 1;
  }
  for (int col = 0; col < m; ++col) {
    for (int k = 0; k < n; ++k) {
      for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
          if ((mat[i][k][col] && mat[k][j][col])) {
            mat[i][j][col] = 1;
          }
        }
      }
    }
  }
  int q;
  cin >> q;
  while (q--) {
    int a, b;
    cin >> a >> b;
    --a, --b;
    cout << accumulate(mat[a][b], mat[a][b] + m, 0) << '\n';
  }
  return 0;
}
