#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
const int MOD = 1000000007;
vector<vector<int> > co[102];
bitset<102> vis;
void dfs(int node, int c) {
  vis[node] = 1;
  for (auto it : co[c][node])
    if (!vis[it]) dfs(it, c);
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, m, a, b, c;
  cin >> n >> m;
  for (int i = 0; i < m; ++i) co[i].resize(n);
  for (int i = 0; i < m; ++i) {
    cin >> a >> b >> c, c--, a--, b--;
    co[c][a].push_back(b);
    co[c][b].push_back(a);
  }
  int q;
  cin >> q;
  while (q--) {
    cin >> a >> b;
    a--, b--, c = 0;
    for (int i = 0; i < m; ++i) dfs(a, i), c += vis[b], vis = 0;
    cout << c << "\n";
  }
  return 0;
}
