#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
struct dsu {
  int n, fa[100005];
  void init(int len) {
    n = len;
    for (int i = 1; i <= n; i++) fa[i] = i;
  }
  int get(int x) { return fa[x] == x ? x : (fa[x] = get(fa[x])); }
  void up(int x, int y) {
    x = get(x);
    y = get(y);
    if (rand() & 1) {
      fa[x] = y;
    } else
      fa[y] = x;
  }
} D;
vector<pair<int, int>> co[100005];
const int len = 300;
pair<int, int> qs[100005];
int ans[100005];
long long s[16000000];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  srand(time(0));
  int n, m;
  cin >> n >> m;
  int x, y, z;
  for (int i = 1; i <= m; i++) {
    cin >> x >> y >> z;
    co[z].emplace_back(make_pair(x, y));
  }
  int q;
  cin >> q;
  for (int i = 1; i <= q; i++) {
    cin >> qs[i].first >> qs[i].second;
    if (qs[i].first > qs[i].second) swap(qs[i].first, qs[i].second);
  }
  int cnt = 0;
  for (int i = 1; i <= m; i++) {
    if (co[i].size() >= len) {
      D.init(n);
      for (auto u : co[i]) D.up(u.first, u.second);
      for (int j = 1; j <= q; j++) {
        if (D.get(qs[j].first) == D.get(qs[j].second)) ans[j]++;
      }
    } else {
      vector<int> v;
      for (auto u : co[i]) v.emplace_back(u.first), v.emplace_back(u.second);
      sort(v.begin(), v.end());
      v.erase(unique(v.begin(), v.end()), v.end());
      int l = v.size();
      D.init(l);
      for (auto u : co[i]) {
        D.up(lower_bound(v.begin(), v.end(), u.first) - v.begin() + 1,
             lower_bound(v.begin(), v.end(), u.second) - v.begin() + 1);
      }
      for (int i1 = 0; i1 < l; i1++) {
        for (int i2 = i1 + 1; i2 < l; i2++) {
          if (D.get(i1 + 1) == D.get(i2 + 1)) {
            s[++cnt] = 1ll * v[i1] * (n + 1) + v[i2];
          }
        }
      }
    }
  }
  sort(s + 1, s + cnt + 1);
  for (int i = 1; i <= q; i++) {
    long long t = 1ll * qs[i].first * (n + 1) + qs[i].second;
    ans[i] +=
        upper_bound(s + 1, s + cnt + 1, t) - lower_bound(s + 1, s + cnt + 1, t);
    cout << ans[i] << '\n';
  }
  return 0;
}
