#include <bits/stdc++.h>
using namespace std;
const int sz = 112;
int cnt;
vector<pair<int, int> > adj[sz];
int col[sz];
int vis[sz];
bool dfs(int src, int des, int c) {
  if (src == des) {
    return true;
  }
  vis[src] = 1;
  for (int i = 0; i < adj[src].size(); i++) {
    if ((!vis[adj[src][i].first]) && (adj[src][i].second == c)) {
      if (dfs(adj[src][i].first, des, c)) {
        return true;
      }
    }
  }
  return false;
}
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < sz; i++) {
    col[i] = 0;
  }
  for (int i = 0; i < m; i++) {
    int u, v, c;
    cin >> u >> v >> c;
    u--;
    v--;
    c--;
    col[c] = 1;
    adj[u].push_back(make_pair(v, c));
    adj[v].push_back(make_pair(u, c));
  }
  int q;
  cin >> q;
  while (q--) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    cnt = 0;
    for (int i = 0; i < sz; i++) {
      if (col[i] == 1) {
        for (int j = 0; j < n; j++) {
          vis[j] = 0;
        }
        if (dfs(u, v, i) == true || dfs(v, u, i) == true) {
          cnt++;
        }
      }
    }
    cout << cnt << endl;
  }
}
