#include <bits/stdc++.h>
using namespace std;
vector<vector<pair<int, int> > > adj;
int vstd[109], vstdcol[109];
int dfs(int src, int des, int prevcol) {
  vstd[src] = 1;
  if (src == des) return 1;
  pair<int, int> child;
  int cnt = 0;
  for (int i = 0; i < adj[src].size(); i++) {
    child = adj[src][i];
    if (prevcol != -1) {
      if (vstd[child.first]) continue;
      if (prevcol == child.second) {
        if (dfs(child.first, des, child.second) == 1) return 1;
      }
    } else {
      memset(vstd, 0, sizeof(vstd));
      vstd[src] = 1;
      if (!vstdcol[child.second]) {
        if (dfs(child.first, des, child.second))
          vstdcol[child.second] = 1, cnt++;
      }
    }
  }
  return cnt;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int a, b, c, n, m;
  cin >> n >> m;
  adj = vector<vector<pair<int, int> > >(n + 5);
  for (int i = 0; i < m; i++) {
    cin >> a >> b >> c;
    adj[a].push_back(make_pair(b, c));
    adj[b].push_back(make_pair(a, c));
  }
  cin >> a;
  for (int i = 0; i < a; i++) {
    cin >> b >> c;
    memset(vstdcol, 0, sizeof(vstdcol));
    cout << dfs(b, c, -1) << endl;
  }
  return 0;
}
