#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> v;
vector<vector<pair<int, int>>> g;
vector<int> used;
int N, M;
void dfs(int i, int j, int I, int m) {
  if (i == j) v[I][j]++;
  used[i] = 1;
  for (auto u : g[i]) {
    if (!used[u.second] && m == u.first) dfs(u.second, j, I, m);
  }
}
int main() {
  cin >> N >> M;
  g.resize(N + 1);
  used.resize(N + 1);
  v.resize(N + 1, vector<int>(N + 1));
  for (int i = 0; i < M; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    g[a].push_back({c, b});
    g[b].push_back({c, a});
  }
  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= N; j++) {
      if (i == j) continue;
      for (int f = 1; f <= M; f++) {
        used.assign(N + 1, 0);
        dfs(i, j, i, f);
      }
    }
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int a, b;
    cin >> a >> b;
    cout << v[a][b] << '\n';
  }
}
