#include <bits/stdc++.h>
using namespace std;
const int maxn = 100005;
struct edge {
  int v, color;
  edge(int iv, int ic) {
    v = iv;
    color = ic;
  }
};
vector<edge> G[maxn];
bool vis[maxn];
set<int> sc;
int n, m, q;
int ans, s, t;
bool dfs(int k, int color) {
  vis[k] = true;
  if (k == t) {
    return true;
  }
  for (int i = 0; i < G[k].size(); i++) {
    int co = G[k][i].color, v = G[k][i].v;
    if (co == color && !vis[v]) {
      if (dfs(v, co)) return true;
    }
  }
  return false;
}
int main() {
  cin >> n >> m;
  int a, b, c;
  memset(G, 0, sizeof(G));
  for (int i = 0; i < m; i++) {
    scanf("%d%d%d", &a, &b, &c);
    G[a].push_back(edge(b, c));
    G[b].push_back(edge(a, c));
  }
  cin >> q;
  while (q--) {
    sc.clear();
    scanf("%d%d", &s, &t);
    ans = 0;
    for (int i = 0; i < G[s].size(); i++) {
      int co = G[s][i].color;
      int v = G[s][i].v;
      memset(vis, false, sizeof(vis));
      vis[s] = true;
      if (dfs(v, co)) sc.insert(co);
    }
    printf("%d\n", sc.size());
  }
  return 0;
}
