#include <bits/stdc++.h>
using namespace std;
const int maxn = 105;
vector<vector<pair<int, int> > > graph(maxn);
bool vis[maxn];
bool dfs(int s, int to, int c) {
  vis[s] = 1;
  if (s == to) return 1;
  for (auto i : graph[s]) {
    if (i.second == c && !vis[i.first]) {
      if (dfs(i.first, to, c)) return 1;
    }
  }
  return 0;
}
void init() {
  for (int i = 0; i < maxn; i++) vis[i] = 0;
}
int main() {
  ios::sync_with_stdio(0);
  int n, m;
  cin >> n >> m;
  int a, b, c;
  for (int i = 0; i < m; i++) {
    cin >> a >> b >> c;
    --a;
    --b;
    --c;
    graph[a].push_back(make_pair(b, c));
    graph[b].push_back(make_pair(a, c));
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> a >> b;
    --a;
    --b;
    int ans = 0;
    for (int i = 0; i < m; ++i) {
      init();
      ans += dfs(a, b, i);
    }
    cout << ans << endl;
  }
  return 0;
}
