#include <bits/stdc++.h>
using namespace std;
vector<vector<vector<long long> > > v(110, vector<vector<long long> >(110));
long long vis[200];
void dfs(long long n, long long c) {
  vis[n] = 1;
  for (long long i = 0; i < v[c][n].size(); i++) {
    if (!vis[v[c][n][i]]) dfs(v[c][n][i], c);
  }
}
void solve() {
  long long n, m, i, j, k;
  cin >> n >> m;
  for (i = 0; i < m; i++) {
    long long x, y, z;
    cin >> x >> y >> z;
    v[z][x].push_back(y);
    v[z][y].push_back(x);
  }
  long long q;
  cin >> q;
  while (q--) {
    long long x, y;
    cin >> x >> y;
    long long cnt = 0;
    for (i = 1; i <= m; i++) {
      memset(vis, 0, sizeof(vis));
      dfs(x, i);
      if (vis[y]) cnt++;
    }
    cout << cnt << "\n";
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long q = 1;
  while (q--) solve();
}
