#include <bits/stdc++.h>
using namespace std;
struct uf {
  static const int MAXN = 105;
  int par[MAXN];
  int size[MAXN];
  void init() {
    memset(par, -1, sizeof(par));
    for (int i = 0; i < (MAXN); ++i) size[i] = 1;
  }
  int root(int a) {
    if (par[a] == -1) return a;
    return par[a] = root(par[a]);
  }
  void unite(int a, int b) {
    a = root(a);
    b = root(b);
    if (a == b) return;
    if (size[a] < size[b]) swap(a, b);
    par[b] = a;
    size[a] += size[b];
  }
  bool same(int a, int b) { return root(a) == root(b); }
};
uf u_[105];
int n, m;
int main() {
  for (int i = 0; i < (100); ++i) u_[i].init();
  cin >> n >> m;
  for (int i = 0; i < (m); ++i) {
    int a, b, c;
    cin >> a >> b >> c;
    --a;
    --b;
    --c;
    u_[c].unite(a, b);
  }
  int q;
  cin >> q;
  while (q--) {
    int res = 0;
    int u, v;
    cin >> u >> v;
    --u;
    --v;
    for (int i = 0; i < (100); ++i)
      if (u_[i].same(u, v)) ++res;
    cout << res << endl;
  }
  return 0;
}
