#include <bits/stdc++.h>
using namespace std;
int connections[203][3];
int indexs[101];
int dfs(int to, set<int> &visited, int cv, int cc, set<int> &colors) {
  if (to == cv) {
    colors.insert(cc);
    return 1;
  }
  int answer = 0;
  if (indexs[cv] != -1) {
    int i = indexs[cv];
    while (connections[i][0] == cv) {
      int av = connections[i][1];
      if (visited.find(av) == visited.end() &&
          (connections[i][2] == cc || cc == -1)) {
        visited.insert(av);
        answer += dfs(to, visited, av, connections[i][2], colors);
        visited.erase(av);
      }
      ++i;
    }
  }
  return answer;
}
int main() {
  int n = 0, m = 0;
  cin >> n >> m;
  int cnt = 0;
  memset(indexs, -1, sizeof(indexs));
  for (int i = 0; i < m; ++i) {
    int a, b, c;
    cin >> a >> b >> c;
    connections[cnt][0] = a;
    connections[cnt][1] = b;
    connections[cnt++][2] = c;
    connections[cnt][0] = b;
    connections[cnt][1] = a;
    connections[cnt++][2] = c;
  }
  for (int i = 0; i < 2 * m; ++i) {
    for (int j = 0; j < 2 * m - 1; ++j) {
      if (connections[j][0] > connections[j + 1][0]) {
        swap(connections[j], connections[j + 1]);
      }
    }
  }
  for (int i = 0; i < 2 * m; ++i) {
    for (int j = 0; j < 3; ++j) {
    }
  }
  int el = 0;
  for (int i = 0; i < 2 * m; ++i) {
    if (el != connections[i][0]) {
      indexs[connections[i][0]] = i;
      el = connections[i][0];
    }
  }
  for (int i = 0; i < 10; ++i) {
  }
  int qn = 0;
  cin >> qn;
  for (int i = 0; i < qn; ++i) {
    int from = 0;
    int to = 0;
    cin >> from >> to;
    set<int> visited;
    set<int> colors;
    visited.insert(from);
    dfs(to, visited, from, -1, colors);
    cout << colors.size() << endl;
  }
  return 0;
}
