#include <bits/stdc++.h>
using namespace std;
vector<vector<vector<int> > > g(101, vector<vector<int> >(101));
int n, m;
int vis[101][101];
void bfs(int s, int e) {
  set<int> col;
  queue<pair<int, pair<int, int> > > q;
  for (int i = 1; i <= n; ++i) {
    for (int j = 0; j < g[s][i].size(); ++j) {
      vis[s][g[s][i][j]] = 1;
      q.push(make_pair(g[s][i][j], make_pair(s, i)));
    }
  }
  while (q.size()) {
    int c = q.front().first;
    pair<int, int> p = q.front().second;
    q.pop();
    if (p.second == e) {
      col.insert(c);
      continue;
    }
    if (!vis[p.second][c]) {
      for (int i = 1; i <= n; ++i) {
        for (int j = 0; j < g[p.second][i].size(); ++j) {
          if (g[p.second][i][j] == c) {
            if (!vis[i][c]) {
              q.push(make_pair(c, make_pair(p.second, i)));
            }
          }
        }
      }
      vis[p.second][c] = 1;
    }
  }
  cout << col.size() << endl;
}
int main() {
  cin >> n >> m;
  for (int i = 0, u, v, c; i < m; ++i) {
    cin >> u >> v >> c;
    g[u][v].push_back(c);
    g[v][u].push_back(c);
  }
  int t;
  cin >> t;
  while (t--) {
    memset(vis, 0, sizeof vis);
    int s, e;
    cin >> s >> e;
    bfs(s, e);
  }
}
