#include <bits/stdc++.h>
using namespace std;
struct edge {
  int v, c;
};
vector<edge> a[200];
int n, m;
bool p[200];
bool dfs(int x, int t, int c) {
  p[x] = true;
  if (x == t) return true;
  for (int i = 0; i < a[x].size(); i++)
    if (a[x][i].c == c && !p[a[x][i].v])
      if (dfs(a[x][i].v, t, c)) return true;
  return false;
}
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= m; i++) {
    edge e;
    int u;
    scanf("%d %d %d", &u, &e.v, &e.c);
    a[u].push_back(e);
    int v = e.v;
    e.v = u;
    a[v].push_back(e);
  }
  int q;
  scanf("%d", &q);
  for (int i = 1; i <= q; i++) {
    int res = 0;
    int x, y;
    scanf("%d %d", &x, &y);
    for (int j = 1; j <= m; j++) {
      memset(p, 0, sizeof(p));
      if (dfs(x, y, j)) res++;
    }
    printf("%d\n", res);
  }
  return 0;
}
