#include <bits/stdc++.h>
using namespace std;
const int maxN = 1 << 17;
const int maxK = 1 << 10;
struct DSU {
  int p[maxN];
  DSU() {
    for (int i = 0; i < maxN; i++) {
      p[i] = i;
    }
  }
  int findSet(int v) { return p[v] = ((p[v] == v) ? v : findSet(p[v])); }
  void unite(int v, int u) {
    if (rand() & 1) {
      swap(v, u);
    }
    p[findSet(v)] = findSet(u);
  }
};
DSU B[maxN / maxK];
vector<int> G[maxN];
int mark[maxN];
vector<pair<int, int> > C[maxN];
unordered_map<long long, int> M;
unordered_set<long long> used;
int id[maxN];
vector<int> cur;
void dfs(int v) {
  if (mark[v]) {
    return;
  }
  cur.push_back(v);
  mark[v] = 1;
  for (auto u : G[v]) {
    dfs(u);
  }
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    C[c].push_back({a, b});
  }
  vector<pair<int, int> > Q;
  int q;
  scanf("%d", &q);
  while (q--) {
    int a, b;
    scanf("%d%d", &a, &b);
    used.insert(((long long)a << 17) | b);
    Q.push_back({a, b});
  }
  int t = 0;
  for (int i = 1; i <= m; i++) {
    if (C[i].size() > maxK) {
      for (auto x : C[i]) {
        B[t].unite(x.first, x.second);
      }
      t++;
    } else {
      for (auto x : C[i]) {
        mark[x.first] = mark[x.second] = 0;
        G[x.first].clear();
        G[x.second].clear();
      }
      for (auto x : C[i]) {
        G[x.first].push_back(x.second);
        G[x.second].push_back(x.first);
      }
      for (auto x : C[i]) {
        cur.clear();
        dfs(x.first);
        for (auto v : cur) {
          for (auto u : cur) {
            if (v == u) {
              continue;
            }
            long long id = ((long long)v << 17) | u;
            if (used.find(id) != used.end()) {
              M[id]++;
            }
          }
        }
      }
    }
  }
  for (auto x : Q) {
    int v = x.first, u = x.second;
    int res = M[((long long)v << 17) | u];
    for (int i = 0; i < t; i++) {
      res += (B[i].findSet(v) == B[i].findSet(u));
    }
    printf("%d\n", res);
  }
  return 0;
}
