#include <bits/stdc++.h>
using namespace std;
long long fac[2000005] = {0};
void factorial() {
  fac[0] = 1;
  fac[1] = 1;
  for (long long i = 2; i < 200005; i++)
    fac[i] = ((i % 1000000007) * (fac[i - 1] % 1000000007)) % 1000000007;
}
long long power(long long n, long long m) {
  long long p = 1;
  if (m == 0) return 1;
  p = (power(n, m / 2) % 1000000007);
  p = (p % 1000000007 * p % 1000000007) % 1000000007;
  return (m & 1 ? ((p % 1000000007 * n % 1000000007) % 1000000007)
                : (p % 1000000007));
}
long long ncr(long long n, long long r) {
  return ((fac[n] * power(fac[r], 1000000007 - 2)) % 1000000007 *
          power(fac[n - r], 1000000007 - 2)) %
         1000000007;
}
vector<pair<long long, long long> > graph[102];
long long n, m;
long long cnt = 0;
long long used[101] = {0};
void dfs(long long x, long long y, long long col) {
  used[x] = 1;
  if (x == y) {
    ++cnt;
    return;
  }
  for (long long i = 0; i < graph[x].size(); i++) {
    pair<long long, long long> p = graph[x][i];
    if (p.second == col && !used[p.first]) {
      dfs(p.first, y, col);
    }
  }
  return;
}
void ram5564() {
  cin >> n >> m;
  while (m--) {
    long long a, b, c;
    cin >> a >> b >> c;
    graph[a].push_back(make_pair(b, c));
    graph[b].push_back(make_pair(a, c));
  }
  long long q;
  cin >> q;
  while (q--) {
    long long x, y;
    cin >> x >> y;
    cnt = 0;
    for (long long i = 1; i < 101; i++) {
      memset(used, 0, sizeof(used));
      dfs(x, y, i);
    }
    cout << cnt << "\n";
  }
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long t = 1;
  while (t--) ram5564();
  return 0;
}
