#include <bits/stdc++.h>
using namespace std;
const int N = 1e6;
vector<pair<int, int> > v[N];
int vis[N], a[N];
void dfs(int x, int y, int c) {
  if (vis[x]) return;
  vis[x] = 1;
  if (x == y) a[c] = 1;
  for (auto i : v[x]) {
    if ((!vis[i.first]) && i.second == c) dfs(i.first, y, c);
  }
}
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    int a, b, c;
    cin >> a >> b >> c;
    v[a].push_back({b, c});
    v[b].push_back({a, c});
  }
  int q;
  cin >> q;
  for (int i = 1; i <= q; ++i) {
    int l, r, f = 0;
    cin >> l >> r;
    for (auto j : v[l]) {
      dfs(l, r, j.second);
      memset(vis, 0, sizeof(vis));
    }
    for (int i = 1; i <= m; ++i) f += a[i];
    cout << f;
    cout << "\n";
    memset(a, 0, sizeof(a));
  }
  return 0;
}
