#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
vector<pair<int, int> > adj[110];
int visit[110][110];
int resp = 0;
void dfs(int n, int k, int cor) {
  if (cor != -1) {
    if (visit[n][cor] == 1) return;
    if (n == k) {
      visit[n][cor] = 1;
      resp++;
      return;
    }
    visit[n][cor] = 1;
  }
  for (int i = 0; i < adj[n].size(); i++) {
    if (adj[n][i].second == cor || cor == -1)
      dfs(adj[n][i].first, k, adj[n][i].second);
  }
  return;
}
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    adj[a].push_back({b, c});
    adj[b].push_back({a, c});
  }
  int k;
  cin >> k;
  for (int i = 0; i < k; i++) {
    memset(visit, 0, sizeof(visit));
    resp = 0;
    int p, l;
    cin >> p >> l;
    dfs(p, l, -1);
    cout << resp << endl;
  }
  return 0;
}
