#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 10;
int n, m, qq, top = 0, blo, c[N];
vector<int> vec;
vector<pair<int, int> > col[N];
map<pair<int, int>, int> mp;
pair<int, int> q[N];
int pre[N], ans[N];
int find(int x) { return x == pre[x] ? x : pre[x] = find(pre[x]); }
void solve() {
  for (int i = 1; i <= m; i++) {
    vec.clear();
    int cnt = 0;
    for (auto u : col[i]) vec.push_back(u.first), vec.push_back(u.second);
    sort(vec.begin(), vec.end());
    vec.erase(unique(vec.begin(), vec.end()), vec.end());
    for (auto u : vec) pre[u] = u, c[u] = i;
    for (auto u : col[i]) pre[find(u.first)] = find(u.second);
    if (vec.size() > blo) {
      for (int t = 1; t <= top; t++)
        if (c[q[t].first] == i && c[q[t].second] == i &&
            find(q[t].first) == find(q[t].second))
          ans[t]++;
    } else
      for (auto u : vec)
        for (auto v : vec)
          if (u != v && find(u) == find(v)) mp[pair<int, int>(u, v)]++;
  }
}
int main() {
  cin >> n >> m;
  blo = (int)sqrt(n + 0.5);
  for (int i = 1; i <= m; i++) {
    int u, v, c;
    scanf("%d%d%d", &u, &v, &c);
    col[c].push_back(pair<int, int>(u, v));
  }
  cin >> qq;
  for (int i = 1; i <= qq; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    q[++top] = pair<int, int>(u, v);
  }
  solve();
  for (int i = 1; i <= top; i++) printf("%d\n", ans[i] + mp[q[i]]);
}
