#include <bits/stdc++.h>
using namespace std;
int N, Ed, Q, Par[100005];
map<pair<int, int>, int> M;
vector<pair<int, int>> C[100005], E[100005];
int find(int n) { return Par[n] == n ? n : Par[n] = find(Par[n]); }
void join(int a, int b) { Par[find(a)] = find(b); }
void process(int c) {
  for (pair<int, int> p : E[c])
    Par[p.first] = p.first, Par[p.second] = p.second;
  for (pair<int, int> p : E[c]) join(p.first, p.second);
  for (pair<int, int> p : E[c]) {
    if (C[p.first].empty() || C[p.first].back().first != c)
      C[p.first].push_back({c, find(p.first)});
    if (C[p.second].empty() || C[p.second].back().first != c)
      C[p.second].push_back({c, find(p.second)});
  }
  for (pair<int, int> p : E[c])
    Par[p.first] = p.first, Par[p.second] = p.second;
}
int query(int a, int b) {
  if (((int)C[a].size()) > ((int)C[b].size())) swap(a, b);
  if (M.find({a, b}) != M.end()) return M[{a, b}];
  int ret = 0;
  for (pair<int, int> p : C[a]) {
    auto it = lower_bound(C[b].begin(), C[b].end(), p);
    if (it != C[b].end() && *it == p) ret++;
  }
  return M[{a, b}] = ret;
}
int main() {
  scanf("%d%d", &N, &Ed);
  for (int i = (1); i <= (Ed); i++) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    E[c].push_back({a, b});
  }
  for (int c = (1); c <= (Ed); c++) process(c);
  for (int c = (1); c <= (Ed); c++) sort(C[c].begin(), C[c].end());
  cin >> Q;
  while (Q--) {
    int a, b;
    scanf("%d%d", &a, &b);
    printf("%d\n", query(a, b));
  }
}
