#include <bits/stdc++.h>
using namespace std;
vector<vector<pair<int, int> > > h;
vector<int> used;
int dfs(int a, int b, int c) {
  used[a] = 1;
  if (a == b) {
    used[a] = 0;
    return 1;
  }
  for (int i = 0; i != h[a].size(); ++i)
    if ((!used[h[a][i].first]) and (h[a][i].second == c)) {
      if (dfs(h[a][i].first, b, c)) {
        used[a] = 0;
        return 1;
      }
    }
  used[a] = 0;
  return 0;
}
int main() {
  int n, a, b, c, m, q, i, k, j;
  scanf("%d%d", &n, &m);
  h.assign(n + 1, vector<pair<int, int> >());
  used.assign(n + 1, 0);
  for (i = 0; i < m; ++i) {
    scanf("%d%d%d", &a, &b, &c);
    h[a].push_back(make_pair(b, c));
    h[b].push_back(make_pair(a, c));
  }
  scanf("%d", &q);
  for (i = 0; i < q; ++i) {
    k = 0;
    scanf("%d%d", &a, &b);
    for (j = 1; j <= m; ++j) {
      k += dfs(a, b, j);
    }
    printf("%d\n", k);
  }
  return 0;
}
