#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m;
  cin >> n >> m;
  vector<pair<int, int> > adj[n + 1];
  for (int i = 0; i < m; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    adj[a].push_back(make_pair(b, c));
    adj[b].push_back(make_pair(a, c));
  }
  int q;
  cin >> q;
  while (q--) {
    int st, en;
    cin >> st >> en;
    bool visited[n + 1][m + 1];
    for (int i = 0; i <= n; i++)
      for (int j = 0; j <= m; j++) visited[i][j] = false;
    bool color[m + 1];
    for (int i = 0; i <= m; i++) color[i] = true;
    int cnt = 0;
    for (int i = 0; i < adj[st].size(); i++) {
      pair<int, int> f = adj[st][i];
      int col = f.second;
      int star = f.first;
      if ((!visited[star][col]) && (color[col])) {
        visited[star][col] = true;
        queue<int> q;
        q.push(star);
        while (!q.empty()) {
          int u = q.front();
          if (u == en) {
            cnt++;
            color[col] = false;
            break;
          }
          q.pop();
          for (int j = 0; j < adj[u].size(); j++) {
            pair<int, int> ins = adj[u][j];
            if ((!visited[ins.first][ins.second]) && (ins.second == col)) {
              visited[ins.first][ins.second] = true;
              q.push(ins.first);
            }
          }
        }
      }
    }
    cout << cnt << endl;
  }
}
