#include <bits/stdc++.h>
using namespace std;
vector<int> v[105][105];
const long long int lol_max = numeric_limits<long long int>::max();
int n, m, a, b, c, q, ans;
int visited[105];
int dfs(int c, int s, int e) {
  stack<int> S;
  int t;
  S.push(s);
  while (!S.empty()) {
    t = S.top();
    S.pop();
    if (visited[t] == 0) {
      if (t == e) {
        for (int i = 0; i < 105; i++) visited[i] = 0;
        return 1;
      }
      visited[t] = 1;
      int sz = v[c][t].size();
      for (int i = 0; i < sz; i++) {
        S.push(v[c][t][i]);
      }
    }
  }
  for (int i = 0; i < 105; i++) visited[i] = 0;
  return 0;
}
int main() {
  int st, end;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> a >> b >> c;
    v[c][a].push_back(b);
    v[c][b].push_back(a);
  }
  cin >> q;
  while (q--) {
    int val;
    ans = 0;
    cin >> st >> end;
    for (int c = 1; c <= m; c++) {
      val = dfs(c, st, end);
      if (val) ans++;
    }
    cout << ans << endl;
  }
  return 0;
}
