#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> adj[101];
int vis[101][101];
int id;
bool dfs(const int &idx, int cur, int to) {
  if (cur == to) return true;
  bool ret = false;
  for (int i = 0; i < (int)adj[idx][cur].size(); i++) {
    if (vis[idx][adj[idx][cur][i]] == id) continue;
    vis[idx][adj[idx][cur][i]] = id;
    ret |= dfs(idx, adj[idx][cur][i], to);
  }
  return ret;
}
int main() {
  int n, m, q;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < 101; i++) adj[i].resize(n);
  for (int i = 0, u, v, c; i < m; i++) {
    scanf("%d%d%d", &u, &v, &c);
    adj[c - 1][u - 1].push_back(v - 1);
    adj[c - 1][v - 1].push_back(u - 1);
  }
  int u, v;
  scanf("%d", &q);
  while (q--) {
    id++;
    scanf("%d%d", &u, &v);
    int ans = 0;
    for (int i = 0; i < m; i++) ans += dfs(i, u - 1, v - 1);
    printf("%d\n", ans);
  }
}
