#include <bits/stdc++.h>
using namespace std;
template <class T>
bool uax(T& a, const T& b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
bool uin(T& a, const T& b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
const int N = 100500;
bool randb() {
  static int x = rand(), j = 15;
  if (--j < 0) x = rand(), j = 15;
  bool r = x & 1;
  x /= 2;
  return r;
}
vector<pair<int, int> > ce[N];
int dsu[N];
int get(int v) { return dsu[v] == v ? v : dsu[v] = get(dsu[v]); }
bool unite(int v, int u) {
  if ((v = get(v)) != (u = get(u))) {
    if (randb()) swap(u, v);
    dsu[v] = u;
    return true;
  } else
    return false;
}
int ans[N];
int cans[N];
vector<pair<int, int> > q[N];
int invrt[N];
int cvrt;
void solve() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < (int)(m); ++i) {
    int a, b, c;
    cin >> a >> b >> c;
    --a;
    --b;
    --c;
    if (a > b) swap(a, b);
    ce[c].emplace_back(a, b);
  }
  for (int i = 0; i < (int)(m); ++i) sort((ce[i]).begin(), (ce[i]).end());
  int qn;
  cin >> qn;
  for (int i = 0; i < (int)(qn); ++i) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    if (a > b) swap(a, b);
    q[a].emplace_back(b, i);
  }
  const int Q = 500;
  for (int c = 0; c < (int)(m); ++c) {
    vector<int> vrt;
    ++cvrt;
    for (pair<int, int> e : ce[c]) {
      if (invrt[e.first] != cvrt) {
        vrt.push_back(e.first);
        invrt[e.first] = cvrt;
      }
      if (invrt[e.second] != cvrt) {
        vrt.push_back(e.second);
        invrt[e.second] = cvrt;
      }
    }
    for (int v : vrt) dsu[v] = v;
    for (pair<int, int> e : ce[c]) unite(e.first, e.second);
    for (int v : vrt)
      if (q[v].size() <= Q) {
        for (pair<int, int> qi : q[v])
          if (invrt[qi.first] == cvrt && get(v) == get(qi.first))
            ++ans[qi.second];
      }
  }
  for (int v = 0; v < (int)(n); ++v)
    if (q[v].size() > Q) {
      for (int i = 0; i < (int)(n); ++i) cans[i] = 0;
      for (int c = 0; c < (int)(m); ++c) {
        vector<int> vrt;
        ++cvrt;
        for (pair<int, int> e : ce[c]) {
          if (invrt[e.first] != cvrt) {
            vrt.push_back(e.first);
            invrt[e.first] = cvrt;
          }
          if (invrt[e.second] != cvrt) {
            vrt.push_back(e.second);
            invrt[e.second] = cvrt;
          }
        }
        if (invrt[v] != cvrt) continue;
        for (int u : vrt) dsu[u] = u;
        for (pair<int, int> e : ce[c]) unite(e.first, e.second);
        for (int u : vrt)
          if (get(u) == get(v)) ++cans[u];
      }
      for (pair<int, int> qi : q[v]) ans[qi.second] += cans[qi.first];
    }
  for (int i = 0; i < (int)(qn); ++i) cout << ans[i] << '\n';
}
void gen() {
  freopen("input.txt", "w", stdout);
  int n = 4000, m = 100000;
  cout << n << ' ' << m << '\n';
  for (int i = 0; i < (int)(m); ++i)
    cout << 1 + rand() % n << ' ' << 1 + rand() % n << ' ' << 1 + rand() % 100
         << '\n';
  int q = 100000;
  cout << q << '\n';
  for (int i = 0; i < (int)(m); ++i)
    cout << 1 + rand() % n << ' ' << 1 + rand() % n << '\n';
  exit(0);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout << fixed;
  cout.precision(15);
  cerr << fixed;
  cerr.precision(15);
  solve();
  return 0;
}
