#include <bits/stdc++.h>
using namespace std;
long long int ans, a, b;
long long int vis[500];
vector<pair<long long int, long long int> > graph[500];
void dfs(long long int u, long long int c) {
  vis[u] = 1;
  if (u == b) {
    ans++;
    return;
  }
  for (long long int i = 0; i < graph[u].size(); i++) {
    if (!vis[graph[u][i].first] && graph[u][i].second == c) {
      vis[graph[u][i].first] = 1;
      dfs(graph[u][i].first, c);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  long long int n, m, q;
  cin >> n >> m;
  for (long long int i = 0; i < m; i++) {
    long long int x, y, z;
    cin >> x >> y >> z;
    graph[x].push_back(make_pair(y, z));
    graph[y].push_back(make_pair(x, z));
  }
  cin >> q;
  for (long long int i = 0; i < q; i++) {
    long long int u, v;
    cin >> a >> b;
    ans = 0;
    for (long long int i = 1; i <= m; i++) {
      memset(vis, 0, sizeof(vis));
      dfs(a, i);
    }
    cout << ans << endl;
  }
  return 0;
}
