#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:16777216")
using namespace std;
vector<bool> used(105, false);
void dfs(int s, vector<vector<int> >& g) {
  used[s] = true;
  for (long long i = 0; i < (int)g[s].size(); i++) {
    if (!used[g[s][i]]) dfs(g[s][i], g);
  }
}
int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int> > g[105];
  vector<bool> have(105, false);
  for (long long i = 0; i < (int)105; i++) {
    g[i].resize(n + 1);
  }
  for (long long i = 0; i < (int)m; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    have[c] = true;
    g[c][a].push_back(b);
    g[c][b].push_back(a);
  }
  int q;
  cin >> q;
  for (long long i = 0; i < (int)q; i++) {
    int u, vector;
    cin >> u >> vector;
    int sol = 0;
    for (long long i = 0; i < (int)105; i++) {
      if (have[i]) {
        used.clear();
        used.assign(105, false);
        dfs(u, g[i]);
        if (used[vector]) sol++;
      }
    }
    cout << sol << endl;
  }
  return 0;
}
