#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e2 + 10;
const int INF = 0x3f3f3f3f;
int n, m;
bool vis[MAXN];
vector<pair<int, int> > V[MAXN];
bool DFS(int u, int v, int c) {
  vis[u] = true;
  if (u == v) return true;
  for (int i = 0; i < V[u].size(); ++i) {
    pair<int, int> p = V[u][i];
    if (p.second == c && !vis[p.first]) {
      if (DFS(p.first, v, c)) return true;
    }
  }
  return false;
}
int main(void) {
  while (scanf("%d%d", &n, &m) == 2) {
    for (int i = 1; i <= m; ++i) {
      int u, v, c;
      scanf("%d%d%d", &u, &v, &c);
      V[u].push_back(make_pair(v, c));
      V[v].push_back(make_pair(u, c));
    }
    int q;
    scanf("%d", &q);
    while (q--) {
      int u, v;
      scanf("%d%d", &u, &v);
      int ans = 0;
      for (int i = 1; i <= m; ++i) {
        memset(vis, false, sizeof(vis));
        if (DFS(u, v, i)) ans++;
      }
      printf("%d\n", ans);
    }
  }
  return 0;
}
