#include <bits/stdc++.h>
using namespace std;
const int INF = 0x1f1f1f1f;
struct UnionFind {
  vector<int> data;
  UnionFind() { data.clear(); }
  UnionFind(int size) : data(size, -1) {}
  bool unionSet(int x, int y) {
    x = root(x);
    y = root(y);
    if (x != y) {
      if (data[y] < data[x]) swap(x, y);
      data[x] += data[y];
      data[y] = x;
    }
    return x != y;
  }
  bool findSet(int x, int y) { return root(x) == root(y); }
  int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
  int size(int x) { return -data[root(x)]; }
  void reset(int s) {
    data.clear();
    data.resize(s, -1);
  }
};
UnionFind Uni[100];
int main() {
  int n, m, q, a, b, c, i, j, r;
  scanf("%d %d", &n, &m);
  ;
  for (i = 0; i < m; i++) {
    Uni[i].reset(n);
  }
  for (i = 0; i < m; i++) {
    scanf("%d %d %d", &a, &b, &c);
    ;
    Uni[c - 1].unionSet(a - 1, b - 1);
  }
  scanf("%d", &q);
  ;
  for (i = 0; i < q; i++) {
    scanf("%d %d", &a, &b);
    ;
    r = 0;
    for (j = 0; j < m; j++) {
      if (Uni[j].findSet(a - 1, b - 1)) {
        r++;
      }
    }
    printf("%d\n", (int)(r));
    ;
  }
  return 0;
}
