#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 100;
const int SQ = 300;
int cmp = 0;
vector<pair<int, pair<int, int> > > e;
vector<int> a[N], b[N];
bool mark[N], flag[N];
map<pair<int, int>, int> mp;
void add(int u, int v) {
  a[u].push_back(v);
  a[v].push_back(u);
}
void remove(int v) {
  a[v].clear();
  mark[v] = false;
}
void dfs(int v) {
  b[v].push_back(cmp);
  mark[v] = true;
  for (int i = 0, u; i < a[v].size(); i++) {
    u = a[v][i];
    if (!mark[u]) dfs(u);
  }
}
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  for (int i = 0, u, v, c; i < m; i++) {
    cin >> u >> v >> c;
    u--;
    v--;
    e.push_back(make_pair(c, make_pair(u, v)));
  }
  sort(e.begin(), e.end());
  for (int i = 0, u, v; i < m; i++) {
    int s = i, en;
    while (i < m - 1 && e[i].first == e[i + 1].first) {
      add(e[i].second.first, e[i].second.second);
      i++;
    }
    add(e[i].second.first, e[i].second.second);
    en = i + 1;
    for (int j = s, u; j < en; j++) {
      u = e[j].second.first;
      if (!mark[u]) {
        cmp++;
        dfs(u);
      }
    }
    for (int j = s, u, v; j < en; j++) {
      u = e[j].second.first, v = e[j].second.second;
      remove(u);
      remove(v);
    }
  }
  int q;
  cin >> q;
  for (int _ = 0, u, v; _ < q; _++) {
    cin >> u >> v;
    u--;
    v--;
    if (b[u].size() > b[v].size()) swap(u, v);
    if (mp[make_pair(u, v)] != 0) {
      cout << mp[make_pair(u, v)] - 1 << "\n";
      continue;
    }
    int ans = 0;
    if (b[u].size() < SQ) {
      for (int i = 0, l; i < b[u].size(); i++) {
        l = lower_bound(b[v].begin(), b[v].end(), b[u][i]) - b[v].begin();
        if (l != b[v].size() && b[v][l] == b[u][i]) ans++;
      }
    } else {
      for (int i = 0; i < b[u].size(); i++) {
        flag[b[u][i]] = true;
      }
      for (int i = 0; i < b[v].size(); i++) {
        if (flag[b[v][i]]) ans++;
      }
      for (int i = 0; i < b[u].size(); i++) {
        flag[b[u][i]] = false;
      }
    }
    cout << ans << "\n";
    mp[make_pair(u, v)] = ans + 1;
  }
  return 0;
}
