#include <bits/stdc++.h>
using namespace std;
template <class T>
void debug(T a, T b) {
  for (; a != b; ++a) cerr << *a << ' ';
  cerr << endl;
}
template <class T>
void chmin(T& a, const T& b) {
  if (a > b) a = b;
}
template <class T>
void chmax(T& a, const T& b) {
  if (a < b) a = b;
}
namespace std {
template <class S, class T>
ostream& operator<<(ostream& out, const pair<S, T>& a) {
  out << '(' << a.first << ',' << a.second << ')';
  return out;
}
}  // namespace std
int n, m;
vector<pair<int, int> > g[105];
int vis[105];
bool dfs(int v, int col, int dst) {
  vis[v] = 1;
  if (v == dst) return 1;
  for (int i = 0; i < (g[v].size()); ++i) {
    pair<int, int> e = g[v][i];
    if (e.second == col && !vis[e.first]) {
      if (dfs(e.first, col, dst)) return true;
    }
  }
  return false;
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < (m); ++i) {
    int a, b, c;
    cin >> a >> b >> c;
    --a;
    --b;
    --c;
    g[a].push_back(make_pair(b, c));
    g[b].push_back(make_pair(a, c));
  }
  int q;
  cin >> q;
  for (int hoge = 0; hoge < (q); ++hoge) {
    int res = 0;
    int u, v;
    cin >> u >> v;
    --u;
    --v;
    for (int i = 0; i < (100); ++i) {
      memset((vis), 0, sizeof(vis));
      if (dfs(u, i, v)) ++res;
    }
    cout << res << endl;
  }
  return 0;
}
