#include <bits/stdc++.h>
using namespace std;
long long M = 1e9 + 7;
const long long N = 1e3 + 10;
const long long IM = 1e15 + 37;
const long long PI = 3.14159265;
void PV(vector<long long> v) {
  for (long long i = 0; i < v.size(); i++) cout << v[i] << " ";
  cout << "\n";
}
void PA(long long v[], long long n, long long x = 0) {
  for (long long i = x; i < n + x; i++) cout << v[i] << " ";
  cout << "\n";
}
void IN(long long a[], long long n, long long x = 0) {
  for (long long i = x; i < n + x; i++) cin >> a[i];
}
inline void PP(long long n) { cout << n; }
inline void PP(long long n, long long m) { cout << n << " " << m << "\n"; }
inline void PP(long long n, long long m, long long q) {
  cout << n << " " << m << " " << q << "\n";
}
inline void PP(long long n, long long m, long long q, long long u) {
  cout << n << " " << m << " " << q << " " << u << "\n";
}
inline void op() {}
long long n, k;
vector<pair<long long, long long> > v[N];
map<pair<long long, long long>, long long> m;
map<long long, long long> z;
long long ans = 0;
void dfs(long long x, long long y, long long col = -1) {
  m[{x, col}]++;
  if (x == y && z[col] == 0) {
    ans++;
    z[col]++;
    return;
  }
  for (auto i : v[x]) {
    long long f = i.first;
    long long s = i.second;
    if (col == -1) {
      m[{x, s}]++;
      dfs(f, y, s);
    } else {
      if (col == s && !m[{f, s}]) {
        dfs(f, y, s);
      }
    }
  }
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  op();
  long long i, j, x, y, u, l;
  {
    i = j = x = y = u = l = 0;
    cin >> n >> k;
    for (long long i = 0; i < k; i++) {
      cin >> x >> y >> u;
      v[x].push_back({y, u});
      v[y].push_back({x, u});
    }
    cin >> l;
    while (l--) {
      m.clear();
      ans = 0;
      z.clear();
      cin >> x >> y;
      dfs(x, y);
      cout << (ans) << "\n";
    }
  }
  return 0;
}
