#include <bits/stdc++.h>
using namespace std;
const int M = 1e9 + 7;
const int N = 1e4;
long long po(int, int);
vector<pair<int, int>> e[N];
vector<bool> vis;
set<int> s;
void dfs(int u, int c, int tr) {
  vis[u] = 1;
  if (u == tr) {
    s.insert(c);
    return;
  }
  for (auto i : e[u]) {
    if (!vis[i.first] && i.second == c) {
      dfs(i.first, c, tr);
    }
  }
}
void solve() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int x, y, colour;
    cin >> x >> y >> colour;
    e[x].push_back({y, colour});
    e[y].push_back({x, colour});
  }
  int q;
  cin >> q;
  while (q--) {
    int start, target;
    cin >> start >> target;
    s.clear();
    for (auto i : e[start]) {
      vis.assign(n + 1, 0);
      vis[start] = 1;
      dfs(i.first, i.second, target);
    }
    cout << s.size() << "\n";
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t = 1;
  for (int i = 1; i < t + 1; i++) {
    solve();
  }
  return 0;
}
long long po(int a, int b) {
  if (b == 0) return 1;
  long long ans = 1;
  if (b % 2 == 0) {
    ans = po(a, b / 2) % M;
    ans = (ans % M * ans % M) % M;
  } else {
    ans = po(a, (b - 1) / 2) % M;
    ans = (ans % M * ans % M * a) % M;
  }
  return ans % M;
}
