#include <bits/stdc++.h>
using namespace std;
int n, m;
bool c[101][101][101];
bool bfs(int u, int v, int p) {
  queue<int> q;
  bool visit[101] = {false};
  q.push(u);
  visit[u] = true;
  while (!q.empty()) {
    int cur = q.front();
    q.pop();
    for (int i = 1; i <= n; i++)
      if (c[p][cur][i]) {
        if (!visit[i]) {
          visit[i] = true;
          q.push(i);
        }
      }
  }
  return visit[v];
}
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 0; i < m; i++) {
    int a, b, t;
    scanf("%d %d %d", &a, &b, &t);
    c[t][a][b] = c[t][b][a] = true;
  }
  int q;
  scanf("%d", &q);
  while (q--) {
    int s, e;
    int cnt = 0;
    scanf("%d %d", &s, &e);
    for (int i = 1; i <= m; i++) {
      if (bfs(s, e, i)) cnt++;
    }
    printf("%d\n", cnt);
  }
}
