#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int> > adj[105];
int cnt;
bool ck_color[105];
bool ck_vertex[105];
void dfs(int edge, int v, int color) {
  if (ck_vertex[edge]) {
    return;
  }
  ck_vertex[edge] = true;
  if (edge == v) {
    ck_color[color] = true;
    cnt++;
  } else {
    for (int i = 0; i < adj[edge].size(); i++) {
      int t = adj[edge][i].second;
      if (!ck_vertex[t] && color == adj[edge][i].first && !ck_color[color]) {
        dfs(t, v, color);
      }
    }
  }
}
int main() {
  int n, m, a, b, c, q, u, v;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> a >> b >> c;
    adj[a].push_back(make_pair(c, b));
    adj[b].push_back(make_pair(c, a));
  }
  cin >> q;
  while (q--) {
    cin >> u >> v;
    memset(ck_color, false, sizeof ck_color);
    for (int i = 0; i < adj[u].size(); i++) {
      memset(ck_vertex, false, sizeof(ck_vertex));
      if (!ck_color[adj[u][i].first]) dfs(adj[u][i].second, v, adj[u][i].first);
    }
    cout << cnt << endl;
    cnt = 0;
  }
  return 0;
}
