#include <bits/stdc++.h>
using namespace std;
int visited[110];
vector<int> A[110], c[110];
void _visit() {
  for (int i = 0; i < 110; i++) visited[i] = false;
}
bool dfs(int u, int ed, int color) {
  if (u == ed) return true;
  int i, m = A[u].size(), v, j = false;
  visited[u] = true;
  for (i = 0; i < m; i++) {
    v = A[u][i];
    if (!visited[v] && c[u][i] == color) j = j || dfs(v, ed, color);
  }
  return j;
}
int main() {
  int i, j, k, n, e, sum = 0, temp, r, q;
  scanf("%d%d", &n, &e);
  while (e--) {
    scanf("%d%d%d", &i, &j, &k);
    A[i].push_back(j);
    A[j].push_back(i);
    c[i].push_back(k);
    c[j].push_back(k);
  }
  scanf("%d", &q);
  while (q--) {
    scanf("%d%d", &i, &j);
    sum = 0;
    for (k = 1; k <= 100; k++) {
      _visit();
      sum += dfs(i, j, k);
    }
    printf("%d\n", sum);
  }
  return 0;
}
