#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int> > adj[200];
bool seen[201] = {false};
void DFS(int x, int y) {
  seen[x] = true;
  for (int i = 0; i < adj[x].size(); i++) {
    if (adj[x][i].second == y && !seen[adj[x][i].first]) {
      DFS(adj[x][i].first, y);
    }
  }
}
int ans;
void solve(int a, int b, int y) {
  for (int j = 0; j <= y; j++) {
    memset(seen, false, 201);
    DFS(a, j);
    if (seen[b]) ans++;
  }
}
int main() {
  int x, y;
  cin >> x >> y;
  for (int i = 0; i < y; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    adj[a].push_back(make_pair(b, c));
    adj[b].push_back(make_pair(a, c));
  }
  int T;
  cin >> T;
  while (T--) {
    int a, b;
    cin >> a >> b;
    ans = 0;
    solve(a, b, y);
    cout << ans << endl;
  }
  return 0;
}
