#include <bits/stdc++.h>
using namespace std;
const int inf = LLONG_MAX / 2;
const int MX = 105;
vector<pair<int, int> > v[MX];
bool vis[MX];
bool dfs(int col, int src, int dest) {
  vis[src] = 1;
  if (src == dest) return 1;
  for (int i = 0; i < v[src].size(); i++) {
    auto cur = v[src][i];
    if (cur.second == col && !vis[cur.first]) {
      if (dfs(col, cur.first, dest)) return true;
    }
  }
  return false;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m, a, b, c;
  cin >> n >> m;
  for (int(i) = (0); (i) <= (m - 1); ++(i)) {
    cin >> a >> b >> c;
    a--;
    b--;
    v[a].push_back(make_pair(b, c));
    v[b].push_back(make_pair(a, c));
  }
  int u, v, cnt = 0, q;
  cin >> q;
  while (q--) {
    cin >> u >> v;
    u--;
    v--;
    cnt = 0;
    for (int(i) = (1); (i) <= (100); ++(i)) {
      memset((vis), (0), sizeof(vis));
      if (dfs(i, u, v)) cnt++;
    }
    cout << cnt << endl;
    ;
  }
  return 0;
}
