#include <bits/stdc++.h>
using namespace std;
template <int POS, class TUPLE>
void deploy(std::ostream &os, const TUPLE &tuple) {}
template <int POS, class TUPLE, class H, class... Ts>
void deploy(std::ostream &os, const TUPLE &t) {
  os << (POS == 0 ? "" : ", ") << get<POS>(t);
  deploy<POS + 1, TUPLE, Ts...>(os, t);
}
template <class... Ts>
std::ostream &operator<<(std::ostream &os, const std::tuple<Ts...> &t) {
  os << "(";
  deploy<0, std::tuple<Ts...>, Ts...>(os, t);
  os << ")";
  return os;
}
template <class T>
std::ostream &operator<<(std::ostream &os, std::vector<T> &v) {
  int remain = v.size();
  os << "{";
  for (auto e : v) os << e << (--remain == 0 ? "" : ", ");
  os << "}";
  return os;
}
template <class T>
std::ostream &operator<<(std::ostream &os, std::set<T> &v) {
  int remain = v.size();
  os << "{";
  for (auto e : v) os << e << (--remain == 0 ? "" : ", ");
  os << "}";
  return os;
}
template <class T>
std::ostream &operator<<(std::ostream &os, std::queue<T> &q) {
  auto qq = q;
  os << "{";
  for (; !qq.empty(); qq.pop()) {
    os << qq.front() << (qq.size() != 1 ? ", " : "");
  }
  os << "}";
  return os;
}
template <class T>
std::ostream &operator<<(std::ostream &os, std::priority_queue<T> &q) {
  auto qq = q;
  os << "{";
  for (; !qq.empty(); qq.pop()) {
    os << qq.top() << (qq.size() != 1 ? ", " : "");
  }
  os << "}";
  return os;
}
template <class T, class K>
std::ostream &operator<<(std::ostream &os, std::pair<T, K> &p) {
  os << "(" << p.first << ", " << p.second << ")";
  return os;
}
template <class T, class K>
std::ostream &operator<<(std::ostream &os, std::map<T, K> &mp) {
  int remain = mp.size();
  os << "{";
  for (auto e : mp)
    os << "(" << e.first << " -> " << e.second << ")"
       << (--remain == 0 ? "" : ", ");
  os << "}";
  return os;
}
template <class T, class K>
std::ostream &operator<<(std::ostream &os, std::unordered_map<T, K> &mp) {
  int remain = mp.size();
  os << "{";
  for (auto e : mp)
    os << "(" << e.first << " -> " << e.second << ")"
       << (--remain == 0 ? "" : ", ");
  os << "}";
  return os;
}
const int32_t MAX = 100500;
int32_t parents[MAX];
set<int32_t> used_parents;
void setUsed(int32_t pos) { used_parents.insert(pos); }
void initParents() {
  for (auto t : used_parents) parents[t] = t;
  used_parents.clear();
}
int32_t findParent(int32_t pos) {
  if (parents[pos] == pos) return pos;
  return parents[pos] = findParent(parents[pos]);
}
int main() {
  for (int32_t i = 0; i < ((int32_t)(MAX)); ++i) setUsed(i);
  initParents();
  unordered_map<int32_t, vector<tuple<int32_t, int32_t>>> edges;
  int32_t n, m;
  cin >> n >> m;
  for (int32_t i = 0; i < ((int32_t)(m)); ++i) {
    int32_t a, b, c;
    cin >> a >> b >> c;
    --a;
    --b;
    --c;
    edges[c].push_back(make_tuple(a, b));
  }
  int32_t q;
  cin >> q;
  vector<int32_t> ans(q, 0);
  vector<tuple<int32_t, int32_t>> input(q);
  for (int32_t i = 0; i < ((int32_t)(q)); ++i)
    cin >> get<0>(input[i]) >> get<1>(input[i]);
  for (int32_t i = 0; i < ((int32_t)(q)); ++i)
    --get<0>(input[i]), --get<1>(input[i]);
  map<pair<int32_t, int32_t>, int32_t> cnts;
  for (int32_t i = 0; i < ((int32_t)(q)); ++i)
    cnts[make_pair(get<0>(input[i]), get<1>(input[i]))] = 0;
  for (int32_t c = 0; c < ((int32_t)(m)); ++c) {
    vector<tuple<int32_t, int32_t>> &e = edges[c];
    for (auto tup : e) {
      int32_t a = get<0>(tup);
      int32_t b = get<1>(tup);
      setUsed(a);
      setUsed(b);
      parents[findParent(a)] = findParent(b);
    }
    if (((int32_t)((e).size())) < sqrt(MAX)) {
      for (auto t_0 : used_parents) {
        for (auto t_1 : used_parents) {
          if (findParent(t_0) == findParent(t_1) &&
              cnts.count(make_pair(t_0, t_1))) {
            cnts[make_pair(t_0, t_1)] += 1;
          }
        }
      }
    } else {
      for (int32_t i = 0; i < ((int32_t)(q)); ++i) {
        if (findParent(get<0>(input[i])) == findParent(get<1>(input[i])))
          ++ans[i];
      }
    }
    initParents();
  }
  for (int32_t i = 0; i < ((int32_t)(q)); ++i)
    ans[i] += cnts[make_pair(get<0>(input[i]), get<1>(input[i]))];
  for (int32_t i = 0; i < ((int32_t)(q)); ++i) cout << ans[i] << endl;
}
