#include <bits/stdc++.h>
using namespace std;
int n, m, q;
bool vis[500];
struct node {
  int v, c;
  node *next;
} edge[500];
node *cnt = &edge[0];
node *adj[500];
void Addedge(int u, int v, int c) {
  node *p = ++cnt;
  p->v = v;
  p->c = c;
  p->next = adj[u];
  adj[u] = p;
}
void Init() {
  int i, u, v, c;
  scanf("%d%d", &n, &m);
  for (i = 1; i <= m; ++i) {
    scanf("%d%d%d", &u, &v, &c);
    Addedge(u, v, c);
    Addedge(v, u, c);
  }
}
bool DFS(int u, int v, int c) {
  vis[u] = true;
  if (u == v) return true;
  for (node *p = adj[u]; p; p = p->next) {
    if (vis[p->v]) continue;
    if (p->c == c && DFS(p->v, v, c)) return true;
  }
  return false;
}
void Work() {
  int q, i, j, u, v, ans;
  scanf("%d", &q);
  for (i = 1; i <= q; ++i) {
    scanf("%d%d", &u, &v);
    for (ans = 0, j = 1; j <= m; ++j) {
      memset(vis, 0, sizeof(vis));
      ans += DFS(u, v, j);
    }
    printf("%d\n", ans);
  }
}
int main() {
  Init();
  Work();
  return 0;
}
